- 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 utils.match (name,map_pattern):
+ if self.options.verbose:
+ utils.header("TestMapper/{} : applying rules '{}' on {}"\
+ .format(type, map_pattern, name))
+ for (k,v) in rename_dict.items():
+ # apply : separator
+ path = k.split(':')
+ # step down but last step in path
+ o = obj
+ for step in path[:-1]:
+ if step not in o:
+ o[step] = {}
+ if self.options.verbose:
+ utils.header ("WARNING : created step {} in path {} on {} {}"\
+ .format(step,path,type,name))
+ o = o[step]
+ # last step is the one for side-effect
+ step = path[-1]
+ if self.options.verbose:
+ if step not in o:
+ utils.header ("WARNING : inserting key {} for path {} on {} {}"\
+ .format(step, path, type, name))
+ # apply formatting if '%s' found in the value
+ if v is None:
+ if self.options.verbose: print("TestMapper WARNING - None value - ignored, key=",k)
+ continue
+ if v.find('%s') >= 0:
+ v = v % obj[k]
+ if self.options.verbose:
+ print(("TestMapper, rewriting {}: {} into {}"\
+ .format(name, k, v)))
+ o[step] = v
+ # only apply first rule
+ return