Skip to main content

多路径配置文件加载器

Project description

mrconfig

多路径配置文件加载器,支持 JSON、YAML、TOML 格式

安装

pip install mrconfig

支持格式

格式 扩展名 说明
JSON .json 标准 JSON 格式
YAML .yaml, .yml YAML 格式
TOML .toml TOML 格式(Python 3.11+)

快速开始

方式一:应用模式(推荐)

from mrconfig import load_config

# 一步到位,自动搜索配置
config = load_config("my_app")

# 等价于搜索以下路径:
# 1. 环境变量 MY_APP_CONFIG 指定的路径
# 2. 当前目录 .my_app.yaml
# 3. XDG 配置目录下 my-app/config.yaml

方式二:传统模式

from mrconfig import ConfigLoader

loader = ConfigLoader(name=".myapp.yaml", xdg="app/config.yaml")
config = loader.load(default={"debug": False})

功能特性

路径搜索优先级

  1. 环境变量(最高优先级)- 通过 {APP}_CONFIG 环境变量指定
  2. 当前目录 - name 参数指定的文件
  3. XDG 配置目录 - 平台特定的标准配置路径

路径安全

  • ~ 自动展开为用户目录
  • 路径解析为绝对路径,防止路径遍历攻击
  • 符号链接自动解析

XDG 配置路径

~/.config/{filename}

可通过环境变量覆盖:

  • Linux/macOS/Windows: XDG_CONFIG_HOME

API 参考

ConfigLoader

from mrconfig import ConfigLoader

# 传统初始化
loader = ConfigLoader(
    name=".myapp.yaml",      # 当前目录配置文件
    xdg="app/config.yaml",  # XDG 目录下配置
    env="MYAPP_CONFIG",      # 环境变量(最高优先级)
)

# 应用模式快速创建
loader = ConfigLoader.from_app("my_app")

config = loader.load()

核心方法

方法 说明
load(default=None) 加载配置,返回字典或默认值
get_paths() 获取所有可能的配置路径(已去重)
get_active_path() 获取实际生效的配置文件路径
from_app(app, use_env=True, ext=".yaml") 从应用名称快速创建加载器

工具函数

from mrconfig import load_config, load_file, get_active_config_path

# 加载配置(支持应用模式)
config = load_config("my_app")
config = load_config(name=".myapp.yaml")

# 加载指定文件
config = load_file("/path/to/config.yaml")

# 获取生效的配置文件路径
path = get_active_config_path("my_app")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mrconfig-0.1.4.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mrconfig-0.1.4-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file mrconfig-0.1.4.tar.gz.

File metadata

  • Download URL: mrconfig-0.1.4.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mrconfig-0.1.4.tar.gz
Algorithm Hash digest
SHA256 2fd18fcaa686095e337eb4b09a03094f13644f18d07dfc24b1f3c898f947c9d4
MD5 ed75f5c17a37e565c842f6a3c96b501f
BLAKE2b-256 4a941a856a420cafdf42adb3560805d14109a313d4a479f67745781fe5b43dd5

See more details on using hashes here.

Provenance

The following attestation bundles were made for mrconfig-0.1.4.tar.gz:

Publisher: release.yml on ricsy/mrconfig

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mrconfig-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: mrconfig-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mrconfig-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ab42f6eea1ccd99d4c4b02bd070cd1251622706703244bd1a1bc9675fe7a11fa
MD5 2da61c994103d764092631aef517c3a4
BLAKE2b-256 36027f037dff86de03a2e02abca24819715753627f90e4944e10fb785752837e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mrconfig-0.1.4-py3-none-any.whl:

Publisher: release.yml on ricsy/mrconfig

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page