|
29 | 29 | Keywords1, |
30 | 30 | TargetMode, |
31 | 31 | ) |
| 32 | +from .uri import parse_uri |
32 | 33 | from xsdata.formats.dataclass.models.generics import DerivedElement |
33 | 34 |
|
34 | 35 | from .constants import ( |
|
39 | 40 | RawFile, |
40 | 41 | EPCRelsRelationshipType, |
41 | 42 | MimeType, |
| 43 | + content_type_to_qualified_type, |
| 44 | + qualified_type_to_content_type, |
42 | 45 | split_identifier, |
43 | 46 | get_property_kind_dict_path_as_dict, |
44 | 47 | ) |
|
49 | 52 | from .introspection import ( |
50 | 53 | get_class_from_content_type, |
51 | 54 | get_obj_type, |
| 55 | + is_dor, |
52 | 56 | search_attribute_matching_type, |
53 | 57 | get_obj_version, |
54 | 58 | get_obj_uuid, |
|
65 | 69 | set_attribute_from_path, |
66 | 70 | set_attribute_value, |
67 | 71 | get_object_attribute, |
68 | | - get_qualified_type_from_class, get_class_fields, |
| 72 | + get_qualified_type_from_class, |
| 73 | + get_class_fields, |
69 | 74 | ) |
70 | 75 | from .manager import get_class_pkg, get_class_pkg_version |
71 | 76 | from .serialization import ( |
@@ -631,32 +636,74 @@ def as_dor(obj_or_identifier: Any, dor_qualified_type: str = "eml23.DataObjectRe |
631 | 636 | """ |
632 | 637 | dor = None |
633 | 638 | if obj_or_identifier is not None: |
634 | | - if isinstance(obj_or_identifier, str): # is an identifier |
635 | | - cls = get_class_from_qualified_type(dor_qualified_type) |
636 | | - dor = cls() |
637 | | - if len(__CACHE_PROP_KIND_DICT__) == 0: |
638 | | - # update the cache to check if it is a |
639 | | - update_prop_kind_dict_cache() |
640 | | - try: |
641 | | - uuid, version = split_identifier(obj_or_identifier) |
642 | | - if uuid in __CACHE_PROP_KIND_DICT__: |
643 | | - return as_dor(__CACHE_PROP_KIND_DICT__[uuid]) |
644 | | - else: |
645 | | - set_attribute_from_path(dor, "uuid", uuid) |
646 | | - set_attribute_from_path(dor, "ObjectVersion", version) |
647 | | - except AttributeError: |
648 | | - logging.error(f"Failed to parse identifier {obj_or_identifier}. DOR will be empty") |
| 639 | + cls = get_class_from_qualified_type(dor_qualified_type) |
| 640 | + dor = cls() |
| 641 | + if isinstance(obj_or_identifier, str): # is an identifier or uri |
| 642 | + parsed_uri = parse_uri(obj_or_identifier) |
| 643 | + if parsed_uri is not None: |
| 644 | + if hasattr(dor, "qualified_type"): |
| 645 | + set_attribute_from_path(dor, "qualified_type", parsed_uri.get_qualified_type()) |
| 646 | + if hasattr(dor, "content_type"): |
| 647 | + set_attribute_from_path( |
| 648 | + dor, "content_type", qualified_type_to_content_type(parsed_uri.get_qualified_type()) |
| 649 | + ) |
| 650 | + set_attribute_from_path(dor, "uuid", parsed_uri.uuid) |
| 651 | + if hasattr(dor, "object_version"): |
| 652 | + set_attribute_from_path(dor, "version_string", parsed_uri.version) |
| 653 | + if hasattr(dor, "version_string"): |
| 654 | + set_attribute_from_path(dor, "version_string", parsed_uri.version) |
| 655 | + |
| 656 | + else: # identifier |
| 657 | + if len(__CACHE_PROP_KIND_DICT__) == 0: |
| 658 | + # update the cache to check if it is a |
| 659 | + try: |
| 660 | + update_prop_kind_dict_cache() |
| 661 | + except FileNotFoundError as e: |
| 662 | + logging.error(f"Failed to parse propertykind dict {e}") |
| 663 | + try: |
| 664 | + uuid, version = split_identifier(obj_or_identifier) |
| 665 | + if uuid in __CACHE_PROP_KIND_DICT__: |
| 666 | + return as_dor(__CACHE_PROP_KIND_DICT__[uuid]) |
| 667 | + else: |
| 668 | + set_attribute_from_path(dor, "uuid", uuid) |
| 669 | + set_attribute_from_path(dor, "ObjectVersion", version) |
| 670 | + except AttributeError: |
| 671 | + logging.error(f"Failed to parse identifier {obj_or_identifier}. DOR will be empty") |
649 | 672 | else: |
650 | | - cls = get_class_from_qualified_type(dor_qualified_type) |
651 | | - dor = cls() |
652 | | - if hasattr(dor, "qualified_type"): |
653 | | - set_attribute_from_path(dor, "qualified_type", get_qualified_type_from_class(obj_or_identifier)) |
654 | | - if hasattr(dor, "content_type"): |
655 | | - set_attribute_from_path(dor, "content_type", get_content_type_from_class(obj_or_identifier)) |
656 | | - |
657 | | - set_attribute_from_path(dor, "uuid", get_object_attribute(obj_or_identifier, "uuid")) |
658 | | - set_attribute_from_path(dor, "object_version", get_object_attribute(obj_or_identifier, "ObjectVersion")) |
659 | | - set_attribute_from_path(dor, "title", get_object_attribute(obj_or_identifier, "Citation.Title")) |
| 673 | + if is_dor(obj_or_identifier): |
| 674 | + # If it is a dor, we create a dor conversionif hasattr(dor, "qualified_type"): |
| 675 | + if hasattr(dor, "qualified_type"): |
| 676 | + if hasattr(obj_or_identifier, "qualified_type"): |
| 677 | + dor.qualified_type = get_object_attribute(obj_or_identifier, "qualified_type") |
| 678 | + elif hasattr(obj_or_identifier, "content_type"): |
| 679 | + dor.qualified_type = content_type_to_qualified_type( |
| 680 | + get_object_attribute(obj_or_identifier, "content_type") |
| 681 | + ) |
| 682 | + |
| 683 | + if hasattr(dor, "content_type"): |
| 684 | + if hasattr(obj_or_identifier, "qualified_type"): |
| 685 | + dor.content_type = qualified_type_to_content_type( |
| 686 | + get_object_attribute(obj_or_identifier, "qualified_type") |
| 687 | + ) |
| 688 | + elif hasattr(obj_or_identifier, "content_type"): |
| 689 | + dor.content_type = get_object_attribute(obj_or_identifier, "content_type") |
| 690 | + |
| 691 | + set_attribute_from_path(dor, "title", get_object_attribute(obj_or_identifier, "Title")) |
| 692 | + |
| 693 | + else: |
| 694 | + if hasattr(dor, "qualified_type"): |
| 695 | + set_attribute_from_path(dor, "qualified_type", get_qualified_type_from_class(obj_or_identifier)) |
| 696 | + if hasattr(dor, "content_type"): |
| 697 | + set_attribute_from_path(dor, "content_type", get_content_type_from_class(obj_or_identifier)) |
| 698 | + |
| 699 | + set_attribute_from_path(dor, "title", get_object_attribute(obj_or_identifier, "Citation.Title")) |
| 700 | + |
| 701 | + set_attribute_from_path(dor, "uuid", get_obj_uuid(obj_or_identifier)) |
| 702 | + |
| 703 | + if hasattr(dor, "object_version"): |
| 704 | + set_attribute_from_path(dor, "object_version", get_obj_version(obj_or_identifier)) |
| 705 | + if hasattr(dor, "version_string"): |
| 706 | + set_attribute_from_path(dor, "version_string", get_obj_version(obj_or_identifier)) |
660 | 707 |
|
661 | 708 | return dor |
662 | 709 |
|
|
0 commit comments