From 45ff6bd2c00e075cba42f3bf7833e7f6bfc84bfa Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Wed, 5 Mar 2014 23:42:43 +0100 Subject: command: start adding cmd alias mechanism with the extra class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- command.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'command.py') diff --git a/command.py b/command.py index e6657e0..78e155a 100644 --- a/command.py +++ b/command.py @@ -473,3 +473,34 @@ class Command(object): ret = cls.probe(fnc.__name__, (cls, ), attrs) return ret return deco_fnc + + +class CommandAlias(object): + """Way to define either static or dynamic command alias""" + __metaclass__ = commands + + @classmethod + def deco(outer_cls, decl): + if not hasattr(decl, '__call__'): + assert issubclass(decl, Command) + fnc = lambda **kwargs: decl + else: + fnc = decl + log.debug("CommandAlias: deco for {0}".format(fnc)) + + def new(cls, cmds): + # XXX really pass mutable cmds dict? + use_obj = fnc(cmds) + if not isinstance(use_obj, Command): + assert isinstance(use_obj, basestring) + use_obj = cmds[use_obj] + assert isinstance(use_obj, Command) + return use_obj + + attrs = { + '__module__': fnc.__module__, + '__new__': new, + } + # optimization: shorten type() -> new() -> probe + ret = outer_cls.probe(fnc.__name__, (outer_cls, ), attrs) + return ret -- cgit