smarter way to write configs, provide structure and map to avail. resources (testboxe...
[tests.git] / system / TestMapper.py
index 1d2dd3c..87ec6c4 100644 (file)
@@ -5,7 +5,7 @@
 # 
 # this works on a spec as defined in a config file
 # and allows to remap various fields, typically to another testbox 
-# see an example in config-testbox2.py
+# see an example in config_onelab_testbox32.py
 # 
 
 import re
@@ -13,9 +13,8 @@ import utils
 
 class TestMapper:
 
-    def __init__ (self,plcs,mapper,options):
+    def __init__ (self,plcs,options):
         self.plcs=plcs
-        self.mapper=mapper
         self.options=options
 
     @staticmethod
@@ -31,37 +30,61 @@ class TestMapper:
     def node_name (node):
         return node['node_fields']['hostname']
 
-    def apply_first_map (self, obj, name, type, maplist):
-        for (key,dict) in maplist:
-            if TestMapper.match (name,key):
-                utils.header("TestMapper/%s : applying match key %s on plc %s"%(type,key,name))
-                for (k,v) in dict.iteritems():
-                    if not obj.has_key(k):
-                        utils.header ("WARNING : no such key %s in %s %s",k,type,name)
-                    else:
-                        # apply formatting if found
-                        if v.find('%s')>=0:
-                            v=v%obj[k]
-                        if self.options.verbose:
-                            utils.header("mapping %s->%s towards %s"%(name,k,v))
-                        obj[k]=v
-                break
+    def apply_first_map (self, type, name, obj, maplist):
+        for (map_pattern,rename_dict) in maplist:
+            if TestMapper.match (name,map_pattern):
+                utils.header("TestMapper/%s : applying match key %s on plc %s"%(type,map_pattern,name))
+                for (k,v) in rename_dict.iteritems():
+                    # apply : separator
+                    path=k.split(':')
+                    # step down but last step in path
+                    o=obj
+                    for step in path[:-1]:
+                        if not o.has_key(step):
+                            o[step]={}
+                            utils.header ("WARNING : created step %s in path %s on %s %s"%(
+                                    step,path,type,name))
+                        o=o[step]
+                    # last step is the one for side-effect
+                    step=path[-1]
+                    if not o.has_key(step):
+                        utils.header ("WARNING : inserting key %s for path %s on %s %s"%(
+                                step,path,type,name))
+                    # apply formatting if '%s' found in the value
+                    if v.find('%s')>=0:
+                        v=v%obj[k]
+                    if self.options.verbose:
+                        utils.header("mapping %s->%s towards %s"%(name,k,v))
+                    o[step]=v
+                # only apply first rule
+                return
 
-    def map (self):
+    def node_names (self):
+        result=[]
+        for plc in self.plcs:
+            for site in plc['sites']:
+                for node in site['nodes']:
+                    result.append(node['node_fields']['hostname'])
+        return result
 
-        plc_maps = self.mapper['plc']
+    def map (self,mapper):
+
+        try:
+            plc_maps = mapper['plc']
+        except:
+            plc_maps = []
+        try:
+            node_maps = mapper['node']
+        except:
+            node_maps = []
 
         for plc in self.plcs:
             name=TestMapper.plc_name(plc)
-            self.apply_first_map (plc,name,'plc',plc_maps)
-
-            node_maps = self.mapper['node']
+            self.apply_first_map ('plc',name,plc,plc_maps)
 
             for site in plc['sites']:
                 for node in site['nodes']:
                     nodename = TestMapper.node_name(node)
-                    self.apply_first_map(node,nodename,"node",node_maps)
+                    self.apply_first_map('node',nodename,node,node_maps)
 
         return self.plcs
-                                               
-