mirror of
				https://github.com/kevinveenbirkenbach/homepage.veen.world.git
				synced 2025-11-04 09:27:58 +00:00 
			
		
		
		
	Solved link bug with subitems
This commit is contained in:
		@@ -1,3 +1,4 @@
 | 
			
		||||
from pprint import pprint
 | 
			
		||||
class ConfigurationResolver:
 | 
			
		||||
    """
 | 
			
		||||
    A class to resolve `link` entries in a nested configuration structure.
 | 
			
		||||
@@ -21,10 +22,13 @@ class ConfigurationResolver:
 | 
			
		||||
            for key, value in list(current_config.items()):
 | 
			
		||||
                if key == "link":
 | 
			
		||||
                    try:
 | 
			
		||||
                        target = self._find_entry(root_config, value.lower())
 | 
			
		||||
                        target = self._find_entry(root_config, value.lower(), True)
 | 
			
		||||
                        if isinstance(target, list) and len(target) > 2:
 | 
			
		||||
                            target = self._find_entry(root_config, value.lower(), False)
 | 
			
		||||
                            
 | 
			
		||||
                        current_config.clear()
 | 
			
		||||
                        current_config.update(target)
 | 
			
		||||
                    except Exception as e:
 | 
			
		||||
                    except Exception as e: 
 | 
			
		||||
                        raise ValueError(
 | 
			
		||||
                            f"Error resolving link '{value}': {str(e)}. "
 | 
			
		||||
                            f"Current path: {key}, Current config: {current_config}"
 | 
			
		||||
@@ -35,7 +39,18 @@ class ConfigurationResolver:
 | 
			
		||||
            for item in current_config:
 | 
			
		||||
                self._recursive_resolve(item, root_config)
 | 
			
		||||
 | 
			
		||||
    def _find_entry(self, config, path):
 | 
			
		||||
    def _get_subitems(self,current):
 | 
			
		||||
        if isinstance(current, dict) and ("subitems" in current and current["subitems"]):
 | 
			
		||||
            current = current["subitems"]
 | 
			
		||||
        return current
 | 
			
		||||
 | 
			
		||||
    def _find_by_name(self,current, part):
 | 
			
		||||
        return next(
 | 
			
		||||
                    (item for item in current if isinstance(item, dict) and item.get("name", "").lower() == part),
 | 
			
		||||
                    None
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
    def _find_entry(self, config, path, subitems):
 | 
			
		||||
        """
 | 
			
		||||
        Finds an entry in the configuration by a dot-separated path.
 | 
			
		||||
        Supports both dictionaries and lists with `subitems` navigation.
 | 
			
		||||
@@ -45,10 +60,7 @@ class ConfigurationResolver:
 | 
			
		||||
        for part in parts:
 | 
			
		||||
            if isinstance(current, list):
 | 
			
		||||
                # Look for a matching name in the list
 | 
			
		||||
                found = next(
 | 
			
		||||
                    (item for item in current if isinstance(item, dict) and item.get("name", "").lower() == part),
 | 
			
		||||
                    None
 | 
			
		||||
                )
 | 
			
		||||
                found = self._find_by_name(current,part)
 | 
			
		||||
                if found:
 | 
			
		||||
                    print(
 | 
			
		||||
                        f"Matching entry for '{part}' in list. Path so far: {' > '.join(parts[:parts.index(part)+1])}. "
 | 
			
		||||
@@ -64,20 +76,22 @@ class ConfigurationResolver:
 | 
			
		||||
                # Case-insensitive dictionary lookup
 | 
			
		||||
                key = next((k for k in current if k.lower() == part), None)
 | 
			
		||||
                if key is None:
 | 
			
		||||
                    raise KeyError(
 | 
			
		||||
                        f"Key '{part}' not found in dictionary. Path so far: {' > '.join(parts[:parts.index(part)+1])}. "
 | 
			
		||||
                        f"Current dictionary: {current}"
 | 
			
		||||
                    )
 | 
			
		||||
                current = current[key]
 | 
			
		||||
                    current = self._find_by_name(current["subitems"],part)
 | 
			
		||||
                    if not current:
 | 
			
		||||
                        raise KeyError(
 | 
			
		||||
                            f"Key '{part}' not found in dictionary. Path so far: {' > '.join(parts[:parts.index(part)+1])}. "
 | 
			
		||||
                            f"Current dictionary: {current}"
 | 
			
		||||
                        )
 | 
			
		||||
                else: 
 | 
			
		||||
                    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"]
 | 
			
		||||
            if subitems:
 | 
			
		||||
                current = self._get_subitems(current)
 | 
			
		||||
 | 
			
		||||
        return current
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user