certflow.config package

Submodules

Module contents

配置模块

提供配置加载和管理功能,是CertFlow配置系统的入口模块.

该模块导出配置加载器和配置模型的主要类,方便其他模块统一导入使用.

Exports:

ConfigLoader: 配置加载器类,负责加载YAML配置文件、处理环境变量和文件包含 RootConfig: 根配置模型类,定义所有配置项的数据结构和验证规则 get_config: 全局配置单例获取函数,提供便捷的配置访问接口

示例

>>> from certflow.config import get_config, ConfigLoader
>>>
>>> # 方式1: 使用全局单例
>>> config = get_config()
>>> print(config.app.name)
"CertFlow"
>>>
>>> # 方式2: 创建独立加载器实例
>>> loader = ConfigLoader()
>>> config = loader.load("custom_config.yaml")
class certflow.config.ConfigLoader(config_dir=None)[源代码]

基类:object

配置加载器,负责加载和处理YAML配置文件

提供完整的配置文件加载功能,包括:
  • 自动适配开发环境和打包环境

  • 环境变量替换 (${ENV_VAR:default})

  • 配置文件包含 (!include)

  • 配置验证 (Pydantic)

参数:

config_dir (Path | None)

config_dir

配置文件所在目录的路径

Type:

Path

_config

内部存储的配置对象

Type:

RootConfig | None

示例

>>> # 基本使用
>>> loader = ConfigLoader()
>>> config = loader.load()  # 自动查找配置文件
>>> print(config.app.name)
"CertFlow"
>>>
>>> # 指定配置目录
>>> loader = ConfigLoader(Path("/path/to/config"))
>>> config = loader.load("custom_config.yaml")
load(config_file='config.yaml')[源代码]

加载配置文件并进行处理

执行完整的配置加载流程:
  1. 查找配置文件

  2. 读取文件内容

  3. 展开顶层 !include 指令

  4. 解析 YAML

  5. 处理嵌套的 !include 标签

  6. 替换环境变量

  7. 验证配置结构

参数:

config_file (str) -- 配置文件名,默认为 "config.yaml"

返回:

经过验证的配置对象

返回类型:

RootConfig

抛出:
  • FileNotFoundError -- 指定的配置文件不存在

  • yaml.YAMLError -- YAML语法错误或格式不正确

  • ValidationError -- 配置内容不符合定义的数据模型

示例

>>> loader = ConfigLoader()
>>> config = loader.load()  # 加载默认配置
>>> print(config.app.name)
"CertFlow"
>>>
>>> # 加载自定义配置文件
>>> config = loader.load("custom_config.yaml")
reload(config_file='config.yaml')[源代码]

重新加载配置文件

清除缓存配置并重新加载,用于运行时配置更新.

参数:

config_file (str) -- 配置文件名

返回:

重新加载后的配置对象

返回类型:

RootConfig

示例

>>> loader = ConfigLoader()
>>> config = loader.load()  # 首次加载
>>> # ... 配置文件被修改 ...
>>> config = loader.reload()  # 重新加载
property config: RootConfig

获取已加载的配置对象

必须先调用 load() 方法,否则会抛出运行时错误.

返回:

已加载的配置对象

返回类型:

RootConfig

抛出:

RuntimeError -- 配置尚未加载

示例

>>> loader = ConfigLoader()
>>> loader.load()
>>> config = loader.config  # 获取配置
class certflow.config.RootConfig(*, app=<factory>, paths=<factory>, sales_plan=<factory>, certificate=<factory>, import_deduplication=<factory>, database=<factory>, logging=<factory>, ui=<factory>, color_mapping=<factory>, numbering=None, duplicate_check=None, unique_key=None, priority_rules=None, export=None, sales_plan_fill=None, field_fallbacks=None, field_fill_rules=None, sales_importer=None, qualified_list=None, import_mapping=None, export_debug=None, test_report=None, report=<factory>, printer=None, environment=None, colors=None, field_templates=None, print_templates=None, vba_import=None, completed_orders_backfill=None, query_page=<factory>, main_window=<factory>, window=<factory>, title_bar=<factory>, nav_buttons=<factory>, correction_queue=<factory>, welcome_page=<factory>, import_page=<factory>, log_page=<factory>, status_bar=<factory>, styles=<factory>, grouping=None, sorting=None, id_generator=None, production_status=None, import_config=None, import_placeholder=<factory>, bom_import=<factory>, sync=None, print=None, background=None, print_log_empty_markers=None, output=None, dictionary=<factory>, lookup_tables=<factory>, quarantine=<factory>, sort_rules=<factory>)[源代码]

基类:_StrictModel

根配置模型

所有配置的顶层容器,聚合所有子配置模块. 顶层已闭环为 extra='forbid'(任务 2.3 / §六 UI 域严格校验),未建模键触发 ValidationError,杜绝顶层键拼写错误/死配置静默失效;嵌套 UI 段结构由 UIConfigModel 顶层闸门二次把关(见 ui_config.py)。

参数:
app

应用基础配置

Type:

certflow.config.models.AppConfig

paths

路径配置

Type:

certflow.config.models.PathsConfig

sales_plan

销售计划配置

Type:

certflow.config.models.SalesPlanConfig

certificate

合格证配置

Type:

certflow.config.models.CertificateConfig

import_deduplication

导入去重配置

Type:

certflow.config.models.ImportDeduplicationConfig

database

数据库配置

Type:

certflow.config.models.DatabaseConfig

logging

日志配置

Type:

certflow.config.models.LoggingConfig

ui

UI配置

Type:

certflow.config.models.UIConfig

color_mapping

颜色映射配置

Type:

certflow.config.models.ColorMappingConfig

numbering

编号规则配置(V2)

