Use commandparser.parse_check_run() for keys.py

This commit is contained in:
Florian Bruhin 2014-01-22 17:25:47 +01:00
parent 7c33bffdb2
commit 8e71f0cb8c
2 changed files with 16 additions and 11 deletions

View File

@ -3,7 +3,8 @@ import re
from PyQt5.QtCore import QObject, pyqtSignal
from qutebrowser.commands.utils import CommandParser, ArgumentCountError
from qutebrowser.commands.utils import (CommandParser, ArgumentCountError,
NoSuchCommandError)
class KeyParser(QObject):
"""Parser for vim-like key sequences"""
@ -86,17 +87,15 @@ class KeyParser(QObject):
count = int(countstr) if countstr else None
try:
self.commandparser.parse(cmdstr_hay)
except ValueError:
self.commandparser.parse_check_run(cmdstr_hay, count=count,
ignore_exc=False)
except NoSuchCommandError:
return
try:
self.commandparser.check()
except ArgumentCountError:
logging.debug('Filling statusbar with partial command {}'.format(
cmdstr_hay))
self.set_cmd_text.emit(cmdstr_hay + ' ')
return
self.commandparser.run(count=count)
def _match_key(self, cmdstr_needle):
"""Tries to match a given cmdstr with any defined command"""

View File

@ -11,6 +11,9 @@ cmd_dict = {}
class ArgumentCountError(TypeError):
pass
class NoSuchCommandError(ValueError):
pass
def register_all():
"""Register and initialize all commands."""
# We do this here to avoid a circular import, since commands.commands
@ -43,7 +46,7 @@ class CommandParser(QObject):
cmd = cmd_dict[cmdstr]
except KeyError:
self.error.emit("{}: no such command".format(cmdstr))
raise ValueError
raise NoSuchCommandError
if len(parts) == 1:
args = []
@ -67,13 +70,16 @@ class CommandParser(QObject):
else:
self.cmd.run(self.args)
def parse_check_run(self, text, count=None):
def parse_check_run(self, text, count=None, ignore_exc=True):
try:
self.parse(text)
self.check()
except (ArgumentCountError, ValueError):
return
self.run()
except (ArgumentCountError, NoSuchCommandError):
if ignore_exc:
return
else:
raise
self.run(count=count)
class Command(QObject):
"""Base skeleton for a command. See the module help for