diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 3984eddc4..4bd81ca02 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -1,16 +1,16 @@ import sys from PyQt5.QtWidgets import QWidget, QApplication from qutebrowser.widgets.mainwindow import MainWindow -from qutebrowser.commands import CommandParser +import qutebrowser.commands as cmds def main(): app = QApplication(sys.argv) mw = MainWindow() - cp = CommandParser() + cp = cmds.CommandParser() mw.status.cmd.returnPressed.connect(lambda: cp.parse(mw.status.cmd.text())) - cp.openurl.connect(mw.tabs.openurl) - cp.tabopen.connect(mw.tabs.tabopen) + cmds.cmd_dict['openurl'].connect(mw.tabs.openurl) + cmds.cmd_dict['tabopen'].connect(mw.tabs.tabopen) mw.show() sys.exit(app.exec_()) diff --git a/qutebrowser/commands.py b/qutebrowser/commands.py index 132602f81..5043acefb 100644 --- a/qutebrowser/commands.py +++ b/qutebrowser/commands.py @@ -1,12 +1,44 @@ from PyQt5.QtCore import QObject, pyqtSignal +import inspect, sys + +cmd_dict = {} + +def register_all(): + def is_cmd(obj): + return (inspect.isclass(obj) and obj.__module__ == __name__ and + obj.__name__.endswith('Cmd')) + + for (name, cls) in inspect.getmembers(sys.modules[__name__], is_cmd): + cls.bind() class CommandParser(QObject): - openurl = pyqtSignal(str) - tabopen = pyqtSignal(str) + def parse(self, test): + parts = text.lstrip(':').strip().split() + cmd = parts[0] + args = parts[1:] + cmd_dict[cmd]().run(args) - def parse(self, cmd): - c = cmd.split() - if c[0] == ':open': - self.openurl.emit(c[1]) - elif c[0] == ':tabopen': - self.tabopen.emit(c[1]) +class Command(QObject): + nargs = 0 + name = '' + signal = None + + @classmethod + def bind(cls): + if cls.name: + cmd_dict[cls.name] = cls + + def run(self, *args): + self.signal.emit(*args) + +class OpenCmd(Command): + nargs = 1 + name = 'open' + signal = pyqtSignal(str) + +class TabOpenCmd(Command): + nargs = 1 + name = 'tabopen' + signal = pyqtSignal(str) + +register_all()