From e527db156013dbf740c05c8d5566c7cb34884a2f Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 25 Sep 2014 07:41:51 +0200 Subject: [PATCH] Add scope argument to cmdutils.register/commands. --- qutebrowser/commands/cmdutils.py | 14 ++++++++------ qutebrowser/commands/command.py | 6 ++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/qutebrowser/commands/cmdutils.py b/qutebrowser/commands/cmdutils.py index 6f499f85d..68467a148 100644 --- a/qutebrowser/commands/cmdutils.py +++ b/qutebrowser/commands/cmdutils.py @@ -91,7 +91,8 @@ class register: # pylint: disable=invalid-name much cleaner to implement. Attributes: - _instance: The instance to be used as "self", as a dotted string. + _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. _split: Whether to split the arguments. _hide: Whether to hide the command or not. @@ -105,7 +106,7 @@ class register: # pylint: disable=invalid-name def __init__(self, instance=None, name=None, split=True, hide=False, completion=None, modes=None, not_modes=None, needs_js=False, - debug=False, ignore_args=False): + debug=False, ignore_args=False, scope='global'): """Save decorator arguments. Gets called on parse-time with the decorator arguments. @@ -120,6 +121,7 @@ class register: # pylint: disable=invalid-name self._split = split self._hide = hide self._instance = instance + self._scope = scope self._completion = completion self._modes = modes self._not_modes = not_modes @@ -179,10 +181,10 @@ class register: # pylint: disable=invalid-name raise ValueError("{} is already registered!".format(name)) cmd = command.Command( name=names[0], split=self._split, hide=self._hide, - instance=self._instance, 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, handler=func) + 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, handler=func) for name in names: cmd_dict[name] = cmd aliases += names[1:] diff --git a/qutebrowser/commands/command.py b/qutebrowser/commands/command.py index bcb77b907..b1bad6ada 100644 --- a/qutebrowser/commands/command.py +++ b/qutebrowser/commands/command.py @@ -49,6 +49,7 @@ class Command: _not_modes: The modes the command can not be executed in. _count: Whether the command supports a count, or not. _instance: The object to bind 'self' to. + _scope: The scope to get _instance for in the object registry. Class attributes: AnnotationInfo: Named tuple for info from an annotation. @@ -61,7 +62,7 @@ class Command: def __init__(self, name, split, hide, instance, completion, modes, not_modes, needs_js, is_debug, ignore_args, - handler): + handler, scope): # I really don't know how to solve this in a better way, I tried. # pylint: disable=too-many-arguments,too-many-locals self.name = name @@ -71,6 +72,7 @@ class Command: self.completion = completion self._modes = modes self._not_modes = not_modes + self._scope = scope self._needs_js = needs_js self.debug = is_debug self.ignore_args = ignore_args @@ -300,7 +302,7 @@ class Command: args: The positional argument list. Gets modified directly. """ assert param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD - obj = objreg.get(self._instance) + obj = objreg.get(self._instance, scope=self._scope) args.append(obj) def _get_count_arg(self, param, args, kwargs):