2014-02-06 14:01:23 +01:00
|
|
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-02-18 14:37:49 +01:00
|
|
|
"""Configuration storage and config-related utilities."""
|
2014-02-17 12:23:52 +01:00
|
|
|
|
2014-01-27 21:42:00 +01:00
|
|
|
import os
|
2014-01-31 10:24:00 +01:00
|
|
|
import io
|
2014-02-17 12:23:52 +01:00
|
|
|
import os.path
|
2014-01-27 21:42:00 +01:00
|
|
|
import logging
|
2014-02-18 14:34:46 +01:00
|
|
|
from configparser import (ConfigParser, ExtendedInterpolation, NoSectionError,
|
|
|
|
NoOptionError)
|
2014-01-27 21:42:00 +01:00
|
|
|
|
2014-02-18 10:07:52 +01:00
|
|
|
from qutebrowser.utils.misc import read_file
|
|
|
|
|
2014-01-28 12:21:00 +01:00
|
|
|
config = None
|
2014-02-18 11:57:35 +01:00
|
|
|
state = None
|
2014-01-28 12:21:00 +01:00
|
|
|
colordict = {}
|
2014-02-10 07:08:52 +01:00
|
|
|
fontdict = {}
|
2014-01-28 12:21:00 +01:00
|
|
|
|
2014-02-18 14:34:46 +01:00
|
|
|
# Special value for an unset fallback, so None can be passed as fallback.
|
|
|
|
_UNSET = object()
|
|
|
|
|
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-02-18 11:57:35 +01:00
|
|
|
global config, state, colordict, fontdict
|
|
|
|
logging.debug("Config init, confdir {}".format(confdir))
|
|
|
|
config = Config(confdir, 'qutebrowser.conf', read_file('qutebrowser.conf'))
|
|
|
|
state = Config(confdir, 'state', always_save=True)
|
2014-01-28 12:21:00 +01:00
|
|
|
try:
|
|
|
|
colordict = ColorDict(config['colors'])
|
|
|
|
except KeyError:
|
|
|
|
colordict = ColorDict()
|
2014-02-10 07:03:51 +01:00
|
|
|
fontdict = FontDict(config['fonts'])
|
2014-01-28 12:21:00 +01:00
|
|
|
|
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-02-10 07:03:51 +01:00
|
|
|
return template.strip().format(color=colordict, font=fontdict)
|
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-02-07 20:21:50 +01:00
|
|
|
|
2014-01-29 15:30:19 +01:00
|
|
|
"""A dict aimed at Qt stylesheet colors."""
|
|
|
|
|
2014-02-12 23:28:03 +01:00
|
|
|
# FIXME we should validate colors in __setitem__ based on:
|
|
|
|
# http://qt-project.org/doc/qt-4.8/stylesheet-reference.html#brush
|
|
|
|
# http://www.w3.org/TR/CSS21/syndata.html#color-units
|
|
|
|
|
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-02-07 20:21:50 +01:00
|
|
|
"""
|
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__.
|
|
|
|
|
2014-02-13 18:57:19 +01:00
|
|
|
Return a value, or None if the value wasn't found.
|
2014-02-07 20:21:50 +01:00
|
|
|
|
2014-01-29 15:30:19 +01:00
|
|
|
"""
|
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-02-10 07:03:51 +01:00
|
|
|
class FontDict(dict):
|
|
|
|
|
|
|
|
"""A dict aimed at Qt stylesheet fonts."""
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
"""Override dict __getitem__.
|
|
|
|
|
|
|
|
If a value wasn't found, return an empty string.
|
|
|
|
(Color not defined, so no output in the stylesheet)
|
|
|
|
|
|
|
|
In all other cases, return font: <value>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
val = super().__getitem__(key)
|
|
|
|
except KeyError:
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
return 'font: {};'.format(val)
|
|
|
|
|
|
|
|
def getraw(self, key):
|
|
|
|
"""Get a value without the transformations done in __getitem__.
|
|
|
|
|
2014-02-13 18:57:19 +01:00
|
|
|
Return a value, or None if the value wasn't found.
|
2014-02-10 07:03:51 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return super().__getitem__(key)
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2014-02-18 14:21:39 +01:00
|
|
|
|
2014-01-27 21:42:00 +01:00
|
|
|
class Config(ConfigParser):
|
2014-02-07 20:21:50 +01:00
|
|
|
|
2014-01-29 15:30:19 +01:00
|
|
|
"""Our own ConfigParser subclass."""
|
|
|
|
|
2014-01-27 21:42:00 +01:00
|
|
|
configdir = None
|
2014-01-30 23:05:39 +01:00
|
|
|
default_cp = None
|
|
|
|
config_loaded = False
|
2014-01-27 21:42:00 +01:00
|
|
|
|
2014-02-18 11:57:35 +01:00
|
|
|
def __init__(self, configdir, fname, default_config=None,
|
|
|
|
always_save=False):
|
2014-01-29 15:30:19 +01:00
|
|
|
"""Config constructor.
|
|
|
|
|
|
|
|
configdir -- directory to store the config in.
|
2014-02-18 11:57:35 +01:00
|
|
|
fname -- Filename of the config file.
|
|
|
|
default_config -- Default config as string.
|
|
|
|
always_save -- Whether to always save the config, even when it wasn't
|
|
|
|
loaded.
|
2014-02-07 20:21:50 +01:00
|
|
|
|
2014-01-29 15:30:19 +01:00
|
|
|
"""
|
2014-02-07 14:01:17 +01:00
|
|
|
super().__init__(interpolation=ExtendedInterpolation())
|
2014-02-18 11:57:35 +01:00
|
|
|
self.always_save = always_save
|
|
|
|
self.configdir = configdir
|
2014-02-07 14:01:17 +01:00
|
|
|
self.default_cp = ConfigParser(interpolation=ExtendedInterpolation())
|
2014-01-30 23:05:39 +01:00
|
|
|
self.default_cp.optionxform = lambda opt: opt # be case-insensitive
|
2014-02-18 11:57:35 +01:00
|
|
|
if default_config is not None:
|
|
|
|
self.default_cp.read_string(default_config)
|
2014-02-01 20:55:37 +01:00
|
|
|
if not self.configdir:
|
|
|
|
return
|
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
|
2014-02-18 11:57:35 +01:00
|
|
|
self.configfile = os.path.join(self.configdir, fname)
|
2014-01-27 21:42:00 +01:00
|
|
|
if not os.path.isfile(self.configfile):
|
2014-01-30 23:05:39 +01:00
|
|
|
return
|
2014-01-27 21:42:00 +01:00
|
|
|
logging.debug("Reading config from {}".format(self.configfile))
|
|
|
|
self.read(self.configfile)
|
2014-01-30 23:05:39 +01:00
|
|
|
self.config_loaded = True
|
2014-01-27 21:42:00 +01:00
|
|
|
|
2014-01-30 23:05:39 +01:00
|
|
|
def __getitem__(self, key):
|
|
|
|
"""Get an item from the configparser or default dict.
|
|
|
|
|
2014-02-13 18:57:19 +01:00
|
|
|
Extend ConfigParser's __getitem__.
|
2014-02-07 20:21:50 +01:00
|
|
|
|
2014-01-30 23:05:39 +01:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return super().__getitem__(key)
|
|
|
|
except KeyError:
|
|
|
|
return self.default_cp[key]
|
|
|
|
|
2014-02-18 14:34:46 +01:00
|
|
|
def get(self, *args, raw=False, vars=None, fallback=_UNSET):
|
2014-01-30 23:05:39 +01:00
|
|
|
"""Get an item from the configparser or default dict.
|
|
|
|
|
2014-02-13 18:57:19 +01:00
|
|
|
Extend ConfigParser's get().
|
2014-02-07 20:21:50 +01:00
|
|
|
|
2014-02-18 11:57:19 +01:00
|
|
|
This is a bit of a hack, but it (hopefully) works like this:
|
|
|
|
- Get value from original configparser.
|
|
|
|
- If that's not available, try the default_cp configparser
|
|
|
|
- If that's not available, try the fallback given as kwarg
|
|
|
|
- If that's not available, we're doomed.
|
|
|
|
|
2014-01-30 23:05:39 +01:00
|
|
|
"""
|
2014-02-18 14:34:46 +01:00
|
|
|
# pylint: disable=redefined-builtin
|
|
|
|
try:
|
|
|
|
return super().get(*args, raw=raw, vars=vars)
|
|
|
|
except (NoSectionError, NoOptionError):
|
|
|
|
pass
|
2014-02-18 11:57:19 +01:00
|
|
|
try:
|
2014-02-18 14:34:46 +01:00
|
|
|
return self.default_cp.get(*args, raw=raw, vars=vars)
|
|
|
|
except (NoSectionError, NoOptionError):
|
|
|
|
if fallback is _UNSET:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
return fallback
|
2014-01-27 21:42:00 +01:00
|
|
|
|
|
|
|
def save(self):
|
2014-01-29 15:30:19 +01:00
|
|
|
"""Save the config file."""
|
2014-02-18 11:57:19 +01:00
|
|
|
if self.configdir is None or (not self.config_loaded and
|
|
|
|
not self.always_save):
|
|
|
|
logging.error("Not saving config (dir {}, loaded {})".format(
|
|
|
|
self.configdir, 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 15:58:14 +01:00
|
|
|
f.flush()
|
|
|
|
os.fsync(f.fileno())
|
2014-01-31 10:24:00 +01:00
|
|
|
|
|
|
|
def dump_userconfig(self):
|
2014-02-07 20:21:50 +01:00
|
|
|
"""Return the part of the config which was changed by the user."""
|
2014-01-31 10:24:00 +01:00
|
|
|
with io.StringIO() as f:
|
|
|
|
self.write(f)
|
|
|
|
return f.getvalue()
|