Rewrite command parsing

This commit is contained in:
Florian Bruhin 2014-01-16 17:12:55 +01:00
parent 64568de851
commit bc6d7fee25
2 changed files with 44 additions and 12 deletions

View File

@ -1,16 +1,16 @@
import sys import sys
from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtWidgets import QWidget, QApplication
from qutebrowser.widgets.mainwindow import MainWindow from qutebrowser.widgets.mainwindow import MainWindow
from qutebrowser.commands import CommandParser import qutebrowser.commands as cmds
def main(): def main():
app = QApplication(sys.argv) app = QApplication(sys.argv)
mw = MainWindow() mw = MainWindow()
cp = CommandParser() cp = cmds.CommandParser()
mw.status.cmd.returnPressed.connect(lambda: cp.parse(mw.status.cmd.text())) mw.status.cmd.returnPressed.connect(lambda: cp.parse(mw.status.cmd.text()))
cp.openurl.connect(mw.tabs.openurl) cmds.cmd_dict['openurl'].connect(mw.tabs.openurl)
cp.tabopen.connect(mw.tabs.tabopen) cmds.cmd_dict['tabopen'].connect(mw.tabs.tabopen)
mw.show() mw.show()
sys.exit(app.exec_()) sys.exit(app.exec_())

View File

@ -1,12 +1,44 @@
from PyQt5.QtCore import QObject, pyqtSignal 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): class CommandParser(QObject):
openurl = pyqtSignal(str) def parse(self, test):
tabopen = pyqtSignal(str) parts = text.lstrip(':').strip().split()
cmd = parts[0]
args = parts[1:]
cmd_dict[cmd]().run(args)
def parse(self, cmd): class Command(QObject):
c = cmd.split() nargs = 0
if c[0] == ':open': name = ''
self.openurl.emit(c[1]) signal = None
elif c[0] == ':tabopen':
self.tabopen.emit(c[1]) @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()