qutebrowser/qutebrowser/utils/config.py

198 lines
5.4 KiB
Python
Raw Normal View History

2014-01-29 15:30:19 +01:00
"""Configuration storage and config-related utilities.
config -- The main Config object.
colordict -- All configured colors.
default_config -- The default config as dict.
MONOSPACE -- A list of suitable monospace fonts.
"""
2014-01-27 21:42:00 +01:00
import os.path
import os
2014-01-31 10:24:00 +01:00
import io
2014-01-27 21:42:00 +01:00
import logging
from configparser import ConfigParser
2014-01-28 12:21:00 +01:00
config = None
colordict = {}
2014-01-30 08:25:49 +01:00
default_config = """
[general]
show_completion = true
space_scroll = 200
ignorecase = true
wrapsearch = true
startpage = http://www.duckduckgo.com/
2014-01-30 08:25:49 +01:00
[keybind]
o = open
O = tabopen
d = tabclose
J = tabnext
K = tabprev
r = reload
H = back
L = forward
h = scroll -50 0
j = scroll 0 50
k = scroll 0 -50
l = scroll 50 0
u = undo
gg = scroll_perc_y 0
G = scroll_perc_y
n = nextsearch
yy = yank
yY = yank sel
yt = yanktitle
yT = yanktitle sel
pp = paste
pP = paste sel
Pp = tabpaste
PP = tabpaste sel
[colors]
completion.fg = #333333
completion.item.bg = white
completion.category.bg = qlineargradient(x1:0, y1:0, x2:0, y2:1,
stop:0 #e4e4e4, stop:1 #dbdbdb)
completion.category.border.top = #808080
completion.category.border.bottom = #bbbbbb
completion.item.selected.fg = #333333
completion.item.selected.bg = #ffec8b
completion.item.selected.border.top = #f2f2c0
completion.item.selected.border.bottom = #e6e680
completion.match.fg = red
statusbar.progress.bg = white
statusbar.progress.bg.error = red
statusbar.bg = black
statusbar.fg = white
statusbar.bg.error = red
tab.bg = grey
tab.bg.selected = black
tab.fg = white
tab.seperator = white
"""
2014-01-27 21:42:00 +01:00
2014-01-28 15:20:58 +01:00
_MONOSPACE = ['Monospace', 'DejaVu Sans Mono', 'Consolas', 'Monaco',
'Bitstream Vera Sans Mono', 'Andale Mono', 'Liberation Mono',
'Courier New', 'Courier', 'monospace', 'Fixed', 'Terminal']
MONOSPACE = ', '.join(_MONOSPACE)
2014-01-28 23:04:02 +01:00
2014-01-28 12:21:00 +01:00
def init(confdir):
2014-01-29 15:30:19 +01:00
"""Initialize the global objects based on the config in configdir."""
2014-01-28 12:21:00 +01:00
global config, colordict
config = Config(confdir)
try:
colordict = ColorDict(config['colors'])
except KeyError:
colordict = ColorDict()
2014-01-28 23:04:02 +01:00
2014-01-28 12:21:00 +01:00
def get_stylesheet(template):
2014-01-29 15:30:19 +01:00
"""Return a formatted stylesheet based on a template."""
2014-01-28 15:20:58 +01:00
return template.strip().format(color=colordict, monospace=MONOSPACE)
2014-01-28 12:21:00 +01:00
2014-01-28 23:04:02 +01:00
2014-01-28 12:21:00 +01:00
class ColorDict(dict):
2014-01-29 15:30:19 +01:00
"""A dict aimed at Qt stylesheet colors."""
2014-01-28 12:21:00 +01:00
def __getitem__(self, key):
2014-01-29 15:30:19 +01:00
"""Override dict __getitem__.
If a value wasn't found, return an empty string.
(Color not defined, so no output in the stylesheet)
If the key has a .fg. element in it, return color: X;.
If the key has a .bg. element in it, return background-color: X;.
In all other cases, return the plain value.
"""
2014-01-28 12:21:00 +01:00
try:
val = super().__getitem__(key)
except KeyError:
return ''
if 'fg' in key.split('.'):
return 'color: {};'.format(val)
elif 'bg' in key.split('.'):
return 'background-color: {};'.format(val)
else:
return val
def getraw(self, key):
2014-01-29 15:30:19 +01:00
"""Get a value without the transformations done in __getitem__.
Returns a value, or None if the value wasn't found.
"""
2014-01-28 12:21:00 +01:00
try:
return super().__getitem__(key)
except KeyError:
return None
2014-01-28 23:04:02 +01:00
2014-01-27 21:42:00 +01:00
class Config(ConfigParser):
2014-01-29 15:30:19 +01:00
"""Our own ConfigParser subclass."""
2014-01-27 21:42:00 +01:00
configdir = None
FNAME = 'config'
default_cp = None
config_loaded = False
2014-01-27 21:42:00 +01:00
def __init__(self, configdir):
2014-01-29 15:30:19 +01:00
"""Config constructor.
configdir -- directory to store the config in.
"""
2014-01-27 21:42:00 +01:00
super().__init__()
self.default_cp = ConfigParser()
self.default_cp.optionxform = lambda opt: opt # be case-insensitive
self.default_cp.read_string(default_config)
2014-01-28 23:04:02 +01:00
self.optionxform = lambda opt: opt # be case-insensitive
2014-01-27 21:42:00 +01:00
self.configdir = configdir
self.configfile = os.path.join(self.configdir, self.FNAME)
if not os.path.isfile(self.configfile):
return
2014-01-27 21:42:00 +01:00
logging.debug("Reading config from {}".format(self.configfile))
self.read(self.configfile)
self.config_loaded = True
2014-01-27 21:42:00 +01:00
def __getitem__(self, key):
"""Get an item from the configparser or default dict.
Extends ConfigParser's __getitem__.
"""
try:
return super().__getitem__(key)
except KeyError:
return self.default_cp[key]
def get(self, *args, **kwargs):
"""Get an item from the configparser or default dict.
Extends ConfigParser's get().
"""
if 'fallback' in kwargs:
del kwargs['fallback']
fallback = self.default_cp.get(*args, **kwargs)
return super().get(*args, fallback=fallback, **kwargs)
2014-01-27 21:42:00 +01:00
def save(self):
2014-01-29 15:30:19 +01:00
"""Save the config file."""
if self.configdir is None or not self.config_loaded:
2014-01-27 21:42:00 +01:00
return
if not os.path.exists(self.configdir):
os.makedirs(self.configdir, 0o755)
logging.debug("Saving config to {}".format(self.configfile))
with open(self.configfile, 'w') as f:
self.write(f)
2014-01-31 10:24:00 +01:00
def dump_userconfig(self):
"""Returns the part of the config which was changed by the user as a
string.
"""
with io.StringIO() as f:
self.write(f)
return f.getvalue()