Skip to content

Commit 930df98

Browse files
Merge pull request #3 from BrunoSilvaAndrade/develop
Develop
2 parents cc7fe9b + dfacf06 commit 930df98

3 files changed

Lines changed: 55 additions & 27 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
.idea/
22
python_json_config_parser.egg-info/
3+
venv
4+
dist
5+
build
6+
wheel

README.md

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
# python-config-parser
22
This 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

512
HOW TO INSTALL
613
---------------------------
714
Use pip to install it.
815

9-
```
16+
```shell
1017
pip install python-config-parser
1118
```
1219

13-
1420
HOW TO USE
1521
---------------------------
1622

17-
The model file.
23+
The Schema validation.
24+
1825
You may use or not schema validation, if you want to use it, it will validate your whole config object before returning it.
1926

2027
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.
2128

2229
How to use schema
23-
```
30+
31+
```python
2432
from schema import Use, And
2533

2634
SCHEMA_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

4451
The config.yml file
45-
```
52+
```yaml
4653
core:
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
5865
The instance of Config Class:
59-
```
60-
from pyconfigparser import Config, ConfigException
66+
```python
67+
from pyconfigparser import Config, ConfigError
6168
import logging
6269

6370
try:
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

7279
fmt = 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

7582
logging.getLogger(__name__)
7683

7784
logging.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

97112
config = 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

101125
CONTRIBUTE
102126
----------

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='python-config-parser',
8-
version='2.0.6',
8+
version='2.1.0',
99
author='Bruno Silva de Andrade',
1010
author_email='brunojf.andrade@gmail.com',
1111
description='Project created to given the possibility of create dynamics config files',

0 commit comments

Comments
 (0)