71 """Main code to translate the configuration given by 'top_level' into the
72 final configuration by expanding the regexes using the objects in 'td'"""
73 log.info('Translating regexes...')
75
76 refmapcache = {}
77 mapping = {}
78 mapping_regex = {}
79 mapping_groups = {'top_level': top_level}
80 mapping_assessors_final = {}
82 iterate_hcfg(top_level, mapping, mapping_regex, mapping_groups)
83 mapping_assessors_final.update(mapping)
84 for path, g in mapping_groups.items():
85 newgroup = HanConfigGroup()
86 newgroup.SetName(g.GetName())
87 newgroup.SetPathName(path)
88 copyMetadata(newgroup, g)
89 mapping_groups_final[path] = newgroup
90
91
92 l: List[str] = []
93 iterate_objs(td, l, td.GetPath())
94 for path, (a, p) in mapping_regex.items():
95 pre = re.compile(p)
96 for h in l:
97 m = pre.fullmatch(h)
98 if m:
99 log.debug(f'match {p} with {h}')
100 tokenized_path = path.split('/')
101 orig_fullpath = []
102 new_fullpath = []
103 for tok in tokenized_path[:-1]:
104 formatted_tok = string.Template(tok).substitute(m.groupdict())
105 orig_fullpath.append(tok)
106 new_fullpath.append(formatted_tok)
107 orig = '/'.join(orig_fullpath)
108 target = '/'.join(new_fullpath)
109 if target not in mapping_groups_final:
110 log.debug(f'Need to adapt {orig} to {target}')
111 if orig in mapping_groups_final:
112 log.debug(f'Deleting {orig}')
113 del mapping_groups_final[orig]
114 newgroup = HanConfigGroup()
115 newgroup.SetName(formatted_tok)
116 newgroup.SetPathName(target)
117 oldgroup = mapping_groups[orig]
118 copyMetadata(newgroup, oldgroup)
119 mapping_groups_final[target] = newgroup
120
121 tok = tokenized_path[-1]
122 orig_fullpath.append(tok)
123 hname = h.split('/')[-1]
124
125 hextra = '' if '@' not in tok else ('@' + tok.rsplit('@',1)[1])
126 hname += hextra
127 new_fullpath.append(hname)
128 orig = '/'.join(orig_fullpath)
129 target = '/'.join(new_fullpath)
130 log.debug(f'Map from {orig} to {target}')
131
133 newass.SetName(h + hextra)
134 algrefname = a.GetAlgRefName()
135
136 if a.GetAlgRefName() != "":
137 ref = refmapcache.get(a.GetAlgRefName())
138 if ref is None:
139 ref = config.Get(a.GetAlgRefName())
140 refmapcache[a.GetAlgRefName()] = ref
141 if not ref:
142 log.error('Unable to find references for %s', orig)
143 else:
144 if isinstance(ref, ROOT.TMap):
145 algrefnameptr = ref.GetValue(h)
146 algrefname = algrefnameptr.GetString().
Data()
if algrefnameptr
else ""
147 if algrefname and isinstance(config.Get(algrefname), ROOT.TMap):
148 log.error(f'Reference for {newass} is somehow still a TMap')
149 copyMetadata(newass, mapping_regex[orig][0], input=h, algrefname=algrefname)
150 mapping_assessors_final[target] = (newass, newass.GetHistPath())
151 log.debug(f'change {orig} to {target}')
152
153
154
155 for p, (a, _) in mapping_assessors_final.items():
156 try:
157 mapping_groups_final[os.path.dirname(p)].AddAssessor(a)
158 except Exception as e:
159 log.error(f'Unable to look up assessor parent directory. Full error follows\n{e}: path {p}, assessor {a}, assessor name {a.GetName()}')
160
161 keydepths = count_slashes(mapping_groups_final.items())
162 for _, l2 in sorted(keydepths.items(), reverse=True):
163 for p, g in l2:
164 if log.level <= logging.DEBUG:
165 log.debug(f'Final additions for {p}, {g.GetName()}, {g.GetPathName()} into {os.path.dirname(p)}')
166 if p != 'top_level':
167 try:
168 mapping_groups_final[os.path.dirname(p)].AddGroup(g)
169 except KeyError:
170 log.error(f'Unable to attach group to parent. Details: group path {g.GetPathName()}, attempted parent is {os.path.dirname(p)}')
171
172 for v in refmapcache.values():
173
174 if isinstance(v, ROOT.TMap):
175 v.DeleteAll()
176
177 return mapping_groups_final['top_level']
178
179