Simplify calling commands with an instance.
We used to use a signal and a function in app.py which executes the command handler. Now we instead use QCoreApplication.instance() which makes everything a lot easier and cleaner.
This commit is contained in:
parent
0c0cb48471
commit
1d5a1a29e8
@ -49,7 +49,6 @@ import qutebrowser.config.config as config
|
|||||||
import qutebrowser.network.qutescheme as qutescheme
|
import qutebrowser.network.qutescheme as qutescheme
|
||||||
import qutebrowser.config.websettings as websettings
|
import qutebrowser.config.websettings as websettings
|
||||||
import qutebrowser.network.proxy as proxy
|
import qutebrowser.network.proxy as proxy
|
||||||
import qutebrowser.utils.message as message
|
|
||||||
import qutebrowser.browser.quickmarks as quickmarks
|
import qutebrowser.browser.quickmarks as quickmarks
|
||||||
from qutebrowser.network.networkmanager import NetworkManager
|
from qutebrowser.network.networkmanager import NetworkManager
|
||||||
from qutebrowser.config.config import ConfigManager
|
from qutebrowser.config.config import ConfigManager
|
||||||
@ -60,13 +59,11 @@ from qutebrowser.keyinput.modeparsers import (NormalKeyParser, HintKeyParser,
|
|||||||
PromptKeyParser)
|
PromptKeyParser)
|
||||||
from qutebrowser.keyinput.keyparser import PassthroughKeyParser
|
from qutebrowser.keyinput.keyparser import PassthroughKeyParser
|
||||||
from qutebrowser.commands.managers import CommandManager, SearchManager
|
from qutebrowser.commands.managers import CommandManager, SearchManager
|
||||||
from qutebrowser.commands.exceptions import CommandError
|
|
||||||
from qutebrowser.config.iniparsers import ReadWriteConfigParser
|
from qutebrowser.config.iniparsers import ReadWriteConfigParser
|
||||||
from qutebrowser.config.lineparser import LineConfigParser
|
from qutebrowser.config.lineparser import LineConfigParser
|
||||||
from qutebrowser.browser.cookies import CookieJar
|
from qutebrowser.browser.cookies import CookieJar
|
||||||
from qutebrowser.utils.message import MessageBridge
|
from qutebrowser.utils.message import MessageBridge
|
||||||
from qutebrowser.utils.misc import (dotted_getattr, get_standard_dir,
|
from qutebrowser.utils.misc import get_standard_dir, actute_warning
|
||||||
actute_warning)
|
|
||||||
from qutebrowser.utils.readline import ReadlineBridge
|
from qutebrowser.utils.readline import ReadlineBridge
|
||||||
from qutebrowser.utils.debug import set_trace # pylint: disable=unused-import
|
from qutebrowser.utils.debug import set_trace # pylint: disable=unused-import
|
||||||
|
|
||||||
@ -142,7 +139,6 @@ class QuteBrowser(QApplication):
|
|||||||
self.networkmanager = NetworkManager(self.cookiejar)
|
self.networkmanager = NetworkManager(self.cookiejar)
|
||||||
self.commandmanager = CommandManager()
|
self.commandmanager = CommandManager()
|
||||||
self.searchmanager = SearchManager()
|
self.searchmanager = SearchManager()
|
||||||
self._init_cmds()
|
|
||||||
self.mainwindow = MainWindow()
|
self.mainwindow = MainWindow()
|
||||||
|
|
||||||
self.modeman.mainwindow = self.mainwindow
|
self.modeman.mainwindow = self.mainwindow
|
||||||
@ -310,20 +306,6 @@ class QuteBrowser(QApplication):
|
|||||||
# pylint: disable=no-member
|
# pylint: disable=no-member
|
||||||
faulthandler.register(signal.SIGUSR1)
|
faulthandler.register(signal.SIGUSR1)
|
||||||
|
|
||||||
def _init_cmds(self):
|
|
||||||
"""Initialisation of the qutebrowser commands.
|
|
||||||
|
|
||||||
Registers all commands and connects their signals.
|
|
||||||
"""
|
|
||||||
for key, cmd in sorted(cmdutils.cmd_dict.items()):
|
|
||||||
cmd.signal.connect(self.command_handler)
|
|
||||||
if cmd.instance is not None:
|
|
||||||
func = '.'.join([cmd.instance if cmd.instance else 'app',
|
|
||||||
cmd.handler.__name__])
|
|
||||||
else:
|
|
||||||
func = cmd.handler.__name__
|
|
||||||
logging.debug("Registered command: {} -> {}".format(key, func))
|
|
||||||
|
|
||||||
def _process_init_args(self):
|
def _process_init_args(self):
|
||||||
"""Process initial positional args.
|
"""Process initial positional args.
|
||||||
|
|
||||||
@ -672,30 +654,3 @@ class QuteBrowser(QApplication):
|
|||||||
"""
|
"""
|
||||||
logging.debug("Shutdown complete, quitting.")
|
logging.debug("Shutdown complete, quitting.")
|
||||||
self.quit()
|
self.quit()
|
||||||
|
|
||||||
@pyqtSlot(tuple)
|
|
||||||
def command_handler(self, tpl):
|
|
||||||
"""Handle commands which need an instance..
|
|
||||||
|
|
||||||
Args:
|
|
||||||
tpl: An (instance, func, count, args) tuple.
|
|
||||||
instance: How to get the current instance of the target object
|
|
||||||
from app.py, as a dotted string, e.g.
|
|
||||||
'mainwindow.tabs.cur'.
|
|
||||||
func: The function name to be called (as string).
|
|
||||||
count: The count given to the command, or None.
|
|
||||||
args: A list of arguments given to the command.
|
|
||||||
"""
|
|
||||||
(instance, func, count, args) = tpl
|
|
||||||
if instance == '':
|
|
||||||
obj = self
|
|
||||||
else:
|
|
||||||
obj = dotted_getattr(self, instance)
|
|
||||||
handler = getattr(obj, func)
|
|
||||||
try:
|
|
||||||
if count is not None:
|
|
||||||
handler(*args, count=count)
|
|
||||||
else:
|
|
||||||
handler(*args)
|
|
||||||
except CommandError as e:
|
|
||||||
message.error(e)
|
|
||||||
|
@ -21,12 +21,13 @@ import logging
|
|||||||
|
|
||||||
from qutebrowser.commands.exceptions import (ArgumentCountError,
|
from qutebrowser.commands.exceptions import (ArgumentCountError,
|
||||||
PrerequisitesError)
|
PrerequisitesError)
|
||||||
|
from qutebrowser.utils.misc import dotted_getattr
|
||||||
|
|
||||||
from PyQt5.QtCore import pyqtSignal, QObject, QCoreApplication
|
from PyQt5.QtCore import QCoreApplication
|
||||||
from PyQt5.QtWebKit import QWebSettings
|
from PyQt5.QtWebKit import QWebSettings
|
||||||
|
|
||||||
|
|
||||||
class Command(QObject):
|
class Command:
|
||||||
|
|
||||||
"""Base skeleton for a command.
|
"""Base skeleton for a command.
|
||||||
|
|
||||||
@ -42,18 +43,12 @@ class Command(QObject):
|
|||||||
handler: The handler function to call.
|
handler: The handler function to call.
|
||||||
completion: Completions to use for arguments, as a list of strings.
|
completion: Completions to use for arguments, as a list of strings.
|
||||||
needs_js: Whether the command needs javascript enabled
|
needs_js: Whether the command needs javascript enabled
|
||||||
|
|
||||||
Signals:
|
|
||||||
signal: Gets emitted when something should be called via handle_command
|
|
||||||
from the app.py context.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO:
|
# TODO:
|
||||||
# we should probably have some kind of typing / argument casting for args
|
# we should probably have some kind of typing / argument casting for args
|
||||||
# this might be combined with help texts or so as well
|
# this might be combined with help texts or so as well
|
||||||
|
|
||||||
signal = pyqtSignal(tuple)
|
|
||||||
|
|
||||||
def __init__(self, name, split, hide, nargs, count, desc, instance,
|
def __init__(self, name, split, hide, nargs, count, desc, instance,
|
||||||
handler, completion, modes, not_modes, needs_js):
|
handler, completion, modes, not_modes, needs_js):
|
||||||
# I really don't know how to solve this in a better way, I tried.
|
# I really don't know how to solve this in a better way, I tried.
|
||||||
@ -119,10 +114,6 @@ class Command(QObject):
|
|||||||
Args:
|
Args:
|
||||||
args: Arguments to the command.
|
args: Arguments to the command.
|
||||||
count: Command repetition count.
|
count: Command repetition count.
|
||||||
|
|
||||||
Emit:
|
|
||||||
signal: When the command has an instance and should be handled from
|
|
||||||
the app.py context.
|
|
||||||
"""
|
"""
|
||||||
dbgout = ["command called:", self.name]
|
dbgout = ["command called:", self.name]
|
||||||
if args:
|
if args:
|
||||||
@ -131,13 +122,18 @@ class Command(QObject):
|
|||||||
dbgout.append("(count={})".format(count))
|
dbgout.append("(count={})".format(count))
|
||||||
logging.debug(' '.join(dbgout))
|
logging.debug(' '.join(dbgout))
|
||||||
|
|
||||||
if self.instance is not None and self.count and count is not None:
|
kwargs = {}
|
||||||
self.signal.emit((self.instance, self.handler.__name__, count,
|
app = QCoreApplication.instance()
|
||||||
args))
|
|
||||||
elif self.instance is not None:
|
if self.instance is not None:
|
||||||
self.signal.emit((self.instance, self.handler.__name__, None,
|
# Add the 'self' parameter.
|
||||||
args))
|
if self.instance == '':
|
||||||
elif count is not None and self.count:
|
obj = app
|
||||||
self.handler(*args, count=count)
|
|
||||||
else:
|
else:
|
||||||
self.handler(*args)
|
obj = dotted_getattr(app, self.instance)
|
||||||
|
args.insert(0, obj)
|
||||||
|
|
||||||
|
if count is not None and self.count:
|
||||||
|
kwargs = {'count': count}
|
||||||
|
|
||||||
|
self.handler(*args, **kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user