Python配置库

rez*_*eza 6 python configuration-files

我正在寻找一个python配置库,它将多个文本配置文件合并为单个对象,就像json一样.

谁知道一个好人?

pyl*_*ver 6

我为此编写了pymlconf.配置语法是yaml.

例如:

配置文件:

#app/conf/users/sites.mysite.conf:
name: mysite.com
owner:
   name: My Name
   phone: My Phone Number
   address: My Address


#app/conf/admin/root.conf:
server:
   version: 0.3a
sites:
   admin:
      name: admin.site.com
      owner:
         name: Admin Name
         phone: Admin Phone Number
         address: Admin Address

#app/conf/admin/server.conf:
host: 0.0.0.0
port: 80

#../other_path/../special.conf:
licence_file: /path/to/file
log_file: /path/to/file

#app/src/builtin_config.py:
_builtin_config={
   'server':{'name':'Power Server'}
}

OR:

_builtin_config="""
    server:
       name: Power Server
"""
Run Code Online (Sandbox Code Playgroud)

然后看单行用法:

from pymlconf import ConfigManager
from app.builtin_config import _builtin_config

config_root = ConfigManager(
   _builtin_config,
   ['app/conf/admin','app/conf/users'],
   '../other_path/../special.conf')
Run Code Online (Sandbox Code Playgroud)

获取配置条目:

# All from app/conf/users/sites.mysite.conf
print config_root.sites.mysite.name
print config_root.sites.mysite.owner.name
print config_root.sites.mysite.owner.address
print config_root.sites.mysite.owner.phone

# All from app/conf/admin/root.conf
print config_root.sites.admin.name
print config_root.sites.admin.owner.name
print config_root.sites.admin.owner.address
print config_root.sites.admin.owner.phone

print config_root.server.name       # from _builtin_config
print config_root.server.version    # from app/conf/admin/root.conf
print config_root.server.host       # from app/conf/admin/server.conf
print config_root.server.port       # from app/conf/admin/server.conf

print config_root.licence_file      # from ../other_path/../special.conf
print config_root.log_file          # from ../other_path/../special.conf
Run Code Online (Sandbox Code Playgroud)

这似乎涵盖了你的问题.但你可以在github上分叉它

链接:

  1. Python包索引
  2. 源于github
  3. 文档


Cal*_*ean 5

标准配置文件解析器是ConfigParser标准Python发行版的一部分.在这里阅读所有相关内容:http: //docs.python.org/2/library/configparser.html

它也支持多个文件.

好用且易于使用:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('example.cfg')

# Retrieve a variable
myvar = config.get("sectionName", "variableName", 0)
Run Code Online (Sandbox Code Playgroud)