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
- management_api_tools uses parserconfig to insert data into pytest during test execution.
- offprem creates subclasses of parserconfig.
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
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 Distributions
Built Distribution
Hashes for parserconfig-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c69df7d809bf3ad0246914c0b7f5ad0451b24a482c3145dbf8f012941ca67a7 |
|
MD5 | 969a591280c6ca4338f817b6e24cc024 |
|
BLAKE2b-256 | 373ed9d1bb3330802590c6a7a66e63d49eabffa79136d8ba5af2d16c3ece2ffe |