Skip to content

Commit e80d75d

Browse files
committed
[terminology] add function(s) to get a section by type
1 parent 5792b48 commit e80d75d

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

odml/terminology.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,77 @@ def type_list(self):
164164
else:
165165
self.types[s.type] = [(k[0], s.get_path())]
166166
return self.types
167+
168+
def _compare_repo(self, candidate_repo, candidate_path, pattern, relaxed):
169+
parts = pattern.lower().split()
170+
match = True
171+
repo = candidate_repo.lower()
172+
path = candidate_path.lower()
173+
for p in parts:
174+
if p.startswith("!"):
175+
if relaxed:
176+
match = match or (p[1:] not in repo.lower() and p[1:] not in path)
177+
else:
178+
match = match and (p[1:] not in repo and p[1:] not in path)
179+
else:
180+
if relaxed:
181+
match = match or (p in repo or p in path)
182+
else:
183+
match = match and (p in repo or p in path)
184+
return match
185+
186+
def _find_match(self, type_matches, pattern, relaxed=False):
187+
if pattern:
188+
for i, (r, p) in enumerate(type_matches):
189+
if self._compare_repo(r, p, pattern, relaxed):
190+
return type_matches[i]
191+
else: # simply return first
192+
return type_matches[0]
193+
return None
194+
195+
def _get_section_by_type(self, section_type, pattern=None, relaxed=False):
196+
if self.empty() or len(self.types) == 0:
197+
self.type_list()
198+
match = None
199+
if section_type in self.types:
200+
match = self._find_match(self.types[section_type], pattern, relaxed)
201+
if match:
202+
return self[match[0]].get_section_by_path(match[1]).clone()
203+
else:
204+
return None
205+
206+
167207
terminologies = Terminologies()
168208
load = terminologies.load
169209
deferred_load = terminologies.deferred_load
170210

171211

212+
def get_section_by_type(section_type, pattern=None, relaxed=False):
213+
"""
214+
Finds a section type in the cached repositories and returns it.
215+
216+
@param section_type the type of the section must be a valid full match. Returns the
217+
first match.
218+
@param pattern a optional filter pattern, i.e. a string with characteristics
219+
regarding the repository the section should originate from
220+
and its path in the file (see below)
221+
@param relaxed optional, defines whether all criteria must be met or not.
222+
223+
@return Section the first match or None
224+
225+
Example:
226+
Suppose we are looking for a section type 'analysis' and it should be from the g-node
227+
terminologies.
228+
s = get_section_by_type("analysis", "g-node")
229+
print(s)
230+
<Section Analysis[analysis] (0)>
231+
If we want to exclude the g-node terminologies, simply put an ! in front of the pattern
232+
s = get_section_by_type("analysis", "!g-node")
233+
234+
Multiple criteria can be combined (e.g. get_section_by_type("setup/daq", "g-node blackrock !cerebus")).
235+
The relaxed parameter controls whether all criteria have to match.
236+
"""
237+
return terminologies._get_section_by_type(section_type, pattern, relaxed)
172238

173239

174240
if __name__ == "__main__":

0 commit comments

Comments
 (0)