7d0547f3e40c72891049ca2487d284eeecca803d
[tests.git] / system / TestMapper.py
1 #
2 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
3 # Copyright (C) 2010 INRIA 
4 #
5 #
6 # mapper class
7
8 # this works on a spec as defined in a config file
9 # and allows to remap various fields on the local substrate
10
11
12 import utils
13
14 class TestMapper:
15
16     def __init__ (self,plcs,options):
17         self.plcs=plcs
18         self.options=options
19
20     @staticmethod
21     def plc_name (plc):
22         return plc['name']
23
24     @staticmethod
25     def node_name (node):
26         return node['name']
27
28     def node_names (self):
29         result=[]
30         for plc in self.plcs:
31             for site in plc['sites']:
32                 for node in site['nodes']:
33                     result.append(node['name'])
34         return result
35
36     def apply_first_map (self, type, name, obj, maplist):
37         for (map_pattern,rename_dict) in maplist:
38             if utils.match (name,map_pattern):
39                 if self.options.verbose:
40                     utils.header("TestMapper/%s : applying rules '%s' on %s"%(type,map_pattern,name))
41                 for (k,v) in rename_dict.iteritems():
42                     # apply : separator
43                     path=k.split(':')
44                     # step down but last step in path
45                     o=obj
46                     for step in path[:-1]:
47                         if not o.has_key(step):
48                             o[step]={}
49                             if self.options.verbose:
50                                 utils.header ("WARNING : created step %s in path %s on %s %s"%(
51                                         step,path,type,name))
52                         o=o[step]
53                     # last step is the one for side-effect
54                     step=path[-1]
55                     if self.options.verbose:
56                         if not o.has_key(step):
57                             utils.header ("WARNING : inserting key %s for path %s on %s %s"%(
58                                     step,path,type,name))
59                     # apply formatting if '%s' found in the value
60                     if v is None:
61                         if self.options.verbose: print "TestMapper WARNING - None value - ignored, key=",k
62                         continue
63                     if v.find('%s')>=0:
64                         v=v%obj[k]
65                     if self.options.verbose:
66                         print("TestMapper, rewriting %s: %s into %s"%(name,k,v))
67                     o[step]=v
68                 # only apply first rule
69                 return
70
71     def map (self,mapper):
72
73         plc_maps  = mapper.get('plc',[])
74         node_maps = mapper.get('node',[])
75
76         for plc in self.plcs:
77             name=TestMapper.plc_name(plc)
78             self.apply_first_map ('plc',name,plc,plc_maps)
79
80             for site in plc['sites']:
81                 for node in site['nodes']:
82                     nodename = TestMapper.node_name(node)
83                     self.apply_first_map('node',nodename,node,node_maps)
84
85         return self.plcs