Project created to given the possibility of create dynamics config files
Project description
python-config-parser
This project was created to give you the possibility of creating runtime configuration objects using json or yaml files.
MAIN FEATURES
- Declarative configurations without using .ini files
- Access OOP or subscriptable, which means that you can iterate the config object items
- Runtime validation using schema
- Automatic environment variables interpolation
- Automatic parser selecting by config file extension
HOW TO INSTALL
Use pip to install it.
pip install python-config-parser
HOW TO USE
First of all the config file will look for default config files if you do not pass your own config file for this.
The default config directory is ./config(if you do not pass the config directory of your preference) assuming your current directory.
The default config files names are -> (config.json, config.yaml, config.yml)
The Schema validation.
You may use or not schema validation, if you want to use it, it will validate your whole config object before returning it.
If you don't want to use it, it won't validate the config object before returning that, and it may generate runtime access inconsistencies.
How to use schema
from schema import Use, And
SCHEMA_CONFIG = {
'core': {
'logging': {
'format': And(Use(str), lambda string: len(string) > 0),
'date_fmt': And(Use(str), lambda string: len(string) > 0),
'random_env_variable': str
},
'allowed_clients': [{
'ip': str, # <- Here you can use regex to validate the ip format
'timeout': int
}
]
}
}
The config.yml file
core:
random_env_variable: ${RANDOM_ENV_VARIABLE}
logging:
format: "[%(asctime)s][%(levelname)s]: %(message)s"
date_fmt: "%d-%b-%y %H:%M:%S"
allowed_clients:
- ip: 192.168.0.10
timeout: 60
- ip: 192.168.0.11
timeout: 100
This config file as a json would be something like:
{
"core": {
"random_env_variable": "${RANDOM_ENV_VARIABLE}",
"logging": {
"format": "[%(asctime)s][%(levelname)s]: %(message)s",
"date_fmt": "%d-%b-%y %H:%M:%S"
},
"allowed_clients": [
{
"ip": "192.168.0.10",
"timeout": 60
},
{
"ip": "192.168.0.11",
"timeout": 100
}
]
}
}
The instance of Config Class:
from pyconfigparser import Config, ConfigError
import logging
try:
config = Config.get_config(SCHEMA_CONFIG) # <- Here I'm using that SCHEMA_CONFIG we had declared, and the dir file default value is being used
except ConfigError as e:
print(e)
exit()
#to access your config you need just:
fmt = config.core.logging.format #look this, at this point I'm already using the config variable
date_fmt = config['core']['logging']['date_fmt'] #here subscriptable access
logging.getLogger(__name__)
logging.basicConfig(
format=fmt,
datefmt=date_fmt,
level=logging.INFO
)
#the list of object example:
for client in config.core.allowed_clients:
print(client.ip)
print(client.timeout)
#The config object's parts which is not a list can also be itered but, it'll give you the attribute's names
#So you can access the values by subscriptale access
for logging_section_attr_key in config.core.logging:
print(config.core.logging[logging_section_attr_key])
#Accessing the environment variable already resolved
print(config.random_env_variable)
Since you've already created the first Config's instance this instance will be cached inside Config class, so after this first creation you can just invoke Config.get_config()
from pyconfigparser import Config
config = Config.get_config() #At this point you already have the configuration properties in your config object
You can also disable the action to cache the instance config
from pyconfigparser import Config
Config.set_hold_an_instance(False)
CONTRIBUTE
Fork https://github.com/BrunoSilvaAndrade/python-config-parser/ , create commit and pull request to develop
.
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
Hashes for python-config-parser-2.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 863062e9b0f6e0ab56d85e663d691c996ad903360760cddfa0b6f1b6cc04be97 |
|
MD5 | fe24751aa482b994af5203625cafa7d5 |
|
BLAKE2b-256 | 45221c7fd19a220ceae1bac8c9954b43028effb65c00ed6c4bb5fd7a16d5a814 |
Hashes for python_config_parser-2.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7218e926a1971ffe8da14e8a72408826ba8c2040522349a66bf9f4de0ced89fe |
|
MD5 | 932277be02eadd52b75b7ae532150cbe |
|
BLAKE2b-256 | eb6c8eeab2fdc0346b2f8eb92e2cf5b17559c014dbee7829f376fda9dcf4e5b7 |