跳转到主要内容

将TOML文件和命令行参数转换为配置数据类

项目描述

xconf - 命令配置的数据类和TOML

演示

xconf_demo.py中可以看到如何使用xconf.Commandxconf.fieldxconf.config的示例。运行它以查看其配置键。

$ python xconf_demo.py demo_command -h
demo_command: Demo command
usage: xconf_demo.py demo_command [-c CONFIG_FILE] [-h] [-v] [--dump-config] [vars ...]

positional arguments:
  vars                  Config variables set with 'key.key.key=value' notation

optional arguments:
  -c CONFIG_FILE, --config-file CONFIG_FILE
                        Path to config file (default: demo_command.conf)
  -h, --help            Print usage information
  -v, --verbose         Enable debug logging
  --dump-config         Dump final configuration state as TOML and exit

configuration keys:
  collections
      dict[str, ExtendedThingie]
  collections.<str>.name
      str
     <ExtendedThingie>
  collections.<str>.extended
      bool
     <ExtendedThingie>
  either_one
      [int, str]
  should_bar
      bool
     (default: False)
  should_foo
      bool
    Whether demo should foo
  number_list
      list[int]
    List of favorite numbers
  float_list
      list[float]
    List of favorite floating-point numbers
  str_list
      list[str]
    List of favorite strings
  sequence
      list[ExtendedThingie]
     (default: [])
  sequence[#].name
      str
     <ExtendedThingie>
  sequence[#].extended
      bool
     <ExtendedThingie>

默认配置文件

命令名称demo_command是从类名生成的,并用于在当前目录中查找默认配置文件(demo_command.conf)。

在命令行中提供参数

帮助输出中的任何配置键都可以以dotted.name=value格式在命令行中提供。

对于原始类型列表(strintfloat),您只需使用逗号分隔等于号右侧的值。例如:number_list=1,2,3

要覆盖列表中的单个条目,请使用some_name[#]dotted[#].name=value,其中#是一个整数索引。例如:number_list[0]=99

字符串值是裸的(即没有围绕value的引号)。布尔值不区分大小写,为truet或1表示真,为falsef或0表示假。

构建命令

请参阅xconf_demo.py以获取示例。请注意,命令必须继承自xconf.Command并应用@xconf.config装饰器。选项由数据类的层次结构定义。(出于一些不重要的原因,它们不是严格意义上的import dataclass数据类。)

许可证

xconf/vendor/之外的所有代码均根据MIT许可证提供,但xconf_demo.pydemo_command.conf除外,它们被发布到公有领域,以便您构建。

请注意,xconf/vendor/下的代码根据那里列出的许可证使用。

项目详情


下载文件

下载适用于您平台文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。

源分发

xconf-0.0.2.tar.gz (32.6 kB 查看散列)

上传时间

构建分发

xconf-0.0.2-py3-none-any.whl (34.1 kB 查看散列)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面