Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python configuration library [closed]

I am looking for a python configuration library that merge multiple text configuration files into single object just like json.

has anybody know a good one?

like image 969
reza Avatar asked Nov 17 '12 17:11

reza


People also ask

How do you close a config file in Python?

close() closes the opened resource after its usage in any case. This is offered to you, already included in the box with ConfigParser.

Is Configparser part of Python?

ConfigParser is a Python class which implements a basic configuration language for Python programs. It provides a structure similar to Microsoft Windows INI files.

What is config () in Python?

A Python configuration file is a pure Python file that populates a configuration object. This configuration object is a Config instance.


1 Answers

I wrote the pymlconf for this purpose.the configuration syntax is yaml.

For example:

Config files:

#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
"""

Then look at single line usage:

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')

Fetching config entries:

# 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

It seems this covers your problem.but you can fork it on github

Links:

  1. Python package index
  2. Source on github
  3. Documentation
like image 92
pylover Avatar answered Sep 22 '22 22:09

pylover