Make arguments an internal feature of cli.commands
authorStavros Sachtouris <saxtouri@gmail.com>
Sat, 22 Dec 2012 17:38:43 +0000 (19:38 +0200)
committerStavros Sachtouris <saxtouri@gmail.com>
Sat, 22 Dec 2012 17:38:43 +0000 (19:38 +0200)
Now any cli.commands subclass instance:
- sets args like this:
 mycmd['myargname'] = Argument()
instead of
 mycmd.arguments['myargname']
- gets args like this:
 mycmd['myargname'] = Argument()
instead of
 mycmd.arguments['myargname']
-get argument values like this:
 mycmd['myargname'].value
instead of
 mycmd.get_argument('myargname')
Backwards compatibility is preserved -old methds still ok

kamaki/cli/commands/__init__.py

index b1504e2..66a9400 100644 (file)
@@ -39,11 +39,43 @@ recvlog = logging.getLogger('clients.recv')
 
 class _command_init(object):
     def __init__(self, arguments={}):
-        self.arguments = arguments
+        if not hasattr(self, 'arguments'):
+            self.arguments = {}
+        self.arguments.update(arguments)
         try:
-            self.config = self.get_argument('config')
+            self.config = self['config'].value
+            #self.config = self.get_argument('config')
         except KeyError:
             pass
 
+    def __getitem__(self, argterm):
+        """
+        :param argterm: (str) the name/label of an argument in self.arguments
+
+        :returns: (Argument)
+
+        :raises KeyError: if argterm not in self.arguments of this object
+        """
+        return self.arguments[argterm]
+
+    def __setitem__(self, argterm, arg):
+        """Install an argument as argterm
+        If argterm points to another argument, the other argument is lost
+
+        :param argterm: (str)
+
+        :param arg: (Argument)
+        """
+        if not hasattr(self, 'arguments'):
+            self.arguments = {}
+        self.arguments[argterm] = arg
+
     def get_argument(self, argterm):
+        """
+        :param argterm: (str) the name/label of an argument in self.arguments
+
+        :returns: the value of the arument object
+
+        :raises KeyError: if argterm not in self.arguments of this object
+        """
         return self.arguments[argterm].value