Hopefully sane default config handling.

This now reads the values from the config, and from a default config if
it's not overridden.
This commit is contained in:
Florian Bruhin 2014-01-30 23:05:39 +01:00
parent a6db387ef7
commit 053ce35032
4 changed files with 30 additions and 25 deletions

3
TODO
View File

@ -31,9 +31,6 @@ Marks
open with preset url (go)
show infos in statusline, temporary/longer
set settings/colors/bindings via commandline
sane default config handling (default always loaded, userconfig overrides, provide unbind)
check if settings fallbacks/KeyError catching is needed (default always
should be there)
write default config with comments
proper exception handling with saving pages

View File

@ -106,8 +106,8 @@ class QuteBrowser(QApplication):
if not opened_urls:
logging.debug('Opening startpage')
for url in config.config.get('general', 'startpage',
fallback='http://ddg.gg/').split(','):
# pylint: disable=maybe-no-member
for url in config.config.get('general', 'startpage').split(','):
self.mainwindow.tabs.tabopen(url)
def _tmp_exception_hook(self, exctype, excvalue, tb):

View File

@ -136,6 +136,8 @@ class Config(ConfigParser):
configdir = None
FNAME = 'config'
default_cp = None
config_loaded = False
def __init__(self, configdir):
"""Config constructor.
@ -143,34 +145,41 @@ class Config(ConfigParser):
configdir -- directory to store the config in.
"""
super().__init__()
self.default_cp = ConfigParser()
self.default_cp.optionxform = lambda opt: opt # be case-insensitive
self.default_cp.read_string(default_config)
self.optionxform = lambda opt: opt # be case-insensitive
self.configdir = configdir
if self.configdir is None:
self.init_config()
return
self.configfile = os.path.join(self.configdir, self.FNAME)
if not os.path.isfile(self.configfile):
self.init_config()
return
logging.debug("Reading config from {}".format(self.configfile))
self.read(self.configfile)
self.config_loaded = True
def init_config(self):
"""Initialize Config from default_config and save it."""
logging.info("Initializing default config.")
if self.configdir is None:
self.read_string(default_config)
return
cp = ConfigParser()
cp.optionxform = lambda opt: opt # be case-insensitive
cp.read_string(default_config)
if not os.path.exists(self.configdir):
os.makedirs(self.configdir, 0o755)
with open(self.configfile, 'w') as f:
cp.write(f)
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)
def save(self):
"""Save the config file."""
if self.configdir is None:
if self.configdir is None or not self.config_loaded:
return
if not os.path.exists(self.configdir):
os.makedirs(self.configdir, 0o755)

View File

@ -67,8 +67,7 @@ class CompletionView(QTreeView):
def __init__(self, parent=None):
super().__init__(parent)
self.enabled = config.config.getboolean('general', 'show_completion',
fallback=True)
self.enabled = config.config.getboolean('general', 'show_completion')
self.completion_models[''] = None
self.completion_models['command'] = CommandCompletionModel()
self.model = CompletionFilterModel()