Refactor Command._get_self_arg

This commit is contained in:
Florian Bruhin 2018-11-30 13:06:53 +01:00
parent 7a90340299
commit 4e56d0e8b3

View File

@ -336,27 +336,18 @@ class Command:
else: else:
return str return str
def _get_self_arg(self, win_id, param, args): def _get_objreg(self, *, win_id, name, scope):
"""Get the self argument for a function call. """Get an object from the objreg."""
if scope == 'global':
Arguments:
win_id: The window id this command should be executed in.
param: The count parameter.
args: The positional argument list. Gets modified directly.
"""
assert param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
if self._scope == 'global':
tab_id = None tab_id = None
win_id = None win_id = None
elif self._scope == 'tab': elif scope == 'tab':
tab_id = 'current' tab_id = 'current'
elif self._scope == 'window': elif scope == 'window':
tab_id = None tab_id = None
else: else:
raise ValueError("Invalid scope {}!".format(self._scope)) raise ValueError("Invalid scope {}!".format(scope))
obj = objreg.get(self._instance, scope=self._scope, window=win_id, return objreg.get(name, scope=scope, window=win_id, tab=tab_id)
tab=tab_id)
args.append(obj)
def _add_special_arg(self, *, value, param, args, kwargs, optional=False): def _add_special_arg(self, *, value, param, args, kwargs, optional=False):
"""Add a special argument value to a function call. """Add a special argument value to a function call.
@ -435,8 +426,12 @@ class Command:
for i, param in enumerate(signature.parameters.values()): for i, param in enumerate(signature.parameters.values()):
arg_info = self.get_arg_info(param) arg_info = self.get_arg_info(param)
if i == 0 and self._instance is not None: if i == 0 and self._instance is not None:
# Special case for 'self'. assert param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
self._get_self_arg(win_id, param, args) self_value = self._get_objreg(win_id=win_id,
name=self._instance,
scope=self._scope)
self._add_special_arg(value=self_value, param=param,
args=args, kwargs=kwargs)
continue continue
elif arg_info.value == usertypes.CommandValue.count: elif arg_info.value == usertypes.CommandValue.count:
self._add_special_arg(value=self._count, param=param, self._add_special_arg(value=self._count, param=param,