Skip to content

Commit

Permalink
check nameType in nxdl2nyaml direction
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspie committed Feb 27, 2025
1 parent d1af7ef commit 44a99fb
Show file tree
Hide file tree
Showing 6 changed files with 599 additions and 105 deletions.
13 changes: 13 additions & 0 deletions src/nyaml/nxdl2nyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
get_yaml_escape_char_dict,
is_copyright_comment,
remove_namespace_from_tag,
check_for_proper_nameType,
)

DEPTH_SIZE = 2 * " "
Expand Down Expand Up @@ -595,6 +596,10 @@ def handle_group_or_field(self, depth, node, file_out):
indent = depth * DEPTH_SIZE
file_out.write(f"{indent}{name_type}:\n")

name = node_attr.get("name")
nameType = node_attr.get("nameType")
check_for_proper_nameType(name, nameType, name)

for key in rm_key_list:
del node_attr[key]

Expand Down Expand Up @@ -862,6 +867,7 @@ def handle_attributes(self, depth, node, file_out):
name = ""
nm_attr = "name"
node_attr = node.attrib

# Maintain order: name and type in form name(type) or (type)name that come first
name = node_attr.pop(nm_attr, "")
if not name:
Expand All @@ -872,6 +878,10 @@ def handle_attributes(self, depth, node, file_out):

tmp_dict = {}
exists_dict = {}

nameType = node_attr.get("nameType")
check_for_proper_nameType(name, nameType, name)

for key, val in node_attr.items():
if key not in NXDL_ATTRIBUTES_ATTRIBUTES:
raise ValueError(
Expand Down Expand Up @@ -935,6 +945,9 @@ def handle_link(self, depth, node, file_out):
indent = depth * DEPTH_SIZE
file_out.write(f"{indent}{name}(link):\n")

nameType = node_attr.get("nameType")
check_for_proper_nameType(name, nameType, name)

depth_ = depth + 1
# Handle general cases
for attr_key, val in node_attr.items():
Expand Down
199 changes: 199 additions & 0 deletions tests/data/nxdl2yaml/allowed_nameType.nxdl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type="text/xsl" href="nxdlformat.xsl"?>
<!--
# NeXus - Neutron and X-ray Common Data Format
#
# Copyright (C) 2025-2025 NeXus International Advisory Committee (NIAC)
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# For further information, see http://www.nexusformat.org
-->
<definition xmlns="http://definition.nexusformat.org/nxdl/3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" category="base" type="group" name="NXallowed_name_type" extends="NXobject" xsi:schemaLocation="http://definition.nexusformat.org/nxdl/3.1 ../nxdl.xsd">
<doc>
NXnametype test
</doc>
<group name="lower_case_group_no_nametype" type="NXobject">
<doc>
An all lower case group with no nameType.
</doc>
<field name="lower_case_field_no_nametype">
<doc>
An all lower case field with no nameType.
</doc>
<attribute name="lower_case_attribute_no_nametype">
<doc>
An all lower case attribute with no nameType.
</doc>
</attribute>
</field>
<link name="lower_case_link_no_nametype" target="field">
<doc>
An all lower case link with no nameType.
</doc>
</link>
</group>
<group name="lower_case_group_specified" type="NXobject" nameType="specified">
<doc>
An all lower case group with nameType="specified".
</doc>
<field name="lower_case_field_specified" nameType="specified">
<doc>
An all lower case field with nameType="specified".
</doc>
<attribute name="lower_case_attribute_specified" nameType="specified">
<doc>
An all lower case attribute with nameType="specified".
</doc>
</attribute>
<link name="lower_case_link_specified" target="field">
<doc>
An all lower case link with no nameType.
</doc>
</link>
</field>
<group type="NXobject">
<doc>
An unnamed group with no nameType. Interpretead as nameType="any".
</doc>
<field name="FIELD">
<doc>
An unnamed field with no nameType. Interpretead as nameType="any".
</doc>
<attribute name="ATTRIBUTE">
<doc>
An unnamed attribute no nameType. Interpretead as nameType="any".
</doc>
</attribute>
<link name="LINK" target="field">
<doc>
An unnamed link with no nameType. Interpretead as nameType="any".
</doc>
</link>
</field>
</group>
<group type="NXdata" nameType="any">
<doc>
An unnamed group with nameType="any".
</doc>
<field name="FIELD" nameType="any">
<doc>
An unnamed field with nameType="any".
</doc>
<attribute name="ATTRIBUTE" nameType="any">
<doc>
An unnamed attribute with nameType="any".
</doc>
</attribute>
<link name="LINK" nameType="any" target="field">
<doc>
An unnamed link with nameType="any".
</doc>
</link>
</field>
</group>
<group name="objectOBJECT" type="NXobject" nameType="partial">
<doc>
A partially-renameable group with nameType="partial". Variadic part at the end.
</doc>
<field name="fieldFIELD" nameType="partial">
<doc>
A partially-renameable field with nameType="partial". Variadic part at the end.
</doc>
<attribute name="attributeATTRIBUTE" nameType="partial">
<doc>
A partially-renameable attribute nameType="partial". Variadic part at the end.
</doc>
</attribute>
<link name="linkLINK" nameType="partial" target="field">
<doc>
A partially-renameable link nameType="partial". Variadic part at the end.
</doc>
</link>
</field>
</group>
<group name="objectOBJECTobject" type="NXobject" nameType="specified">
<doc>
A partially-renameable group with nameType="partial". Variadic part in the
middle.
</doc>
<field name="fieldFIELDfield" nameType="specified">
<doc>
A partially-renameable field with nameType="partial". Variadic part in the
middle.
</doc>
<attribute name="attributeATTRIBUTEattribute" nameType="specified">
<doc>
A partially-renameable attribute with nameType="partial". Variadic part in the
middle.
</doc>
</attribute>
<link name="linkLINKlink" target="field" nameType="partial">
<doc>
A partially-renameable link with nameType="partial". Variadic part in the
middle.
</doc>
</link>
</field>
</group>
<group name="OBJECTobject" type="NXobject" nameType="partial">
<doc>
A partially-renameable group with nameType="partial". Variadic part at the
start.
</doc>
<field name="FIELDfield" nameType="partial">
<doc>
A partially-renameable field with nameType="partial". Variadic part at the
start.
</doc>
<attribute name="ATTRIBUTEattribute" nameType="partial">
<doc>
A partially-renameable attribute with nameType="partial". Variadic part at the
start.
</doc>
</attribute>
<link name="LINKlink" target="field" nameType="partial">
<doc>
A partially-renameable link with nameType="partial". Variadic part at the start.
</doc>
</link>
</field>
</group>
<group name="OBJECTobjectOBJECTobject" type="NXobject" nameType="specified">
<doc>
A partially-renameable group with nameType="partial". Multiple variadic and non-
variadic parts.
</doc>
<field name="FIELDfieldFIELDfield" nameType="specified">
<doc>
A partially-renameable field with nameType="partial". Multiple variadic and non-
variadic parts.
</doc>
<attribute name="ATTRIBUTEattributeATTRIBUTEattribute" nameType="specified">
<doc>
A partially-renameable attribute with nameType="partial". Multiple variadic and
non-variadic parts.
</doc>
</attribute>
<link name="LINKlinkLINKlink" target="field" nameType="specified">
<doc>
A partially-renameable link with nameType="partial". Multiple variadic and non-
variadic parts.
</doc>
</link>
</field>
</group>
</group>
</definition>
Loading

0 comments on commit 44a99fb

Please sign in to comment.