Skip to main content

Plug-n-Play ConfigParser for extensible use cases.

Project description

ParserConfig

ParserConfig initializes ConfigParser in fewer lines of code.

Installation

Requires Python 3.7+:

python -m pip install parserconfig

Usage

To follow along, create a settings.ini file in your home directory with the following content:

[tutorial]
username = my_username
password = my_password
bearer_token = my_bearer_token

The configuration file can be read using the following code:

from pathlib import Path
from parserconfig import ParserConfig

configuration_file = Path.home().joinpath('settings.ini')
settings = ParserConfig(configuration_file=configuration_file)

The credentials helper method returns a dictionary containing key/value pairs. This can be leveraged in several ways:

credentials = settings.credentials('username', 'password', section='tutorial')
print(f'{credentials = }. Kwargs unpacking is supported via **credentials')

username, password = credentials.values()
print(f'{username = }, {password = }')

The pattern for querying a single value varies slightly:

bearer_token = settings.credentials('bearer_token', section='tutorial')
print(f'{bearer_token = }. Single value also supports kwargs unpacking via **bearer_token')

bearer = bearer_token.get('bearer_token')
print(f'{bearer = }')

token, *_ = bearer_token.values()
print(f'{token = }')

Access to ConfigParser is provided via configuration_file_parser, and can be used to enable ExtendedInterpolation:

from configparser import ConfigParser, ExtendedInterpolation

settings.configuration_file_parser = ConfigParser(interpolation=ExtendedInterpolation())
settings.load_configuration_file()

Advanced Usage

Local Development and Testing

Development

Development and testing should be done in a virtual environment.

$ git clone https://github.com/Fauxsys/parserconfig.git
$ cd parserconfig
$ python -m venv venv --prompt parserconfig
$ source venv/bin/activate
(parserconfig) $ python -m pip install -U pip

Install parserconfig locally.

(parserconfig) $ python -m pip install -e ".[test]"

Testing

You can test any changes locally with pytest.

(parserconfig) $ python -m pytest --cov=parserconfig 

You can also test parserconfig as an installed package.

(parserconfig) $ python -m tox

Building a wheel

(parserconfig) $ python -m pip install build
(parserconfig) $ python -m build --wheel

There should now be a wheel in the dist directory.

(parserconfig) $ ls -1 dist
parserconfig-1.0.0-py3-none-any.whl

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

parserconfig-1.0.0-py3-none-any.whl (15.9 kB view hashes)

Uploaded Python 3

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