Skip to main content
https://secure.travis-ci.org/mortbauer/configreader.png?branch=master

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.

Metadata

Release files for configreader 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for configreader 0.0.6
File Size Uploaded Provenance Actions
configreader-0.0.6.tar.gz 4.3 kB Without provenance Details for configreader-0.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for configreader 0.0.6
File Interpreter ABI Platform Actions
configreader-0.0.6-py2-none-any.whl Python 2 none any Details for configreader-0.0.6-py2-none-any.whl

Total release size: 11.2 kB

Release files / configreader-0.0.6.tar.gz

Download URL configreader-0.0.6.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d1fcd6a539135137b15440574a68ec1545db8b823675282d2b1c28818e40b4e4
BLAKE2b-256 checksum
How to use checksums
22eb2b4fdbdd2cb026329368d2102fc41968dd318846dc83603dd43fb8f3433c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / configreader-0.0.6-py2-none-any.whl

Download URL configreader-0.0.6-py2-none-any.whl
Size 6.9 kB
Tags Python 2
SHA-256 checksum
How to use checksums
b67a96fa7f3cf08c7e6bafa8f3694b42cacb30cb246646f7a09e97b483a26063
BLAKE2b-256 checksum
How to use checksums
587231ddf74227900847cb23b6222b6be1ba0787e4b9863dd3a665c4e9c67a27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for configreader 0.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page