Add scope argument to cmdutils.register/commands.

This commit is contained in:
Florian Bruhin 2014-09-25 07:41:51 +02:00
parent df5ac3ab2f
commit e527db1560
2 changed files with 12 additions and 8 deletions

View File

@ -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:]

View File

@ -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):