Initial :config-write-py implementation

This commit is contained in:
Florian Bruhin 2017-10-04 11:14:24 +02:00
parent f18b730f24
commit 7fc5e42cca
3 changed files with 58 additions and 4 deletions

View File

@ -223,6 +223,11 @@ class Config(QObject):
self._mutables = {}
self._yaml = yaml_config
def __iter__(self):
"""Iterate over Option, value tuples."""
for name, value in sorted(self._values.items()):
yield (self.get_opt(name), value)
def init_save_manager(self, save_manager):
"""Make sure the config gets saved properly.
@ -365,10 +370,9 @@ class Config(QObject):
The changed config part as string.
"""
lines = []
for optname, value in sorted(self._values.items()):
opt = self.get_opt(optname)
for opt, value in self:
str_value = opt.typ.to_str(value)
lines.append('{} = {}'.format(optname, str_value))
lines.append('{} = {}'.format(opt.name, str_value))
if not lines:
lines = ['<Default configuration>']
return '\n'.join(lines)

View File

@ -27,7 +27,7 @@ from PyQt5.QtCore import QUrl
from qutebrowser.commands import cmdexc, cmdutils
from qutebrowser.completion.models import configmodel
from qutebrowser.utils import objreg, utils, message, standarddir
from qutebrowser.config import configtypes, configexc, configfiles
from qutebrowser.config import configtypes, configexc, configfiles, configdata
from qutebrowser.misc import editor
@ -246,3 +246,31 @@ class ConfigCommands:
filename = os.path.join(standarddir.config(), 'config.py')
ed.edit_file(filename)
@cmdutils.register(instance='config-commands')
def config_write_py(self, filename=None, force=False, defaults=False):
"""Write the current configuration to a config.py file.
Args:
filename: The file to write to, or None for the default config.py.
force: Force overwriting existing files.
defaults: Write the defaults instead of values configured via :set.
"""
if filename is None:
filename = os.path.join(standarddir.config(), 'config.py')
else:
filename = os.path.expanduser(filename)
if os.path.exists(filename) and not force:
raise cmdexc.CommandError("{} already exists - use --force to "
"overwrite!".format(filename))
if defaults:
options = [(opt, opt.default)
for _name, opt in sorted(configdata.DATA.items())]
bindings = dict(configdata.DATA['bindings.default'].default)
else:
options = list(self._config)
bindings = dict(self._config.get_obj('bindings.commands'))
configfiles.write_config_py(filename, options, bindings)

View File

@ -245,6 +245,28 @@ class ConfigAPI:
self._keyconfig.unbind(key, mode=mode)
def write_config_py(filename, options, bindings):
"""Write a config.py file from given values."""
with open(filename, 'w', encoding='utf-8') as f:
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.{} = {!r}\n\n'.format(opt.name, value))
normal_bindings = bindings.pop('normal', {})
f.write('# Bindings for normal mode\n')
for key, command in sorted(normal_bindings.items()):
f.write('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))
for key, command in sorted(mode_bindings.items()):
f.write('config.bind({!r}, {!r}, mode={!r})\n'.format(
key, command, mode))
def read_config_py(filename, raising=False):
"""Read a config.py file.