Skip to content

Commit 60bd557

Browse files
Add explanation of how the project works
1 parent a849a65 commit 60bd557

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,90 @@
11
# python-json-config-parser
22
Project created to given the possibility of create dynamics Json config files and use that using oriented object paradigm
3+
4+
5+
HOW IT WORKS
6+
---------------------------
7+
The Class Config takes two arguments, The first is a Schema(https://github.com/keleshev/schema) to secure the model of config,
8+
the second is the str path of json config file.
9+
If the second argument didn't take, the default value('./config.json') is used when you call Config Class trying find this file in the same dir file where you call this Class.
10+
11+
For example:
12+
13+
The model file.
14+
```
15+
SCHEMA_CONFIG = {
16+
"core":{
17+
"logging":{
18+
"format": And(Use(str), lambda string: len(string) > 0),
19+
"datefmt": And(Use(str), lambda string: len(string) > 0)
20+
},
21+
"allowed_clients":[
22+
{
23+
"ip":str, # <- Here you can use regex to valid the ip format
24+
"timeout":int
25+
}
26+
]
27+
}
28+
}
29+
30+
```
31+
32+
The config.json file
33+
```
34+
{
35+
"core":{
36+
"logging":{
37+
"format":"[%(asctime)s][%(levelname)s]: %(message)s",
38+
"datefmt": "%d-%b-%y %H:%M:%S"
39+
},
40+
"allowed_clients":[
41+
{
42+
"ip":"192.168.0.10",
43+
"timeout":60
44+
},
45+
{
46+
"ip":"192.168.0.11",
47+
"timeout":100
48+
}
49+
]
50+
}
51+
}
52+
```
53+
54+
The istance of Config Class:
55+
```
56+
from jsonconfigparser import Config, ConfigException
57+
import logging
58+
59+
try:
60+
config = Config(SCHEMA_CONFIG) # <- Here I'm using that SCHEMA_CONFIG we had declared, and the dir file default value is being used
61+
except ConfigException as e:
62+
print(e)
63+
exit()
64+
65+
#to access your config you need just:
66+
logging.getLogger(__name__)
67+
logging.basicConfig(format=config.core.logging.format, datefmt=config.core.logging.datefmt, level=20)# look this, at this point I'm already using the config variable
68+
69+
#the list of object example:
70+
71+
for client in config.core.allowed_clients:
72+
print(client.ip)
73+
print(client.timeout)
74+
75+
```
76+
77+
After of create the fist obj you don't need to pass the args again, you can instance of Config in other files of your project
78+
just calling the Config without args like that:
79+
80+
```
81+
from jsonconfigparser import Config, ConfigException
82+
83+
config = Config() #At this point you already have the configuration properties in your config object
84+
```
85+
86+
87+
CONTRIBUTE
88+
----------
89+
90+
Fork https://github.com/BrunoSilvaAndrade/python-json-config-parser/ , create commit and pull request to ``develop``.

0 commit comments

Comments
 (0)