From f15f3d38ac11026ba878637034c5c1ab0cb64da8 Mon Sep 17 00:00:00 2001 From: Lukas Pielsticker <50139597+lukaspie@users.noreply.github.com> Date: Mon, 2 Sep 2024 09:42:45 +0200 Subject: [PATCH] implement mapping in __to_section --- src/pynxtools/nomad/schema.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/pynxtools/nomad/schema.py b/src/pynxtools/nomad/schema.py index fd5f1d2bf..83f7ce973 100644 --- a/src/pynxtools/nomad/schema.py +++ b/src/pynxtools/nomad/schema.py @@ -44,6 +44,7 @@ BaseSection, Entity, Activity, + Measurement, Instrument, CompositeSystem, ) @@ -90,7 +91,7 @@ __XML_PARENT_MAP: Dict[ET.Element, ET.Element] __NX_DOC_BASE = "https://manual.nexusformat.org/classes" -BASESECTIONS_MAP = { +BASESECTIONS_MAP: Dict[str, Any] = { "NXinstrument": Instrument, "NXsample": CompositeSystem, } @@ -258,6 +259,26 @@ def __get_documentation_url( return f"{__NX_DOC_BASE}/{nx_package}/{anchor_segments[-1]}.html#{anchor}" +# def __to_section(name: str, **kwargs) -> Section: +# """ +# Returns the 'existing' metainfo section for a given top-level nexus base-class name. + +# This function ensures that sections for these base-classes are only created once. +# This allows to access the metainfo section even before it is generated from the base +# class nexus definition. +# """ +# if name in __section_definitions: +# section = __section_definitions[name] +# section.more.update(**kwargs) +# return section + +# section = Section(validate=VALIDATE, name=name, **kwargs) + +# __section_definitions[name] = section + +# return section + + def __to_section(name: str, **kwargs) -> Section: """ Returns the 'existing' metainfo section for a given top-level nexus base-class name. @@ -271,6 +292,12 @@ class nexus definition. section.more.update(**kwargs) return section + if not name.startswith("NX"): + nx_type = kwargs.get("nx_type") + base_section_cls = BASESECTIONS_MAP.get(nx_type, BaseSection) + else: + base_section_cls = BASESECTIONS_MAP.get(name, BaseSection) + section = Section(validate=VALIDATE, name=name, **kwargs) __section_definitions[name] = section