跳转到主要内容

管理多个库的配置

项目描述

Gifnoc

Gifnoc是一个Python模块的统一配置系统。

Gifnoc的主要目标是统一配置文件、环境变量和命令行选项,跨多个模块。例如,模块A和模块B都可以通过gifnoc定义它们自己的配置模型,将一些环境变量映射到该配置的键上,然后您可以在同一个文件中配置A和B。

Gifnoc还旨在通过基于dataclasses的强类型模型和由gifnoc依赖的apischema包实现的模型来验证配置。

功能

  • 使用dataclasses和apischema进行强类型配置
  • 使用单个配置树为多个库提供支持
  • 多个配置文件可以轻松合并
  • 可以轻松地将配置文件嵌入到彼此中

示例

main.py

from dataclasses import dataclass
import gifnoc

@dataclass
class User:
    name: str
    email: str
    admin: bool

@dataclass
class Server:
    port: int = 8080
    host: str = "localhost"
    users: list[User]

server_config = gifnoc.define(
    field="server",
    model=Server,
    environ={
        APP_PORT="port",
        APP_HOST="host",
    }
)

if __name__ == "__main__":
    with gifnoc.cli(
        # Environment variable for the configuration path (defaults to GIFNOC_FILE)
        envvar="APP_CONFIG",
        # Command-line argument for the configuration path (can give multiple)
        config_argument="--config",
        # You can easily register command-line arguments to parts of the configuration
        options_map={"--port": "server.port"},
    ):
        # The `server_config` object will always refer to the `server` key in the
        # current configuration
        print("Port:", server_config.port)

config.yaml

server:
  port: 1234
  host: here
  users:
    - name: Olivier
      email: ob@here
      admin: true
    # You can write a file path instead of an object
    - mysterio.yaml

mysterio.yaml

- name: Mysterio
  email: me@myster.io
  admin: false

用法

python main.py --config config.yaml
APP_CONFIG=config.yaml python main.py
APP_PORT=8903 python main.py --config config.yaml
python main.py --config config.yaml --port 8903

项目详情


下载文件

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

源分布

gifnoc-0.4.0.tar.gz (41.3 kB 查看哈希值)

上传时间 源代码

构建发行版

gifnoc-0.4.0-py3-none-any.whl (20.7 kB 查看哈希值)

上传时间 Python 3

由以下支持