Skip to main content

Advanced configuration parser for Python

Project description

Dotconf is an advanced configuration parser which allow nested sections to any level, typed values in syntax, file include and so more. Dotconf is also shipped with a powerful schema validation system.

Features

  • Configuration format kept as simple as possible.

  • Developer friendly APIs.

  • Types are expressed in the syntax, so the parser can guess it without validation.

  • Four primitive types: string, number, boolean, or list.

  • More complex composite types can be created using the schema validation system.

  • Nested section of any deep. Sections can take a special “argument” value (look at the example).

  • Schema validation system, all schema can be defined using declarative or imperative way.

  • External file include with globbing.

  • Integration with argparse.

  • Tests (only parser is covered yet)

  • Released under MIT license.

Example

This is an example of configuration file for an imaginary web server:

daemon = True
pidfile = '/var/run/myapp.pid'
interface = '0.0.0.0:80'
interface_ssl = '0.0.0.0:443'

host 'example.org' {
    path '/' {
        rate_limit = 30
    }
}

host 'protected.example.org' {
    enable_ssl = yes

    path '/files' {
        enable_auth = yes
        user 'foo' {
            password = 'bar'
        }
    }
}

You can access to each values using the developer friendly API:

>>> from dotconf import Dotconf
>>> parsed_conf = Dotconf.from_file('mywebserver.conf')
>>> print parsed_conf.get('daemon', False)
True

Even more exciting, you can create a validation schema to avoid you the painful chore of manual configuration file validation:

from dotconf.schema import many, once
from dotconf.schema.containers import Section, Value
from dotconf.schema.types import Boolean, Integer, Float, String

# Schema definition:

class UserSection(Section):
    password = Value(String())
    _meta = {'repeat': many, 'unique': True}

class PathSection(Section):
    rate_limit = Value(Float(), default=0)
    enable_auth = Value(Boolean(), default=False)
    user = UserSection()

class VirtualHostSection(Section):
    base_path = Value(String())
    enable_ssl = Value(Boolean(), default=False)
    path = PathSection()
    _meta = {'repeat': many, 'unique': True}

class MyWebserverConfiguration(Section):
    daemon = Value(Boolean()default=False)
    pidfile = Value(String(), default=None)
    interface = Value(String(), default='127.0.0.1:80')
    interface_ssl = Value(String(), default='127.0.0.1:443')
    host = VirtualHostSection()

Then you can use the API exactly as if it was not validated:

>>> from dotconf import Dotconf
>>> from myconfschema import MyWebserverConfiguration
>>> parsed_conf = Dotconf(conf, schema=MyWebserverConfiguration)
>>> print 'daemon:', parsed_conf.get('daemon')
daemon: True
>>> for vhost in parsed_conf.subsections('host'):
>>>     print vhost.args[0]
>>>     if vhost.get('enable_ssl'):
>>>         print '  SSL enabled'
>>>     for path in vhost.subsections('path'):
>>>         print '  ' + path.args[0]
>>>         if path.get('enable_auth'):
>>>             print '    Following users can access to this directory:'
>>>             for user in path.subsections('user'):
>>>                 print '     - ' + user.args[0]
>>>
example.org
  /
protected.example.org
  SSL enabled
  /files
    Following users can access to this directory:
      - foo

TODO

  • More test.

Changelog

v0.2 released on 03/04/2012

  • Added argparse integration feature & documentation

  • Cleanup

v0.1 released on 24/03/2012

  • Initial version.

Contribute

You can contribute to Dotconf through these ways:

Feel free to contact me for any question/suggestion: <antoine@inaps.org>.

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

dotconf-0.2.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

dotconf-0.2-py2.7.egg (25.5 kB view details)

Uploaded Egg

File details

Details for the file dotconf-0.2.tar.gz.

File metadata

  • Download URL: dotconf-0.2.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dotconf-0.2.tar.gz
Algorithm Hash digest
SHA256 c5b58530d5c1d211283c710d65c2b967bb077cccc2cfa8be39a5af648e45f369
MD5 9fb08b8cb60a7f651562b69d30505439
BLAKE2b-256 183727dbdbc789b077616519a3a42b22b716064ace2021e1b1139f6d290b9d7d

See more details on using hashes here.

File details

Details for the file dotconf-0.2-py2.7.egg.

File metadata

  • Download URL: dotconf-0.2-py2.7.egg
  • Upload date:
  • Size: 25.5 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dotconf-0.2-py2.7.egg
Algorithm Hash digest
SHA256 626ba4cf179737de58c54c80e202d05e04650f46fe7785f2073dab3d34fec2aa
MD5 e51312f940c7cc4109b9e1fd4a614b18
BLAKE2b-256 1318125107761f3c9d2d4f73b6bed902d7965a5a6cbd809acb1de469eb2d74ce

See more details on using hashes here.

Supported by

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