Skip to content

Commit ca0065f

Browse files
bugfix for epcStream id use
1 parent 31c873e commit ca0065f

6 files changed

Lines changed: 61 additions & 39 deletions

File tree

energyml-utils/example/attic/arrays_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ def read_pointset(
365365
# meshes = []
366366
meshes = read_mesh_object(energyml_object=pointset, workspace=epc)
367367

368+
print(epc.get_obj_rels(pointset))
369+
368370
# logging.debug("=" * 40)
369371
# print_tuple_list(search_attribute_matching_name_with_path(pointset, r"NodePatch.[\d]+.Geometry.Points"))
370372
# logging.debug("=" * 40)

energyml-utils/src/energyml/utils/data/mesh.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ def read_point_representation(
275275
) + search_attribute_matching_name_with_path( # resqml 2.0.1
276276
energyml_object, r"NodePatchGeometry.[\d]+.Points"
277277
)
278-
logging.debug(f"Found {len(patches_geom)} patches for point representation")
279-
logging.debug(f"\t=> {patches_geom}")
278+
# logging.debug(f"Found {len(patches_geom)} patches for point representation")
279+
# logging.debug(f"\t=> {patches_geom}")
280280

281281
for points_path_in_obj, points_obj in patches_geom:
282282
points = read_array(

energyml-utils/src/energyml/utils/epc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
get_file_folder,
6868
make_path_relative_to_other_file,
6969
make_path_relative_to_filepath_list,
70+
as_identifier,
7071
)
7172

7273

@@ -150,7 +151,8 @@ def remove(self, obj: Any) -> None:
150151
def get_by_identifier(self, identifier: Union[str, Uri]) -> Optional[Any]:
151152
"""Get object by identifier (O(1) lookup)."""
152153
# Try identifier lookup first
153-
obj = self._by_identifier.get(str(identifier))
154+
# obj = self._by_identifier.get(str(identifier))
155+
obj = self._by_identifier.get(as_identifier(identifier))
154156
if obj is not None:
155157
return obj
156158

energyml-utils/src/energyml/utils/epc_stream.py

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,10 @@
5858
)
5959
from energyml.utils.uri import Uri, create_uri_from_content_type_or_qualified_type
6060
from energyml.utils.constants import (
61-
CORE_PROPERTIES_FOLDER_NAME,
6261
EPCRelsRelationshipType,
6362
EpcExportVersion,
6463
MimeType,
6564
OptimizedRegex,
66-
file_extension_to_mime_type,
6765
date_to_datetime,
6866
)
6967
from energyml.utils.epc_utils import (
@@ -72,6 +70,7 @@
7270
gen_core_props_path,
7371
make_path_relative_to_filepath_list,
7472
make_path_relative_to_other_file,
73+
as_identifier,
7574
)
7675

