From 223bbd0b2c2b1c7507cea95f7b3d9a85a8a881f4 Mon Sep 17 00:00:00 2001 From: Valentin Gauthier Date: Fri, 14 Jun 2024 16:32:15 +0200 Subject: [PATCH] logging --- energyml-utils/example/main.py | 24 +++++----- energyml-utils/src/energyml/utils/data/hdf.py | 7 +-- .../src/energyml/utils/data/helper.py | 26 +++++----- .../src/energyml/utils/data/mesh.py | 15 +++--- energyml-utils/src/energyml/utils/epc.py | 26 +++++----- .../src/energyml/utils/introspection.py | 47 ++++++++++--------- energyml-utils/src/energyml/utils/manager.py | 11 +++-- .../src/energyml/utils/serialization.py | 37 ++++++--------- .../src/energyml/utils/validation.py | 4 +- energyml-utils/src/energyml/utils/xml.py | 5 +- 10 files changed, 99 insertions(+), 103 deletions(-) diff --git a/energyml-utils/example/main.py b/energyml-utils/example/main.py index 52dd7e7..3d2d376 100644 --- a/energyml-utils/example/main.py +++ b/energyml-utils/example/main.py @@ -465,17 +465,17 @@ def test_obj_attribs(): if __name__ == "__main__": - # tests_0() - # tests_content_type() - # - # tests_epc() - # tests_dor() - # test_verif() - # test_ast() - # test_introspection() - # - # tests_hdf() - # test_local_depth_crs() + tests_0() + tests_content_type() + + tests_epc() + tests_dor() + test_verif() + test_ast() + test_introspection() + + tests_hdf() + test_local_depth_crs() test_wellbore_marker_frame_representation() - # test_obj_attribs() + test_obj_attribs() diff --git a/energyml-utils/src/energyml/utils/data/hdf.py b/energyml-utils/src/energyml/utils/data/hdf.py index 1bcb402..bea23f8 100644 --- a/energyml-utils/src/energyml/utils/data/hdf.py +++ b/energyml-utils/src/energyml/utils/data/hdf.py @@ -1,5 +1,6 @@ # Copyright (c) 2023-2024 Geosiris. # SPDX-License-Identifier: Apache-2.0 +import logging import os from dataclasses import dataclass from io import BytesIO @@ -167,13 +168,13 @@ def get_hdf5_path_from_external_path( # resqml 2.0.1 if hdf_proxy_lst is not None and len(hdf_proxy_lst) > 0: hdf_proxy = hdf_proxy_lst - # print("h5Proxy", hdf_proxy) + # logging.debug("h5Proxy", hdf_proxy) while isinstance(hdf_proxy, list): hdf_proxy = hdf_proxy[0] hdf_proxy_obj = epc.get_object_by_identifier( get_obj_identifier(hdf_proxy) ) - print("hdf_proxy_obj : ", hdf_proxy_obj, " hdf_proxy : ", hdf_proxy) + logging.debug("hdf_proxy_obj : ", hdf_proxy_obj, " hdf_proxy : ", hdf_proxy) if hdf_proxy_obj is not None: for rel in epc.additional_rels.get( get_obj_identifier(hdf_proxy_obj), [] @@ -214,7 +215,7 @@ def get_hdf5_path_from_external_path( if len(result) == 0: result = [epc.epc_file_path[:-4] + ".h5"] - print( + logging.debug( external_path_obj, result, "\n\t", diff --git a/energyml-utils/src/energyml/utils/data/helper.py b/energyml-utils/src/energyml/utils/data/helper.py index 8ba7754..e9c80ff 100644 --- a/energyml-utils/src/energyml/utils/data/helper.py +++ b/energyml-utils/src/energyml/utils/data/helper.py @@ -187,7 +187,7 @@ def read_external_array( try: return h5_reader.read_array(h5_io, path_in_external) except Exception as e: - print(traceback.format_exc()) + logging.error(traceback.format_exc()) pass return self.read_external_array( energyml_array=energyml_array, @@ -218,7 +218,7 @@ def read_external_array( f"Failed to read h5 file. Paths tried : {hdf5_paths}" ) - # print(f"\tpath_in_root : {path_in_root}") + # logging.debug(f"\tpath_in_root : {path_in_root}") # if path_in_root.lower().endswith("points") and len(result_array) > 0 and len(result_array[0]) == 3: # crs = None # try: @@ -230,7 +230,7 @@ def read_external_array( # ) # except ObjectNotFoundNotError as e: # logging.error("No CRS found, not able to check zIncreasingDownward") - # print(f"\tzincreasing_downward : {zincreasing_downward}") + # logging.debug(f"\tzincreasing_downward : {zincreasing_downward}") # zincreasing_downward = is_z_reversed(crs) # if zincreasing_downward: @@ -254,13 +254,13 @@ def get_crs_obj( :return: """ if workspace is None: - print("@get_crs_obj no Epc file given") + logging.error("@get_crs_obj no Epc file given") else: crs_list = search_attribute_matching_name( context_obj, r"\.*Crs", search_in_sub_obj=True, deep_search=False ) if crs_list is not None and len(crs_list) > 0: - # print(crs_list[0]) + # logging.debug(crs_list[0]) crs = workspace.get_object_by_identifier( get_obj_identifier(crs_list[0]) ) @@ -400,7 +400,7 @@ def read_array( workspace=workspace, ) else: - print( + logging.error( f"Type {array_type_name} is not supported: function read_{snake_case(array_type_name)} not found" ) raise Exception( @@ -422,12 +422,12 @@ def read_constant_array( :param workspace: :return: """ - # print(f"Reading constant array\n\t{energyml_array}") + # logging.debug(f"Reading constant array\n\t{energyml_array}") value = get_object_attribute_no_verif(energyml_array, "value") count = get_object_attribute_no_verif(energyml_array, "count") - # print(f"\tValue : {[value for i in range(0, count)]}") + # logging.debug(f"\tValue : {[value for i in range(0, count)]}") return [value for i in range(0, count)] @@ -562,7 +562,7 @@ def read_point3d_zvalue_array( sup_geom_array[i][2] = zvalues_array[i] except Exception as e: if count == 0: - print(e, f": {i} is out of bound of {len(zvalues_array)}") + logging.error(e, f": {i} is out of bound of {len(zvalues_array)}") count = count + 1 return sup_geom_array @@ -590,13 +590,13 @@ def read_point3d_from_representation_lattice_array( energyml_array, "supporting_representation" ) ) - # print(f"energyml_array : {energyml_array}\n\t{supporting_rep_identifier}") + # logging.debug(f"energyml_array : {energyml_array}\n\t{supporting_rep_identifier}") supporting_rep = workspace.get_object_by_identifier( supporting_rep_identifier ) # TODO chercher un pattern \.*patch\.*.[d]+ pour trouver le numero du patch dans le path_in_root puis lire le patch - # print(f"path_in_root {path_in_root}") + # logging.debug(f"path_in_root {path_in_root}") result = [] if "grid2d" in str(type(supporting_rep)).lower(): @@ -725,7 +725,7 @@ def read_point3d_lattice_array( crs_sa_count[0] == fastest_size - 1 and crs_fa_count[0] == slowest_size - 1 ): - print("reversing order") + logging.debug("reversing order") # if offset were given in the wrong order tmp_table = slowest_table slowest_table = fastest_table @@ -788,4 +788,4 @@ def read_point3d_lattice_array( # path_in_root: Optional[str] = None, # workspace: Optional[EnergymlWorkspace] = None # ): -# print(energyml_array) +# logging.debug(energyml_array) diff --git a/energyml-utils/src/energyml/utils/data/mesh.py b/energyml-utils/src/energyml/utils/data/mesh.py index f177372..702313f 100644 --- a/energyml-utils/src/energyml/utils/data/mesh.py +++ b/energyml-utils/src/energyml/utils/data/mesh.py @@ -1,6 +1,7 @@ # Copyright (c) 2023-2024 Geosiris. # SPDX-License-Identifier: Apache-2.0 import inspect +import logging import os import re import sys @@ -147,7 +148,7 @@ def read_mesh_object( workspace=workspace, ) else: - print( + logging.error( f"Type {array_type_name} is not supported: function read_{snake_case(array_type_name)} not found" ) raise Exception( @@ -392,7 +393,7 @@ def read_grid2d_representation( fa_count = fa_count[0] sa_count = sa_count[0] - # print(f"sa_count {sa_count} fa_count {fa_count}") + # logging.debug(f"sa_count {sa_count} fa_count {fa_count}") points_no_nan = [] @@ -423,13 +424,13 @@ def read_grid2d_representation( sa_count = sa_count + 1 fa_count = fa_count + 1 - # print(f"sa_count {sa_count} fa_count {fa_count} : {sa_count*fa_count} - {len(points)} ") + # logging.debug(f"sa_count {sa_count} fa_count {fa_count} : {sa_count*fa_count} - {len(points)} ") for sa in range(sa_count - 1): for fa in range(fa_count - 1): line = sa * fa_count # if sa+1 == int(sa_count / 2) and fa == int(fa_count / 2): - # print( + # logging.debug( # "\n\t", (line + fa), " : ", (line + fa) in indice_to_final_indice, # "\n\t", (line + fa + 1), " : ", (line + fa + 1) in indice_to_final_indice, # "\n\t", (line + fa_count + fa + 1), " : ", (line + fa_count + fa + 1) in indice_to_final_indice, @@ -458,7 +459,7 @@ def read_grid2d_representation( indice_to_final_indice[line + fa_count + fa], ] ) - # print(indices) + # logging.debug(indices) meshes.append( SurfaceMesh( identifier=f"{get_obj_identifier(energyml_object)}_patch{patch_idx}", @@ -713,7 +714,7 @@ def export_multiple_data( f".{file_format.value}" ) file_path = f"{output_folder_path}/{file_name}" - print(f"Exporting : {file_path}") + logging.debug(f"Exporting : {file_path}") mesh_list = read_mesh_object( energyml_object=energyml_obj, workspace=EPCWorkspace(epc=epc), @@ -731,4 +732,4 @@ def export_multiple_data( out=f, ) else: - print(f"Code is not written for format {file_format}") + logging.error(f"Code is not written for format {file_format}") diff --git a/energyml-utils/src/energyml/utils/epc.py b/energyml-utils/src/energyml/utils/epc.py index 711a9df..bc7b3e4 100644 --- a/energyml-utils/src/energyml/utils/epc.py +++ b/energyml-utils/src/energyml/utils/epc.py @@ -5,6 +5,7 @@ """ import datetime +import logging import re import traceback import zipfile @@ -429,7 +430,7 @@ def read_stream(cls, epc_file_io: BytesIO): # returns an Epc instance _read_files.append(content_type_file_name) if content_type_info is None: - print(f"No {content_type_file_name} file found") + logging.error(f"No {content_type_file_name} file found") else: content_type_obj: Types = read_energyml_xml_bytes( epc_file.read(content_type_file_name) @@ -438,7 +439,7 @@ def read_stream(cls, epc_file_io: BytesIO): # returns an Epc instance for ov in content_type_obj.override: ov_ct = ov.content_type ov_path = ov.part_name - # print(ov_ct) + # logging.debug(ov_ct) while ov_path.startswith("/") or ov_path.startswith( "\\" ): @@ -455,16 +456,15 @@ def read_stream(cls, epc_file_io: BytesIO): # returns an Epc instance path_to_obj[ov_path] = ov_obj obj_list.append(ov_obj) except Exception as e: - print(traceback.format_exc()) - print( + logging.error(traceback.format_exc()) + logging.error( f"Epc.@read_stream failed to parse file {ov_path} for content-type: {ov_ct} => {get_class_from_content_type(ov_ct)}\n\n", get_class_from_content_type(ov_ct), ) try: - print(epc_file.read(ov_path)) + logging.debug(epc_file.read(ov_path)) except: pass - print(e) # raise e elif ( get_class_from_content_type(ov_ct) @@ -490,13 +490,13 @@ def read_stream(cls, epc_file_io: BytesIO): # returns an Epc instance ) ) except IOError as e: - print(traceback.format_exc()) + logging.error(traceback.format_exc()) elif ( f_info.filename != "_rels/.rels" ): # CoreProperties rels file # RELS FILES READING START - # print(f"reading rels {f_info.filename}") + # logging.debug(f"reading rels {f_info.filename}") ( rels_folder, rels_file_name, @@ -528,7 +528,7 @@ def read_stream(cls, epc_file_io: BytesIO): # returns an Epc instance ) ) for rel in rels_file.relationship: - # print(f"\t\t{rel.type_value}") + # logging.debug(f"\t\t{rel.type_value}") if ( rel.type_value != EPCRelsRelationshipType.DESTINATION_OBJECT.get_type() @@ -548,15 +548,15 @@ def read_stream(cls, epc_file_io: BytesIO): # returns an Epc instance additional_rels_key ].append(rel) except AttributeError: - print(traceback.format_exc()) + logging.error(traceback.format_exc()) pass # 'CoreProperties' object has no attribute 'object_version' except Exception as e: - print( + logging.error( f"Error with obj path {obj_path} {path_to_obj[obj_path]}" ) raise e else: - print( + logging.error( f"xml file '{f_info.filename}' is not associate to any readable object " f"(or the object type is not supported because" f" of a lack of a dependency module) " @@ -569,7 +569,7 @@ def read_stream(cls, epc_file_io: BytesIO): # returns an Epc instance additional_rels=additional_rels, ) except zipfile.BadZipFile as error: - print(error) + logging.error(error) return None diff --git a/energyml-utils/src/energyml/utils/introspection.py b/energyml-utils/src/energyml/utils/introspection.py index a0b8e14..eba87e2 100644 --- a/energyml-utils/src/energyml/utils/introspection.py +++ b/energyml-utils/src/energyml/utils/introspection.py @@ -1,6 +1,7 @@ # Copyright (c) 2023-2024 Geosiris. # SPDX-License-Identifier: Apache-2.0 import inspect +import logging import random import re import sys @@ -94,7 +95,7 @@ def find_class_in_module(module_name, class_name): return cls except Exception as e: pass - print(f"Not Found : {module_name}; {class_name}") + logging.error(f"Not Found : {module_name}; {class_name}") return None @@ -131,7 +132,7 @@ def get_class_from_name(class_name_and_module: str) -> Optional[type]: return find_class_in_module(module_name, last_ns_part) except AttributeError as e: # if "2d" in last_ns_part: - # print("replace 2D") + # logging.debug("replace 2D") # return get_class_from_name( # class_name_and_module.replace("2d", "2D") # ) @@ -145,7 +146,7 @@ def get_class_from_name(class_name_and_module: str) -> Optional[type]: # ) # elif "2D" in last_ns_part or "3D" in last_ns_part: # idx = -1 - # print(class_name_and_module) + # logging.debug(class_name_and_module) # try: # idx = class_name_and_module.rindex("2D") + 2 # except: @@ -156,13 +157,13 @@ def get_class_from_name(class_name_and_module: str) -> Optional[type]: # + class_name_and_module[idx].lower() # + class_name_and_module[idx + 1:] # ) - # print(f"reformated {reformated}") + # logging.debug(f"reformated {reformated}") # return get_class_from_name(reformated) # else: - # print(e) - print(e) + # logging.debug(e) + logging.error(e) except KeyError: - print(f"[ERR] module not found : '{module_name}'") + logging.error(f"[ERR] module not found : '{module_name}'") return None @@ -174,13 +175,13 @@ def get_energyml_module_dev_version(pkg: str, current_version: str): current_version = current_version.replace("-", "_").replace(".", "_") res = [] if pkg in accessible_modules: - # print("\t", pkg, current_version) + # logging.debug("\t", pkg, current_version) for am_pkg_version in accessible_modules[pkg]: if ( am_pkg_version != current_version and am_pkg_version.startswith(current_version) ): - # print("\t\t", am_pkg_version) + # logging.debug("\t\t", am_pkg_version) res.append(get_module_name(pkg, am_pkg_version)) return res @@ -199,8 +200,8 @@ def get_energyml_class_in_related_dev_pkg(cls: type): try: res.append(get_class_from_name(f"{dev_module_name}.{class_name}")) except Exception as e: - print(f"FAILED {dev_module_name}.{class_name}") - print(e) + logging.error(f"FAILED {dev_module_name}.{class_name}") + logging.error(e) pass return res @@ -229,7 +230,7 @@ def get_class_from_content_type(content_type: str) -> Optional[type]: domain = ct.group("domain") if domain is None: - # print(f"\tdomain {domain} xmlDomain {ct.group('xmlDomain')} ") + # logging.debug(f"\tdomain {domain} xmlDomain {ct.group('xmlDomain')} ") domain = "opc" if domain == "opc": xml_domain = ct.group("xmlDomain") @@ -242,7 +243,7 @@ def get_class_from_content_type(content_type: str) -> Optional[type]: # xml_domain = xml_domain[1:] # opc_type = pascal_case(xml_domain).replace("-", "") - # print("\tenergyml.opc.opc." + opc_type) + # logging.debug("\tenergyml.opc.opc." + opc_type) return get_class_from_name("energyml.opc.opc." + opc_type) else: domain = ct.group("domain") @@ -256,7 +257,7 @@ def get_class_from_content_type(content_type: str) -> Optional[type]: if domain.lower() == "resqml" and version_num.startswith("2_0"): version_num = "2_0_1" - # print(get_module_name(domain, version_num) + # logging.debug(get_module_name(domain, version_num) # + "." # + obj_type) return get_class_from_name( @@ -284,7 +285,7 @@ def import_related_module(energyml_module_name: str) -> None: import_module(m) except Exception as e: pass - # print(e) + # logging.error(e) def get_class_fields(cls: Union[type, Any]) -> Dict[str, Field]: @@ -333,7 +334,7 @@ def get_matching_class_attribute_name( # search regex after to avoid shadowing perfect match pattern = re.compile(attribute_name, flags=re_flags) for name, cf in class_fields.items(): - # print(f"\t->{name} : {attribute_name} {pattern.match(name)} {('name' in cf.metadata and pattern.match(cf.metadata['name']))}") + # logging.error(f"\t->{name} : {attribute_name} {pattern.match(name)} {('name' in cf.metadata and pattern.match(cf.metadata['name']))}") if pattern.match(name) or ( "name" in cf.metadata and pattern.match(cf.metadata["name"]) ): @@ -781,7 +782,7 @@ def get_obj_version(obj: Any) -> str: try: return get_object_attribute_no_verif(obj, "version_string") except Exception: - print(f"Error with {type(obj)}") + logging.error(f"Error with {type(obj)}") # raise e @@ -814,7 +815,7 @@ def get_obj_pkg_pkgv_type_uuid_version( if ct is not None: ct_match = parse_content_type(ct) - print("ct : ", ct_match) + logging.debug("ct : ", ct_match) if ct_match is not None: pkg = ct_match.group("domain") pkg_v = ct_match.group("domainVersion") @@ -823,7 +824,7 @@ def get_obj_pkg_pkgv_type_uuid_version( try: qt = get_object_attribute_no_verif(obj, "qualified_type") qt_match = parse_qualified_type(qt) - print("qt : ", qt, obj.__dict__, qt_match) + logging.debug("qt : ", qt, obj.__dict__, qt_match) if qt_match is not None: pkg = qt_match.group("domain") pkg_v = qt_match.group("domainVersion") @@ -927,7 +928,7 @@ def get_content_type_from_class( + get_object_type_for_file_path_from_class(cls) ) - print(f"@get_content_type_from_class not supported type : {cls}") + logging.error(f"@get_content_type_from_class not supported type : {cls}") return None @@ -1134,7 +1135,7 @@ def _random_value_from_class( ] args = {} for k, v in get_class_fields(chosen_type).items(): - # print(f"get_class_fields {k} : {v}") + # logging.debug(f"get_class_fields {k} : {v}") args[k] = _random_value_from_class( cls=get_class_from_simple_name( simple_name=v.type, @@ -1150,10 +1151,10 @@ def _random_value_from_class( return chosen_type(**args) except Exception as e: - print(f"exception on attribute '{attribute_name}' for class {cls} :") + logging.error(f"exception on attribute '{attribute_name}' for class {cls} :") raise e - print( + logging.error( f"@_random_value_from_class Not supported object type generation {cls}" ) return None diff --git a/energyml-utils/src/energyml/utils/manager.py b/energyml-utils/src/energyml/utils/manager.py index da8c398..eb0926a 100644 --- a/energyml-utils/src/energyml/utils/manager.py +++ b/energyml-utils/src/energyml/utils/manager.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 import importlib import inspect +import logging import pkgutil from typing import Union, Any, Dict @@ -32,10 +33,10 @@ def dict_energyml_modules() -> Dict: modules = {} energyml_module = importlib.import_module("energyml") - # print("> energyml") + # logging.debug("> energyml") for mod in pkgutil.iter_modules(energyml_module.__path__): - # print(f"{mod.name}") + # logging.debug(f"{mod.name}") if mod.name in ENERGYML_MODULES_NAMES: energyml_sub_module = importlib.import_module( f"energyml.{mod.name}" @@ -54,7 +55,7 @@ def list_energyml_modules(): energyml_module = importlib.import_module("energyml") modules = [] for obj in pkgutil.iter_modules(energyml_module.__path__): - # print(f"{obj.name}") + # logging.debug(f"{obj.name}") if obj.name in ENERGYML_MODULES_NAMES: modules.append(obj.name) return modules @@ -76,7 +77,7 @@ def list_classes(module_path: str) -> List: class_list.append(obj) return class_list except ModuleNotFoundError: - print(f"Err : module {module_path} not found") + logging.error(f"Err : module {module_path} not found") return [] @@ -158,7 +159,7 @@ def get_class_pkg(cls): m = p.search(cls.__module__) return m.group("pkg") except AttributeError as e: - print(f"Exception to get class package for '{cls}'") + logging.error(f"Exception to get class package for '{cls}'") raise e diff --git a/energyml-utils/src/energyml/utils/serialization.py b/energyml-utils/src/energyml/utils/serialization.py index 01b64de..373667e 100644 --- a/energyml-utils/src/energyml/utils/serialization.py +++ b/energyml-utils/src/energyml/utils/serialization.py @@ -62,7 +62,7 @@ def _read_energyml_xml_bytes_as_class(file: bytes, obj_class: Type, fail_on_unkn logging.error(f"Failed to parse file {file} as class {obj_class}") if len(e.args) > 0: if "unknown property" in e.args[0].lower(): - print(e) + logging.error(e) logging.error( "A property has not been found, please check if your 'xsi::type' values contains " "the xml namespace (e.g. 'xsi:type=\"eml:VerticalCrsEpsgCode\"')." @@ -86,19 +86,19 @@ def read_energyml_xml_bytes( except xsdata.exceptions.ParserError as e: if len(e.args) > 0: if "unknown property" in e.args[0].lower(): - print(f"Trying reading without fail on unknown attribute/property") + logging.error(f"Trying reading without fail on unknown attribute/property") try: return _read_energyml_xml_bytes_as_class(file, obj_type, False, False) except Exception as e: - print(traceback.print_stack()) + logging.error(traceback.print_stack()) pass # Otherwise for obj_type_dev in get_energyml_class_in_related_dev_pkg(obj_type): try: - print(f"Trying with class : {obj_type_dev}") + logging.debug(f"Trying with class : {obj_type_dev}") obj = _read_energyml_xml_bytes_as_class(file, obj_type_dev) - print(f" ==> succeed read with {obj_type_dev}") + logging.debug(f" ==> succeed read with {obj_type_dev}") return obj except Exception: pass @@ -147,7 +147,7 @@ def _read_energyml_json_bytes_as_class( try: return parser.from_bytes(file, obj_class) except ParserError as e: - print(f"Failed to parse file {file} as class {obj_class}") + logging.error(f"Failed to parse file {file} as class {obj_class}") raise e elif json_version == JSON_VERSION.OSDU_OFFICIAL: return read_json_dict(json.loads(file)) @@ -170,18 +170,18 @@ def read_energyml_json_bytes( try: return _read_energyml_json_bytes_as_class(file, obj_type) except xsdata.exceptions.ParserError as e: - print( + logging.error( f"Failed to read file with type {obj_type}: {get_energyml_class_in_related_dev_pkg(obj_type)}" ) for obj_type_dev in get_energyml_class_in_related_dev_pkg( obj_type ): try: - print(f"Trying with class : {obj_type_dev}") + logging.debug(f"Trying with class : {obj_type_dev}") obj = _read_energyml_json_bytes_as_class( file, obj_type_dev ) - print(f" ==> succeed read with {obj_type_dev}") + logging.debug(f" ==> succeed read with {obj_type_dev}") return obj except Exception: pass @@ -312,14 +312,13 @@ def _read_json_dict(obj_json: Any, sub_obj: List) -> Any: elif not att.startswith("$"): if att == "_": att = "value" - # print(f"setting : {att} {get_matching_class_attribute_name(obj, att)}") setattr( obj, get_matching_class_attribute_name(obj, att), _read_json_dict(val, sub_obj), ) except Exception as e: - print( + logging.error( f"Err on {att}", search_attribute_matching_name( obj=obj, @@ -334,7 +333,7 @@ def _read_json_dict(obj_json: Any, sub_obj: List) -> Any: elif isinstance(obj_json, list): return [_read_json_dict(o, sub_obj) for o in obj_json] elif is_primitive(obj_json): - # print(f"PRIM : {obj_json}") + # logging.debug(f"PRIM : {obj_json}") return obj_json else: raise NotParsableType(type(obj_json) + " " + obj_json) @@ -374,9 +373,6 @@ def _fill_dict_with_attribs( _parent: Optional[Any] = None, ) -> None: - # if "WellboreMarker" in str(type(obj)): - # print("===================================\n\t", list(map(lambda f: f[0], get_class_fields(obj).items()))) - for att_name, field in get_class_fields(obj).items(): field_name = ( field.metadata["name"] @@ -393,10 +389,8 @@ def _fill_dict_with_attribs( ) value = getattr(obj, att_name) - # if "WellboreMarker" in str(type(obj)): - # print(f"\t> {field_name}, {att_name} : {value}") if "Any_element" in str(field_name): - print(f"\t> {field_name}, {att_name} : {value}, {type(obj)}") + logging.debug(f"\t> {field_name}, {att_name} : {value}, {type(obj)}") if (value is not None or mandatory) and ( not isinstance(value, list) or len(value) > 0 @@ -417,7 +411,7 @@ def _fill_dict_with_attribs( ref_value, f_identifier_to_obj ) else: - print(f"NotFound : {ref_identifier}") + logging.debug(f"NotFound : {ref_identifier}") def _to_json_dict_fn( @@ -434,8 +428,6 @@ def _to_json_dict_fn( in the original object given by the user :return: Any """ - if "WellboreMarker" in str(type(obj)): - print(obj) if obj is None: return None elif is_enum(obj): @@ -449,7 +441,6 @@ def _to_json_dict_fn( elif isinstance(obj, xsdata.models.datatype.XmlDateTime): return str(obj) elif isinstance(obj, DerivedElement): - # res = {"$type": "DisabledMarkers"} res = {"$type": get_qualified_type_from_class(obj.value)} # _fill_dict_with_attribs(res, obj.value, f_identifier_to_obj, _parent) return res @@ -461,7 +452,7 @@ def _to_json_dict_fn( _fill_dict_with_attribs(res, obj, f_identifier_to_obj, _parent) return res except Exception as e: - print(f"Except on qt: {obj} - {type(obj)}") + logging.error(f"Except on qt: {obj} - {type(obj)}") raise e diff --git a/energyml-utils/src/energyml/utils/validation.py b/energyml-utils/src/energyml/utils/validation.py index 50fcbaf..87cdaab 100644 --- a/energyml-utils/src/energyml/utils/validation.py +++ b/energyml-utils/src/energyml/utils/validation.py @@ -218,9 +218,9 @@ def _patterns_validation( val, root_obj, f"{current_attribute_dot_path}.{k}" ) else: - # print(get_class_fields(obj)) + # logging.debug(get_class_fields(obj)) for att_name, att_field in get_class_fields(obj).items(): - # print(f"att_name : {att_field.metadata}") + # logging.debug(f"att_name : {att_field.metadata}") error_list = error_list + validate_attribute( get_object_attribute(obj, att_name, False), root_obj, diff --git a/energyml-utils/src/energyml/utils/xml.py b/energyml-utils/src/energyml/utils/xml.py index 85405f3..e6f7c46 100644 --- a/energyml-utils/src/energyml/utils/xml.py +++ b/energyml-utils/src/energyml/utils/xml.py @@ -1,5 +1,6 @@ # Copyright (c) 2023-2024 Geosiris. # SPDX-License-Identifier: Apache-2.0 +import logging from io import BytesIO from typing import Optional, Any, Union @@ -29,7 +30,7 @@ def get_class_name_from_xml(tree: ETREE.Element) -> str: root_namespace = get_root_namespace(tree) pkg = get_pkg_from_namespace(root_namespace) if pkg is None: - print(f"No pkg found for elt {tree}") + logging.error(f"No pkg found for elt {tree}") else: if pkg == "opc": return "energyml.opc.opc." + get_root_type(tree) @@ -39,7 +40,7 @@ def get_class_name_from_xml(tree: ETREE.Element) -> str: .replace(".", "_") .replace("-", "_") ) - # print(schema_version) + # logging.debug(schema_version) if pkg == "resqml" and schema_version == "2_0": schema_version = "2_0_1"