Write commented lines with :write-config-py when writing defaults

This commit is contained in:
Florian Bruhin 2017-10-05 08:38:35 +02:00
parent 9257538dcf
commit 8dc34cf78a
2 changed files with 34 additions and 17 deletions

View File

@ -269,8 +269,11 @@ class ConfigCommands:
options = [(opt, opt.default)
for _name, opt in sorted(configdata.DATA.items())]
bindings = dict(configdata.DATA['bindings.default'].default)
commented = True
else:
options = list(self._config)
bindings = dict(self._config.get_obj('bindings.commands'))
commented = False
configfiles.write_config_py(filename, options, bindings)
configfiles.write_config_py(filename, options, bindings,
commented=commented)

View File

@ -245,45 +245,59 @@ class ConfigAPI:
self._keyconfig.unbind(key, mode=mode)
def write_config_py(filename, options, bindings):
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("# Autogenerated config.py - please remove lines you don't "
"customize!\n")
f.write("# Documentation:\n")
f.write("# qute://help/configuring.html\n")
f.write("# qute://help/settings.html\n\n")
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:
# 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")
else:
f.write("# Uncomment this to still load settings configured via "
"autoconfig.yml\n")
f.write("# config.load_autoconfig()\n\n")
for opt, value in options:
if opt.name in ['bindings.commands', 'bindings.default']:
continue
for line in textwrap.wrap(opt.description):
f.write('# {}\n'.format(line))
f.write(c + "# {}\n".format(line))
f.write('# Type: {}\n'.format(opt.typ.get_name()))
f.write(c + "# Type: {}\n".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("# Valid values:\n")
f.write(c + "# Valid values:\n")
for val in valid_values:
try:
desc = valid_values.descriptions[val]
f.write("# - {}: {}".format(val, desc) + "\n")
f.write(c + "# - {}: {}".format(val, desc) + "\n")
except KeyError:
f.write("# - {}".format(val) + "\n")
f.write(c + "# - {}".format(val) + "\n")
f.write('c.{} = {!r}\n\n'.format(opt.name, value))
f.write(cs + 'c.{} = {!r}\n\n'.format(opt.name, value))
normal_bindings = bindings.pop('normal', {})
f.write('# Bindings for normal mode\n')
if normal_bindings:
f.write(c + '# Bindings for normal mode\n')
for key, command in sorted(normal_bindings.items()):
f.write('config.bind({!r}, {!r})\n'.format(key, command))
f.write(cs + 'config.bind({!r}, {!r})\n'.format(key, command))
for mode, mode_bindings in sorted(bindings.items()):
f.write('\n# Bindings for {} mode\n'.format(mode))
f.write('\n')
f.write(c + '# Bindings for {} mode\n'.format(mode))
for key, command in sorted(mode_bindings.items()):
f.write('config.bind({!r}, {!r}, mode={!r})\n'.format(
f.write(cs + 'config.bind({!r}, {!r}, mode={!r})\n'.format(
key, command, mode))