From 411c5171a24b7c83829d6456d6a3c89b31bd6693 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 10 Dec 2018 09:38:23 +0100 Subject: [PATCH] Load components dynamically --- qutebrowser/app.py | 4 ++-- qutebrowser/extensions/__init__.py | 0 qutebrowser/extensions/loader.py | 35 ++++++++++++++++++++++++++++++ qutebrowser/utils/log.py | 3 ++- scripts/dev/run_vulture.py | 3 +++ scripts/dev/src2asciidoc.py | 2 ++ 6 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 qutebrowser/extensions/__init__.py create mode 100644 qutebrowser/extensions/loader.py diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 6c948e10c..65c7395eb 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -68,6 +68,7 @@ from qutebrowser.browser import (urlmarks, adblock, history, browsertab, from qutebrowser.browser.network import proxy from qutebrowser.browser.webkit import cookies, cache from qutebrowser.browser.webkit.network import networkmanager +from qutebrowser.extensions import loader from qutebrowser.keyinput import macros from qutebrowser.mainwindow import mainwindow, prompt from qutebrowser.misc import (readline, ipc, savemanager, sessions, @@ -77,8 +78,6 @@ from qutebrowser.utils import (log, version, message, utils, urlutils, objreg, usertypes, standarddir, error, qtutils) # pylint: disable=unused-import # We import those to run the cmdutils.register decorators. -from qutebrowser.components import (scrollcommands, caretcommands, - zoomcommands, misccommands) from qutebrowser.mainwindow.statusbar import command from qutebrowser.misc import utilcmds # pylint: enable=unused-import @@ -166,6 +165,7 @@ def init(args, crash_handler): qApp.setQuitOnLastWindowClosed(False) _init_icon() + loader.load_components() try: _init_modules(args, crash_handler) except (OSError, UnicodeDecodeError, browsertab.WebTabError) as e: diff --git a/qutebrowser/extensions/__init__.py b/qutebrowser/extensions/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/qutebrowser/extensions/loader.py b/qutebrowser/extensions/loader.py new file mode 100644 index 000000000..9b5aadd25 --- /dev/null +++ b/qutebrowser/extensions/loader.py @@ -0,0 +1,35 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2018 Florian Bruhin (The Compiler) +# +# 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 . + +"""Loader for qutebrowser extensions.""" + +import pkgutil + +from qutebrowser import components +from qutebrowser.utils import log + + +def load_components() -> None: + """Load everything from qutebrowser.components.""" + for info in pkgutil.walk_packages(components.__path__): + if info.ispkg: + continue + log.extensions.debug("Importing {}".format(info.name)) + loader = info.module_finder.find_module(info.name) + loader.load_module(info.name) diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index bbc025515..115c53352 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -137,6 +137,7 @@ prompt = logging.getLogger('prompt') network = logging.getLogger('network') sql = logging.getLogger('sql') greasemonkey = logging.getLogger('greasemonkey') +extensions = logging.getLogger('extensions') LOGGER_NAMES = [ 'statusbar', 'completion', 'init', 'url', @@ -146,7 +147,7 @@ LOGGER_NAMES = [ 'js', 'qt', 'rfc6266', 'ipc', 'shlexer', 'save', 'message', 'config', 'sessions', 'webelem', 'prompt', 'network', 'sql', - 'greasemonkey' + 'greasemonkey', 'extensions', ] diff --git a/scripts/dev/run_vulture.py b/scripts/dev/run_vulture.py index f3217694e..7874f6a79 100755 --- a/scripts/dev/run_vulture.py +++ b/scripts/dev/run_vulture.py @@ -30,6 +30,7 @@ import argparse import vulture import qutebrowser.app # pylint: disable=unused-import +from qutebrowser.extensions import loader from qutebrowser.misc import objects from qutebrowser.utils import utils from qutebrowser.browser.webkit import rfc6266 @@ -43,6 +44,8 @@ from qutebrowser.config import configtypes def whitelist_generator(): # noqa """Generator which yields lines to add to a vulture whitelist.""" + loader.load_components() + # qutebrowser commands for cmd in objects.commands.values(): yield utils.qualname(cmd.handler) diff --git a/scripts/dev/src2asciidoc.py b/scripts/dev/src2asciidoc.py index ba4e9b69c..f0536c045 100755 --- a/scripts/dev/src2asciidoc.py +++ b/scripts/dev/src2asciidoc.py @@ -35,6 +35,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir, # We import qutebrowser.app so all @cmdutils-register decorators are run. import qutebrowser.app from qutebrowser import qutebrowser, commands +from qutebrowser.extensions import loader from qutebrowser.commands import argparser from qutebrowser.config import configdata, configtypes from qutebrowser.utils import docutils, usertypes @@ -549,6 +550,7 @@ def regenerate_cheatsheet(): def main(): """Regenerate all documentation.""" utils.change_cwd() + loader.load_components() print("Generating manpage...") regenerate_manpage('doc/qutebrowser.1.asciidoc') print("Generating settings help...")