File size: 1,430 Bytes
51ff9e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import Any

from pydantic import RootModel


class ExtendedConfig(RootModel[dict[str, Any]]):
    """Configuration for extended functionalities.

    This is implemented as a root model so that the entire input is stored
    as the root value. This allows arbitrary keys to be stored and later
    accessed via attribute or dictionary-style access.
    """

    def __str__(self) -> str:
        # Use the root dict to build a string representation.
        root_dict: dict[str, Any] = self.model_dump()
        attr_str = [f'{k}={repr(v)}' for k, v in root_dict.items()]
        return f'ExtendedConfig({", ".join(attr_str)})'

    def __repr__(self) -> str:
        return self.__str__()

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> 'ExtendedConfig':
        # Create an instance directly by wrapping the input dict.
        return cls(data)

    def __getitem__(self, key: str) -> Any:
        # Provide dictionary-like access via the root dict.
        root_dict: dict[str, Any] = self.model_dump()
        return root_dict[key]

    def __getattr__(self, key: str) -> Any:
        # Fallback for attribute access using the root dict.
        try:
            root_dict: dict[str, Any] = self.model_dump()
            return root_dict[key]
        except KeyError as e:
            raise AttributeError(
                f"'ExtendedConfig' object has no attribute '{key}'"
            ) from e