Use **kwargs to simplify cmdutils.register.

This commit is contained in:
Florian Bruhin 2015-04-06 17:25:42 +02:00
parent 20f0ef7ccc
commit b1c475c61d
2 changed files with 26 additions and 54 deletions

View File

@ -23,7 +23,7 @@ Module attributes:
cmd_dict: A mapping from command-strings to command objects. 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 from qutebrowser.commands import command, cmdexc
cmd_dict = {} cmd_dict = {}
@ -99,24 +99,11 @@ class register: # pylint: disable=invalid-name
Attributes: Attributes:
_instance: The object from the object registry to be used as "self". _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. _name: The name (as string) or names (as list) of the command.
_maxsplit: The maximum amounts of splits to do for the commandline, or _kwargs: The arguments to pass to Command.
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.
""" """
def __init__(self, instance=None, name=None, maxsplit=None, hide=False, def __init__(self, *, instance=None, name=None, **kwargs):
completion=None, modes=None, not_modes=None, needs_js=False,
debug=False, ignore_args=False, deprecated=False,
no_cmd_split=False, scope='global'):
"""Save decorator arguments. """Save decorator arguments.
Gets called on parse-time with the decorator arguments. Gets called on parse-time with the decorator arguments.
@ -124,30 +111,9 @@ class register: # pylint: disable=invalid-name
Args: Args:
See class attributes. 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._instance = instance
self._scope = scope self._name = name
self._completion = completion self._kwargs = kwargs
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))
def _get_names(self, func): def _get_names(self, func):
"""Get the name(s) which should be used for the current command. """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: for name in names:
if name in cmd_dict: if name in cmd_dict:
raise ValueError("{} is already registered!".format(name)) raise ValueError("{} is already registered!".format(name))
cmd = command.Command( cmd = command.Command(name=names[0], instance=self._instance,
name=names[0], maxsplit=self._maxsplit, hide=self._hide, handler=func, **self._kwargs)
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)
for name in names: for name in names:
cmd_dict[name] = cmd cmd_dict[name] = cmd
aliases += names[1:] aliases += names[1:]

View File

@ -25,7 +25,8 @@ import collections
from PyQt5.QtWebKit import QWebSettings from PyQt5.QtWebKit import QWebSettings
from qutebrowser.commands import cmdexc, argparser 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: class Command:
@ -64,11 +65,22 @@ class Command:
['kwargs', 'type', 'name', 'flag', ['kwargs', 'type', 'name', 'flag',
'special']) 'special'])
def __init__(self, name, maxsplit, hide, instance, completion, modes, def __init__(self, *, handler, name, instance=None, maxsplit=None,
not_modes, needs_js, is_debug, ignore_args, deprecated, hide=False, completion=None, modes=None, not_modes=None,
no_cmd_split, handler, scope): 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. # I really don't know how to solve this in a better way, I tried.
# pylint: disable=too-many-arguments,too-many-locals # 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.name = name
self.maxsplit = maxsplit self.maxsplit = maxsplit
self.hide = hide self.hide = hide
@ -79,7 +91,7 @@ class Command:
self._not_modes = not_modes self._not_modes = not_modes
self._scope = scope self._scope = scope
self._needs_js = needs_js self._needs_js = needs_js
self.debug = is_debug self.debug = debug
self.ignore_args = ignore_args self.ignore_args = ignore_args
self.handler = handler self.handler = handler
self.no_cmd_split = no_cmd_split self.no_cmd_split = no_cmd_split
@ -238,7 +250,7 @@ class Command:
self._type_conv.update(self._get_typeconv(param, typ)) self._type_conv.update(self._get_typeconv(param, typ))
self._name_conv.update( self._name_conv.update(
self._get_nameconv(param, annotation_info)) self._get_nameconv(param, annotation_info))
callsig = debug.format_call( callsig = debug_utils.format_call(
self.parser.add_argument, args, kwargs, self.parser.add_argument, args, kwargs,
full=False) full=False)
log.commands.vdebug('Adding arg {} of type {} -> {}'.format( log.commands.vdebug('Adding arg {} of type {} -> {}'.format(
@ -501,5 +513,5 @@ class Command:
posargs, kwargs = self._get_call_args(win_id) posargs, kwargs = self._get_call_args(win_id)
self._check_prerequisites(win_id) self._check_prerequisites(win_id)
log.commands.debug('Calling {}'.format( log.commands.debug('Calling {}'.format(
debug.format_call(self.handler, posargs, kwargs))) debug_utils.format_call(self.handler, posargs, kwargs)))
self.handler(*posargs, **kwargs) self.handler(*posargs, **kwargs)