From 6a725aa87fec5d8c6921c07727a73fe9e1d78cf5 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 1 May 2014 20:51:07 +0200 Subject: [PATCH] Fix escaping in config --- TODO | 1 - qutebrowser/config/_iniparsers.py | 2 +- qutebrowser/config/config.py | 20 ++++++++++++++------ qutebrowser/config/configdata.py | 5 +++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index 8f33cb746..6a539645c 100644 --- a/TODO +++ b/TODO @@ -8,7 +8,6 @@ Style _foo = QApplication.instance().obj.foo for global singletons? Use py3.4 Enums with a backport to qutebrowser.utils? -Make config escaping more clear (escape all ;? Use \ to convert $-signs?) Major features ============== diff --git a/qutebrowser/config/_iniparsers.py b/qutebrowser/config/_iniparsers.py index fbff6995b..e8ddd7385 100644 --- a/qutebrowser/config/_iniparsers.py +++ b/qutebrowser/config/_iniparsers.py @@ -39,7 +39,7 @@ class ReadConfigParser(ConfigParser): configdir: Directory to read the config from. fname: Filename of the config file. """ - super().__init__(interpolation=None) + super().__init__(interpolation=None, comment_prefixes='#') self.optionxform = lambda opt: opt # be case-insensitive self._configdir = configdir self._configfile = os.path.join(self._configdir, fname) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 7dd25f514..81ef83c7c 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -88,8 +88,9 @@ class ConfigManager(QObject): """Configuration manager for qutebrowser. Class attributes: - SPECIAL_CHARS: Chars which need escaping when they occur as first char - in a line. + KEY_ESCAPE: Chars which need escaping when they occur as first char + in a line. + VALUE_ESCAPE: Chars to escape inside values. ESCAPE_CHAR: The char to be used for escaping Attributes: @@ -108,7 +109,8 @@ class ConfigManager(QObject): Args: the changed section and option. """ - SPECIAL_CHARS = r'\#;[' + KEY_ESCAPE = r'\#[' + VALUE_ESCAPE = r'\$' ESCAPE_CHAR = '\\' changed = pyqtSignal(str, str) @@ -198,11 +200,15 @@ class ConfigManager(QObject): """Get the option items as string for section.""" lines = [] for optname, option in section.items(): - for c in self.SPECIAL_CHARS: + value = option.get_first_value(startlayer='conf') + for c in self.KEY_ESCAPE: if optname.startswith(c): optname = optname.replace(c, self.ESCAPE_CHAR + c, 1) - keyval = '{} = {}'.format(optname, option.get_first_value( - startlayer='conf')) + for c in self.VALUE_ESCAPE: + logging.warn("{} Before escaping {}: {}".format(optname, c, value)) + value = value.replace(c, self.ESCAPE_CHAR + c) + logging.warn("After escaping {}: {}".format(c, value)) + keyval = '{} = {}'.format(optname, value) lines.append(keyval) return lines @@ -218,6 +224,8 @@ class ConfigManager(QObject): for k, v in cp[secname].items(): if k.startswith(self.ESCAPE_CHAR): k = k[1:] + for c in self.VALUE_ESCAPE: + v = v.replace(self.ESCAPE_CHAR + c, c) try: self.set('conf', secname, k, v) except ValidationError as e: diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 4ebf5ec4d..6cd3a43bb 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -50,6 +50,11 @@ FIRST_COMMENT = """ # This is the default config, so if you want to remove anything from # here (as opposed to change/add), for example a keybinding, set it to # an empty value. +# +# You will need to escape the following value: +# - # at the start of the line (at the first position of the key) (\#) +# - $ in a value (\$) +# - \ in a value (\\) """