summaryrefslogtreecommitdiffstats
path: root/command.py
diff options
context:
space:
mode:
authorJan Pokorný <jpokorny@redhat.com>2014-03-05 23:42:43 +0100
committerJan Pokorný <jpokorny@redhat.com>2014-03-05 23:44:47 +0100
commit45ff6bd2c00e075cba42f3bf7833e7f6bfc84bfa (patch)
tree6ea60a9d5c96e59889c6bf08fec42efaadf07ff3 /command.py
parentb3d55f40aac0a9146b89d076e30c6c5b2e96ad20 (diff)
downloadclufter-45ff6bd2c00e075cba42f3bf7833e7f6bfc84bfa.tar.gz
clufter-45ff6bd2c00e075cba42f3bf7833e7f6bfc84bfa.tar.xz
clufter-45ff6bd2c00e075cba42f3bf7833e7f6bfc84bfa.zip
command: start adding cmd alias mechanism with the extra class
Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
Diffstat (limited to 'command.py')
-rw-r--r--command.py31
1 files changed, 31 insertions, 0 deletions
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