define PLC_MAIL_FROM_ADDRESS
[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/{} : applying rules '{}' on {}"\
41                                  .format(type, map_pattern, name))
42                 for (k,v) in rename_dict.items():
43                     # apply : separator
44                     path = k.split(':')
45                     # step down but last step in path
46                     o = obj
47                     for step in path[:-1]:
48                         if step not in o:
49                             o[step] = {}
50                             if self.options.verbose:
51                                 utils.header ("WARNING : created step {} in path {} on {} {}"\
52                                               .format(step,path,type,name))
53                         o = o[step]
54                     # last step is the one for side-effect
55                     step = path[-1]
56                     if self.options.verbose:
57                         if step not in o:
58                             utils.header ("WARNING : inserting key {} for path {} on {} {}"\
59                                           .format(step, path, type, name))
60                     # apply formatting if '%s' found in the value
61                     if v is None:
62                         if self.options.verbose: print("TestMapper WARNING - None value - ignored, key=",k)
63                         continue
64                     if v.find('%s') >= 0:
65                         v = v % obj[k]
66                     if self.options.verbose:
67                         print(("TestMapper, rewriting {}: {} into {}"\
68                               .format(name, k, v)))
69                     o[step] = v
70                 # only apply first rule
71                 return
72
73     def map(self, mapper):
74
75         plc_maps  = mapper.get('plc',[])
76         node_maps = mapper.get('node',[])
77
78         for plc in self.plcs:
79             name = TestMapper.plc_name(plc)
80             self.apply_first_map('plc', name, plc, plc_maps)
81
82             for site in plc['sites']:
83                 for node in site['nodes']:
84                     nodename = TestMapper.node_name(node)
85                     self.apply_first_map('node', nodename, node, node_maps)
86
87         return self.plcs