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 and access them using OOP
HOW TO INSTALL
Use pip to install it.
pip install python-config-parser
HOW TO USE
The model file. 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),
"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
Hashes for python-config-parser-2.0.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e37a9595c3ef465ed2449bce1da642d0e04fd92dddbdf9a032d229d766560dd4 |
|
MD5 | 1f03440d96448628652c702954759ac7 |
|
BLAKE2b-256 | 2f3c4f7607bb9fe3e455c6e3813044a36039ab55b1a30fd91a8683b7f2af55be |
Hashes for python_config_parser-2.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c66c29ed768da248812f43f4c08bf2b4755a03a5e2ef5a7a0ebc5a8820603306 |
|
MD5 | 682d2708d25d0a2ac099c9452543098b |
|
BLAKE2b-256 | 7d8915024d481d25e26b9a0d8b30a75f552a5a7a44b811412f70564a5e9ad461 |