From 86365625792a6faf59c252bffb5c36b44ecc3a19 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 28 Sep 2014 00:27:22 +0200 Subject: [PATCH] Move config init out of Application --- doc/TODO | 1 - qutebrowser/app.py | 60 ++-------------------------------- qutebrowser/config/config.py | 62 ++++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 60 deletions(-) diff --git a/doc/TODO b/doc/TODO index 98d78bc14..685e3901a 100644 --- a/doc/TODO +++ b/doc/TODO @@ -130,7 +130,6 @@ style - Always use a list as argument for namedtuple? - Refactor enum() so it uses a list as second argument (like python enum/namedtuple). -- Config init and some persistent objects should be moved out of QApplication. - Use separate logger for config - Use unittest.Mock and especially unittest.patch more. - Use decorators for on_config_changed diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 7e3135ded..84d41eec1 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -31,14 +31,13 @@ import base64 import functools import traceback -from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox +from PyQt5.QtWidgets import QApplication, QDialog from PyQt5.QtCore import (pyqtSlot, QTimer, QEventLoop, Qt, QStandardPaths, qInstallMessageHandler, QObject, QUrl) import qutebrowser from qutebrowser.commands import userscripts, runners, cmdutils -from qutebrowser.config import (style, config, websettings, iniparsers, - lineparser, configtypes, keyconfparser) +from qutebrowser.config import style, config, websettings from qutebrowser.network import qutescheme, proxy from qutebrowser.browser import quickmarks, cookies, downloads, cache, hints from qutebrowser.widgets import mainwindow, console, crash @@ -144,7 +143,7 @@ class Application(QApplication): objreg.register('readline-bridge', readline_bridge) log.init.debug("Initializing config...") - self._init_config() + config.init(self._args) log.init.debug("Initializing crashlog...") self._handle_segfault() log.init.debug("Initializing modes...") @@ -180,59 +179,6 @@ class Application(QApplication): debug_console = console.ConsoleWidget() objreg.register('debug-console', debug_console) - def _init_config(self): - """Inizialize and read the config.""" - if self._args.confdir is None: - confdir = utils.get_standard_dir(QStandardPaths.ConfigLocation) - elif self._args.confdir == '': - confdir = None - else: - confdir = self._args.confdir - try: - config_obj = config.ConfigManager( - confdir, 'qutebrowser.conf', self) - except (configtypes.ValidationError, - config.NoOptionError, - config.NoSectionError, - config.UnknownSectionError, - config.InterpolationSyntaxError, - configparser.InterpolationError, - configparser.DuplicateSectionError, - configparser.DuplicateOptionError, - configparser.ParsingError) as e: - log.init.exception(e) - errstr = "Error while reading config:" - if hasattr(e, 'section') and hasattr(e, 'option'): - errstr += "\n\n{} -> {}:".format(e.section, e.option) - errstr += "\n{}".format(e) - msgbox = QMessageBox(QMessageBox.Critical, - "Error while reading config!", errstr) - msgbox.exec_() - # We didn't really initialize much so far, so we just quit hard. - sys.exit(1) - else: - objreg.register('config', config_obj) - try: - key_config = keyconfparser.KeyConfigParser(confdir, 'keys.conf') - except keyconfparser.KeyConfigError as e: - log.init.exception(e) - errstr = "Error while reading key config:\n" - if e.lineno is not None: - errstr += "In line {}: ".format(e.lineno) - errstr += str(e) - msgbox = QMessageBox(QMessageBox.Critical, - "Error while reading key config!", errstr) - msgbox.exec_() - # We didn't really initialize much so far, so we just quit hard. - sys.exit(1) - else: - objreg.register('key-config', key_config) - state_config = iniparsers.ReadWriteConfigParser(confdir, 'state') - objreg.register('state-config', state_config) - command_history = lineparser.LineConfigParser( - confdir, 'cmd_history', ('completion', 'history-length')) - objreg.register('command-history', command_history) - def _handle_segfault(self): """Handle a segfault from a previous run.""" # FIXME If an empty logfile exists, we log to stdout instead, which is diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index bf7732e04..daf35f89a 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -25,15 +25,18 @@ we borrow some methods and classes from there where it makes sense. """ import os +import sys import os.path import functools import configparser import collections.abc -from PyQt5.QtCore import pyqtSignal, QObject +from PyQt5.QtCore import pyqtSignal, QObject, QStandardPaths +from PyQt5.QtWidgets import QMessageBox from qutebrowser.utils import log -from qutebrowser.config import configdata, iniparsers, configtypes, textwrapper +from qutebrowser.config import (configdata, iniparsers, configtypes, + textwrapper, lineparser, keyconfparser) from qutebrowser.commands import cmdexc, cmdutils from qutebrowser.utils import message, objreg, utils from qutebrowser.utils.usertypes import Completion @@ -49,6 +52,61 @@ def section(sect): return objreg.get('config')[sect] +def init(args): + """Initialize the config. + + Args: + args: The argparse namespace. + """ + if args.confdir is None: + confdir = utils.get_standard_dir(QStandardPaths.ConfigLocation) + elif args.confdir == '': + confdir = None + else: + confdir = args.confdir + try: + app = objreg.get('app') + config_obj = ConfigManager(confdir, 'qutebrowser.conf', app) + except (configtypes.ValidationError, NoOptionError, NoSectionError, + UnknownSectionError, InterpolationSyntaxError, + configparser.InterpolationError, + configparser.DuplicateSectionError, + configparser.DuplicateOptionError, + configparser.ParsingError) as e: + log.init.exception(e) + errstr = "Error while reading config:" + if hasattr(e, 'section') and hasattr(e, 'option'): + errstr += "\n\n{} -> {}:".format(e.section, e.option) + errstr += "\n{}".format(e) + msgbox = QMessageBox(QMessageBox.Critical, + "Error while reading config!", errstr) + msgbox.exec_() + # We didn't really initialize much so far, so we just quit hard. + sys.exit(1) + else: + objreg.register('config', config_obj) + try: + key_config = keyconfparser.KeyConfigParser(confdir, 'keys.conf') + except keyconfparser.KeyConfigError as e: + log.init.exception(e) + errstr = "Error while reading key config:\n" + if e.lineno is not None: + errstr += "In line {}: ".format(e.lineno) + errstr += str(e) + msgbox = QMessageBox(QMessageBox.Critical, + "Error while reading key config!", errstr) + msgbox.exec_() + # We didn't really initialize much so far, so we just quit hard. + sys.exit(1) + else: + objreg.register('key-config', key_config) + state_config = iniparsers.ReadWriteConfigParser(confdir, 'state') + objreg.register('state-config', state_config) + command_history = lineparser.LineConfigParser( + confdir, 'cmd_history', ('completion', 'history-length')) + objreg.register('command-history', command_history) + + class NoSectionError(configparser.NoSectionError): """Exception raised when a section was not found."""