summaryrefslogtreecommitdiffstats
path: root/pgcli/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'pgcli/config.py')
-rw-r--r--pgcli/config.py45
1 files changed, 40 insertions, 5 deletions
diff --git a/pgcli/config.py b/pgcli/config.py
index 0fc42dd..22f08dc 100644
--- a/pgcli/config.py
+++ b/pgcli/config.py
@@ -3,6 +3,8 @@ import shutil
import os
import platform
from os.path import expanduser, exists, dirname
+import re
+from typing import TextIO
from configobj import ConfigObj
@@ -16,11 +18,15 @@ def config_location():
def load_config(usr_cfg, def_cfg=None):
- cfg = ConfigObj()
- cfg.merge(ConfigObj(def_cfg, interpolation=False))
- cfg.merge(ConfigObj(expanduser(usr_cfg), interpolation=False, encoding="utf-8"))
+ # avoid config merges when possible. For writing, we need an umerged config instance.
+ # see https://github.com/dbcli/pgcli/issues/1240 and https://github.com/DiffSK/configobj/issues/171
+ if def_cfg:
+ cfg = ConfigObj()
+ cfg.merge(ConfigObj(def_cfg, interpolation=False))
+ cfg.merge(ConfigObj(expanduser(usr_cfg), interpolation=False, encoding="utf-8"))
+ else:
+ cfg = ConfigObj(expanduser(usr_cfg), interpolation=False, encoding="utf-8")
cfg.filename = expanduser(usr_cfg)
-
return cfg
@@ -44,12 +50,16 @@ def upgrade_config(config, def_config):
cfg.write()
+def get_config_filename(pgclirc_file=None):
+ return pgclirc_file or "%sconfig" % config_location()
+
+
def get_config(pgclirc_file=None):
from pgcli import __file__ as package_root
package_root = os.path.dirname(package_root)
- pgclirc_file = pgclirc_file or "%sconfig" % config_location()
+ pgclirc_file = get_config_filename(pgclirc_file)
default_config = os.path.join(package_root, "pgclirc")
write_default_config(default_config, pgclirc_file)
@@ -62,3 +72,28 @@ def get_casing_file(config):
if casing_file == "default":
casing_file = config_location() + "casing"
return casing_file
+
+
+def skip_initial_comment(f_stream: TextIO) -> int:
+ """
+ Initial comment in ~/.pg_service.conf is not always marked with '#'
+ which crashes the parser. This function takes a file object and
+ "rewinds" it to the beginning of the first section,
+ from where on it can be parsed safely
+
+ :return: number of skipped lines
+ """
+ section_regex = r"\s*\["
+ pos = f_stream.tell()
+ lines_skipped = 0
+ while True:
+ line = f_stream.readline()
+ if line == "":
+ break
+ if re.match(section_regex, line) is not None:
+ f_stream.seek(pos)
+ break
+ else:
+ pos += len(line)
+ lines_skipped += 1
+ return lines_skipped