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 配置路径

平台 路径
Linux ~/.config/{filename}
macOS ~/.config/{filename}
Windows %APPDATA%\{filename}

可通过环境变量覆盖:

  • Linux/macOS: XDG_CONFIG_HOME
  • Windows: APPDATA

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.3.tar.gz (9.6 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.3-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mrconfig-0.1.3.tar.gz
  • Upload date:
  • Size: 9.6 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.3.tar.gz
Algorithm Hash digest
SHA256 13ddf060acc7968ac59eccd64166e3f325365e3bd42107c65ec54f6cb1391d01
MD5 213e555ae5c726ff5e5b48c5e8020037
BLAKE2b-256 c5bedd07430f2945610d829ca7135760cdea41b09cde5be34a18ef929eac7b66

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mrconfig-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.0 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 10e954dab2690a7ce474a1777a4e643c3c411b92f814d9b73d05f51926e76c47
MD5 50f7282680c04682ac9c5c1079a8b5e1
BLAKE2b-256 6f1e21eb20e6a658f26071e5fab68b71551e4952d6d88cfeb934a30034cdead6

See more details on using hashes here.

Provenance

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