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.5.tar.gz (9.9 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.5-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mrconfig-0.1.5.tar.gz
  • Upload date:
  • Size: 9.9 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.5.tar.gz
Algorithm Hash digest
SHA256 e34bf037d3b4247caa81ea463405f4e7c1d0ca3aa607c1d97af72fdd0eff19ad
MD5 12eb1a4fe0d21ac38d68687a78242b9a
BLAKE2b-256 5b664bc5bb04ca73171fb56398f78ace2d13ca73ed0bc2f64742ffbd85bd6963

See more details on using hashes here.

Provenance

The following attestation bundles were made for mrconfig-0.1.5.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.5-py3-none-any.whl.

File metadata

  • Download URL: mrconfig-0.1.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a3164929a6ed52511f5db50bd0fcd564c13d20e7a2a9eba2743727386f303a68
MD5 62da885a5ceea522be2cb69aca407229
BLAKE2b-256 e89831f22eac298439e69ceed6aae097d79f18ca773455994aa5285e3715d188

See more details on using hashes here.

Provenance

The following attestation bundles were made for mrconfig-0.1.5-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