solved link bug

This commit is contained in:
2025-01-09 17:14:48 +01:00
parent 3c240fc16b
commit f7282eba73
2 changed files with 26 additions and 25 deletions

View File

@@ -1,9 +1,4 @@
class ConfigurationResolver:
"""
A class to resolve `link` entries in a nested configuration structure.
Supports navigation through dictionaries, lists, and `subitems`.
"""
def __init__(self, config):
self.config = config
@@ -21,15 +16,22 @@ class ConfigurationResolver:
for key, value in list(current_config.items()):
if key == "link":
try:
target = self._find_entry(root_config, value.lower())
current_config.clear()
current_config.update(target)
# Attempt to find the target entry in the root configuration
target = self._find_entry(root_config, value.lower().replace(" ", "_"))
if isinstance(target, dict):
# Replace the current config dictionary with the target dictionary
current_config.clear()
current_config.update(target)
else:
current_config[value.split(".")[-1]] = target
except Exception as e:
raise ValueError(
f"Error resolving link '{value}': {str(e)}. "
f"Current path: {key}, Current config: {current_config}"
)
else:
# Recurse into nested structures
self._recursive_resolve(value, root_config)
elif isinstance(current_config, list):
for item in current_config:
@@ -43,26 +45,26 @@ class ConfigurationResolver:
parts = path.split('.')
current = config
for part in parts:
part = part.replace(" ", "_")
if isinstance(current, list):
# Look for a matching name in the list
# Search for a matching entry in a list
found = next(
(item for item in current if isinstance(item, dict) and item.get("name", "").lower() == part),
(
item
for item in current
if isinstance(item, dict) and item.get("name", "").lower().replace(" ", "_") == part
),
None
)
if found:
print(
f"Matching entry for '{part}' in list. Path so far: {' > '.join(parts[:parts.index(part)+1])}. "
f"Current list: {current}"
)
else:
if not found:
raise ValueError(
f"No matching entry for '{part}' in list. Path so far: {' > '.join(parts[:parts.index(part)+1])}. "
f"Current list: {current}"
)
current = found
elif isinstance(current, dict):
# Case-insensitive dictionary lookup
key = next((k for k in current if k.lower() == part), None)
# Perform a case-insensitive dictionary lookup
key = next((k for k in current if k.lower().replace(" ", "_") == part), None)
if key is None:
raise KeyError(
f"Key '{part}' not found in dictionary. Path so far: {' > '.join(parts[:parts.index(part)+1])}. "
@@ -74,9 +76,9 @@ class ConfigurationResolver:
f"Invalid path segment '{part}'. Current type: {type(current)}. "
f"Path so far: {' > '.join(parts[:parts.index(part)+1])}"
)
# Navigate into `subitems` if present
if isinstance(current, dict) and ("subitems" in current and current["subitems"]):
if isinstance(current, dict) and "subitems" in current and current["subitems"]:
current = current["subitems"]
return current