Use @cmdutils.argument for win_id/count

This commit is contained in:
Florian Bruhin 2016-05-10 19:51:11 +02:00
parent 04367851c3
commit 35135c4b0d
11 changed files with 132 additions and 87 deletions

View File

@ -177,7 +177,8 @@ class HostBlocker:
message.info('current',
"Run :adblock-update to get adblock lists.")
@cmdutils.register(instance='host-blocker', win_id='win_id')
@cmdutils.register(instance='host-blocker')
@cmdutils.argument('win_id', win_id=True)
def adblock_update(self, win_id):
"""Update the adblock block lists.

View File

@ -199,8 +199,8 @@ class CommandDispatcher:
"{!r}!".format(conf_selection))
return None
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def tab_close(self, left=False, right=False, opposite=False, count=None):
"""Close the current/[count]th tab.
@ -226,8 +226,9 @@ class CommandDispatcher:
tabbar.setSelectionBehaviorOnRemove(old_selection_behavior)
@cmdutils.register(instance='command-dispatcher', name='open',
maxsplit=0, scope='window', count='count',
maxsplit=0, scope='window',
completion=[usertypes.Completion.url])
@cmdutils.argument('count', count=True)
def openurl(self, url=None, bg=False, tab=False, window=False, count=None):
"""Open a URL in the current/[count]th tab.
@ -268,7 +269,8 @@ class CommandDispatcher:
curtab.openurl(url)
@cmdutils.register(instance='command-dispatcher', name='reload',
scope='window', count='count')
scope='window')
@cmdutils.argument('count', count=True)
def reloadpage(self, force=False, count=None):
"""Reload the current/[count]th tab.
@ -283,8 +285,8 @@ class CommandDispatcher:
else:
tab.reload()
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def stop(self, count=None):
"""Stop loading in the current/[count]th tab.
@ -296,7 +298,8 @@ class CommandDispatcher:
tab.stop()
@cmdutils.register(instance='command-dispatcher', name='print',
scope='window', count='count')
scope='window')
@cmdutils.argument('count', count=True)
def printpage(self, preview=False, count=None):
"""Print the current/[count]th tab.
@ -391,8 +394,8 @@ class CommandDispatcher:
raise cmdexc.CommandError("At beginning of history.")
widget.back()
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def back(self, tab=False, bg=False, window=False, count=1):
"""Go back in the history of the current tab.
@ -404,8 +407,8 @@ class CommandDispatcher:
"""
self._back_forward(tab, bg, window, count, forward=False)
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def forward(self, tab=False, bg=False, window=False, count=1):
"""Go forward in the history of the current tab.
@ -500,7 +503,8 @@ class CommandDispatcher:
"`where'.".format(where))
@cmdutils.register(instance='command-dispatcher', hide=True,
scope='window', count='count')
scope='window')
@cmdutils.argument('count', count=True)
def scroll_px(self, dx: {'type': int}, dy: {'type': int}, count=1):
"""Scroll the current tab by 'count * dx/dy' pixels.
@ -516,7 +520,8 @@ class CommandDispatcher:
self._current_widget().page().currentFrame().scroll(dx, dy)
@cmdutils.register(instance='command-dispatcher', hide=True,
scope='window', count='count')
scope='window')
@cmdutils.argument('count', count=True)
def scroll(self, direction: {'type': (str, int)}, count=1):
"""Scroll the current tab in the given direction.
@ -575,7 +580,8 @@ class CommandDispatcher:
widget.keyReleaseEvent(release_evt)
@cmdutils.register(instance='command-dispatcher', hide=True,
scope='window', count='count')
scope='window')
@cmdutils.argument('count', count=True)
@cmdutils.argument('horizontal', flag='x')
def scroll_perc(self, perc: {'type': float}=None, horizontal=False,
count=None):
@ -612,7 +618,8 @@ class CommandDispatcher:
frame.setScrollBarValue(orientation, int(m * perc / 100))
@cmdutils.register(instance='command-dispatcher', hide=True,
scope='window', count='count')
scope='window')
@cmdutils.argument('count', count=True)
def scroll_page(self, x: {'type': float}, y: {'type': float}, *,
top_navigate: {'type': ('prev', 'decrement'),
'metavar': 'ACTION'}=None,
@ -699,8 +706,8 @@ class CommandDispatcher:
message.info(self._win_id, "Yanked {} to {}: {}".format(
what, target, s))
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def zoom_in(self, count=1):
"""Increase the zoom level for the current tab.
@ -714,8 +721,8 @@ class CommandDispatcher:
raise cmdexc.CommandError(e)
message.info(self._win_id, "Zoom level: {}%".format(perc))
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def zoom_out(self, count=1):
"""Decrease the zoom level for the current tab.
@ -729,8 +736,8 @@ class CommandDispatcher:
raise cmdexc.CommandError(e)
message.info(self._win_id, "Zoom level: {}%".format(perc))
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def zoom(self, zoom: {'type': int}=None, count=None):
"""Set the zoom level for the current tab.
@ -781,8 +788,8 @@ class CommandDispatcher:
except IndexError:
raise cmdexc.CommandError("Nothing to undo!")
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def tab_prev(self, count=1):
"""Switch to the previous tab, or switch [count] tabs back.
@ -801,8 +808,8 @@ class CommandDispatcher:
else:
raise cmdexc.CommandError("First tab")
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def tab_next(self, count=1):
"""Switch to the next tab, or switch [count] tabs forward.
@ -912,8 +919,8 @@ class CommandDispatcher:
window.raise_()
tabbed_browser.setCurrentIndex(idx-1)
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def tab_focus(self, index: {'type': (int, 'last')}=None, count=None):
"""Select the tab given as argument/[count].
@ -945,8 +952,8 @@ class CommandDispatcher:
raise cmdexc.CommandError("There's no tab with index {}!".format(
idx))
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def tab_move(self, direction: {'type': ('+', '-')}=None, count=None):
"""Move the current tab.
@ -1477,7 +1484,8 @@ class CommandDispatcher:
self._tabbed_browser.search_flags = flags
@cmdutils.register(instance='command-dispatcher', hide=True,
scope='window', count='count')
scope='window')
@cmdutils.argument('count', count=True)
def search_next(self, count=1):
"""Continue the search to the ([count]th) next term.
@ -1498,7 +1506,8 @@ class CommandDispatcher:
view.search(view.search_text, view.search_flags)
@cmdutils.register(instance='command-dispatcher', hide=True,
scope='window', count='count')
scope='window')
@cmdutils.argument('count', count=True)
def search_prev(self, count=1):
"""Continue the search to the ([count]th) previous term.
@ -1526,7 +1535,8 @@ class CommandDispatcher:
view.search(view.search_text, flags)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_next_line(self, count=1):
"""Move the cursor or selection to the next line.
@ -1542,7 +1552,8 @@ class CommandDispatcher:
webview.triggerPageAction(act)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_prev_line(self, count=1):
"""Move the cursor or selection to the prev line.
@ -1558,7 +1569,8 @@ class CommandDispatcher:
webview.triggerPageAction(act)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_next_char(self, count=1):
"""Move the cursor or selection to the next char.
@ -1574,7 +1586,8 @@ class CommandDispatcher:
webview.triggerPageAction(act)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_prev_char(self, count=1):
"""Move the cursor or selection to the previous char.
@ -1590,7 +1603,8 @@ class CommandDispatcher:
webview.triggerPageAction(act)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_end_of_word(self, count=1):
"""Move the cursor or selection to the end of the word.
@ -1611,7 +1625,8 @@ class CommandDispatcher:
webview.triggerPageAction(a)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_next_word(self, count=1):
"""Move the cursor or selection to the next word.
@ -1632,7 +1647,8 @@ class CommandDispatcher:
webview.triggerPageAction(a)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_prev_word(self, count=1):
"""Move the cursor or selection to the previous word.
@ -1670,7 +1686,8 @@ class CommandDispatcher:
webview.triggerPageAction(act)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_start_of_next_block(self, count=1):
"""Move the cursor or selection to the start of next block.
@ -1689,7 +1706,8 @@ class CommandDispatcher:
webview.triggerPageAction(a)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_start_of_prev_block(self, count=1):
"""Move the cursor or selection to the start of previous block.
@ -1708,7 +1726,8 @@ class CommandDispatcher:
webview.triggerPageAction(a)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_end_of_next_block(self, count=1):
"""Move the cursor or selection to the end of next block.
@ -1727,7 +1746,8 @@ class CommandDispatcher:
webview.triggerPageAction(a)
@cmdutils.register(instance='command-dispatcher', hide=True,
modes=[KeyMode.caret], scope='window', count='count')
modes=[KeyMode.caret], scope='window')
@cmdutils.argument('count', count=True)
def move_to_end_of_prev_block(self, count=1):
"""Move the cursor or selection to the end of previous block.
@ -1806,7 +1826,8 @@ class CommandDispatcher:
self._current_widget().triggerPageAction(QWebPage.MoveToNextChar)
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count', debug=True)
debug=True)
@cmdutils.argument('count', count=True)
def debug_webaction(self, action, count=1):
"""Execute a webaction.
@ -1899,8 +1920,8 @@ class CommandDispatcher:
nam = self._current_widget().page().networkAccessManager()
nam.clear_all_ssl_errors()
@cmdutils.register(instance='command-dispatcher', scope='window',
count='count')
@cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('count', count=True)
def edit_url(self, url=None, bg=False, tab=False, window=False,
count=None):
"""Navigate to a url formed in an external editor.

View File

@ -946,8 +946,8 @@ class DownloadManager(QAbstractListModel):
raise cmdexc.CommandError("There's no download!")
raise cmdexc.CommandError("There's no download {}!".format(count))
@cmdutils.register(instance='download-manager', scope='window',
count='count')
@cmdutils.register(instance='download-manager', scope='window')
@cmdutils.argument('count', count=True)
def download_cancel(self, all_=False, count=0):
"""Cancel the last/[count]th download.
@ -973,8 +973,8 @@ class DownloadManager(QAbstractListModel):
.format(count))
download.cancel()
@cmdutils.register(instance='download-manager', scope='window',
count='count')
@cmdutils.register(instance='download-manager', scope='window')
@cmdutils.argument('count', count=True)
def download_delete(self, count=0):
"""Delete the last/[count]th download from disk.
@ -993,8 +993,8 @@ class DownloadManager(QAbstractListModel):
self.remove_item(download)
log.downloads.debug("deleted download {}".format(download))
@cmdutils.register(instance='download-manager', scope='window',
count='count')
@cmdutils.register(instance='download-manager', scope='window')
@cmdutils.argument('count', count=True)
def download_open(self, count=0):
"""Open the last/[count]th download.
@ -1011,8 +1011,8 @@ class DownloadManager(QAbstractListModel):
raise cmdexc.CommandError("Download {} is not done!".format(count))
download.open_file()
@cmdutils.register(instance='download-manager', scope='window',
count='count')
@cmdutils.register(instance='download-manager', scope='window')
@cmdutils.argument('count', count=True)
def download_retry(self, count=0):
"""Retry the first failed/[count]th download.
@ -1097,8 +1097,8 @@ class DownloadManager(QAbstractListModel):
finished_items = [d for d in self.downloads if d.done]
self.remove_items(finished_items)
@cmdutils.register(instance='download-manager', scope='window',
count='count')
@cmdutils.register(instance='download-manager', scope='window')
@cmdutils.argument('count', count=True)
def download_remove(self, all_=False, count=0):
"""Remove the last/[count]th download from the list.

