Skip to main content

Config Handler

Handle config files in your projects the easy way.

PyPI version Build Status

GitHub issues GitHub closed issues GitHub closed pull requests

PyPI - Python Version GitHub GitHub last commit

Installation

> pip install config-handler

Rules

Rule #1

Use INI files for config.
Regarding that please follow INI file language: https://en.wikipedia.org/wiki/INI_file

Rule #2

Use only strings, integers and booleans.
If you need to use anything else, the design isn't right.

Rule #3

Don't put your config file (config.ini) in git repository.
Instead you have a config template file (config.ini.template).

Rule #4

Config file should contain ONLY the stuff that could be changed by user.
If you have the data that will never change, use constants.

Config template file

This is how config.ini.template file should look like:

Config template file

config.ini.template file supports template variables (${project_root_path}).

Usage

Scenario #1

You are running your app for the first time and config.ini doesn't exist yet.
So you need to create it from config.ini.template file (use template variables if needed):

import os
from config_handler import ConfigHandler

template_config_variables = {
    'project_root_path': os.path.join('path', 'to', 'project', 'root')
}
ConfigHandler('./config.ini').sync(template_config_variables)

After this call, config.ini file was created in the same directory where config.ini.template is located and should look like this:

Config file

Scenario #2

Now read your newly created config.ini:

from config_handler import ConfigHandler

config = ConfigHandler('./config.ini').read()

print(config.sections())
# >>> ['app1', 'app2']

print(dict(config['DEFAULT']))
# >>> {'send_email': 'true', 'authenticate_user': 'true'}

print(dict(config['app1']))
# >>> {'send_email': 'false', 'line_height': '12', 'input_path': 'path/to/project/root/input/app1', 'authenticate_user': 'true'}

print(config['DEFAULT']['send_email'])
# >>> 'true'

print(config['app1']['front_page_title'])
# >>> 'Hello World!'

print(config.getboolean('DEFAULT', 'send_email'))
# >>> True

print(config.getboolean('app2', 'send_email'))
# >>> False

.read() returns ConfigParser objects from python's native Configuration file parser lib

Scenario #3

User made some changes in config.ini file:

Config file

And some changes are made in config.ini.template file by the developers:

Config template file

Then you need to run sync again:

import os
from config_handler import ConfigHandler

template_config_variables = {
    'project_root_path': os.path.join('path', 'to', 'project', 'root')
}
ConfigHandler('./config.ini').sync(template_config_variables)

After the sync, config.ini file should look like this:

Config file

So, what user changes or adds stays in config.ini.
Everything new that was added in config.ini.template was added in config.ini as well.

Scenario #4

You can sync and read at the same time:

from config_handler import ConfigHandler

config = ConfigHandler('./config.ini').sync({...}).read()

...

Exception handling

from config_handler import (ConfigHandler, 
                            ConfigHandlerException,
                            ConfigHandlerFileReadException)

try:
    config = ConfigHandler('./config.ini').sync({...}).read()

except ConfigHandlerFileReadException:
    """Read exceptions"""

except ConfigHandlerException:
    """All Config Handler exceptions"""

Metadata

Release files for Config-Handler 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Config-Handler 0.1.0
File Size Uploaded
Config Handler-0.1.0.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Config-Handler 0.1.0
File Interpreter ABI Platform
Config_Handler-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / Config Handler-0.1.0.tar.gz

Download URL Config Handler-0.1.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e3e355b6e3f1ee9a5fe0eb9e7f7e1aa8e5dc96511c24c828054c95d8a507d8b6
BLAKE2b-256 checksum
How to use checksums
a0748d077b81e2afc57cc51097fcdbe3550353e6d94814fc15910641997df78e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Release files / Config_Handler-0.1.0-py3-none-any.whl

Download URL Config_Handler-0.1.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6dd3ad8c27180a3267fd597bd7324b9b85bd9e2b76e390483ed348b346a403ec
BLAKE2b-256 checksum
How to use checksums
7a4958812841be303a8006cdacd8306ef3bbf0af9c56bcd7e2a648baec339ac0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page