Add a recipes section to configuring.asciidoc
Closes #2987 Closes #2969 Closes #3009 See #2975
This commit is contained in:
parent
38449e3e2b
commit
6e226c6885
@ -262,3 +262,106 @@ qutebrowser tries to display errors which are easy to understand even for people
|
|||||||
who are not used to writing Python. If you see a config error which you find
|
who are not used to writing Python. If you see a config error which you find
|
||||||
confusing or you think qutebrowser could handle better, please
|
confusing or you think qutebrowser could handle better, please
|
||||||
https://github.com/qutebrowser/qutebrowser/issues[open an issue]!
|
https://github.com/qutebrowser/qutebrowser/issues[open an issue]!
|
||||||
|
|
||||||
|
Recipes
|
||||||
|
~~~~~~~
|
||||||
|
|
||||||
|
Reading a YAML file
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
To read a YAML config like this:
|
||||||
|
|
||||||
|
.config.yml:
|
||||||
|
----
|
||||||
|
tabs.position: left
|
||||||
|
tabs.show: switching
|
||||||
|
----
|
||||||
|
|
||||||
|
You can use:
|
||||||
|
|
||||||
|
.config.py:
|
||||||
|
[source,python]
|
||||||
|
----
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
with (config.configdir / 'config.yml').open() as f:
|
||||||
|
yaml_data = yaml.load(f)
|
||||||
|
|
||||||
|
for k, v in yaml_data.items():
|
||||||
|
config.set(k, v)
|
||||||
|
----
|
||||||
|
|
||||||
|
Reading a nested YAML file
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
To read a YAML file with nested values like this:
|
||||||
|
|
||||||
|
.colors.yml:
|
||||||
|
----
|
||||||
|
colors:
|
||||||
|
statusbar:
|
||||||
|
normal:
|
||||||
|
bg: lime
|
||||||
|
fg: black
|
||||||
|
url:
|
||||||
|
fg: red
|
||||||
|
----
|
||||||
|
|
||||||
|
You can use:
|
||||||
|
|
||||||
|
.config.py:
|
||||||
|
[source,python]
|
||||||
|
----
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
with (config.configdir / 'colors.yml').open() as f:
|
||||||
|
yaml_data = yaml.load(f)
|
||||||
|
|
||||||
|
def dict_attrs(obj, path=''):
|
||||||
|
if isinstance(obj, dict):
|
||||||
|
for k, v in obj.items():
|
||||||
|
yield from dict_attrs(v, '{}.{}'.format(path, k) if path else k)
|
||||||
|
else:
|
||||||
|
yield path, obj
|
||||||
|
|
||||||
|
for k, v in dict_attrs(yaml_data):
|
||||||
|
config.set(k, v)
|
||||||
|
----
|
||||||
|
|
||||||
|
Note that this won't work for values which are dictionaries.
|
||||||
|
|
||||||
|
Binding chained commands
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
If you have a lot of chained comamnds you want to bind, you can write a helper
|
||||||
|
to do so:
|
||||||
|
|
||||||
|
[source,python]
|
||||||
|
----
|
||||||
|
def bind_chained(key, *commands, force=False):
|
||||||
|
config.bind(key, ' ;; '.join(commands), force=force)
|
||||||
|
|
||||||
|
bind_chained('<Escape>', 'clear-keychain', 'search', force=True)
|
||||||
|
----
|
||||||
|
|
||||||
|
Avoiding flake8 errors
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
If you use an editor with flake8 integration which complains about `c` and `config` being undefined, you can use:
|
||||||
|
|
||||||
|
[source,python]
|
||||||
|
----
|
||||||
|
c = c # noqa: F821
|
||||||
|
config = config # noqa: F821
|
||||||
|
----
|
||||||
|
|
||||||
|
For type annotation support (note that those imports aren't guaranteed to be
|
||||||
|
stable across qutebrowser versions):
|
||||||
|
|
||||||
|
[source,python]
|
||||||
|
----
|
||||||
|
from qutebrowser.config.configfiles import ConfigAPI # noqa: F401
|
||||||
|
from qutebrowser.config.config import ConfigContainer # noqa: F401
|
||||||
|
config = config # type: ConfigAPI # noqa: F821
|
||||||
|
c = c # type: ConfigContainer # noqa: F821
|
||||||
|
----
|
||||||
|
Loading…
Reference in New Issue
Block a user