Skip to content

Commit 52a0b18

Browse files
bugfix for validation
1 parent 7a4b767 commit 52a0b18

3 files changed

Lines changed: 59 additions & 17 deletions

File tree

energyml-utils/src/energyml/utils/introspection.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,13 +1130,14 @@ def copy_attributes(
11301130
# Utility functions
11311131

11321132

1133-
def get_obj_uuid(obj: Any) -> str:
1133+
def get_obj_uuid(obj: Any) -> Optional[str]:
11341134
"""
11351135
Return the object uuid (attribute must match the following regex : "[Uu]u?id|UUID").
11361136
:param obj:
11371137
:return:
11381138
"""
1139-
return get_object_attribute_rgx(obj, "[Uu]u?id|UUID")
1139+
return getattr(obj, "uuid", None) or getattr(obj, "uid", None)
1140+
# return get_object_attribute_rgx(obj, "[Uu]u?id|UUID")
11401141

11411142

11421143
def get_obj_version(obj: Any) -> Optional[str]:
@@ -1146,20 +1147,21 @@ def get_obj_version(obj: Any) -> Optional[str]:
11461147
:return:
11471148
"""
11481149
try:
1149-
return get_object_attribute_no_verif(obj, "object_version")
1150+
return (
1151+
getattr(obj, "object_version", None)
1152+
or getattr(obj, "version_string", None)
1153+
or (getattr(obj, "citation", None) and getattr(obj.citation, "version_string", None))
1154+
)
11501155
except AttributeError:
1151-
# AttributeError is expected when attribute doesn't exist - try alternative
1152-
try:
1153-
return get_object_attribute_no_verif(obj, "version_string")
1154-
except Exception:
1155-
# Log with full call stack to see WHO called this function
1156-
logging.error(
1157-
f"Error getting version for {type(obj)} -- {obj}",
1158-
exc_info=True,
1159-
stack_info=True, # This shows the full call stack including caller
1160-
)
1161-
return None
1162-
# raise e
1156+
# Log with full call stack to see WHO called this function
1157+
# logging.error(
1158+
# f"Error getting version for {type(obj)} -- {obj}",
1159+
# exc_info=True,
1160+
# stack_info=True, # This shows the full call stack including caller
1161+
# )
1162+
pass
1163+
return None
1164+
# raise e
11631165

11641166

11651167
def get_obj_title(obj: Any) -> Optional[str]:
@@ -1169,7 +1171,8 @@ def get_obj_title(obj: Any) -> Optional[str]:
11691171
:return:
11701172
"""
11711173
try:
1172-
return get_object_attribute_advanced(obj, "citation.title")
1174+
return getattr(obj, "citation", None) and getattr(obj.citation, "title", None)
1175+
# return get_object_attribute_advanced(obj, "citation.title")
11731176
except AttributeError:
11741177
return None
11751178

@@ -1430,6 +1433,9 @@ def get_content_type_from_class(cls: Union[type, Any], print_dev_version=True, n
14301433

14311434

14321435
def get_object_type_for_file_path_from_class(cls) -> str:
1436+
"""
1437+
Return the object type to use in file path or content type. It is not always the same as the class name, for example for resqml201, the class "TriangulatedSetRepresentation" has to be written "obj_TriangulatedSetRepresentation" in file path and content type.
1438+
"""
14331439
if not isinstance(cls, type):
14341440
cls = type(cls)
14351441
classic_type = get_obj_type(cls)

energyml-utils/src/energyml/utils/manager.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ def get_class_pkg(cls):
182182
return match.group("pkg") # type: ignore
183183
except AttributeError as e:
184184
logging.error(f"Exception to get class package for '{cls}'")
185+
logging.error(
186+
f"Error getting package for {type(cls)} -- {cls}",
187+
exc_info=True,
188+
stack_info=True, # This shows the full call stack including caller
189+
)
185190
raise e
186191

187192

energyml-utils/src/energyml/utils/validation.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) 2023-2024 Geosiris.
22
# SPDX-License-Identifier: Apache-2.0
3+
import logging
34
import re
45
from dataclasses import dataclass, field, Field
56
from enum import Enum
@@ -203,6 +204,8 @@ def dor_validation_object(
203204
_msg=f"[DOR ERR] has wrong information. Unknown object with uuid '{dor_uuid}'",
204205
)
205206
)
207+
208+
object_version_list_failed = False
206209
if target_uuid is not None and target_identifier is None:
207210
accessible_version = [get_obj_version(ref_obj) for ref_obj in dict_obj_uuid[dor_uuid]]
208211
errs.append(
@@ -214,6 +217,7 @@ def dor_validation_object(
214217
f"Version must be one of {accessible_version}",
215218
)
216219
)
220+
object_version_list_failed = True
217221

218222
if target_prop is not None and target_uuid is None:
219223
errs.append(
@@ -226,6 +230,32 @@ def dor_validation_object(
226230
)
227231

228232
target = target_identifier or target_uuid or target_prop
233+
234+
# debug
235+
if isinstance(target, list):
236+
# logging.error(
237+
# f"Multiple objects found with uuid '{dor_uuid}' for DOR in object '{get_obj_identifier(obj)}' at path '{dor_path}'. This should not happen and can lead to wrong validation results.",
238+
# exc_info=True,
239+
# stack_info=True, # This shows the full call stack including caller
240+
# )
241+
# logging.error(
242+
# f'\t{target} => Object ct and qt {get_object_attribute_rgx(dor, "content_type")} : {get_object_attribute_rgx(dor, "qualified_type")}'
243+
# )
244+
if len(target) == 0:
245+
target = None
246+
else:
247+
if len(target) > 1:
248+
errs.append(
249+
ValidationObjectError(
250+
error_type=ErrorType.WARNING,
251+
target_obj=obj,
252+
attribute_dot_path=dor_path,
253+
_msg=f"[DOR ERR] Multiple objects found with uuid '{dor_uuid}' for DOR in object '{get_obj_identifier(obj)}' at path '{dor_path}'. This should not happen and can lead to wrong validation results.",
254+
)
255+
)
256+
target = target[0]
257+
258+
# ====
229259
if target is not None:
230260
# target = dict_obj_identifier[dor_target_id]
231261
target_title = get_object_attribute_rgx(target, "citation.title")
@@ -267,7 +297,8 @@ def dor_validation_object(
267297
)
268298
)
269299

270-
if target_version != dor_version:
300+
if not object_version_list_failed and target_version != dor_version:
301+
# checking object_version_list_failed to avoid multiple version errors
271302
errs.append(
272303
ValidationObjectError(
273304
error_type=ErrorType.WARNING,

0 commit comments

Comments
 (0)