@@ -208,21 +208,29 @@ def _compare_repo(self, candidate_repo, candidate_path, pattern, relaxed):
208
208
209
209
def _find_match (self , type_matches , pattern , relaxed = False ):
210
210
if pattern :
211
- for i , (r , p ) in enumerate (type_matches ):
211
+ matches = []
212
+ for i , (r , p ) in enumerate (type_matches ):
212
213
if self ._compare_repo (r , p , pattern , relaxed ):
213
- return type_matches [i ]
214
+ matches .append (type_matches [i ])
215
+ return matches
214
216
else : # simply return first
215
- return type_matches [ 0 ]
216
- return None
217
+ return type_matches
218
+ return []
217
219
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 ):
219
221
if self .empty () or len (self .types ) == 0 :
220
222
self .type_list ()
221
- match = None
223
+ matches = None
222
224
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 ()
226
234
else :
227
235
return None
228
236
@@ -232,7 +240,7 @@ def _get_section_by_type(self, section_type, pattern=None, relaxed=False):
232
240
deferred_load = terminologies .deferred_load
233
241
234
242
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 ):
236
244
"""
237
245
Finds a section type in the cached repositories and returns it.
238
246
@@ -242,8 +250,10 @@ def get_section_by_type(section_type, pattern=None, relaxed=False):
242
250
regarding the repository the section should originate from
243
251
and its path in the file (see below)
244
252
@param relaxed optional, defines whether all criteria must be met or not.
253
+ @param find_all optional, sets whether all possible matches are returned
245
254
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.
247
257
248
258
Example:
249
259
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):
254
264
If we want to exclude the g-node terminologies, simply put an ! in front of the pattern
255
265
s = get_section_by_type("analysis", "!g-node")
256
266
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")).
258
268
The relaxed parameter controls whether all criteria have to match.
259
269
"""
260
- return terminologies ._get_section_by_type (section_type , pattern , relaxed )
270
+ return terminologies ._get_section_by_type (section_type , pattern , relaxed , find_all )
261
271
262
272
def find_definitions (section_type ):
263
273
"""
@@ -277,7 +287,8 @@ def find_definitions(section_type):
277
287
if __name__ == "__main__" :
278
288
from IPython import embed
279
289
print ("Terminologies!" )
280
- # from_cache(terminologies)
290
+ from_cache (terminologies )
281
291
# t.load('http://portal.g-node.org/odml/terminologies/v1.0/terminologies.xml')
282
292
# t.load('http://portal.g-node.org/odml/terminologies/v1.0/analysis/power_spectrum.xml')
293
+ find_definitions ("analysis" )
283
294
embed ()
0 commit comments