ahvn.utils.basic.config_utils module¶
- ahvn.utils.basic.config_utils.dmerge(iterable, start=None)[源代码]¶
Merge multiple dictionaries into a single dictionary, with later dictionaries overwriting earlier ones. Nested dictionaries are merged recursively while all other non-dictionary values are overwritten.
警告
The merging of dictionaries is not order-preserving. The order of keys in the resulting dictionary may not match the order of keys in the input dictionaries.
- 参数:
- 返回:
The merged dictionary.
- 返回类型:
示例
>>> d1 = {'a': 1, 'b': {'c': 2, 'f': 5}} >>> d2 = {'b': {'d': 3, 'f': 0}, 'e': 4} >>> dmerge([d1, d2]) {'a': 1, 'b': {'c': 2, 'f': 0, 'd': 3}, 'e': 4} >>> dmerge([d2, d1]) {'b': {'d': 3, 'f': 5, 'c': 2}, 'e': 4, 'a': 1} >>> dmerge([d1, d2], start={'a': 0, 'g': 6}) {'a': 1, 'g': 6, 'b': {'c': 2, 'f': 0, 'd': 3}, 'e': 4}
- ahvn.utils.basic.config_utils.dget(d, key_path=None, default=None)[源代码]¶
Get a value from a dictionary using a dot-separated key path. If the key path does not exist, return the default value.
- 参数:
- 返回:
The value at the specified key path or the default value if not found.
- 返回类型:
示例
>>> dget({'a': {'b': {'c': 42}}}, 'a.b.c') 42 >>> dget({'a': {'b': {'c': 42}}}, 'a.b.d', default='not found') 'not found' >>> dget({'a': {'b': {'c': [1, 2, 3]}}}, 'a.b.c[1]') 2
- ahvn.utils.basic.config_utils.dset(d, key_path, value=None)[源代码]¶
Set a value in a dictionary using a dot-separated key path. If the key path does not exist, it will be created.
- 参数:
- 返回:
True if the value was set successfully, False if the key path is invalid.
- 返回类型:
示例
>>> d = {} >>> dset(d, 'a.b.c', 42) True >>> d {'a': {'b': {'c': 42}}}
- ahvn.utils.basic.config_utils.dunset(d, key_path)[源代码]¶
Unset a value in a dictionary using a dot-separated key path. If the key path does not exist, it will be ignored.
- 参数:
- 返回:
True if the value was unset successfully, False if the key path is invalid.
- 返回类型:
示例
>>> d = {'a': {'b': {'c': 42}}} >>> dunset(d, 'a.b.c') True >>> d {'a': {'b': {}}}
- ahvn.utils.basic.config_utils.dsetdef(d, key_path, default=None)[源代码]¶
Set a default value in a dictionary using a dot-separated key path if the key path does not exist.
Notice that if key_path exists but its value is None, the default value will also be set.
- 参数:
- 返回:
True if the default value was set successfully, False if the key path is invalid or already exists.
- 返回类型:
示例
>>> d = {} >>> dsetdef(d, 'a.b.c', 42) True >>> d {'a': {'b': {'c': 42}}} >>> dsetdef(d, 'a.b.c', 100) False >>> d {'a': {'b': {'c': 42}}}
- ahvn.utils.basic.config_utils.dflat(d, prefix='', enum=False)[源代码]¶
Flatten a nested dictionary into a flat dictionary with dot-separated keys.
- 参数:
- 生成器:
Generator[str, None, None] -- A generator yielding key-value pairs in the flattened format.
- 返回类型:
示例
>>> dict(dflat({'a': {'b': {'c': 42, 'd': [1, 2, 3]}}, 'e': 5})) {'a.b.c': 42, 'a.b.d[0]': 1, 'a.b.d[1]': 2, 'a.b.d[2]': 3, 'e': 5} >>> dict(dflat({'a': {'b': {'c': 42, 'd': [1, 2, 3]}}, 'e': 5}, enum=True)) {'a': {'b': {'c': 42, 'd': [1, 2, 3]}}, 'a.b': {'c': 42, 'd': [1, 2, 3]}, 'a.b.c': 42, 'a.b.d': [1, 2, 3], 'a.b.d[0]': 1, 'a.b.d[1]': 2, 'a.b.d[2]': 3, 'e': 5}
- ahvn.utils.basic.config_utils.dunflat(d)[源代码]¶
Unflatten a flat dictionary with dot-separated keys into a nested dictionary.
- 参数:
- 返回:
The nested dictionary.
- 返回类型:
示例
>>> d = {'a.b.c': 42} >>> dunflat(d) {'a': {'b': {'c': 42}}}
- class ahvn.utils.basic.config_utils.ConfigManager(name=None, package=None, cwd=None)[源代码]¶
基类:
object- set_cwd(cwd=None)[源代码]¶
Set the current working directory for the configuration manager.
- 参数:
cwd (str) -- The new current working directory.
- property local_dir: str¶
Get the local configuration directory.
- 返回:
The path to the local configuration directory.
- 返回类型:
- property local_config_path: str¶
Get the path to the local configuration file.
- 返回:
The path to the local configuration file.
- 返回类型:
- property global_config_path: str¶
Get the path to the global configuration file.
- 返回:
The path to the global configuration file.
- 返回类型:
- property system_config_path: str¶
Get the path to the system configuration file.
- 返回:
The path to the system configuration file.
- 返回类型:
- config_path(level=None)[源代码]¶
Get the path to the configuration file for a specific level.
- 参数:
level (Literal[None, 'local', 'global', 'system']) -- The configuration level to get the path for. If None, returns the local configuration path.
- 返回:
The path to the configuration file for the specified level.
- 返回类型:
- init(reset=False)[源代码]¶
Initialize the local configuration manager by loading the default configuration and creating the necessary directories.
- setup(reset=False)[源代码]¶
Setup the global configuration manager by initializing it and loading the configuration and creating the necessary directories.
- get(key_path=None, default=None, level=None)[源代码]¶
Get a value from the configuration using a dot-separated key path.
- 参数:
- 返回:
The value at the specified key path or the default value if not found.
- 返回类型:
- set(key_path, value=None, level=None)[源代码]¶
Set a value in the configuration using a dot-separated key path.
- 参数:
- 返回:
True if the value was set successfully, False if the key path is invalid.
- 返回类型:
- ahvn.utils.basic.config_utils.hpj(*args, abs=False, cm=None)[源代码]¶
Join a list of strings into a path. Platform-agnostic. Spaces and trailing slashes are stripped from each argument. The following characters will be expanded: - '~' to the user's home directory. - '&' the resources directory of AgentHeaven. It is only recommended to use it at the beginning of the path. - '>' the local root folder (without .ahvn/) of the current AgentHeaven repository. It is only recommended to use it at the beginning of the path.
- 参数:
*args (
List[str]) -- Components of the path to join. Each argument should be a string.abs (bool, optional) -- If True, returns the absolute path. Defaults to False.
cm (Optional[ConfigManager]) -- The configuration manager to use for resource and local directory resolution. Defaults to HEAVEN_CM.
- 返回:
The joined, normalized path. Expands '~' to the user's home directory.
- 返回类型:
示例
>>> hpj("A", "B/C", " D/ ") 'A/B/C/D' >>> hpj("A", "B/C", " D/ ", abs=True) '<path_to_cwd>/A/B/C' >>> hpj("~", "A", "B/C", " D/ ", abs=True) '<path_to_user_dir>/A/B/C' >>> hpj("&", "B", "C") '<path_to_ahvn>/resources/B/C' >>> hpj("& B", "C") '<path_to_ahvn>/resources/B/C' >>> hpj("&/B", "C") '<path_to_ahvn>/resources/B/C' >>> hpj("& /B", "C") # Not recommended '/B/C' >>> hpj("> B/C", " D/ ") '<path_to_repo>/B/C/D'