INI-config validator
configchecker的Python项目详细描述
此模块有助于检查configparser加载的配置。
用法
- 初始化有效配置的说明: schema = configchecker.ConfigSchema()
- 通过使用节名称验证器和布尔标志“section required”调用schema.section,添加可能节的信息。
- 在每个小节中,通过调用sect.value来描述可能的小节值。
名称/值验证器
有基本的验证器:
- ItemDefaultValidator-始终返回true
- ItemStringValidator-检查字符串是否等于给定值(可能不区分大小写)
- ItemRegexValidator-检查字符串与给定regexp的匹配情况
- ItemNumberValidator-检查字符串是否为非负整数
以及允许创建更复杂检查的验证程序生成器:
- ItemNotValidator,ItemAndValidator,ItemOrValidator-验证器上的一阶逻辑
- ItemCountValidator-接受一个验证器和一个函数,该函数检查验证器的真正数(即返回True)
示例
importconfigparserimportconfigcheckerasvconfig=configparser.ConfigParser()config.read_file("config")schema=v.ConfigSchema()# Section with name „REQUIRED“ will be mandatorywithschema.section("REQUIRED")ass:# It must have keys matching regexp r'item_\d+' and numeric value and nothing mores.value(v.ItemRegexValidator(r'item_\d+',value_val=v.ItemNumberValidator()).no_other()# Section with name r'OPT_\w+' (check by regexp) will be optionalwithschema.section(v.ItemRegexValidator(r'OPT_\w+'),required=False)ass:# And it may have anythingpass# Other sections will be restrictedschema.no_other()# Run checksv.ConfigSchemaValidator(schema).validate(config)
此外,您可以在测试中找到许多示例(test_configchecker.py)