Handle config files in your projects the easy way
Project description
Config Handler
Handle config files in your projects the easy way.
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.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:
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:
And some changes are made in config.ini.template file by the developers:
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:
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"""
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 Distribution
Built Distribution
File details
Details for the file Config Handler-0.1.0.tar.gz
.
File metadata
- Download URL: Config Handler-0.1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3e355b6e3f1ee9a5fe0eb9e7f7e1aa8e5dc96511c24c828054c95d8a507d8b6 |
|
MD5 | fbe28bfd92263be2ddbd2302e5416afa |
|
BLAKE2b-256 | a0748d077b81e2afc57cc51097fcdbe3550353e6d94814fc15910641997df78e |
File details
Details for the file Config_Handler-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: Config_Handler-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dd3ad8c27180a3267fd597bd7324b9b85bd9e2b76e390483ed348b346a403ec |
|
MD5 | 7a2d6b5ae788ba798adead8cdb076c0c |
|
BLAKE2b-256 | 7a4958812841be303a8006cdacd8306ef3bbf0af9c56bcd7e2a648baec339ac0 |