Skip to main content

Add line number information to ConfigParser options

Project 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:

    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.


Install with pip:

$ pip install configlines


Given the following two configuration files:

# Line one of data1.cfg
foo = 1

bar = 2
# Line one of data2.cfg
baz = 3

You can read and manipulate them exactly the same as the standard module:

>>> from configlines import ConfigParser
>>> cfg = ConfigParser()
>>>['data1.cfg', 'data2.cfg'])
>>> cfg.get('some_section', 'foo')

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')

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')

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)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for configlines, version 0.3
Filename, size File type Python version Upload date Hashes
Filename, size configlines-0.3.tar.gz (6.1 kB) File type Source Python version None Upload date Hashes View
Filename, size configlines-0.3-py2.py3-none-any.whl (6.7 kB) File type Wheel Python version py2.py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page