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 json and yaml/yml config files dynamically using OOP
HOW TO INSTALL
Use pip to install it.
pip install python-config-parser
HOW TO USE
The model file.
from schema import Use, And
SCHEMA_CONFIG = {
"core":{
"logging":{
"format": And(Use(str), lambda string: len(string) > 0),
"datefmt": And(Use(str), lambda string: len(string) > 0),
"level": str
},
"allowed_clients":[
{
"ip":str, # <- Here you can use regex to validate the ip format
"timeout":int
}
]
}
}
The config.yml file
core:
logging:
format: "[%(asctime)s][%(levelname)s]: %(message)s"
datefmt: "%d-%b-%y %H:%M:%S"
level: ${LEVEL_ENV_VARIABLE}
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, ConfigException
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 ConfigException 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
dtfmt = config.core.logging.datefmt #here again
logging.getLogger(__name__)
logging.basicConfig(
format=fmt,
datefmt=dtfmt
level=logging.INFO
)
#the list of object example:
for client in config.core.allowed_clients:
print(client.ip)
print(client.timeout)
Instanced the first obj, you can instance Config in other files of your project just calling the Config without args like that:
from pyconfigparser import Config, ConfigException
config = Config.get_config() #At this point you already have the configuration properties in your config object
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
Close
Hashes for python-config-parser-2.0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8de811967b2e023799b84b2ba83cea65b71524517f3521c275c810828022da0 |
|
MD5 | 6f86c810ab9d461ecc637179bf20c42c |
|
BLAKE2b-256 | 97d4c13a902f38b9bc84888b0d90317426fa1db14615c8924a3c16cb4d0e42d2 |
Close
Hashes for python_config_parser-2.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71505e1e89ae40f36b87100fc699b1dc0a45443d7faef38166370ada8287153b |
|
MD5 | 857259ce9cd0e435e865793f53c421e4 |
|
BLAKE2b-256 | 9e90858e779be442b419379d0dda264b904e63b87bde4d05ab465250d2c43af5 |