From 231b0522ca46a781e63d3ac151ec8a5ac4db7bb1 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 5 Oct 2017 09:01:33 +0200 Subject: [PATCH] Refactor write_config_py --- qutebrowser/config/configcommands.py | 5 +- qutebrowser/config/configfiles.py | 97 +++++++++++++++++++--------- 2 files changed, 69 insertions(+), 33 deletions(-) diff --git a/qutebrowser/config/configcommands.py b/qutebrowser/config/configcommands.py index 6c2880100..a97e8bc78 100644 --- a/qutebrowser/config/configcommands.py +++ b/qutebrowser/config/configcommands.py @@ -275,5 +275,6 @@ class ConfigCommands: bindings = dict(self._config.get_obj('bindings.commands')) commented = False - configfiles.write_config_py(filename, options, bindings, - commented=commented) + writer = configfiles.ConfigPyWriter(options, bindings, + commented=commented) + writer.write(filename) diff --git a/qutebrowser/config/configfiles.py b/qutebrowser/config/configfiles.py index 0aa61c149..056633ca7 100644 --- a/qutebrowser/config/configfiles.py +++ b/qutebrowser/config/configfiles.py @@ -245,59 +245,94 @@ class ConfigAPI: self._keyconfig.unbind(key, mode=mode) -def write_config_py(filename, options, bindings, *, commented): - """Write a config.py file from given values.""" - c = '#' if commented else '' - cs = '# ' if commented else '' - with open(filename, 'w', encoding='utf-8') as f: - f.write(c + "# Autogenerated config.py\n") - f.write(c + "# Documentation:\n") - f.write(c + "# qute://help/configuring.html\n") - f.write(c + "# qute://help/settings.html\n\n") - if commented: +class ConfigPyWriter: + + """Writer for config.py files from given settings.""" + + def __init__(self, options, bindings, *, commented): + self._options = options + self._bindings = bindings + self._commented = commented + + def write(self, filename): + """Write the config to the given file.""" + with open(filename, 'w', encoding='utf-8') as f: + f.write('\n'.join(self._gen_text())) + + def _line(self, line): + """Get an (optionally commented) line.""" + if self._commented: + if line.startswith('#'): + return '#' + line + else: + return '# ' + line + else: + return line + + def _gen_text(self): + """Generate a config.py with the given settings/bindings. + + Yields individual lines. + """ + yield from self._gen_header() + yield from self._gen_options() + yield from self._gen_bindings() + + def _gen_header(self): + """Generate the intiial header of the config.""" + yield self._line("# Autogenerated config.py") + yield self._line("# Documentation:") + yield self._line("# qute://help/configuring.html") + yield self._line("# qute://help/settings.html\n") + if self._commented: # When generated from an autoconfig.yml with commented=False, # we don't want to load that autoconfig.yml anymore. - f.write("## This is here so configs done via the GUI are still " - "loaded.\n") - f.write("## Remove it to not load settings done via the GUI.\n") - f.write("config.load_autoconfig()\n\n") + yield self._line("# This is here so configs done via the GUI are " + "still loaded.") + yield self._line("# Remove it to not load settings done via the " + "GUI.") + yield self._line("config.load_autoconfig()\n") else: - f.write("# Uncomment this to still load settings configured via " - "autoconfig.yml\n") - f.write("# config.load_autoconfig()\n\n") + yield self._line("# Uncomment this to still load settings " + "configured via autoconfig.yml") + yield self._line("# config.load_autoconfig()\n") - for opt, value in options: + def _gen_options(self): + """Generate the options part of the config.""" + for opt, value in self._options: if opt.name in ['bindings.commands', 'bindings.default']: continue for line in textwrap.wrap(opt.description): - f.write(c + "# {}\n".format(line)) + yield self._line("# {}".format(line)) - f.write(c + "# Type: {}\n".format(opt.typ.get_name())) + yield self._line("# Type: {}".format(opt.typ.get_name())) valid_values = opt.typ.get_valid_values() if valid_values is not None and valid_values.generate_docs: - f.write(c + "# Valid values:\n") + yield self._line("# Valid values:") for val in valid_values: try: desc = valid_values.descriptions[val] - f.write(c + "# - {}: {}".format(val, desc) + "\n") + yield self._line("# - {}: {}".format(val, desc)) except KeyError: - f.write(c + "# - {}".format(val) + "\n") + yield self._line("# - {}".format(val)) - f.write(cs + 'c.{} = {!r}\n\n'.format(opt.name, value)) + yield self._line('c.{} = {!r}\n'.format(opt.name, value)) - normal_bindings = bindings.pop('normal', {}) + def _gen_bindings(self): + """Generate the bindings part of the config.""" + normal_bindings = self._bindings.pop('normal', {}) if normal_bindings: - f.write(c + '# Bindings for normal mode\n') + yield self._line('# Bindings for normal mode') for key, command in sorted(normal_bindings.items()): - f.write(cs + 'config.bind({!r}, {!r})\n'.format(key, command)) + yield self._line('config.bind({!r}, {!r})'.format(key, command)) - for mode, mode_bindings in sorted(bindings.items()): - f.write('\n') - f.write(c + '# Bindings for {} mode\n'.format(mode)) + for mode, mode_bindings in sorted(self._bindings.items()): + yield '' + yield self._line('# Bindings for {} mode'.format(mode)) for key, command in sorted(mode_bindings.items()): - f.write(cs + 'config.bind({!r}, {!r}, mode={!r})\n'.format( + yield self._line('config.bind({!r}, {!r}, mode={!r})'.format( key, command, mode))