Skip to content

Commit 7a0b8c0

Browse files
committed
clean up template if gorup/field was linked to field/group
1 parent 769bfd8 commit 7a0b8c0

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

src/pynxtools/dataconverter/validation.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,21 +1108,36 @@ def handle_group(node: NexusGroup, keys: Mapping[str, Any], prev_path: str):
11081108
# Don't process if this is actually a sub-variant of this group
11091109
continue
11101110
nx_class, _ = split_class_and_name_of(variant)
1111+
if variant.endswith("target"):
1112+
# We need to do this for cases where the target was added automatically,
1113+
# but the group was incorrectly linked to a field.
1114+
continue
11111115
if not isinstance(keys[variant], Mapping):
11121116
# Groups should have subelements
1117+
11131118
if nx_class is not None:
11141119
collector.collect_and_log(
11151120
variant_path,
11161121
ValidationProblem.ExpectedGroup,
11171122
None,
11181123
)
1119-
# TODO: decide if we want to remove such keys
1120-
# collector.collect_and_log(
1121-
# variant_path,
1122-
# ValidationProblem.KeyToBeRemoved,
1123-
# node.nx_type,
1124-
# )
1125-
# keys_to_remove.append(not_visited_key)
1124+
collector.collect_and_log(
1125+
variant_path,
1126+
ValidationProblem.KeyToBeRemoved,
1127+
node.nx_type,
1128+
)
1129+
keys_to_remove.append(variant_path)
1130+
for subkey in keys.keys():
1131+
if subkey.startswith(variant) and subkey != variant:
1132+
name = subkey.split(variant)[-1]
1133+
subkey_path = f"{variant_path}/{name}"
1134+
collector.collect_and_log(
1135+
subkey_path,
1136+
ValidationProblem.KeyToBeRemoved,
1137+
"attribute" if name.startswith("@") else "field",
1138+
)
1139+
keys_to_remove.append(subkey_path)
1140+
11261141
continue
11271142
if node.nx_class == "NXdata":
11281143
handle_nxdata(node, keys[variant], prev_path=variant_path)
@@ -1201,6 +1216,7 @@ def _follow_link(
12011216
if f"{key_path}/@target" not in mapping:
12021217
# Target attribute added automatically
12031218
mapping[f"{key_path}/@target"] = value["link"]
1219+
resolved_keys[f"{key}@target"] = value["link"]
12041220
else:
12051221
attr_target = mapping[f"{key_path}/@target"]
12061222
remove_from_not_visited(f"{key_path}/@target")
@@ -1243,14 +1259,17 @@ def handle_field(node: NexusNode, keys: Mapping[str, Any], prev_path: str):
12431259
ValidationProblem.ExpectedField,
12441260
None,
12451261
)
1246-
# TODO: decide if we want to remove such keys
1247-
# collector.collect_and_log(
1248-
# variant_path,
1249-
# ValidationProblem.KeyToBeRemoved,
1250-
# node.nx_type,
1251-
# )
1252-
# keys_to_remove.append(variant_path)
1253-
continue
1262+
collector.collect_and_log(
1263+
variant_path,
1264+
ValidationProblem.KeyToBeRemoved,
1265+
node.nx_type,
1266+
)
1267+
keys_to_remove.append(variant_path)
1268+
for subkey in keys.keys():
1269+
if subkey.startswith(variant) and subkey != variant:
1270+
subkey_path = f"{prev_path}/{subkey.replace('@', '/@')}"
1271+
keys_to_remove.append(subkey_path)
1272+
12541273
if node.optionality == "required" and isinstance(keys[variant], Mapping):
12551274
# Check if all fields in the dict are actual attributes (startswith @)
12561275
all_attrs = True

0 commit comments

Comments
 (0)