[rhmessaging-commits] rhmessaging commits: r2255 - mgmt/trunk/cumin/python/wooly.

rhmessaging-commits at lists.jboss.org rhmessaging-commits at lists.jboss.org
Tue Aug 5 17:00:15 EDT 2008


Author: eallen
Date: 2008-08-05 17:00:14 -0400 (Tue, 05 Aug 2008)
New Revision: 2255

Modified:
   mgmt/trunk/cumin/python/wooly/__init__.py
   mgmt/trunk/cumin/python/wooly/parameters.py
Log:
Added DictParameter class to handle complex form inputs

Modified: mgmt/trunk/cumin/python/wooly/__init__.py
===================================================================
--- mgmt/trunk/cumin/python/wooly/__init__.py	2008-08-05 20:59:30 UTC (rev 2254)
+++ mgmt/trunk/cumin/python/wooly/__init__.py	2008-08-05 21:00:14 UTC (rev 2255)
@@ -60,6 +60,7 @@
         super(Parameter, self).__init__(app, name)
 
         self.is_collection = False
+        self.is_dictionary = False
 
         app.add_parameter(self)
 
@@ -440,7 +441,15 @@
 
             self.parameter_index = index
 
-        return self.parameter_index.get((page, key))
+        param = self.parameter_index.get((page, key))
+        
+        # check for partial match for dictparameters
+        if not param:
+            if "_" in key:
+                dict_key = key.split("_", 1)[0]
+                param = self.parameter_index.get((page, dict_key))
+                
+        return param
 
     def add_resource_dir(self, dir):
         self.finder.add_dir(dir)
@@ -596,7 +605,10 @@
                 param = self.app.get_parameter(self.get_page(), key)
 
                 if param:
-                    param.add(self, param.unmarshal(value))
+                    if param.is_dictionary:
+                        param.add(self, param.unmarshal(value), key)
+                    else:
+                        param.add(self, param.unmarshal(value))
             except ValueError:
                 pass
 

Modified: mgmt/trunk/cumin/python/wooly/parameters.py
===================================================================
--- mgmt/trunk/cumin/python/wooly/parameters.py	2008-08-05 20:59:30 UTC (rev 2254)
+++ mgmt/trunk/cumin/python/wooly/parameters.py	2008-08-05 21:00:14 UTC (rev 2255)
@@ -2,6 +2,55 @@
 
 from wooly import *
 
+class DictParameter(Parameter):
+    def __init__(self, app, name, key):
+        super(DictParameter, self).__init__(app, name)
+
+        self.default = dict()
+        self.is_dictionary = True
+        self.clear_on_add = False
+
+    def set_clear_on_add(self):
+        self.clear_on_add = True
+        
+    def add(self, session, value, full_key):
+        # full_key looks like <DictParameter.path>_<stuff>
+
+        if self.clear_on_add:
+            self.clear()
+            self.clear_on_add = False
+
+        keys = self.get(session)
+        
+        # separate the DictParameter.path from the stuff
+        foo, stuff = full_key.split("_", 1)
+        if stuff:
+            self.split_stuff(keys, stuff, value)
+
+    def split_stuff(self, keys, stuff, value):
+        """ Each time there is an _ in stuff,
+            create a nested dictionary entry """
+            
+        # can't use: stuff_1, stuff_2 = stuff.split("_", 1)    
+        stuff_list = stuff.split("_", 1)
+        stuff_1 = stuff_list[0]
+        if len(stuff_list) > 1:
+            stuff_2 = stuff_list[1]
+            if not stuff_1 in keys:
+                keys[stuff_1] = dict()
+            self.split_stuff(keys[stuff_1], stuff_2, value)
+        else:
+            if value:
+                keys[stuff_1] = value
+            elif stuff_1 in keys:
+                del keys[stuff_1]
+    
+    def get_instance_key(self, key):
+        return "_".join((self.path, key))
+    
+    def clear(self):
+        self.default = dict()
+        
 class ListParameter(Parameter):
     def __init__(self, app, name, param):
         super(ListParameter, self).__init__(app, name)




More information about the rhmessaging-commits mailing list