Skip to content

Commit

Permalink
allow for optional links in config file of MultiFormatReader
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspie committed Nov 22, 2024
1 parent 7af2e03 commit 4402333
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[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
branch = small-xps-changes
42 changes: 32 additions & 10 deletions src/pynxtools/dataconverter/readers/multi/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ def parse_config_value(value: str) -> Tuple[str, Any]:
if new_entry_dict[key] is not None:
break

if (
value.startswith("!")
and isinstance(new_entry_dict[key], dict)
and "link" in new_entry_dict[key]
):
link_target = new_entry_dict[key]["link"]
print("link_target", link_target)
logger.info(
f"There was no target at {link_target} for the optional link for {key}. "
f"Removing the link."
)
del new_entry_dict[key]
return

if value.startswith("!") and new_entry_dict[key] is None:
group_to_delete = key.rsplit("/", 1)[0]
logger.info(
Expand Down Expand Up @@ -256,28 +270,36 @@ def has_missing_main(key: str) -> bool:
return True
return False

def dict_sort_key(keyval: Tuple[str, Any]) -> bool:
def dict_sort_key(keyval: Tuple[str, Any]) -> Tuple[int, str]:
"""
The function to sort the dict by.
This just sets False for keys starting with "!" to put them at the beginning.
Besides, pythons sorted is stable, so this will keep the order of the keys
which have the same sort key.
Sort keys by their value's priority:
- Values starting with "!link" go last (return 2).
- Values starting with "!" but not "!link" go first (return 0).
- All other values are sorted normally (return 1).
"""
if isinstance(keyval[1], str):
return not keyval[1].startswith("!")
return True
value = keyval[1]
if isinstance(value, str):
if value.startswith("!@link"):
return (2, keyval[0]) # Last
if value.startswith("!"):
return (0, keyval[0]) # First
return (1, keyval[0]) # Middle

if callbacks is None:
# Use default callbacks if none are explicitly provided
callbacks = ParseJsonCallbacks()

optional_groups_to_remove: List[str] = []
new_entry_dict = {}

# Process '!...' keys first, but '!link' keys last
sorted_keys = dict(sorted(config_dict.items(), key=dict_sort_key))
for key, val in sorted_keys.items():
print(key, val)

for entry_name in entry_names:
callbacks.entry_name = entry_name

# Process '!...' keys first
sorted_keys = dict(sorted(config_dict.items(), key=dict_sort_key))
for key in sorted_keys:
value = config_dict[key]
key = key.replace("/ENTRY/", f"/ENTRY[{entry_name}]/")
Expand Down
2 changes: 1 addition & 1 deletion src/pynxtools/definitions
2 changes: 1 addition & 1 deletion src/pynxtools/nexus-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2022.07-1490-gd3c5237d
v2022.07-1513-g4a596fc2

0 comments on commit 4402333

Please sign in to comment.