In between commit bug solving

This commit is contained in:
2025-01-10 01:18:37 +01:00
parent 96ab80eaf2
commit 166055422e
3 changed files with 216 additions and 237 deletions

View File

@@ -8,7 +8,7 @@ class ConfigurationResolver:
"""
self._recursive_resolve(self.config, self.config)
def _recursive_resolve(self, current_config, root_config):
def _recursive_resolve(self, current_config, root_config, path=""):
"""
Recursively resolves `link` entries in the configuration.
"""
@@ -16,38 +16,39 @@ class ConfigurationResolver:
for key, value in list(current_config.items()):
if key == "link":
try:
# Attempt to find the target entry in the root configuration
print(f"Resolving link '{value}' at path '{path}'") # Debugging
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)
elif isinstance(target, str):
current_config[key] = target
else:
current_config[value.split(".")[-1]] = target
except Exception as e:
raise ValueError(f"Expected a dictionary or string for link '{value}', got {type(target)}")
except KeyError as e:
raise ValueError(
f"Error resolving link '{value}': {str(e)}. "
f"Current path: {key}, Current config: {current_config}"
f"Key error while resolving link '{value}': {str(e)}. Current path: {key}, Current config: {current_config}"
)
else:
# Recurse into nested structures
self._recursive_resolve(value, root_config)
self._recursive_resolve(value, root_config, path=f"{path}.{key}")
elif isinstance(current_config, list):
for item in current_config:
self._recursive_resolve(item, root_config)
for index, item in enumerate(current_config):
self._recursive_resolve(item, root_config, path=f"{path}[{index}]")
def _find_entry(self, config, path):
"""
Finds an entry in the configuration by a dot-separated path.
Supports both dictionaries and lists with `subitems` navigation.
Supports both dictionaries and lists, but does not navigate into `subitems`
unless explicitly required by the path.
"""
parts = path.split('.')
current = config
for part in parts:
part = part.replace(" ", "_")
part = part.replace(" ", "_") # Normalize the part name
if isinstance(current, list):
# Search for a matching entry in a list
# Look for a matching entry in the list
found = next(
(
item
@@ -57,13 +58,14 @@ class ConfigurationResolver:
None
)
if not found:
raise ValueError(
raise KeyError(
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):
# Perform a case-insensitive dictionary lookup
# Look for a key match in the dictionary
key = next((k for k in current if k.lower().replace(" ", "_") == part), None)
if key is None:
raise KeyError(
@@ -71,18 +73,27 @@ class ConfigurationResolver:
f"Current dictionary: {current}"
)
current = current[key]
else:
raise ValueError(
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"]:
current = current["subitems"]
# Stop navigating into `subitems` if the path doesn't explicitly require it
if isinstance(current, dict) and "subitems" in current and isinstance(current["subitems"], list):
if part == "subitems":
current = current["subitems"]
else:
break # Do not navigate further unless explicitly in the path
# Ensure the resolved target is a dictionary or string
if not isinstance(current, (dict, str)):
raise ValueError(f"Expected a dictionary or string for path '{path}', got {type(current)}. Current value: {current}")
return current
def get_config(self):
"""
Returns the resolved configuration.