config.main¶
主要部分
Attributes¶
默认配置池 |
|
|
|
|
|
|
|
|
|
|
Classes¶
基础连锁配置文件SL处理器 |
|
基础压缩配置文件SL处理器 |
|
基础配置SL管理器 提供了一些实用功能 |
|
基础本地配置文件SL处理器 |
|
配置池 |
|
配置获取器,可作装饰器使用 |
|
对需求的键进行存在检查、类型检查、填充默认值 |
Functions¶
|
包装意料内的异常 |
Module Contents¶
- class BasicChainConfigSL(*, reg_alias: str | None = None, create_dir: bool = True)¶
Bases:
BasicConfigSL,abc.ABC基础连锁配置文件SL处理器
小心
会临时在配置文件池中添加文件以传递SL操作
在 0.2.0 版本加入.
- after_save(config_pool: config.abc.ABCConfigPool, config_file: config.abc.ABCConfigFile[Any], file_path: str, root_path: str, namespace: str, file_name: str) None¶
保存后处理
- 参数:
config_pool (ABCConfigPool) -- 配置池
config_file (ABCConfigFile[Any]) -- 配置文件
file_path (str) -- 文件路径
root_path (str) -- 根路径
namespace (str) -- 命名空间
file_name (str) -- 文件名
- before_load(config_pool: config.abc.ABCConfigPool, file_path: str, root_path: str, namespace: str, file_name: str) None¶
加载前处理
- 参数:
config_pool (ABCConfigPool) -- 配置池
file_path (str) -- 文件路径
root_path (str) -- 根路径
namespace (str) -- 命名空间
file_name (str) -- 文件名
- filename_formatter(file_name: str) str¶
格式化文件名以传递给其他SL处理器
- 默认实现:
如果为
str且file_name.endswith成立则返回移除后缀后的结果如果为
re.Pattern且Pattern.fullmatch(file_name)成立则返回Pattern.sub(file_name, "")直接返回
- initialize(processor_pool: config.abc.ABCSLProcessorPool, root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) config.abc.ABCConfigFile[Any]¶
初始化一个受SL处理器支持的配置文件
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 返回:
配置对象
- 返回类型:
在 0.2.0 版本加入.
- load(processor_pool: config.abc.ABCSLProcessorPool, root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) config.abc.ABCConfigFile[Any]¶
加载处理器
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 返回:
配置对象
- 返回类型:
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 删除参数
config_file_cls添加参数
processor_pool
- load_file(config_pool: config.abc.ABCConfigPool, namespace: str, file_name: str, *args: Any, **kwargs: Any) config.abc.ABCConfigFile[Any]¶
加载指定命名空间的配置
- 参数:
config_pool (ABCConfigPool) -- 配置池
namespace (str) -- 命名空间
file_name (str) -- 文件名
- 返回:
配置文件
- 返回类型:
ABCConfigFile[Any]
小心
传递SL处理前没有清理已经缓存在配置池里的配置文件,返回的可能不是最新数据
- register_to(config_pool: config.abc.ABCSLProcessorPool | None = None) Self¶
注册到配置池中
- 参数:
config_pool (ABCSLProcessorPool | None) -- 配置池
- 返回:
返回当前实例便于链式调用
- 返回类型:
Self
在 0.3.0 版本发生变更: 返回当前实例便于链式调用
- save(processor_pool: config.abc.ABCSLProcessorPool, config_file: config.abc.ABCConfigFile[Any], root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) None¶
保存处理器
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
config_file (ABCConfigFile) -- 待保存配置
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 添加参数
processor_pool
- save_file(config_pool: config.abc.ABCConfigPool, config_file: config.abc.ABCConfigFile[Any], namespace: str, file_name: str, *args: Any, **kwargs: Any) None¶
保存指定命名空间的配置
- 参数:
config_pool (ABCConfigPool) -- 配置池
config_file (ABCConfigFile) -- 配置文件
namespace (str) -- 命名空间
file_name (str) -- 文件名
- __slots__ = ()¶
- create_dir = True¶
- raises¶
- property supported_file_classes: list[type[ABCConfigFile[Any]]]¶
- Abstractmethod:
- 返回:
支持的配置文件类
在 0.2.0 版本加入.
- property supported_file_patterns: tuple[str | re.Pattern[Any], Ellipsis]¶
- Abstractmethod:
- 返回:
支持的文件名匹配
在 0.2.0 版本发生变更: 重命名
file_ext为supported_file_patterns
- class BasicCompressedConfigSL(*, reg_alias: str | None = None, create_dir: bool = True)¶
Bases:
BasicCachedConfigSL,abc.ABC基础压缩配置文件SL处理器
在 0.2.0 版本加入.
- after_save(config_pool: config.abc.ABCConfigPool, config_file: config.abc.ABCConfigFile[Any], file_path: str, root_path: str, namespace: str, file_name: str) None¶
保存后处理
- 参数:
config_pool (ABCConfigPool) -- 配置池
config_file (ABCConfigFile[Any]) -- 配置文件
file_path (str) -- 文件路径
root_path (str) -- 根路径
namespace (str) -- 命名空间
file_name (str) -- 文件名
- before_load(config_pool: config.abc.ABCConfigPool, file_path: str, root_path: str, namespace: str, file_name: str) None¶
加载前处理
- 参数:
config_pool (ABCConfigPool) -- 配置池
file_path (str) -- 文件路径
root_path (str) -- 根路径
namespace (str) -- 命名空间
file_name (str) -- 文件名
- filename_formatter(file_name: str) str¶
格式化文件名以传递给其他SL处理器
- 默认实现:
如果为
str且file_name.endswith成立则返回移除后缀后的结果如果为
re.Pattern且Pattern.fullmatch(file_name)成立则返回Pattern.sub(file_name, "")直接返回
- initialize(processor_pool: config.abc.ABCSLProcessorPool, root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) config.abc.ABCConfigFile[Any]¶
初始化一个受SL处理器支持的配置文件
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 返回:
配置对象
- 返回类型:
在 0.2.0 版本加入.
- load(processor_pool: config.abc.ABCSLProcessorPool, root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) config.abc.ABCConfigFile[Any]¶
加载处理器
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 返回:
配置对象
- 返回类型:
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 删除参数
config_file_cls添加参数
processor_pool
- load_file(config_pool: config.abc.ABCConfigPool, namespace: str, file_name: str, *args: Any, **kwargs: Any) config.abc.ABCConfigFile[Any]¶
加载指定命名空间的配置
- 参数:
config_pool (ABCConfigPool) -- 配置池
namespace (str) -- 命名空间
file_name (str) -- 文件名
- 返回:
配置文件
- 返回类型:
ABCConfigFile[Any]
小心
传递SL处理前没有清理已经缓存在配置池里的配置文件,返回的可能不是最新数据
- register_to(config_pool: config.abc.ABCSLProcessorPool | None = None) Self¶
注册到配置池中
- 参数:
config_pool (ABCSLProcessorPool | None) -- 配置池
- 返回:
返回当前实例便于链式调用
- 返回类型:
Self
在 0.3.0 版本发生变更: 返回当前实例便于链式调用
- save(processor_pool: config.abc.ABCSLProcessorPool, config_file: config.abc.ABCConfigFile[Any], root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) None¶
保存处理器
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
config_file (ABCConfigFile) -- 待保存配置
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 添加参数
processor_pool
- save_file(config_pool: config.abc.ABCConfigPool, config_file: config.abc.ABCConfigFile[Any], namespace: str, file_name: str, *args: Any, **kwargs: Any) None¶
保存指定命名空间的配置
- 参数:
config_pool (ABCConfigPool) -- 配置池
config_file (ABCConfigFile) -- 配置文件
namespace (str) -- 命名空间
file_name (str) -- 文件名
- __slots__ = ()¶
- create_dir = True¶
- raises¶
- property supported_file_classes: list[type[ABCConfigFile[Any]]]¶
- Abstractmethod:
- 返回:
支持的配置文件类
在 0.2.0 版本加入.
- property supported_file_patterns: tuple[str | re.Pattern[Any], Ellipsis]¶
- Abstractmethod:
- 返回:
支持的文件名匹配
在 0.2.0 版本发生变更: 重命名
file_ext为supported_file_patterns
- class BasicConfigSL(*, reg_alias: str | None = None)¶
Bases:
config.abc.ABCConfigSL,abc.ABC基础配置SL管理器 提供了一些实用功能
在 0.2.0 版本发生变更: 重命名
BaseConfigSL为BasicConfigSL- 参数:
reg_alias (Optional[str]) -- sl处理器注册别名
- initialize(processor_pool: config.abc.ABCSLProcessorPool, root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) config.abc.ABCConfigFile[Any]¶
初始化一个受SL处理器支持的配置文件
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 返回:
配置对象
- 返回类型:
在 0.2.0 版本加入.
- abstractmethod load(processor_pool: ABCSLProcessorPool, root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) ABCConfigFile[Any]¶
加载处理器
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 返回:
配置对象
- 返回类型:
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 删除参数
config_file_cls添加参数
processor_pool
- register_to(config_pool: config.abc.ABCSLProcessorPool | None = None) Self¶
注册到配置池中
- 参数:
config_pool (ABCSLProcessorPool | None) -- 配置池
- 返回:
返回当前实例便于链式调用
- 返回类型:
Self
在 0.3.0 版本发生变更: 返回当前实例便于链式调用
- abstractmethod save(processor_pool: ABCSLProcessorPool, config_file: ABCConfigFile[Any], root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) None¶
保存处理器
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
config_file (ABCConfigFile) -- 待保存配置
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 添加参数
processor_pool
- __slots__ = ()¶
- property supported_file_classes: list[type[ABCConfigFile[Any]]]¶
- Abstractmethod:
- 返回:
支持的配置文件类
在 0.2.0 版本加入.
- property supported_file_patterns: tuple[str | re.Pattern[Any], Ellipsis]¶
- Abstractmethod:
- 返回:
支持的文件名匹配
在 0.2.0 版本发生变更: 重命名
file_ext为supported_file_patterns
- class BasicLocalFileConfigSL(s_arg: config.abc.SLArgumentType = None, l_arg: config.abc.SLArgumentType = None, *, reg_alias: str | None = None, create_dir: bool = True)¶
Bases:
BasicConfigSL,abc.ABC基础本地配置文件SL处理器
在 0.2.0 版本发生变更: 重命名从
BaseLocalFileConfigSL为BasicLocalFileConfigSL- 参数:
s_arg (SLArgumentType) -- 保存器默认参数
l_arg (SLArgumentType) -- 加载器默认参数
reg_alias (Optional[str]) -- sl处理器注册别名
create_dir (bool) -- 是否允许创建目录
在 0.2.0 版本发生变更: 将
保存加载器参数相关从BasicConfigSL移动到此类- initialize(processor_pool: config.abc.ABCSLProcessorPool, root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) config.abc.ABCConfigFile[Any]¶
初始化一个受SL处理器支持的配置文件
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 返回:
配置对象
- 返回类型:
在 0.2.0 版本加入.
- load(processor_pool: config.abc.ABCSLProcessorPool, root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) config.abc.ABCConfigFile[Any]¶
加载处理器
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 返回:
配置对象
- 返回类型:
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 现在操作是原子的(操作过程发生异常会回滚操作)
现在操作是理论上是多线/进程安全的
删除参数
config_file_cls添加参数
processor_pool
- abstractmethod load_file(source_file: Any, *merged_args: Any, **merged_kwargs: Any) config.abc.ABCConfigFile[Any]¶
从文件加载配置
- 参数:
source_file (Any) -- 源文件对象
merged_args -- 合并后的位置参数
merged_kwargs -- 合并后的关键字参数
- 返回:
本地配置文件对象
- 返回类型:
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 删除参数
config_file_cls更改
source_file参数类型为Any
- register_to(config_pool: config.abc.ABCSLProcessorPool | None = None) Self¶
注册到配置池中
- 参数:
config_pool (ABCSLProcessorPool | None) -- 配置池
- 返回:
返回当前实例便于链式调用
- 返回类型:
Self
在 0.3.0 版本发生变更: 返回当前实例便于链式调用
- save(processor_pool: config.abc.ABCSLProcessorPool, config_file: config.abc.ABCConfigFile[Any], root_path: str, namespace: str, file_name: str, *args: Any, **kwargs: Any) None¶
保存处理器 (原子操作 多线/进程安全)
- 参数:
processor_pool (ABCSLProcessorPool) -- 配置池
config_file (ABCConfigFile) -- 待保存配置
root_path (str) -- 保存的根目录
namespace (str) -- 配置的命名空间
file_name (str) -- 配置文件名
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 现在操作是原子的(操作过程发生异常会回滚操作)
现在操作是理论上是多线/进程安全的
添加参数
processor_pool
- abstractmethod save_file(config_file: config.abc.ABCConfigFile[Any], target_file: Any, *merged_args: Any, **merged_kwargs: Any) None¶
将配置保存到文件
- 参数:
config_file (ABCConfigFile) -- 配置文件
target_file (Any) -- 目标文件对象
merged_args -- 合并后的位置参数
merged_kwargs -- 合并后的关键字参数
- 抛出:
FailedProcessConfigFileError -- 处理配置文件失败
在 0.2.0 版本发生变更: 更改
target_file参数类型为Any
- __slots__ = ()¶
- _loader_args: config.utils.FrozenArguments¶
- _saver_args: config.utils.FrozenArguments¶
- create_dir = True¶
- property loader_args: config.utils.FrozenArguments¶
加载器默认参数
- raises¶
- property saver_args: config.utils.FrozenArguments¶
保存器默认参数
- property supported_file_classes: list[type[ABCConfigFile[Any]]]¶
- Abstractmethod:
- 返回:
支持的配置文件类
在 0.2.0 版本加入.
- property supported_file_patterns: tuple[str | re.Pattern[Any], Ellipsis]¶
- Abstractmethod:
- 返回:
支持的文件名匹配
在 0.2.0 版本发生变更: 重命名
file_ext为supported_file_patterns
- class ConfigPool¶
Bases:
config.basic.core.BasicConfigPool配置池
- require(namespace: str, file_name: str, validator: Any, validator_factory: Any = ValidatorTypes.DEFAULT, static_config: Any | None = None, **kwargs: Any) ConfigRequirementDecorator¶
获取配置
- 参数:
namespace (str) -- 命名空间
file_name (str) -- 文件名
validator -- 详见
RequiredPathvalidator_factory -- 详见
RequiredPathstatic_config -- 详见
RequiredPathkwargs -- 详见
ConfigRequirementDecorator
- 返回:
- 返回类型:
在 0.2.0 版本发生变更: 删除声明于
ABCConfigPool
- class ConfigRequirementDecorator(config_pool: config.abc.ABCConfigPool, namespace: str, file_name: str, required: RequiredPath[Any, ConfigRequirementDecorator.__init__.D], *, validate_only: bool = True, config_formats: str | collections.abc.Iterable[str] | None = None, allow_initialize: bool = True, config_cacher: collections.abc.Callable[[collections.abc.Callable[Ellipsis, ConfigRequirementDecorator.__init__.D], VarArg(), KwArg()], ConfigRequirementDecorator.__init__.D] | None = None, filter_kwargs: dict[str, Any] | None = None)¶
配置获取器,可作装饰器使用
在 0.2.0 版本发生变更: 重命名
RequireConfigDecorator为ConfigRequirementDecorator在 0.3.0 版本发生变更: 修正配置加载逻辑,现在会在每一次获取配置数据时尝试加载而不是仅在初始化时尝试加载
- 参数:
config_pool (ConfigPool) -- 所在的配置池
namespace -- 详见
ConfigPool.load()file_name -- 详见
ConfigPool.load()required (RequiredPath[Any, D]) -- 需求的键
validate_only (bool) -- 如果为
True则返回配置池中完整的配置数据(按引用传递),否则返回验证后的数据(按值传递), 此选项有助于避免意外修改配置池中的数据config_formats -- 详见
ConfigPool.load()allow_initialize -- 详见
ConfigPool.load()config_cacher (Callable[[Callable[..., D], VarArg(), KwArg()], D] | None) -- 缓存配置的装饰器,默认为None,即不缓存
filter_kwargs (dict[str, Any] | None) --
RequiredPath.filter()要绑定的默认参数,这会导致static_config失效
- 抛出:
UnsupportedConfigFormatError -- 不支持的配置格式
在 0.2.0 版本发生变更: 重命名参数
cache_config为config_cacher重命名参数
allow_create为allow_initialize在 0.3.0 版本加入: 添加参数
validate_only- __call__(func: collections.abc.Callable[[config.abc.ABCConfigData, Any], Any]) collections.abc.Callable[Ellipsis, Any]¶
通过装饰器提供配置数据注入,配置数据将会注入到
self(如果为方法而不是函数)后的第一个参数- 参数:
func (Callable[[ABCConfigData, Any], Any]) -- 需要装饰的函数
- 返回:
装饰后的函数
- 返回类型:
Callable[..., Any]
- _wrapped_filter(**kwargs: Any) config.abc.ABCConfigData¶
- check(*, ignore_cache: bool = False, **filter_kwargs: Any) Any¶
手动检查配置
- 参数:
ignore_cache (bool) -- 是否忽略缓存
filter_kwargs -- RequiredConfig.filter的参数
- 返回:
得到的配置数据
- 返回类型:
Any
- _config_cacher: collections.abc.Callable[[collections.abc.Callable[Ellipsis, ConfigRequirementDecorator.__init__.D], VarArg(), KwArg()], ConfigRequirementDecorator.__init__.D]¶
- _config_loader: collections.abc.Callable[[], config.abc.ABCConfigFile[ConfigRequirementDecorator.__init__.D]]¶
- _filter_kwargs = None¶
- _required¶
- _validate_only = True¶
- class RequiredPath(validator: V, validator_factory: ValidatorFactoryType[V, D] | config.validators.ValidatorTypes | 'custom' | 'pydantic' | 'component' | None = ValidatorTypes.DEFAULT, static_config: config.validators.ValidatorOptions | None = None)¶
对需求的键进行存在检查、类型检查、填充默认值
- 参数:
validator (Any) -- 数据验证器
validator_factory (ValidatorFactoryType[V, D] | validators.ValidatorTypes | Literal["custom", "pydantic", "component"] | None) -- 数据验证器工厂
static_config (ValidatorOptions | None) -- 静态配置
小技巧
提供
static_config参数可以避免在filter()中反复调用validator_factory以提高性能 (filter()未传入验证器选项参数时优化生效,如果传入了则回退到默认行为)- filter(data: D | config.utils.Ref[D], *, allow_modify: bool | None = None, skip_missing: bool | None = None, **extra: Any) D¶
检查过滤需求的键
- 参数:
- 返回:
处理后的配置数据*快照*
- 返回类型:
D
- 抛出:
ConfigDataTypeError -- 配置数据类型错误
RequiredPathNotFoundError -- 必要的键未找到
UnknownErrorDuringValidateError -- 验证过程中发生未知错误
注意
返回的配置数据是 *快照*
小心
提供了任意配置参数(
allow_modify,skip_missing, ...)时,这次调用将完全舍弃 static_config 使用当前提供的配置参数这会导致调用 validator_factory 产生额外开销(如果你提供 static_config 参数是为了避免反复调用 validator_factory 的话)
在 0.2.0 版本发生变更: 重命名参数
ignore_missing为skip_missingdata参数支持Ref
- ValidatorFactories: ClassVar[dict[config.validators.ValidatorTypes, ValidatorFactoryType[Any, Any]]]¶
验证器工厂注册表
在 0.2.0 版本发生变更: 现在待验证的配置数据必须由
Ref包装后传入
- _validator¶
- _validator_factory: ValidatorFactoryType[V, D]¶
- raises(excs: type[Exception] | tuple[type[Exception], Ellipsis] = Exception) collections.abc.Generator[None, Any, None]¶
包装意料内的异常
提供给子类的便捷方法
- 参数:
excs (type[Exception] | tuple[type[Exception], ...]) -- 意料内的异常
- 抛出:
FailedProcessConfigFileError -- 当触发了对应的异常时
在 0.1.4 版本加入.
在 0.2.0 版本发生变更: 提取为函数
- DefaultConfigPool¶
默认配置池
- get¶
DefaultConfigPool.get()
- load¶
DefaultConfigPool.load()
- requireConfig¶
- save¶
DefaultConfigPool.save()
- saveAll¶
DefaultConfigPool.save_all()
- set_¶
DefaultConfigPool.set()