|
9 | 9 | from enum import Enum, auto |
10 | 10 | from pathlib import Path |
11 | 11 | from shutil import rmtree |
12 | | -from typing import Any, Callable, NamedTuple, Union, cast, no_type_check |
| 12 | +from typing import Any, Callable, NamedTuple, Optional, Union, cast, no_type_check |
13 | 13 | from warnings import warn |
14 | 14 | from xml.etree import ElementTree as ET |
15 | 15 |
|
@@ -396,28 +396,33 @@ def read_json(path: Union[str, Path]) -> Any: |
396 | 396 | return json.loads(f.read()) |
397 | 397 |
|
398 | 398 |
|
399 | | -def ref_extract_obj_id(ref: dict[str, Any]) -> ObjId: |
| 399 | +def ref_extract_obj_id(ref: dict[str, Any], type: Optional[str] = None) -> ObjId: |
400 | 400 | """ |
401 | 401 | Extracts ObjId from a ref dictionary. |
402 | 402 | :param ref: the ref to extract from |
| 403 | + :param type: the type of the object to fall back to in case of string identifier |
403 | 404 | :return: the extracted ObjId |
404 | 405 | :raises ValueError: if the ref is not an identifier |
405 | 406 | """ |
406 | 407 | if "identifier" in ref: |
| 408 | + if isinstance(ref["identifier"], str): |
| 409 | + if not type: |
| 410 | + raise ValueError("identifier is a string, but fallback type is not provided") |
| 411 | + return ObjId(id=ref["identifier"], type=type) |
407 | 412 | return ObjId(id=ref["identifier"]["id"], type=ref["identifier"]["type"]) |
408 | 413 |
|
409 | 414 | raise ValueError("invalid ref. must be identifier") |
410 | 415 |
|
411 | 416 |
|
412 | | -def ref_extract(ref: dict[str, Any]) -> Union[str, ObjId]: |
| 417 | +def ref_extract(ref: dict[str, Any], type: Optional[str] = None) -> Union[str, ObjId]: |
413 | 418 | """ |
414 | 419 | Extracts an object id from a ref dictionary: either an identifier or a localIdentifier. |
415 | 420 | :param ref: the ref to extract from |
416 | 421 | :return: thr extracted object id |
417 | 422 | :raises ValueError: if the ref is not an identifier or localIdentifier |
418 | 423 | """ |
419 | 424 | try: |
420 | | - return ref_extract_obj_id(ref) |
| 425 | + return ref_extract_obj_id(ref, type) |
421 | 426 | except ValueError: |
422 | 427 | pass |
423 | 428 |
|
|
0 commit comments