11# python-config-parser
22This project was created to give you the possibility
3- of creating json and yaml/yml config files dynamically and access them using OOP
3+ of creating runtime configuration objects using json or yaml files.
4+
5+ MAIN FEATURES
6+ ---------------------------
7+ * Declarative configurations without using .ini files
8+ * Access OOP or subscriptable, which means that you can iterate the config object items
9+ * Runtime validation using [ schema] ( https://github.com/keleshev/schema )
10+ * Automatic environment variables interpolation
411
512HOW TO INSTALL
613---------------------------
714Use pip to install it.
815
9- ```
16+ ``` shell
1017pip install python-config-parser
1118```
1219
13-
1420HOW TO USE
1521---------------------------
1622
17- The model file.
23+ The Schema validation.
24+
1825You may use or not schema validation, if you want to use it, it will validate your whole config object before returning it.
1926
2027If you don't want to use it, it won't validate the config object before returning that, and it may generate runtime access inconsistencies.
2128
2229How to use schema
23- ```
30+
31+ ``` python
2432from schema import Use, And
2533
2634SCHEMA_CONFIG = {
27- " core": {
28- " logging": {
29- " format" : And(Use(str), lambda string: len(string) > 0),
30- "datefmt" : And(Use(str), lambda string: len(string) > 0),
31- "level" : str
35+ ' core' : {
36+ ' logging' : {
37+ ' format' : And(Use(str ), lambda string : len (string) > 0 ),
38+ ' date_fmt ' : And(Use(str ), lambda string : len (string) > 0 ),
39+ ' random_env_variable ' : str
3240 },
33- "allowed_clients":[
34- {
35- "ip":str, # <- Here you can use regex to validate the ip format
36- "timeout":int
41+ ' allowed_clients' : [{
42+ ' ip' : str , # <- Here you can use regex to validate the ip format
43+ ' timeout' : int
3744 }
3845 ]
3946 }
@@ -42,12 +49,12 @@ SCHEMA_CONFIG = {
4249```
4350
4451The config.yml file
45- ```
52+ ``` yaml
4653core :
54+ random_env_variable : ${RANDOM_ENV_VARIABLE}
4755 logging :
4856 format : " [%(asctime)s][%(levelname)s]: %(message)s"
49- datefmt: "%d-%b-%y %H:%M:%S"
50- level: ${LEVEL_ENV_VARIABLE}
57+ date_fmt : " %d-%b-%y %H:%M:%S"
5158 allowed_clients :
5259 - ip : 192.168.0.10
5360 timeout : 60
@@ -56,27 +63,27 @@ core:
5663` ` `
5764
5865The instance of Config Class:
59- ```
60- from pyconfigparser import Config, ConfigException
66+ ` ` ` python
67+ from pyconfigparser import Config, ConfigError
6168import logging
6269
6370try :
6471 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
65- except ConfigException as e:
72+ except ConfigError as e :
6673 print(e)
6774 exit()
6875
6976# to access your config you need just:
7077
7178
7279fmt = config.core.logging.format # look this, at this point I'm already using the config variable
73- dtfmt = config. core. logging.datefmt #here again
80+ date_fmt = config[' core'][' logging']['date_fmt'] # here subscriptable access
7481
7582logging.getLogger(__name__)
7683
7784logging.basicConfig(
7885 format=fmt,
79- datefmt=dtfmt
86+ datefmt=date_fmt,
8087 level=logging.INFO
8188)
8289
@@ -86,17 +93,34 @@ for client in config.core.allowed_clients:
8693 print(client.ip)
8794 print(client.timeout)
8895
89- ```
96+
97+ # The config object's parts which is not a list can also be itered but, it'll give you the attribute's names
98+ # So you can access the values by subscriptale access
99+ for logging_section_attr_key in config.core.logging :
100+ print(config.core.logging[logging_section_attr_key])
90101
91- Instanced the first obj, you can instance Config in other files of your project
92- just calling the Config without args like that:
102+ # Accessing the environment variable already resolved
103+ print(config.random_env_variable)
93104
94105```
95- from pyconfigparser import Config, ConfigException
106+ Since you've already created the first Config's instance this instance will be cached inside Config class,
107+ so after this first creation you can just invoke Config.get_config()
108+
109+ ``` python
110+ from pyconfigparser import Config
96111
97112config = Config.get_config() # At this point you already have the configuration properties in your config object
98113```
99114
115+ You can also disable the action to cache the instance config
116+
117+
118+ ``` python
119+ from pyconfigparser import Config
120+
121+ Config.set_hold_an_instance(False )
122+ ```
123+
100124
101125CONTRIBUTE
102126----------
0 commit comments