7776
from energyml.utils.introspection import (
@@ -1623,7 +1622,7 @@ def put_object(self, obj: Any, dataspace: Optional[str] = None) -> Optional[str]
16231622
identifier = uri.as_identifier()
16241623
existing_metadata = self._metadata_mgr.get_metadata(identifier)
16251624
file_path = gen_energyml_object_path(obj, self._metadata_mgr._export_version)
1626-
is_update = existing_metadata is not None
1625+
# is_update = existing_metadata is not None
16271626

16281627
# Write object data and metadata to EPC
16291628
try:
@@ -2082,38 +2081,23 @@ def _update_access_order(self, identifier: str) -> None:
20822081
def _id_from_uri_or_identifier(
20832082
self, identifier: Union[str, Uri, Any], get_first_if_simple_uuid: bool = True
20842083
) -> Optional[str]:
2085-
if identifier is None:
2086-
return None
2087-
elif isinstance(identifier, str):
2088-
if OptimizedRegex.UUID.fullmatch(identifier) is not None:
2089-
if not get_first_if_simple_uuid:
2090-
logging.warning(
2091-
f"Identifier {identifier} is a simple UUID, but get_first_if_simple_uuid is False, cannot resolve to full identifier"
2092-
)
2093-
return None
2094-
# If it's a simple UUID, we need to find the corresponding identifier from metadata
2095-
t_metadata_identifiers = self._metadata_mgr.get_uuid_identifiers(identifier)
2096-
if t_metadata_identifiers is not None and len(t_metadata_identifiers) > 0:
2097-
return t_metadata_identifiers[
2098-
0
2099-
] # If multiple metadata entries for the same UUID, we take the first one (this should not happen in a well-formed EPC file)
2100-
else:
2101-
logging.warning(f"No metadata found for UUID {identifier}, cannot get relationships")
2102-
return None
2084+
try:
2085+
return as_identifier(identifier)
2086+
except Exception:
2087+
if not get_first_if_simple_uuid:
2088+
logging.warning(
2089+
f"Identifier {identifier} is a simple UUID, but get_first_if_simple_uuid is False, cannot resolve to full identifier"
2090+
)
2091+
return None
2092+
# If it's a simple UUID, we need to find the corresponding identifier from metadata
2093+
t_metadata_identifiers = self._metadata_mgr.get_uuid_identifiers(identifier)
2094+
if t_metadata_identifiers is not None and len(t_metadata_identifiers) > 0:
2095+
return t_metadata_identifiers[
2096+
0
2097+
] # If multiple metadata entries for the same UUID, we take the first one (this should not happen in a well-formed EPC file)
21032098
else:
2104-
return identifier
2105-
elif isinstance(identifier, Uri):
2106-
return identifier.as_identifier()
2107-
elif isinstance(identifier, ResourceMetadata):
2108-
return self._id_from_uri_or_identifier(identifier.identifier)
2109-
elif isinstance(identifier, EpcObjectMetadata):
2110-
return self._id_from_uri_or_identifier(identifier.uri)
2111-
else:
2112-
# Try to get URI from object
2113-
obj_uri = get_obj_uri(obj=identifier, dataspace=None)
2114-
if obj_uri is not None:
2115-
return obj_uri.as_identifier()
2116-
return str(identifier)
2099+
logging.warning(f"No metadata found for UUID {identifier}, cannot get relationships")
2100+
return None
21172101

21182102
def _rebuild_all_rels_sequential(self, clean_first: bool = True) -> Dict[str, int]:
21192103
"""

energyml-utils/src/energyml/utils/epc_utils.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import logging
88
import os
99
import os
10-
import re
1110
from typing import Optional, Set, Tuple, Union, Any, List, Dict, Callable
1211
from pathlib import Path
1312
import zipfile
@@ -25,6 +24,8 @@
2524
Override,
2625
)
2726

27+
from energyml.utils.exception import NotEnoughInformationError
28+
2829
from energyml.utils.constants import (
2930
CORE_PROPERTIES_FOLDER_NAME,
3031
EPCRelsRelationshipType,
@@ -65,7 +66,7 @@
6566
from energyml.utils.manager import get_class_pkg
6667
from energyml.utils.serialization import read_energyml_xml_str, serialize_xml, read_energyml_json_str
6768
from energyml.utils.uri import Uri, parse_uri
68-
69+
from energyml.utils.storage_interface import ResourceMetadata
6970

7071
# ____ ___ ________ __
7172
# / __ \/ |/_ __/ / / /
@@ -285,6 +286,32 @@ def make_path_relative_to_filepath_list(paths: List[str], ref_path: Optional[Uni
285286
# /_/ /_/___//____/\____/
286287

287288

289+
def as_identifier(identifier: Union[str, Uri, Any]) -> Optional[str]:
290+
if identifier is None:
291+
return None
292+
elif isinstance(identifier, str):
293+
if identifier.startswith("eml:///"):
294+
return as_identifier(parse_uri(identifier))
295+
if OptimizedRegex.UUID.fullmatch(identifier) is not None:
296+
raise NotEnoughInformationError(
297+
"Simple uuid is not enough to be used as an identifier, please provide a full URI or an object with a valid URI or identifier that contains the version : 'UUID.VERSION' even if VERSION can be an empty string"
298+
)
299+
else:
300+
return identifier
301+
elif isinstance(identifier, Uri):
302+
return identifier.as_identifier()
303+
elif isinstance(identifier, ResourceMetadata):
304+
return as_identifier(identifier.identifier)
305+
elif hasattr(identifier, "uri"): # EpcObjectMetadata
306+
return as_identifier(identifier.uri)
307+
else:
308+
# Try to get URI from object
309+
obj_uri = get_obj_uri(obj=identifier, dataspace=None)
310+
if obj_uri is not None:
311+
return obj_uri.as_identifier()
312+
return str(identifier)
313+
314+
288315
def create_external_relationship(path: str, _id: Optional[str] = None) -> Relationship:
289316
return Relationship(
290317
target=path,

energyml-utils/src/energyml/utils/exception.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ def __init__(self, msg):
4848
super().__init__(msg)
4949

5050

51+
class NotEnoughInformationError(Exception):
52+
"""Exception for not enough information to perform an operation"""
53+
54+
def __init__(self, msg):
55+
super().__init__(msg)
56+
57+
5158
# EPC Validation Exceptions
5259

5360

0 commit comments

Comments
 (0)