Add a :config-source command

See #2794
This commit is contained in:
Florian Bruhin 2017-10-03 16:28:11 +02:00
parent 0695cfccfc
commit 8edaad51c3
3 changed files with 78 additions and 2 deletions

View File

@ -33,6 +33,7 @@ It is possible to run or bind multiple commands by separating them with `;;`.
|<<close,close>>|Close the current window.
|<<config-clear,config-clear>>|Set all settings back to their default.
|<<config-cycle,config-cycle>>|Cycle an option between multiple values.
|<<config-source,config-source>>|Read a config.py file.
|<<config-unset,config-unset>>|Unset an option.
|<<download,download>>|Download a given URL, or current page if no URL given.
|<<download-cancel,download-cancel>>|Cancel the last/[count]th download.
@ -228,6 +229,19 @@ Cycle an option between multiple values.
* +*-t*+, +*--temp*+: Set value temporarily until qutebrowser is closed.
* +*-p*+, +*--print*+: Print the value after setting.
[[config-source]]
=== config-source
Syntax: +:config-source [*--clear*] ['filename']+
Read a config.py file.
==== positional arguments
* +'filename'+: The file to load. If not given, loads the default config.py.
==== optional arguments
* +*-c*+, +*--clear*+: Clear current settings first.
[[config-unset]]
=== config-unset
Syntax: +:config-unset [*--temp*] 'option'+

View File

@ -19,14 +19,15 @@
"""Commands related to the configuration."""
import os.path
import contextlib
from PyQt5.QtCore import QUrl
from qutebrowser.commands import cmdexc, cmdutils
from qutebrowser.completion.models import configmodel
from qutebrowser.utils import objreg, utils, message
from qutebrowser.config import configtypes, configexc
from qutebrowser.utils import objreg, utils, message, standarddir
from qutebrowser.config import configtypes, configexc, configfiles
class ConfigCommands:
@ -203,3 +204,25 @@ class ConfigCommands:
removed.
"""
self._config.clear(save_yaml=save)
@cmdutils.register(instance='config-commands')
def config_source(self, filename=None, clear=False):
"""Read a config.py file.
Args:
filename: The file to load. If not given, loads the default
config.py.
clear: Clear current settings first.
"""
if filename is None:
filename = os.path.join(standarddir.config(), 'config.py')
else:
filename = os.path.expanduser(filename)
if clear:
self.config_clear()
try:
configfiles.read_config_py(filename)
except configexc.ConfigFileErrors as e:
raise cmdexc.CommandError(e)

View File

@ -263,6 +263,45 @@ class TestUnsetAndClear:
assert config_stub._yaml[name] == 'never'
class TestSource:
"""Test :config-source."""
pytestmark = pytest.mark.usefixtures('config_tmpdir', 'data_tmpdir')
@pytest.mark.parametrize('use_default_dir', [True, False])
@pytest.mark.parametrize('clear', [True, False])
def test_config_source(self, tmpdir, commands, config_stub, config_tmpdir,
use_default_dir, clear):
assert config_stub.val.content.javascript.enabled
config_stub.val.ignore_case = 'always'
if use_default_dir:
pyfile = config_tmpdir / 'config.py'
arg = None
else:
pyfile = tmpdir / 'sourced.py'
arg = str(pyfile)
pyfile.write_text('c.content.javascript.enabled = False\n',
encoding='utf-8')
commands.config_source(arg, clear=clear)
assert not config_stub.val.content.javascript.enabled
assert config_stub.val.ignore_case == ('smart' if clear else 'always')
def test_errors(self, commands, config_tmpdir):
pyfile = config_tmpdir / 'config.py'
pyfile.write_text('c.foo = 42', encoding='utf-8')
with pytest.raises(cmdexc.CommandError) as excinfo:
commands.config_source()
expected = ("Errors occurred while reading config.py:\n"
" While setting 'foo': No option 'foo'")
assert str(excinfo.value) == expected
class TestBind:
"""Tests for :bind and :unbind."""