Use **kwargs to simplify cmdutils.register.
This commit is contained in:
parent
20f0ef7ccc
commit
b1c475c61d
@ -23,7 +23,7 @@ Module attributes:
|
||||
cmd_dict: A mapping from command-strings to command objects.
|
||||
"""
|
||||
|
||||
from qutebrowser.utils import usertypes, qtutils, log
|
||||
from qutebrowser.utils import qtutils, log
|
||||
from qutebrowser.commands import command, cmdexc
|
||||
|
||||
cmd_dict = {}
|
||||
@ -99,24 +99,11 @@ class register: # pylint: disable=invalid-name
|
||||
|
||||
Attributes:
|
||||
_instance: The object from the object registry to be used as "self".
|
||||
_scope: The scope to get _instance for.
|
||||
_name: The name (as string) or names (as list) of the command.
|
||||
_maxsplit: The maximum amounts of splits to do for the commandline, or
|
||||
None.
|
||||
_hide: Whether to hide the command or not.
|
||||
_completion: Which completion to use for arguments, as a list of
|
||||
strings.
|
||||
_modes/_not_modes: List of modes to use/not use.
|
||||
_needs_js: If javascript is needed for this command.
|
||||
_debug: Whether this is a debugging command (only shown with --debug).
|
||||
_ignore_args: Whether to ignore the arguments of the function.
|
||||
_deprecated: False, or a string describing why a command is deprecated.
|
||||
_kwargs: The arguments to pass to Command.
|
||||
"""
|
||||
|
||||
def __init__(self, instance=None, name=None, maxsplit=None, hide=False,
|
||||
completion=None, modes=None, not_modes=None, needs_js=False,
|
||||
debug=False, ignore_args=False, deprecated=False,
|
||||
no_cmd_split=False, scope='global'):
|
||||
def __init__(self, *, instance=None, name=None, **kwargs):
|
||||
"""Save decorator arguments.
|
||||
|
||||
Gets called on parse-time with the decorator arguments.
|
||||
@ -124,30 +111,9 @@ class register: # pylint: disable=invalid-name
|
||||
Args:
|
||||
See class attributes.
|
||||
"""
|
||||
# pylint: disable=too-many-arguments
|
||||
if modes is not None and not_modes is not None:
|
||||
raise ValueError("Only modes or not_modes can be given!")
|
||||
self._name = name
|
||||
self._maxsplit = maxsplit
|
||||
self._hide = hide
|
||||
self._instance = instance
|
||||
self._scope = scope
|
||||
self._completion = completion
|
||||
self._modes = modes
|
||||
self._not_modes = not_modes
|
||||
self._needs_js = needs_js
|
||||
self._deprecated = deprecated
|
||||
self._debug = debug
|
||||
self._ignore_args = ignore_args
|
||||
self._no_cmd_split = no_cmd_split
|
||||
if modes is not None:
|
||||
for m in modes:
|
||||
if not isinstance(m, usertypes.KeyMode):
|
||||
raise TypeError("Mode {} is no KeyMode member!".format(m))
|
||||
if not_modes is not None:
|
||||
for m in not_modes:
|
||||
if not isinstance(m, usertypes.KeyMode):
|
||||
raise TypeError("Mode {} is no KeyMode member!".format(m))
|
||||
self._name = name
|
||||
self._kwargs = kwargs
|
||||
|
||||
def _get_names(self, func):
|
||||
"""Get the name(s) which should be used for the current command.
|
||||
@ -191,14 +157,8 @@ class register: # pylint: disable=invalid-name
|
||||
for name in names:
|
||||
if name in cmd_dict:
|
||||
raise ValueError("{} is already registered!".format(name))
|
||||
cmd = command.Command(
|
||||
name=names[0], maxsplit=self._maxsplit, hide=self._hide,
|
||||
instance=self._instance, scope=self._scope,
|
||||
completion=self._completion, modes=self._modes,
|
||||
not_modes=self._not_modes, needs_js=self._needs_js,
|
||||
is_debug=self._debug, ignore_args=self._ignore_args,
|
||||
deprecated=self._deprecated, no_cmd_split=self._no_cmd_split,
|
||||
handler=func)
|
||||
cmd = command.Command(name=names[0], instance=self._instance,
|
||||
handler=func, **self._kwargs)
|
||||
for name in names:
|
||||
cmd_dict[name] = cmd
|
||||
aliases += names[1:]
|
||||
|
@ -25,7 +25,8 @@ import collections
|
||||
from PyQt5.QtWebKit import QWebSettings
|
||||
|
||||
from qutebrowser.commands import cmdexc, argparser
|
||||
from qutebrowser.utils import log, utils, message, debug, docutils, objreg
|
||||
from qutebrowser.utils import log, utils, message, docutils, objreg, usertypes
|
||||
from qutebrowser.utils import debug as debug_utils
|
||||
|
||||
|
||||
class Command:
|
||||
@ -64,11 +65,22 @@ class Command:
|
||||
['kwargs', 'type', 'name', 'flag',
|
||||
'special'])
|
||||
|
||||
def __init__(self, name, maxsplit, hide, instance, completion, modes,
|
||||
not_modes, needs_js, is_debug, ignore_args, deprecated,
|
||||
no_cmd_split, handler, scope):
|
||||
def __init__(self, *, handler, name, instance=None, maxsplit=None,
|
||||
hide=False, completion=None, modes=None, not_modes=None,
|
||||
needs_js=False, debug=False, ignore_args=False,
|
||||
deprecated=False, no_cmd_split=False, scope='global'):
|
||||
# I really don't know how to solve this in a better way, I tried.
|
||||
# pylint: disable=too-many-arguments,too-many-locals
|
||||
if modes is not None and not_modes is not None:
|
||||
raise ValueError("Only modes or not_modes can be given!")
|
||||
if modes is not None:
|
||||
for m in modes:
|
||||
if not isinstance(m, usertypes.KeyMode):
|
||||
raise TypeError("Mode {} is no KeyMode member!".format(m))
|
||||
if not_modes is not None:
|
||||
for m in not_modes:
|
||||
if not isinstance(m, usertypes.KeyMode):
|
||||
raise TypeError("Mode {} is no KeyMode member!".format(m))
|
||||
self.name = name
|
||||
self.maxsplit = maxsplit
|
||||
self.hide = hide
|
||||
@ -79,7 +91,7 @@ class Command:
|
||||
self._not_modes = not_modes
|
||||
self._scope = scope
|
||||
self._needs_js = needs_js
|
||||
self.debug = is_debug
|
||||
self.debug = debug
|
||||
self.ignore_args = ignore_args
|
||||
self.handler = handler
|
||||
self.no_cmd_split = no_cmd_split
|
||||
@ -238,7 +250,7 @@ class Command:
|
||||
self._type_conv.update(self._get_typeconv(param, typ))
|
||||
self._name_conv.update(
|
||||
self._get_nameconv(param, annotation_info))
|
||||
callsig = debug.format_call(
|
||||
callsig = debug_utils.format_call(
|
||||
self.parser.add_argument, args, kwargs,
|
||||
full=False)
|
||||
log.commands.vdebug('Adding arg {} of type {} -> {}'.format(
|
||||
@ -501,5 +513,5 @@ class Command:
|
||||
posargs, kwargs = self._get_call_args(win_id)
|
||||
self._check_prerequisites(win_id)
|
||||
log.commands.debug('Calling {}'.format(
|
||||
debug.format_call(self.handler, posargs, kwargs)))
|
||||
debug_utils.format_call(self.handler, posargs, kwargs)))
|
||||
self.handler(*posargs, **kwargs)
|
||||
|
Loading…
Reference in New Issue
Block a user