Skip to content

Commit

Permalink
flatten_json cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
domna committed Jan 17, 2024
1 parent 872fa05 commit 9a3246b
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions pynxtools/dataconverter/readers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def parse_yml(
def flatten_json(
json_data: Dict[str, Any],
base_key: Optional[str] = None,
replace: Optional[str] = None,
replacement_key: Optional[str] = None,
) -> Dict[str, Any]:
"""
Flattens a json dict into a flat dictionary of absolute paths.
Expand All @@ -215,47 +215,47 @@ def flatten_json(
base_key (Optional[str], optional):
A base key to prefix to all keys.
Defaults to None.
replace (Optional[str], optional):
replacement_key (Optional[str], optional):
A replacement key which replaces all occurences of * with this string.
Defaults to None.
Returns:
Dict[str, Any]: The flattened dict
"""
flattened_config = {}

def update_config(key, value, rkey):
if isinstance(value, dict):
flattened_config.update(
flatten_json(value, base_key=key, replacement_key=rkey)
)
elif isinstance(value, str) and value.startswith("@link:"):
flattened_config[key] = {"link": value.removeprefix("@link:")}
else:
flattened_config[key] = value

for key, value in json_data.items():
if base_key is not None:
key = f"{base_key}/{key}"

if replace is not None:
key = key.replace("*", replace)
if replacement_key is not None:
key = key.replace("*", replacement_key)
if isinstance(value, str):
value = value.replace("*", replace)
value = value.replace("*", replacement_key)

expand_match = short_notation_regex.search(key)
if replace is None and expand_match is not None:
if replacement_key is None and expand_match is not None:
expand_keys = expand_match.group(1).split(",")
for ekey in expand_keys:
rkey = key.replace(expand_match.group(0), ekey)

if isinstance(value, dict):
flattened_config.update(
flatten_json(value, base_key=rkey, replace=ekey)
)
elif isinstance(value, str) and value.startswith("@link:"):
flattened_config[rkey] = {"link": value.removeprefix("@link:")}
elif isinstance(value, str):
flattened_config[rkey.replace("*", ekey)] = value.replace("*", ekey)
else:
flattened_config[rkey.replace("*", ekey)] = value
if isinstance(value, str):
value = value.replace("*", ekey)

update_config(rkey, value, ekey)
continue

if isinstance(value, dict):
flattened_config.update(flatten_json(value, base_key=key))
elif isinstance(value, str) and value.startswith("@link:"):
flattened_config[key] = {"link": value.removeprefix("@link:")}
else:
flattened_config[key] = value
update_config(key, value, None)
return flattened_config


Expand Down

0 comments on commit 9a3246b

Please sign in to comment.