read non-ugly human-readable config files
Project description
Read config file(s) with Python module
Description
This module is a set of OOP methods to read a config file and provide access to its sections, keywords and values. A section is a grouping of keyword = values. A section begins at the beginning of a line and the keywords for that section are indented. A keyword can point to a scalar value, an array of values, or an associative array of values.
Lines can be continued across multiple lines by ending a line with a backslash. Values are separated by commas. To have a comma or a backslash as part of the data, escape them with a backslash.
Other config files can be included, to any depth, via a #include line.
Comments begin with a '#' character (if it isn't a #include) and blank lines are ignored.
To preserve whitespace around values, use matching single or double quotes around a value.
An optional definitions file can be provided so that a different separator than a comma can be given, restriction to a set of allowed values, and provide the type of value there instead of in the main config file. These can be specified for a specific keyword within a section, or globally to a keyword that may be present in multiple sections.
If a definitions file is provided, then keywords cannot be in the config file, unless they are defined in the definitions file. Unless the option 'AcceptUndefinedKeywords' is provided and set to 'yes'.
If a 'type' is provided in both the config file and the definitions file for a section/keyword, then they must match. One does not over-ride the other.
To see documentation, do a pydoc config
Class methods
- set_debug()
Instance methods
- get_sections()
- get_keywords()
- get_type()
- get_values()
Config file example
# This is a comment
section-name1:
keyword1 (scalar) = value1
keyword2 = value2
keyword3 = 'this is a really big multi-line \
value with spaces on the end '
keyword4 (array) = val1, val2, 'val 3 ', val4
keyword5 (hash) = v1 = this, \
v2 = " that ", \
v3 = fooey
#include some/other/file.conf
section-name2:
keyword4 = This keyword4 is separate from the \
keyword4 in the section section-name1
something = 'This has a comma here \, in the data'
section-name1:
more-stuff = more stuff for section-name1
Definitions file example
# This keyword1 will only apply to section section-name1
keyword = section-name1:keyword1
type = array
separator = ;
allowed-values = val1, val2, val3, \
'val 4 '
# this keyword2 will apply to all sections
keyword = keyword2
type = hash
separator = ,
Code example
#!/usr/bin/env python3
from config_moxad import config
import sys
try:
conf = config.Config( 'configs/config.conf', 'configs/config-defs.conf',
AcceptUndefinedKeywords=1)
except ( IOError, SyntaxError, ValueError ) as err:
sys.stderr.write( '%s\n' % str(err))
sys.exit(1)
sections = conf.get_sections()
for section in sections:
print( section + ':' )
keywords = conf.get_keywords( section )
for keyword in keywords:
print( "\t" + keyword + ':' )
try:
type = conf.get_type( section, keyword )
except ( ValueError ) as err:
sys.stderr.write( '%s\n' % str(err))
sys.exit(1)
try:
values = conf.get_values( section, keyword )
except ( ValueError ) as err:
sys.stderr.write( '%s\n' % str(err))
sys.exit(1)
if type == 'scalar':
print( '\t\t\'' + values + "'" )
elif type == 'array':
for v in values:
print( "\t\t\'" + v + "\'" )
elif type == 'hash':
keys = list( values )
for key in keys:
print( '\t\t' + key + ' = ' + "\'" + values[ key ] + "\'" )
else:
print( '\t\tunknown type: ' + type )
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file config_moxad-4.0.2.tar.gz
.
File metadata
- Download URL: config_moxad-4.0.2.tar.gz
- Upload date:
- Size: 32.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0c4c42ed338fd89fac768f1215afe96116ca0f0d7703822b0e2ff0fa7cb3530 |
|
MD5 | eed0d81d5115048f99349ffe4f41d9b2 |
|
BLAKE2b-256 | 41b63464e86e46b027d90b7685b76906c457ae981211fee54314af41f204add6 |
File details
Details for the file config_moxad-4.0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: config_moxad-4.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 24.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5228d9098094ccdb713b484e1f44abc1f73918be92fb287bde2aba392a9c0c2c |
|
MD5 | 37b6d7ad180653e7fac16931885218f4 |
|
BLAKE2b-256 | 97242c4ec797f190f6eda65121aefb592bd75b0abeeceea027a3b376b59eb53f |