Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NX docstring as attribute #415

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "src/pynxtools/definitions"]
path = src/pynxtools/definitions
url = https://github.com/FAIRmat-NFDI/nexus_definitions.git
url = https://github.com/FAIRmat-NFDI/nexus_definitions.git
4 changes: 3 additions & 1 deletion src/pynxtools/data/NXtest.nxdl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<field name="program_name"/>
<field name="definition">
<doc>This is a dummy NXDL to test out the dataconverter.</doc>
<attribute name="version"/>
<attribute name="version">
<doc>This is the version of the definition.</doc>
</attribute>
<enumeration>
<item value="NXTEST"/>
<item value="NXtest"/>
Expand Down
38 changes: 36 additions & 2 deletions src/pynxtools/dataconverter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,13 @@ def convert(
)

helpers.add_default_root_attributes(data=data, filename=os.path.basename(output))
Writer(data=data, nxdl_f_path=nxdl_f_path, output_path=output).write()

write_docs = kwargs.pop("write_docs", False)
docs_format = kwargs.pop("docs_format", None)
Writer(data=data, nxdl_f_path=nxdl_f_path, output_path=output).write(
write_docs=write_docs,
docs_format=docs_format,
)

logger.info(f"The output file generated: {output}.")

Expand Down Expand Up @@ -350,7 +356,21 @@ def main_cli():
default=None,
help="A json config file for the reader",
)
# pylint: disable=too-many-arguments
@click.option(
"--write-docs",
is_flag=True,
default=False,
help="Write docs for the individual NeXus concepts as HDF5 attributes.",
)
@click.option(
"--docs-format",
type=click.Choice(["default", "html", "html5", "xml", "pseudoxml"]),
default=None,
help=(
"Optionally specify the format in which the docs for the individual NeXus concepts is generated. "
"By default, the docs are formatted as in the NXDL file."
),
)
def convert_cli(
files: Tuple[str, ...],
input_file: Tuple[str, ...],
Expand All @@ -363,6 +383,8 @@ def convert_cli(
mapping: str,
config_file: str,
fail: bool,
write_docs: bool,
docs_format: str,
**kwargs,
):
"""This command allows you to use the converter functionality of the dataconverter."""
Expand Down Expand Up @@ -390,6 +412,18 @@ def convert_cli(
if config_file:
kwargs["config_file"] = config_file

if write_docs:
kwargs["write_docs"] = write_docs
if not docs_format:
kwargs["docs_format"] = "default"
else:
kwargs["docs_format"] = docs_format

elif docs_format is not None:
raise click.UsageError(
"Error: --docs-format can only be used with --write-docs."
)

file_list = []
for file in files:
if os.path.isdir(file):
Expand Down
58 changes: 58 additions & 0 deletions src/pynxtools/dataconverter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,64 @@ def get_concept_basepath(path: str) -> str:
return "/" + "/".join(concept_path)


def get_concept_path_from_elem(elem: ET.Element) -> str:
"""
Process individual XML element to generate the NeXus concept path.

Output is e.g. "NXexperiment:/NXentry/NXinstrument/NXdetector".
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is called classpath, but this can be ambiguous if two concept of the same type exists next two one another. This is why the NeXus vocabulary follows the path we call html name which is unique for a given concept. E.g. NXexperiment:/ENTRY/INSTRUMENT/mydetector. Note that this does not hold the information of the nexus classes referened along the path, so if it is needed, a classpath needs to be returned, too.

"""

name = elem.attrib.get("name", "")
elem_type = elem.attrib.get("type", "")
nxdlbase = elem.attrib.get("nxdlbase", "") # .split("/")[-1]
nxdlbase_class = elem.attrib.get("nxdlbase_class", "")
nxdlpath = elem.attrib.get("nxdlpath", "")
category = elem.attrib.get("category", "")
# optional = elem.attrib.get("optional", "")
# extends = elem.attrib.get("extends", "")

# print(f"tag: {tag}")
# print(f"name: {name}")
# print(f"elem_type: {elem_type}")
# print(f"nxdlbase: {nxdlbase}")
# print(f"nxdlbase_class: {nxdlbase_class}")
# print(f"nxdlpath: {nxdlpath}")
# # print(f"optional: {optional}")
# # print(f"extends: {extends}")
# print("\n")

concept_path = ""

if elem.tag.endswith("group"):
if nxdlbase_class and nxdlbase_class == "application":
concept_path += "NXmpes:"
concept_path += nxdlpath # + = f"(elem_type)"

else:
if nxdlbase:
concept_path += nxdlbase.replace(".nxdl.xml", "").split(os.path.sep)[-1]
concept_path += nxdlpath # + = f"(elem_type)"

elif elem.tag.endswith("field"):
pass

elif elem.tag.endswith("attribute"):
pass
elif elem.tag.endswith("definition"):
concept_path += name

return concept_path

# if nxdlpath:
# # Split the nxdlpath and construct the string
# path_parts = nxdlpath.strip("/").split("/")
# formatted_path = "/".join(path_parts)
# return f"{formatted_path}({elem_type})"
# else:
# # For elements with no path, return the name and type
# return f"{name}({elem_type})"


def remove_namespace_from_tag(tag):
"""Helper function to remove the namespace from an XML tag."""

Expand Down
Loading
Loading