View File

@ -737,7 +737,8 @@ class HintManager(QObject):
webview.openurl(url)
@cmdutils.register(instance='hintmanager', scope='tab', name='hint',
win_id='win_id', star_args_optional=True)
star_args_optional=True)
@cmdutils.argument('win_id', win_id=True)
def start(self, rapid=False, group=webelem.Group.all, target=Target.normal,
*args, win_id):
"""Start hinting.

View File

@ -173,7 +173,8 @@ class QuickmarkManager(UrlMarkManager):
win_id, "Add quickmark:", usertypes.PromptMode.text,
functools.partial(self.quickmark_add, win_id, urlstr))
@cmdutils.register(instance='quickmark-manager', win_id='win_id')
@cmdutils.register(instance='quickmark-manager')
@cmdutils.argument('win_id', win_id=True)
def quickmark_add(self, win_id, url, name):
"""Add a new quickmark.

View File

@ -69,8 +69,6 @@ class Command:
completion: Completions to use for arguments, as a list of strings.
debug: Whether this is a debugging command (only shown with --debug).
parser: The ArgumentParser to use to parse this command.
count_arg: The name of the count parameter, or None.
win_id_arg: The name of the win_id parameter, or None.
flags_with_args: A list of flags which take an argument.
no_cmd_split: If true, ';;' to split sub-commands is ignored.
_qute_args: The saved data from @cmdutils.argument
@ -93,8 +91,7 @@ class Command:
hide=False, completion=None, modes=None, not_modes=None,
needs_js=False, debug=False, ignore_args=False,
deprecated=False, no_cmd_split=False,
star_args_optional=False, scope='global', count=None,
win_id=None):
star_args_optional=False, scope='global'):
# I really don't know how to solve this in a better way, I tried.
# pylint: disable=too-many-locals
if modes is not None and not_modes is not None:
@ -110,6 +107,7 @@ class Command:
if scope != 'global' and instance is None:
raise ValueError("Setting scope without setting instance makes "
"no sense!")
self.name = name
self.maxsplit = maxsplit
self.hide = hide
@ -125,8 +123,6 @@ class Command:
self.ignore_args = ignore_args
self.handler = handler
self.no_cmd_split = no_cmd_split
self.count_arg = count
self.win_id_arg = win_id
self.docparser = docutils.DocstringParser(handler)
self.parser = argparser.ArgumentParser(
name, description=self.docparser.short_desc,
@ -225,13 +221,13 @@ class Command:
Return:
True if the parameter is special, False otherwise.
"""
if param.name == self.count_arg:
arg_info = self._get_arg_info(param)
if arg_info.count:
if param.default is inspect.Parameter.empty:
raise TypeError("{}: handler has count parameter "
"without default!".format(self.name))
return True
elif param.name == self.win_id_arg:
elif arg_info.win_id:
return True
def _inspect_func(self):
@ -250,15 +246,6 @@ class Command:
else:
self.desc = ""
if (self.count_arg is not None and
self.count_arg not in signature.parameters):
raise ValueError("count parameter {} does not exist!".format(
self.count_arg))
if (self.win_id_arg is not None and
self.win_id_arg not in signature.parameters):
raise ValueError("win_id parameter {} does not exist!".format(
self.win_id_arg))
if not self.ignore_args:
for param in signature.parameters.values():
annotation_info = self._parse_annotation(param)
@ -268,8 +255,7 @@ class Command:
continue
typ = self._get_type(param, annotation_info)
kwargs = self._param_to_argparse_kwargs(param, annotation_info)
args = self._param_to_argparse_args(param, annotation_info,
arg_info)
args = self._param_to_argparse_args(param, annotation_info)
self._type_conv.update(self._get_typeconv(param, typ))
callsig = debug_utils.format_call(
self.parser.add_argument, args, kwargs,
@ -484,11 +470,11 @@ class Command:
# Special case for 'self'.
self._get_self_arg(win_id, param, args)
continue
elif param.name == self.count_arg:
elif arg_info.count:
# Special case for count parameter.
self._get_count_arg(param, args, kwargs)
continue
elif param.name == self.win_id_arg:
elif arg_info.win_id:
# Special case for win_id parameter.
self._get_win_id_arg(win_id, param, args, kwargs)
continue

View File

@ -686,9 +686,10 @@ class ConfigManager(QObject):
raise cmdexc.CommandError("set: {} - {}".format(
e.__class__.__name__, e))
@cmdutils.register(name='set', instance='config', win_id='win_id',
@cmdutils.register(name='set', instance='config',
completion=[Completion.section, Completion.option,
Completion.value])
@cmdutils.argument('win_id', win_id=True)
def set_command(self, win_id, section_=None, option=None, value=None,
temp=False, print_=False):
"""Set an option.

View File

@ -184,8 +184,9 @@ class SaveManager(QObject):
message.error('current', "Failed to auto-save {}: "
"{}".format(key, e))
@cmdutils.register(instance='save-manager', name='save', win_id='win_id',
@cmdutils.register(instance='save-manager', name='save',
star_args_optional=True)
@cmdutils.argument('win_id', win_id=True)
def save_command(self, win_id, *what):
"""Save configs and state.

View File

@ -384,9 +384,10 @@ class SessionManager(QObject):
for win in old_windows:
win.close()
@cmdutils.register(name=['session-save', 'w'], win_id='win_id',
@cmdutils.register(name=['session-save', 'w'],
completion=[usertypes.Completion.sessions],
instance='session-manager')
@cmdutils.argument('win_id', win_id=True)
def session_save(self, win_id, name: {'type': str}=default, current=False,
quiet=False, force=False):
"""Save a session.

View File

@ -39,7 +39,8 @@ from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication # pylint: disable=unused-import
@cmdutils.register(maxsplit=1, no_cmd_split=True, win_id='win_id')
@cmdutils.register(maxsplit=1, no_cmd_split=True)
@cmdutils.argument('win_id', win_id=True)
def later(ms: {'type': int}, command, win_id):
"""Execute a command after some time.
@ -68,7 +69,8 @@ def later(ms: {'type': int}, command, win_id):
raise
@cmdutils.register(maxsplit=1, no_cmd_split=True, win_id='win_id')
@cmdutils.register(maxsplit=1, no_cmd_split=True)
@cmdutils.argument('win_id', win_id=True)
def repeat(times: {'type': int}, command, win_id):
"""Repeat a given command.
@ -83,7 +85,8 @@ def repeat(times: {'type': int}, command, win_id):
commandrunner.run_safely(command)
@cmdutils.register(hide=True, win_id='win_id')
@cmdutils.register(hide=True)
@cmdutils.argument('win_id', win_id=True)
def message_error(win_id, text):
"""Show an error message in the statusbar.
@ -93,7 +96,8 @@ def message_error(win_id, text):
message.error(win_id, text)
@cmdutils.register(hide=True, win_id='win_id')
@cmdutils.register(hide=True)
@cmdutils.argument('win_id', win_id=True)
def message_info(win_id, text):
"""Show an info message in the statusbar.
@ -103,7 +107,8 @@ def message_info(win_id, text):
message.info(win_id, text)
@cmdutils.register(hide=True, win_id='win_id')
@cmdutils.register(hide=True)
@cmdutils.argument('win_id', win_id=True)
def message_warning(win_id, text):
"""Show a warning message in the statusbar.

View File

@ -227,6 +227,33 @@ class TestRegister:
"""Blah."""
pass
def test_win_id(self):
@cmdutils.register()
@cmdutils.argument('win_id', win_id=True)
def fun(win_id):
"""Blah."""
pass
assert cmdutils.cmd_dict['fun']._get_call_args(42) == ([42], {})
def test_count(self):
@cmdutils.register()
@cmdutils.argument('count', count=True)
def fun(count=0):
"""Blah."""
pass
assert cmdutils.cmd_dict['fun']._get_call_args(42) == ([0], {})
def test_count_without_default(self):
with pytest.raises(TypeError) as excinfo:
@cmdutils.register()
@cmdutils.argument('count', count=True)
def fun(count):
"""Blah."""
pass
expected = "fun: handler has count parameter without default!"
assert str(excinfo.value) == expected
class TestArgument: