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) open with preset url (go)
show infos in statusline, temporary/longer show infos in statusline, temporary/longer
set settings/colors/bindings via commandline 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 write default config with comments
proper exception handling with saving pages proper exception handling with saving pages

View File

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

View File

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

View File

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