Skip to main content

An easy-to-use, powerful configuration module for Python

Project description

Copyright (c) 2013 Martin Ortbauer
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Description: A simple but still powerfull Configuration Parser
#################################################

.. image:: https://secure.travis-ci.org/mortbauer/configreader.png?branch=master
:target: https://secure.travis-ci.org/mortbauer/configreader

There are quite many Configuration Parsers for Python out there, but well, they
all kind of suck in there own way. Let me point out what I dislike:

* `configparser`_: which is the de facto standard, has it's ini like language, in
my opionion quite awful, difficult and limited

* Configuration through yaml: is beautiful, but how about intergration of
simple environment variables or interpolation of already defined values

* How about just execution a python module, very unsafe (but who cares), but it
also pollutes the namespace with all sort of builtins and whatsoever

So I was looking once again and found `python-config`_, which looked really
exactly what I wanted, but well, I had my problems with it, since I wanted to
have acces to stuff like `os.getenv('HOME')` and similar, which was not really
possible since it was forbidden to call a function, though you could still
evaluate stuff and so on. I looke into the source and thought well how can I
allow also calling functions? But I realized they are defining there own
parsing grammar, which seemed kind of reinventing the wheel since I already
knew the `ast`_ module.

Finally I wrote a very simple Configuration Parser which lets `ast`_ and
`operator`_ do most of the work. It has of course its own problems, and is hack
of half an hour so don't expect much. For me it is still usefull, and maybe we
can improve it.

Usage
*******
As a simple example of what is possible and how to use the module, see the
following config file which is by the way completely valid python::

home = os.getenv('HOME')
aster_root = home+"/data/opt/aster"
project = "bikeframe-test"
version = "testing"
# source directory for all files if relative path
srcdir = "."
# output directory for all stuff
outdir = "results"
# input mesh file
meshfile = "mesh.med"
logfile = "asterclient.log"
workdir = "/data/tmp"
# define the studies
calculations = [
{"name":"main",
"commandfile": "main.comm",
"resultfiles":{
"bikeframe.rmed":80,
"bikeframe.msh": 81,
"buckling.rmed": 82,
"bikeframe.table": 39,
"bikeframe.resu": 38,
"buckling.resu": 37,
},
"inputfiles":["parameters.py"],
},
{"name": "post",
"commandfile":"post.comm",
"poursuite": "main",
"resultfiles":{
"vmises.table": 40,
"protocol": "protocol*.rst",
}
}]

Suppose we have this saved as `profile.conf` we could read it like::

import os
from configreader import Config

c = Config(open('profile.conf','r'),namespace={'os.getenv':os.getenv})

which would give as all the values specified and evaluated as a python `dict`.
If we wouldn't have provided the namespace with `os.getenv` the parsing would
have failed since it only allows functions mapped in the namespace, so you have
completele fine grained possibility on what to allow.
As input the `Config` class expects any file like object which is an object
with a read method.

Testing
*******
There are a few simple test cases in `tests` use best `py.test`_ to run them.

.. _python-config: https://github.com/Inkvi/python-config
.. _ast: http://docs.python.org/3.3/library/ast.html
.. _operator: http://docs.python.org/3.3/library/operator.html
.. _configparser: http://docs.python.org/3.3/library/configparser.html
.. _py.test: https://pypi.python.org/pypi/pytest

Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Topic :: Utilities
Classifier: Natural Language :: English
Classifier: perating System :: OS Independent
Classifier: rogramming Language :: Python
Classifier: rogramming Language :: Python :: 2
Classifier: rogramming Language :: Python :: 2.6
Classifier: rogramming Language :: Python :: 2.7
Classifier: rogramming Language :: Python :: 3
Classifier: rogramming Language :: Python :: 3.3
Classifier: rogramming Language :: Python :: 3.4

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

configreader-0.0.4.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

configreader-0.0.4-py2-none-any.whl (8.3 kB view hashes)

Uploaded Python 2

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page