管理多个库的配置
项目描述
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 查看哈希值)
关闭
gifnoc-0.4.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ed6554554f0bf470ffb57eb22a09676e5ddc8dab15e7afa48cf8889204e4e785 |
|
MD5 | 437fb026146a9649e74434f419d29b26 |
|
BLAKE2b-256 | db7e739f74524b3ddac3ef6cf26040d399171301c9d333dfb8c67cf2ca9466c3 |
关闭
gifnoc-0.4.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ab1d1715985dd1de7d08c430bf28a13b8d2ce187f3b89f42bc02ce4e54825fa8 |
|
MD5 | 51cb88261fb1ae84549810834fc16622 |
|
BLAKE2b-256 | 506b5e260af004d9f4eddaea12f6fcecda1e97421e75e93b4621d4df166e0db0 |