@@ -186,20 +186,28 @@ def __init__(self, *args, **kwargs):
186
186
doc = "Whether to include imported ontologies in dir() listing." ,
187
187
)
188
188
189
+ # Other settings
190
+ _colon_in_label = False
191
+ colon_in_label = property (
192
+ fget = lambda self : self ._colon_in_label ,
193
+ fset = lambda self , v : setattr (self , "_colon_in_label" , bool (v )),
194
+ doc = "Whether to accept colon in name-part of IRI. "
195
+ "If true, the name cannot be prefixed." ,
196
+ )
197
+
189
198
def __dir__ (self ):
190
- set_dir = set (super ().__dir__ ())
199
+ dirset = set (super ().__dir__ ())
191
200
lst = list (self .get_entities (imported = self ._dir_imported ))
192
201
if self ._dir_preflabel :
193
- set_dir .update (
202
+ dirset .update (
194
203
_ .prefLabel .first () for _ in lst if hasattr (_ , "prefLabel" )
195
204
)
196
205
if self ._dir_label :
197
- set_dir .update (_ .label .first () for _ in lst if hasattr (_ , "label" ))
206
+ dirset .update (_ .label .first () for _ in lst if hasattr (_ , "label" ))
198
207
if self ._dir_name :
199
- set_dir .update (_ .name for _ in lst if hasattr (_ , "name" ))
200
-
201
- set_dir .difference_update ({None }) # get rid of possible None
202
- return sorted (set_dir )
208
+ dirset .update (_ .name for _ in lst if hasattr (_ , "name" ))
209
+ dirset .difference_update ({None }) # get rid of possible None
210
+ return sorted (dirset )
203
211
204
212
def __getitem__ (self , name ):
205
213
item = super ().__getitem__ (name )
@@ -257,7 +265,12 @@ def get_unabbreviated_triples(
257
265
)
258
266
259
267
def get_by_label (
260
- self , label : str , label_annotations : str = None , prefix : str = None
268
+ self ,
269
+ label : str ,
270
+ label_annotations : str = None ,
271
+ prefix : str = None ,
272
+ imported : bool = True ,
273
+ colon_in_label : bool = None ,
261
274
):
262
275
"""Returns entity with label annotation `label`.
263
276
@@ -272,50 +285,52 @@ def get_by_label(
272
285
the base iri of an ontology (with trailing slash (/) or hash
273
286
(#) stripped off). The search for a matching label will be
274
287
limited to this namespace.
288
+ imported: Whether to also look for `label` in imported ontologies.
289
+ colon_in_label: Whether to accept colon (:) in a label or name-part
290
+ of IRI. Defaults to the `colon_in_label` property of `self`.
291
+ Setting this true cannot be combined with `prefix`.
275
292
276
293
If several entities have the same label, only the one which is
277
294
found first is returned.Use get_by_label_all() to get all matches.
278
295
279
- A NoSuchLabelError is raised if `label` cannot be found.
296
+ Note, if different prefixes are provided in the label and via
297
+ the `prefix` argument a warning will be issued and the
298
+ `prefix` argument will take precedence.
280
299
281
- Note
282
- ----
283
- The current implementation also supports "*" as a wildcard
284
- matching any number of characters. This may change in the future.
300
+ A NoSuchLabelError is raised if `label` cannot be found.
285
301
"""
286
- # pylint: disable=too-many-arguments,too-many-branches
302
+ # pylint: disable=too-many-arguments,too-many-branches,invalid-name
287
303
if not isinstance (label , str ):
288
304
raise TypeError (
289
305
f"Invalid label definition, must be a string: { label !r} "
290
306
)
291
- if " " in label :
292
- raise ValueError (
293
- f"Invalid label definition, { label !r} contains spaces."
294
- )
307
+
295
308
if self ._label_annotations is None :
296
309
for iri in DEFAULT_LABEL_ANNOTATIONS :
297
310
try :
298
311
self .add_label_annotation (iri )
299
312
except ValueError :
300
313
pass
301
314
302
- splitlabel = label .split (":" , 1 )
303
- if len (splitlabel ) > 2 :
304
- raise ValueError (
305
- f"Invalid label definition, { label !r} "
306
- " contains more than one ':' ."
307
- "The string before ':' indicates the prefix. "
308
- "The string after ':' indicates the label."
309
- )
310
- if len (splitlabel ) == 2 :
311
- label = splitlabel [1 ]
312
- if prefix and prefix != splitlabel [0 ]:
313
- warnings .warn (
314
- f"Prefix given both as argument ({ prefix } ) "
315
- f"and in label ({ splitlabel [0 ]} ). "
316
- "Prefix given in label takes presendence "
315
+ if colon_in_label is None :
316
+ colon_in_label = self ._colon_in_label
317
+ if colon_in_label :
318
+ if prefix :
319
+ raise ValueError (
320
+ "`prefix` cannot be combined with `colon_in_label`"
317
321
)
318
- prefix = splitlabel [0 ]
322
+ else :
323
+ splitlabel = label .split (":" , 1 )
324
+ if len (splitlabel ) == 2 and not splitlabel [1 ].startswith ("//" ):
325
+ label = splitlabel [1 ]
326
+ if prefix and prefix != splitlabel [0 ]:
327
+ warnings .warn (
328
+ f"Prefix given both as argument ({ prefix } ) "
329
+ f"and in label ({ splitlabel [0 ]} ). "
330
+ "Prefix given in argument takes presendence "
331
+ )
332
+ if not prefix :
333
+ prefix = splitlabel [0 ]
319
334
320
335
if prefix :
321
336
entitylist = self .get_by_label_all (
@@ -327,36 +342,56 @@ def get_by_label(
327
342
return entitylist [0 ]
328
343
329
344
raise NoSuchLabelError (
330
- f"No label annotations matches { label !r} with prefix "
345
+ f"No label annotations matches { label !r} with prefix "
331
346
f"{ prefix !r} "
332
347
)
333
- # if label in self._namespaces:
334
- # return self._namespaces[label]
335
348
336
- if label_annotations is None :
337
- annotations = (a .name for a in self .label_annotations )
338
- else :
339
- annotations = (
340
- a .name if hasattr (a , "storid" ) else a for a in label_annotations
341
- )
342
- for key in annotations :
343
- entity = self .search_one (** {key : label })
344
- if entity :
345
- return entity
349
+ # Label is a full IRI
350
+ entity = self .world [label ]
351
+ if entity :
352
+ return entity
353
+
354
+ # First entity with matching label annotation
355
+ annotation_ids = (
356
+ (self ._abbreviate (ann , False ) for ann in label_annotations )
357
+ if label_annotations
358
+ else (ann .storid for ann in self .label_annotations )
359
+ )
360
+ get_triples = (
361
+ self .world ._get_data_triples_spod_spod
362
+ if imported
363
+ else self ._get_data_triples_spod_spod
364
+ )
365
+ for annotation_id in annotation_ids :
366
+ for s , _ , _ , _ in get_triples (None , annotation_id , label , None ):
367
+ return self .world [self ._unabbreviate (s )]
346
368
369
+ # Special labels
347
370
if self ._special_labels and label in self ._special_labels :
348
371
return self ._special_labels [label ]
349
372
373
+ # Check if label is a name under base_iri
350
374
entity = self .world [self .base_iri + label ]
351
375
if entity :
352
376
return entity
353
377
354
- raise NoSuchLabelError (f"No label annotations matches { label !r} " )
378
+ # Check if label is a name in any namespace
379
+ for namespace in self ._namespaces .keys ():
380
+ entity = self .world [namespace + label ]
381
+ if entity :
382
+ return entity
383
+
384
+ raise NoSuchLabelError (f"No label annotations matches '{ label } '" )
355
385
356
386
def get_by_label_all (self , label , label_annotations = None , prefix = None ):
357
387
"""Like get_by_label(), but returns a list with all matching labels.
358
388
359
389
Returns an empty list if no matches could be found.
390
+
391
+ Note
392
+ ----
393
+ The current implementation also supports "*" as a wildcard
394
+ matching any number of characters. This may change in the future.
360
395
"""
361
396
if not isinstance (label , str ):
362
397
raise TypeError (
@@ -1582,7 +1617,7 @@ def new_entity(
1582
1617
1583
1618
Throws exception if name consists of more than one word.
1584
1619
"""
1585
- if len ( name . split ( " " )) > 1 :
1620
+ if " " in name :
1586
1621
raise LabelDefinitionError (
1587
1622
f"Error in label name definition '{ name } ': "
1588
1623
f"Label consists of more than one word."
@@ -1684,7 +1719,7 @@ def _get_unabbreviated_triples(
1684
1719
_unabbreviate (self , p , blank = blank ),
1685
1720
_unabbreviate (self , o , blank = blank ),
1686
1721
)
1687
- for s , p , o , d in self ._get_data_triples_spod_spod (* abb , d = "" ):
1722
+ for s , p , o , d in self ._get_data_triples_spod_spod (* abb , d = None ):
1688
1723
yield (
1689
1724
_unabbreviate (self , s , blank = blank ),
1690
1725
_unabbreviate (self , p , blank = blank ),
0 commit comments