Type:

certflow.config.models.NumberingConfig | None

duplicate_check

重复检查配置(V2)

Type:

certflow.config.models.DuplicateCheckConfig | None

unique_key

唯一键配置(V2)

Type:

certflow.config.models.UniqueKeyConfig | None

priority_rules

优先级规则配置(V2)

Type:

certflow.config.models.PriorityRulesConfig | None

export

导出配置(V2)

Type:

certflow.config.models.ExportConfig | None

sales_plan_fill

销售计划填充配置(V2)

Type:

certflow.config.models.SalesPlanFillConfig | None

field_fallbacks

字段回退链配置(V2)

Type:

dict[str, list[str]] | None

field_fill_rules

字段填充规则配置(V2)

Type:

dict[str, list[certflow.config.models.FieldFillRule]] | None

sales_importer

销售计划导入器配置(V2)

Type:

certflow.config.models.SalesImporterConfig | None

import_mapping

导入列映射配置(V2)

Type:

certflow.config.models.ImportMappingConfig | None

export_debug

导出调试配置(V2)

Type:

certflow.config.models.ExportDebugConfig | None

test_report

试压报告配置(V2)

Type:

certflow.config.models.TestReportConfig | None

printer

打印机配置(V2)

Type:

certflow.config.models.PrinterConfig | None

environment

环境信息配置(V2)

Type:

certflow.config.models.EnvironmentConfig | None

colors

颜色常量配置(V2)

Type:

certflow.config.models.ColorsConfig | None

vba_import

VBA导入配置(V2)

Type:

certflow.config.models.VBAImportConfig | None

id_generator

唯一键生成策略配置

Type:

certflow.config.models.IdGeneratorConfig | None

production_status

生产状态配置

Type:

certflow.config.models.ProductionStatusConfig | None

import_config

导入配置(与 QueryView 风格一致)

Type:

certflow.config.models.ImportConfig | None

sync

数据库同步配置(WebDAV)

Type:

certflow.config.models.SyncConfig | None

print

打印引擎配置

Type:

certflow.config.models.PrintConfig | None

background

底板预览配置

Type:

certflow.config.models.BackgroundConfig | None

print_log_empty_markers

打印日志空值默认标记配置

Type:

certflow.config.models.PrintLogEmptyMarkersConfig | None

model_config = {'extra': 'forbid'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

app: AppConfig
paths: PathsConfig
sales_plan: SalesPlanConfig
certificate: CertificateConfig
import_deduplication: ImportDeduplicationConfig
database: DatabaseConfig
logging: LoggingConfig
ui: UIConfig
color_mapping: ColorMappingConfig
numbering: NumberingConfig | None
duplicate_check: DuplicateCheckConfig | None
unique_key: UniqueKeyConfig | None
priority_rules: PriorityRulesConfig | None
export: ExportConfig | None
sales_plan_fill: SalesPlanFillConfig | None
field_fallbacks: dict[str, list[str]] | None
field_fill_rules: dict[str, list[FieldFillRule]] | None
sales_importer: SalesImporterConfig | None
qualified_list: dict[str, Any] | None
import_mapping: ImportMappingConfig | None
export_debug: ExportDebugConfig | None
test_report: TestReportConfig | None
report: dict[str, Any]
printer: PrinterConfig | None
environment: EnvironmentConfig | None
colors: ColorsConfig | None
field_templates: dict[str, Any] | None
print_templates: dict[str, Any] | None
vba_import: VBAImportConfig | None
completed_orders_backfill: CompletedOrdersBackfillConfig | None
query_page: dict[str, Any]
main_window: dict[str, Any]
window: dict[str, Any]
title_bar: dict[str, Any]
nav_buttons: list[Any]
correction_queue: dict[str, Any]
welcome_page: dict[str, Any]
import_page: dict[str, Any]
log_page: dict[str, Any]
status_bar: dict[str, Any]
styles: dict[str, Any]
grouping: GroupingConfig | None
sorting: SortingConfig | None
id_generator: IdGeneratorConfig | None
production_status: ProductionStatusConfig | None
import_config: ImportConfig | None
import_placeholder: ImportPlaceholderConfig | None
bom_import: BomImportConfig | None
sync: SyncConfig | None
print: PrintConfig | None
background: BackgroundConfig | None
print_log_empty_markers: PrintLogEmptyMarkersConfig | None
output: OutputConfig | None
dictionary: DictionaryConfig
lookup_tables: LookupTablesConfig
quarantine: QuarantineConfig
sort_rules: SortRulesConfig
classmethod validate_log_level(v)[源代码]

验证日志级别是否有效

参数:

v (LoggingConfig) -- 待验证的日志配置对象

返回:

验证通过的日志配置对象

返回类型:

LoggingConfig

抛出:

ValueError -- 当日志级别不在有效值范围内时抛出

certflow.config.get_config(config_dir=None, reload=False)[源代码]

获取全局配置实例(单例模式)

提供便捷的全局配置访问接口,避免在多处重复创建 ConfigLoader 实例. 首次调用时会自动加载配置,后续调用返回缓存的配置对象.

参数:
  • config_dir (Path | None) -- 配置文件所在目录路径,首次调用时可指定,后续调用此参数无效

  • reload (bool) -- 是否强制重新加载配置,设为 True 时会重新读取配置文件

返回:

全局配置对象实例

返回类型:

RootConfig

示例

>>> from certflow.config.loader import get_config
>>>
>>> # 首次调用,自动加载配置
>>> config = get_config()
>>> print(config.app.name)
"CertFlow"
>>>
>>> # 使用缓存配置
>>> db_config = get_config().database
>>>
>>> # 强制重新加载
>>> new_config = get_config(reload=True)