Description
This module is an extension to Python’s standard configparser module that adds file and line number information to each stored option. This is primarily useful in reporting error messages: you can point your user to exactly where a bad value occurred:
try:
timeout = config.getint('connection', 'retry')
except ValueError:
filename, line = config.get_location('connection', 'retry')
logging.error("retry must be an integer (%s:%d)", filename, line)
...
This package is compatible with both Python 2 and 3.
Installation
Install with pip:
$ pip install configlines
Usage
Given the following two configuration files:
# Line one of data1.cfg
[some_section]
foo = 1
[DEFAULT]
bar = 2
# Line one of data2.cfg
[some_section]
baz = 3
You can read and manipulate them exactly the same as the standard module:
>>> from configlines import ConfigParser
>>> cfg = ConfigParser()
>>> cfg.read(['data1.cfg', 'data2.cfg'])
>>> cfg.get('some_section', 'foo')
'1'
You can also access file and line information:
>>> cfg.get_location('some_section', 'foo')
('data1.cfg', 3)
>>> cfg.get_location('some_section', 'bar')
('data1.cfg', 6)
>>> cfg.get_location('some_section', 'baz')
('data2.cfg', 3)
In addition to get_location, this module also provides get_line and get_filename functions for convenience.
If an option didn’t come from a file, (i.e., you set it programmatically,) then line number information will not be present:
>>> cfg.set('some_section', 'qwerty', '1234')
>>> cfg.get_location('some_section', 'qwerty')
None
Overwriting options programmatically will erase line number information:
>>> cfg.get_location('some_section', 'foo')
('data1.cfg', 3)
>>> cfg.set('some_section', 'foo', '1234')
>>> cfg.get_location('some_section', 'foo')
None
Line number information can be set explicitly:
>>> cfg.set('some_section', 'foo', '1234', location=('somefile.cfg',10))
>>> cfg.get_location('some_section', 'foo')
('somefile.cfg', 10)
>>> cfg.set('some_section', 'foo', '1234', location='preserve')
>>> cfg.get_location('some_section', 'foo')
('somefile.cfg', 10)
>>> cfg.set_location('some_section', 'foo', ('otherfile.cfg', 50))
>>> cfg.get_location('some_section', 'foo')
('otherfile.cfg', 50)
Metadata
Release files for configlines 0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| configlines-0.3.tar.gz | 6.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| configlines-0.3-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 12.8 kB
Release files / configlines-0.3.tar.gz
| Download URL | configlines-0.3.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
34cc93eaf89479a58e6b6afe53ad25a7e27bdbdeee894650d8925fa65af748d5
|
|
BLAKE2b-256 checksum How to use checksums |
3f2a3dc97e0c846579475ab59335ab30f098ee1e98eed783dc9c2800b337f4c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / configlines-0.3-py2.py3-none-any.whl
| Download URL | configlines-0.3-py2.py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
7a6c69ae312abfb15655460f6c041b6fcbf91633ce90c5ca2b0bcc0194f1d15f
|
|
BLAKE2b-256 checksum How to use checksums |
7c3c7d22bf62d9e8ab89b342b06c78b854aa79ee2d4ebdb07a04e2c5bd5c580a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |