Accessing and Modifying Configuration Data in Python
This project has been renamed iniparse
to avoid being confused with another project
of the same
name on SourceForge. Its new home is:
http://code.google.com/p/iniparse/
cfgparse
is a Python module that provides mechanisms for
managing configuration information. It is backward compatible with
ConfigParser,
in addition to having the following features:
cfg[key]
).ConfigParser
, RawConfigParser
, and
SafeConfigParser
are included that are API-compatible with
the Python standard library. They pass all the unit tests in
Python-2.3.4.Latest version: cfgparse-0.1.tar.gz
Here is an illustration of typical usage:
>>> from cfgparser.iniparser import ini_namespace >>> cfg = ini_namespace(file('options.ini'))Or, alternatively:
>>> from cfgparser.compat import ConfigParser >>> cfgpr = ConfigParser() >>> cfgpr.read('options.ini')
>>> print cfg.playlist.expand_playlist True >>> print cfg.ui.width 150 >>> cfg.ui.width = 200 >>> print cfg['ui']['width'] # alternative access method 200If you are using
cfgparser.compat.ConfigParser
, you can continue
using get/set methods to access/update values. Alternatively, you can access
the underlying namespace
object using cfgpr.data
:
>>> print cfgpr.data.playlist.expand_playlist True >>> print cfgpr.data.ui.width 150 >>> cfgpr.data.ui.width = 200 >>> cfgpr.set('ui', 'width', 175) # can also use get/set methods >>> print cfgpr.get('ui', 'width') # for compatiability 175
>>> print cfg [playlist] expand_playlist = True [ui] display_clock = True display_qlength = True width = 150
>>> from cfgparser.config import basic_namespace >>> n = basic_namespace() >>> n.x = 7 >>> n.name.first = 'paramjit' >>> n.name.last = 'oberoi' >>> n.x 7 >>> n.name.first 'paramjit' >>> print n name.first = paramjit name.last = oberoi x = 7Convert to INI:
>>> from cfgparser.iniparser import ini_namespace >>> i = ini_namespace() >>> del n.x # since INI doesn't support top-level values >>> i.import_namespace(n) >>> print i [name] first = paramjit last = oberoi
For more information, see the automatically generated API documentation. Please send your bug reports, suggestions, comments, etc., to Paramjit Oberoi <param@cs.wisc.edu>.