Skip to content

Commit f54bfab

Browse files
bugfix for xml serialization. namespace was missing for obj_ prefixed classes
1 parent 4223c32 commit f54bfab

3 files changed

Lines changed: 17 additions & 98 deletions

File tree

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

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,11 @@ def as_obj_prefixed_class_if_possible(o: Any) -> Any:
11971197
# print(bc)
11981198
if bc.__name__.lower() == f"obj{get_obj_type(o_type).lower()}":
11991199
try:
1200+
try:
1201+
if bc.Meta is not None:
1202+
bc.Meta.namespace = o_type.Meta.namespace # keep the same namespace
1203+
except Exception:
1204+
logging.error(f"Failed to set namespace for {bc}")
12001205
return bc(**o.__dict__)
12011206
except Exception as e:
12021207
logging.error(f"Failed to convert {o} to {bc}")
@@ -1554,81 +1559,3 @@ def _random_value_from_class(
15541559

15551560
logging.error(f"@_random_value_from_class Not supported object type generation {cls}")
15561561
return None
1557-
1558-
1559-
if __name__ == "__main__":
1560-
1561-
from energyml.eml.v2_3.commonv2 import *
1562-
from energyml.eml.v2_0.commonv2 import Citation as Cit201
1563-
from energyml.resqml.v2_0_1.resqmlv2 import TriangulatedSetRepresentation as Tr20, ObjTriangulatedSetRepresentation
1564-
from energyml.resqml.v2_2.resqmlv2 import (
1565-
TriangulatedSetRepresentation,
1566-
FaultInterpretation,
1567-
)
1568-
from .serialization import *
1569-
1570-
fi_cit = Citation(
1571-
title="An interpretation",
1572-
originator="Valentin",
1573-
creation=epoch_to_date(epoch()),
1574-
editor="test",
1575-
format="Geosiris",
1576-
last_update=epoch_to_date(epoch()),
1577-
)
1578-
1579-
fi = FaultInterpretation(
1580-
citation=fi_cit,
1581-
uuid=gen_uuid(),
1582-
object_version="0",
1583-
)
1584-
1585-
tr_cit = Citation(
1586-
title="--",
1587-
# title="test title",
1588-
originator="Valentin",
1589-
creation=epoch_to_date(epoch()),
1590-
editor="test",
1591-
format="Geosiris",
1592-
last_update=epoch_to_date(epoch()),
1593-
)
1594-
1595-
tr_cit201 = Cit201(
1596-
title="--",
1597-
# title="test title",
1598-
originator="Valentin",
1599-
# creation=str(epoch_to_date(epoch()))
1600-
editor="test",
1601-
format="Geosiris",
1602-
# last_update=str(epoch_to_date(epoch())),
1603-
)
1604-
dor = DataObjectReference(
1605-
uuid=fi.uuid,
1606-
title="a DOR title",
1607-
object_version="0",
1608-
qualified_type="a wrong qualified type",
1609-
)
1610-
tr = TriangulatedSetRepresentation(
1611-
citation=tr_cit,
1612-
uuid=gen_uuid(),
1613-
represented_object=dor,
1614-
)
1615-
1616-
tr201 = Tr20(
1617-
citation=tr_cit201,
1618-
uuid=gen_uuid(),
1619-
)
1620-
tr201_bis = ObjTriangulatedSetRepresentation(
1621-
citation=tr_cit201,
1622-
uuid=gen_uuid(),
1623-
)
1624-
# print(get_obj_uri(tr201, "coucou"))
1625-
1626-
print(get_obj_usable_class(tr))
1627-
print(get_obj_usable_class(tr201))
1628-
1629-
print(serialize_xml(tr201_bis))
1630-
print(serialize_xml(tr201))
1631-
print(serialize_json(tr201))
1632-
print(serialize_xml(as_obj_prefixed_class_if_possible(tr201)))
1633-
print("--> ", serialize_json(tr))
1634-
# print(serialize_xml((get_usable_class(tr201))(tr201)))

energyml-utils/src/energyml/utils/serialization.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ def read_energyml_json_file(
254254
# /____/\___/_/ /_/\__,_/_/_/ /___/\__,_/\__/_/\____/_/ /_/
255255

256256

257-
def serialize_xml(obj) -> str:
258-
obj = as_obj_prefixed_class_if_possible(obj)
257+
def serialize_xml(obj, check_obj_prefixed_classes: bool = True) -> str:
258+
obj = as_obj_prefixed_class_if_possible(obj) if check_obj_prefixed_classes else obj
259259
context = XmlContext(
260260
# element_name_generator=text.camel_case,
261261
# attribute_name_generator=text.kebab_case
@@ -265,8 +265,10 @@ def serialize_xml(obj) -> str:
265265
return serializer.render(obj, ns_map=ENERGYML_NAMESPACES)
266266

267267

268-
def serialize_json(obj, json_version: JSON_VERSION = JSON_VERSION.OSDU_OFFICIAL) -> str:
269-
obj = as_obj_prefixed_class_if_possible(obj)
268+
def serialize_json(
269+
obj, json_version: JSON_VERSION = JSON_VERSION.OSDU_OFFICIAL, check_obj_prefixed_classes: bool = True
270+
) -> str:
271+
obj = as_obj_prefixed_class_if_possible(obj) if check_obj_prefixed_classes else obj
270272
if json_version == JSON_VERSION.XSDATA:
271273
context = XmlContext(
272274
# element_name_generator=text.camel_case,

energyml-utils/src/energyml/utils/xml.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
def get_pkg_from_namespace(namespace: str) -> Optional[str]:
12-
for (k, v) in ENERGYML_NAMESPACES_PACKAGE.items():
12+
for k, v in ENERGYML_NAMESPACES_PACKAGE.items():
1313
if namespace in v:
1414
return k
1515
return None
@@ -22,7 +22,7 @@ def is_energyml_content_type(content_type: str) -> bool:
2222

2323

2424
def get_root_namespace(tree: ETREE.Element) -> str:
25-
return tree.nsmap[tree.prefix]
25+
return tree.nsmap.get(tree.prefix, tree.nsmap.get(None, ""))
2626

2727

2828
def get_class_name_from_xml(tree: ETREE.Element) -> str:
@@ -34,11 +34,7 @@ def get_class_name_from_xml(tree: ETREE.Element) -> str:
3434
if pkg == "opc":
3535
return "energyml.opc.opc." + get_root_type(tree)
3636
else:
37-
schema_version = (
38-
(find_schema_version_in_element(tree) or "")
39-
.replace(".", "_")
40-
.replace("-", "_")
41-
)
37+
schema_version = (find_schema_version_in_element(tree) or "").replace(".", "_").replace("-", "_")
4238
if pkg == "resqml" and schema_version == "2_0":
4339
schema_version = "2_0_1"
4440

@@ -48,7 +44,7 @@ def get_class_name_from_xml(tree: ETREE.Element) -> str:
4844
+ ".v"
4945
+ schema_version
5046
+ "."
51-
+ root_namespace[root_namespace.rindex("/") + 1:]
47+
+ root_namespace[root_namespace.rindex("/") + 1 :]
5248
+ "."
5349
+ get_root_type(tree)
5450
)
@@ -67,11 +63,7 @@ def get_tree(xml_content: Union[bytes, str]) -> ETREE.Element:
6763
if isinstance(xml_bytes, str):
6864
# return ETREE.fromstring(xml_content)
6965
encoding = get_xml_encoding(xml_content)
70-
xml_bytes = xml_content.encode(
71-
encoding=encoding.strip().lower()
72-
if encoding is not None
73-
else "utf-8"
74-
)
66+
xml_bytes = xml_content.encode(encoding=encoding.strip().lower() if encoding is not None else "utf-8")
7567

7668
return ETREE.parse(BytesIO(xml_bytes)).getroot()
7769

@@ -84,9 +76,7 @@ def energyml_xpath(tree: ETREE.Element, xpath: str) -> Optional[list]:
8476
return None
8577

8678

87-
def search_element_has_child_xpath(
88-
tree: ETREE.Element, child_name: str
89-
) -> list:
79+
def search_element_has_child_xpath(tree: ETREE.Element, child_name: str) -> list:
9080
"""
9181
Search elements that has a child named (xml tag) as 'child_name'.
9282
Warning : child_name must contain the namespace (see. ENERGYML_NAMESPACES)

0 commit comments

Comments
 (0)