Skip to content

Commit

Permalink
#339: Convert paths for dimr when serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
priscavdsluis committed Mar 16, 2023
1 parent 100f566 commit 29c2813
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions hydrolib/core/dimr/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from lxml import etree as e

from hydrolib.core.basemodel import ModelSaveSettings, SerializerConfig
from hydrolib.core.utils import FilePathStyleConverter


class DIMRSerializer:
Expand Down Expand Up @@ -42,7 +43,9 @@ def serialize(
attrib=attrib,
nsmap=namespaces,
)
DIMRSerializer._build_tree(root, data, config)

path_style_converter = FilePathStyleConverter()
DIMRSerializer._build_tree(root, data, config, save_settings, path_style_converter)

to_string = minidom.parseString(e.tostring(root))
xml = to_string.toprettyxml(indent=" ", encoding="utf-8")
Expand All @@ -51,27 +54,33 @@ def serialize(
f.write(xml)

@staticmethod
def _build_tree(root, data: dict, config: SerializerConfig):
def _build_tree(root,
data: dict,
config: SerializerConfig,
save_settings: ModelSaveSettings,
path_style_converter: FilePathStyleConverter):
name = data.pop("name", None)
if name:
root.set("name", name)

for key, val in data.items():
if isinstance(val, dict):
c = e.Element(key)
DIMRSerializer._build_tree(c, val, config)
DIMRSerializer._build_tree(c, val, config, save_settings, path_style_converter)
root.append(c)
elif isinstance(val, List):
for item in val:
c = e.Element(key)
DIMRSerializer._build_tree(c, item, config)
DIMRSerializer._build_tree(c, item, config, save_settings, path_style_converter)
root.append(c)
else:
c = e.Element(key)
if isinstance(val, datetime):
c.text = val.isoformat(sep="T", timespec="auto")
elif isinstance(val, float):
c.text = f"{val:{config.float_format}}"
elif isinstance(val, Path):
c.text = path_style_converter.convert_from_os_style(val, save_settings.path_style)
else:
c.text = str(val)
root.append(c)

0 comments on commit 29c2813

Please sign in to comment.