Add a config.source() method

This commit is contained in:
Florian Bruhin 2017-10-18 13:53:26 +02:00
parent 34787edf4e
commit 5a9042ab3e
4 changed files with 45 additions and 0 deletions

View File

@ -29,6 +29,7 @@ Added
command in the completion. command in the completion.
- New `:tab-give` and `:tab-take` commands, to give tabs to another window, or - New `:tab-give` and `:tab-take` commands, to give tabs to another window, or
take them from another window. take them from another window.
- New `config.source(...)` method for `config.py` to source another file.
Fixed Fixed
~~~~~ ~~~~~

View File

@ -238,6 +238,9 @@ that via `import utils` as well.
While it's in some cases possible to import code from the qutebrowser While it's in some cases possible to import code from the qutebrowser
installation, doing so is unsupported and discouraged. installation, doing so is unsupported and discouraged.
To read config data from a different file with `c` and `config` available, you
can use `config.source('otherfile.py')` in your `config.py`.
Getting the config directory Getting the config directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -259,6 +259,15 @@ class ConfigAPI:
with self._handle_error('unbinding', key): with self._handle_error('unbinding', key):
self._keyconfig.unbind(key, mode=mode) self._keyconfig.unbind(key, mode=mode)
def source(self, filename):
if not os.path.isabs(filename):
filename = str(self.configdir / filename)
try:
read_config_py(filename)
except configexc.ConfigFileErrors as e:
self.errors += e.errors
class ConfigPyWriter: class ConfigPyWriter:

View File

@ -580,6 +580,38 @@ class TestConfigPy:
assert isinstance(error.exception, ZeroDivisionError) assert isinstance(error.exception, ZeroDivisionError)
assert error.traceback is not None assert error.traceback is not None
@pytest.mark.parametrize('location', ['abs', 'rel'])
def test_source(self, tmpdir, confpy, location):
if location == 'abs':
subfile = tmpdir / 'subfile.py'
arg = str(subfile)
else:
subfile = tmpdir / 'config' / 'subfile.py'
arg = 'subfile.py'
subfile.write_text("c.content.javascript.enabled = False",
encoding='utf-8')
confpy.write("config.source('{}')".format(arg))
confpy.read()
assert not config.instance._values['content.javascript.enabled']
def test_source_errors(self, tmpdir, confpy):
subfile = tmpdir / 'config' / 'subfile.py'
subfile.write_text("c.foo = 42", encoding='utf-8')
confpy.write("config.source('subfile.py')")
error = confpy.read(error=True)
assert error.text == "While setting 'foo'"
assert isinstance(error.exception, configexc.NoOptionError)
def test_source_not_found(self, confpy):
confpy.write("config.source('doesnotexist.py')")
error = confpy.read(error=True)
assert error.text == "Error while reading doesnotexist.py"
assert isinstance(error.exception, FileNotFoundError)
class TestConfigPyWriter: class TestConfigPyWriter: