Skip to main content

Add line number information to ConfigParser options

Project description

https://travis-ci.org/benfogle/configlines.svg?branch=master https://codecov.io/gh/benfogle/configlines/branch/master/graph/badge.svg

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)

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

configlines-0.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

configlines-0.2-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for configlines-0.2.tar.gz
Algorithm Hash digest
SHA256 844a69fb0e2de416f6cd4a46fdcf5f3253a6bbd4a01b3ee356a3567de1c7a855
MD5 e3e5eaf2095d72e75a1c10b22127eeda
BLAKE2b-256 03a6e95031738b742576e155aada1195fddfce3063ca872825c560e75eff3152

See more details on using hashes here.

File details

Details for the file configlines-0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for configlines-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c58d0596053204201993ab058c0b02d2aec0ce62f55965eaf4d537448f2b54fd
MD5 9f3cd2e197a00d7ef422d784e7d24bfb
BLAKE2b-256 d04bfecb2e1305b630dd250e73184add5c305dafd9eaa4335ccccf6e0f5984cd

See more details on using hashes here.

Supported by

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