Skip to main content

Yet Another Simple Configuration Parser for INI-style configuration files.

Project description

This is Yet Another Simple Configuration Parser module for INI style configurations files.

Installation

pip install yascp

easy_install yascp

USAGE

A sample configuration is looking like this and is saved in file called example.conf:

[database]
login = user
password = topsecret
host = example.com
port = 3306

[backend_api]
login = api_user
password = api_password
url = http://example.com:9080

In scripts

The constructor takes only two arguments - first one is the name or absoluthe path to the configuration file (see below “Configuration File” for more details on this) and a dictionary of default values which will be parsed before any actual configuration file is read. If same section and option is found in a configuration file then the default will be overwritten.

import yascp
config = yascp.parser.Parser(configuration_file_name = 'example.conf', defaults = {'extra.port':'666'})

login      = config.database.username
password   = config.database.password
host       = config.database.host
port       = config.database.port

print config.backend_api.url

There is a convenience method available to print off all of the configuration parsed.

So running:

config.print_all()

would print to the screen following:

backend_api.url = http://example.com:9080
backend_api.password = api_password
backend_api.login = api_user
extra.port = 666
database.host = example.com
database.port = 3306
database.login = user
database.password = topsecret

Notice: Everything is a string. The module does not distinguish any type of data and so the developer should know where an integer for example is expected an perform all required tests/handle possible errors.

In command line

parser.py [path to/name of an INI config file] [section.option to be fetched]

The script takes two arguments

The first argument is the name of a configuration file or full path to one (see below for “Configuration File” section for more details).

The second argument may be “–list-all” to obtain a list of all available options or a key of specific option to get value for this option only (run first with “–list-all” to see what exactly can be used here).

If an option doesn’t belong to any of the sections in the configuration file then it should be addresses with “default” section, i.e:

default.[myoption]

Sample usage:

python parser.py /tmp/example.conf --print-all
backend_api.url = http://example.com:9080
backend_api.password = api_password
backend_api.login = api_user
database.host = example.com
database.port = 3306
database.login = user
database.password = topsecret

Knowing now what options are avaialbe you can fetch only this that you need:

python parser.py /tmp/example.conf backend_api.url
http://example.com:9080

Configuration File

If absolute path is provided to a configuration file then only that file is read but if only a name of configuration file is given then the script will attempt to read a file by that name in following locations and order:

  1. /etc/[config file name]

  2. ~/[config file name]

  3. directory from which the script has been ran.

Notice: If more than one configuration file is present then the duplicated options set for example in a configuratioin file saved in /etc/ will be overwritten by those in user’s home direcotry and so on.

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

yascp-0.2.5.tar.gz (6.4 kB view hashes)

Uploaded Source

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