Use object registry for config.

This commit is contained in:
Florian Bruhin 2014-09-23 22:28:28 +02:00
parent 07dde5be80
commit 953e50721c
4 changed files with 27 additions and 28 deletions

View File

@ -56,7 +56,6 @@ class Application(QApplication):
registry: The object registry of global objects.
meta_registry: The object registry of object registries.
mainwindow: The MainWindow QWidget.
config: The main ConfigManager
_args: ArgumentParser instance.
_commandrunner: The main CommandRunner instance.
_debugconsole: The ConsoleWidget for debugging.
@ -94,7 +93,6 @@ class Application(QApplication):
self._keyparsers = None
self._crashdlg = None
self._crashlogfile = None
self.config = None
self.keyconfig = None
sys.excepthook = self._exception_hook
@ -169,8 +167,8 @@ class Application(QApplication):
else:
confdir = self._args.confdir
try:
self.config = config.ConfigManager(confdir, 'qutebrowser.conf',
self)
config_obj = config.ConfigManager(
confdir, 'qutebrowser.conf', self)
except (configtypes.ValidationError,
config.NoOptionError,
config.NoSectionError,
@ -190,6 +188,8 @@ class Application(QApplication):
msgbox.exec_()
# We didn't really initialize much so far, so we just quit hard.
sys.exit(1)
else:
self.registry['config'] = config_obj
try:
self.keyconfig = keyconfparser.KeyConfigParser(
confdir, 'keys.conf')
@ -346,7 +346,7 @@ class Application(QApplication):
if tabbedbrowser.count() == 0:
log.init.debug("Opening startpage")
for urlstr in self.config.get('general', 'startpage'):
for urlstr in config.get('general', 'startpage'):
try:
url = urlutils.fuzzy_url(urlstr)
except urlutils.FuzzyUrlError as e:
@ -385,6 +385,7 @@ class Application(QApplication):
prompter = self.registry['prompter']
cmd_history = self.registry['cmd_history']
downloadmanager = self.registry['downloadmanager']
config_obj = self.registry['config']
# misc
self.lastWindowClosed.connect(self.shutdown)
@ -423,10 +424,10 @@ class Application(QApplication):
Qt.DirectConnection)
# config
self.config.style_changed.connect(style.get_stylesheet.cache_clear)
config_obj.style_changed.connect(style.get_stylesheet.cache_clear)
for obj in (tabs, completion, mainwin, cmd_history,
websettings, modeman, status, status.txt):
self.config.changed.connect(obj.on_config_changed)
config_obj.changed.connect(obj.on_config_changed)
for obj in kp.values():
self.keyconfig.changed.connect(obj.on_keyconfig_changed)
@ -767,11 +768,16 @@ class Application(QApplication):
except KeyError:
pass
# Save everything
if hasattr(self, 'config') and self.config is not None:
try:
config_obj = self.registry['config']
except KeyError:
log.destroy.debug("Config not initialized yet, so not saving "
"anything.")
else:
to_save = []
if self.config.get('general', 'auto-save-config'):
if config.get('general', 'auto-save-config'):
if hasattr(self, 'config'):
to_save.append(("config", self.config.save))
to_save.append(("config", config_obj.save))
if hasattr(self, 'keyconfig'):
to_save.append(("keyconfig", self.keyconfig.save))
to_save += [("window geometry", self._save_geometry),
@ -802,9 +808,6 @@ class Application(QApplication):
except AttributeError as e:
log.destroy.warning("Could not save {}.".format(what))
log.destroy.debug(e)
else:
log.destroy.debug("Config not initialized yet, so not saving "
"anything.")
# Re-enable faulthandler to stdout, then remove crash log
log.destroy.debug("Deactiving crash log...")
self._destroy_crashlogfile()

View File

@ -30,28 +30,23 @@ import functools
import configparser
import collections.abc
from PyQt5.QtCore import pyqtSignal, QObject, QCoreApplication
from PyQt5.QtCore import pyqtSignal, QObject
from qutebrowser.utils import log
from qutebrowser.config import configdata, iniparsers, configtypes, textwrapper
from qutebrowser.commands import cmdexc, cmdutils
from qutebrowser.utils import message
from qutebrowser.utils import message, utils
from qutebrowser.utils.usertypes import Completion
def instance():
"""Get the global config instance."""
return QCoreApplication.instance().config
def get(*args, **kwargs):
"""Convenience method to call get(...) of the config instance."""
return instance().get(*args, **kwargs)
return utils.get_object('config').get(*args, **kwargs)
def section(sect):
"""Get a config section from the global config."""
return instance()[sect]
return utils.get_object('config')[sect]
class NoSectionError(configparser.NoSectionError):

View File

@ -25,7 +25,7 @@ import jinja2
from PyQt5.QtGui import QColor
from qutebrowser.config import config
from qutebrowser.utils import log
from qutebrowser.utils import log, utils
@functools.lru_cache(maxsize=16)
@ -42,7 +42,7 @@ def get_stylesheet(template_str):
fontdict = FontDict(config.section('fonts'))
template = jinja2.Template(template_str)
return template.render(color=colordict, font=fontdict,
config=config.instance())
config=utils.get_object('config'))
def set_register_stylesheet(obj):
@ -60,7 +60,7 @@ def set_register_stylesheet(obj):
log.style.vdebug("stylesheet for {}: {}".format(
obj.__class__.__name__, qss))
obj.setStyleSheet(qss)
config.instance().changed.connect(
utils.get_object('config').changed.connect(
functools.partial(_update_stylesheet, obj))

View File

@ -23,7 +23,7 @@ from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject
from qutebrowser.config import config, configdata
from qutebrowser.commands import cmdutils
from qutebrowser.utils import usertypes, log
from qutebrowser.utils import usertypes, log, utils
from qutebrowser.models import completion as models
from qutebrowser.models.completionfilter import CompletionFilterModel as CFM
@ -69,6 +69,7 @@ class Completer(QObject):
def _init_setting_completions(self):
"""Initialize setting completion models."""
config_obj = utils.get_object('config')
self._models[usertypes.Completion.section] = CFM(
models.SettingSectionCompletionModel(self), self)
self._models[usertypes.Completion.option] = {}
@ -77,13 +78,13 @@ class Completer(QObject):
model = models.SettingOptionCompletionModel(sectname, self)
self._models[usertypes.Completion.option][sectname] = CFM(
model, self)
config.instance().changed.connect(model.on_config_changed)
config_obj.changed.connect(model.on_config_changed)
self._models[usertypes.Completion.value][sectname] = {}
for opt in configdata.DATA[sectname].keys():
model = models.SettingValueCompletionModel(sectname, opt, self)
self._models[usertypes.Completion.value][sectname][opt] = CFM(
model, self)
config.instance().changed.connect(model.on_config_changed)
config_obj.changed.connect(model.on_config_changed)
def _get_new_completion(self, parts, cursor_part):
"""Get a new completion model.