Skip to content

Commit e930096

Browse files
committed
[terminology] add optional argument to get_section_by_type to get all ..
sections that match the criterion
1 parent fa9761f commit e930096

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

odml/terminology.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,29 @@ def _compare_repo(self, candidate_repo, candidate_path, pattern, relaxed):
208208

209209
def _find_match(self, type_matches, pattern, relaxed=False):
210210
if pattern:
211-
for i, (r, p) in enumerate(type_matches):
211+
matches = []
212+
for i, (r, p) in enumerate(type_matches):
212213
if self._compare_repo(r, p, pattern, relaxed):
213-
return type_matches[i]
214+
matches.append(type_matches[i])
215+
return matches
214216
else: # simply return first
215-
return type_matches[0]
216-
return None
217+
return type_matches
218+
return []
217219

218-
def _get_section_by_type(self, section_type, pattern=None, relaxed=False):
220+
def _get_section_by_type(self, section_type, pattern=None, relaxed=False, find_all=False):
219221
if self.empty() or len(self.types) == 0:
220222
self.type_list()
221-
match = None
223+
matches = None
222224
if section_type in self.types:
223-
match = self._find_match(self.types[section_type], pattern, relaxed)
224-
if match:
225-
return self[match[0]].get_section_by_path(match[1]).clone()
225+
matches = self._find_match(self.types[section_type], pattern, relaxed)
226+
if len(matches) > 0:
227+
if len(matches) > 1 and find_all:
228+
sections = []
229+
for m in matches:
230+
sections.append(self[m[0]].get_section_by_path(m[1]).clone())
231+
return sections
232+
else:
233+
return self[matches[0][0]].get_section_by_path(matches[0][1]).clone()
226234
else:
227235
return None
228236

@@ -232,7 +240,7 @@ def _get_section_by_type(self, section_type, pattern=None, relaxed=False):
232240
deferred_load = terminologies.deferred_load
233241

234242

235-
def get_section_by_type(section_type, pattern=None, relaxed=False):
243+
def get_section_by_type(section_type, pattern=None, relaxed=False, find_all=False):
236244
"""
237245
Finds a section type in the cached repositories and returns it.
238246
@@ -242,8 +250,10 @@ def get_section_by_type(section_type, pattern=None, relaxed=False):
242250
regarding the repository the section should originate from
243251
and its path in the file (see below)
244252
@param relaxed optional, defines whether all criteria must be met or not.
253+
@param find_all optional, sets whether all possible matches are returned
245254
246-
@return Section the first match or None
255+
@return Section or list of sections depending on the find_all parameter, None,
256+
if no match was found.
247257
248258
Example:
249259
Suppose we are looking for a section type 'analysis' and it should be from the g-node
@@ -254,10 +264,10 @@ def get_section_by_type(section_type, pattern=None, relaxed=False):
254264
If we want to exclude the g-node terminologies, simply put an ! in front of the pattern
255265
s = get_section_by_type("analysis", "!g-node")
256266
257-
Multiple criteria can be combined (e.g. get_section_by_type("setup/daq", "g-node blackrock !cerebus")).
267+
Multiple criteria can be combined (e.g. get_section_by_type("setup/daq", "g-node blackrock !cerebus")).
258268
The relaxed parameter controls whether all criteria have to match.
259269
"""
260-
return terminologies._get_section_by_type(section_type, pattern, relaxed)
270+
return terminologies._get_section_by_type(section_type, pattern, relaxed, find_all)
261271

262272
def find_definitions(section_type):
263273
"""
@@ -277,7 +287,8 @@ def find_definitions(section_type):
277287
if __name__ == "__main__":
278288
from IPython import embed
279289
print ("Terminologies!")
280-
# from_cache(terminologies)
290+
from_cache(terminologies)
281291
# t.load('http://portal.g-node.org/odml/terminologies/v1.0/terminologies.xml')
282292
# t.load('http://portal.g-node.org/odml/terminologies/v1.0/analysis/power_spectrum.xml')
293+
find_definitions("analysis")
283294
embed()

0 commit comments

Comments
 (0)