Skip to content

Commit f62686a

Browse files
Refactory __dict_2_obj, Fix try/except block when open and read the config file, add pip command to install in README, add list test in test case
1 parent cc11dec commit f62686a

6 files changed

Lines changed: 17 additions & 9 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# python-json-config-parser
22
Project created to give the possibility of create dynamics Json config files and use this using oriented object paradigm
33

4+
HOW TO INSTALL
5+
---------------------------
6+
Use pip to install it.
7+
8+
```
9+
pip install python-json-config-parse
10+
```
11+
412

513
HOW IT WORKS
614
---------------------------

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"logging":{
44
"format": "format",
55
"datefmt": "datefmt"
6-
}
6+
},
7+
"obj_list":[{"name":"bruno", "age":24}]
78
}
89
}

jsonconfigparser.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __get_file_buff(cls, path_file:str):
3535
with open(path_file, "r") as f:
3636
return f.read()
3737
except Exception as e:
38-
raise
38+
raise ConfigFileOpenReadError(str(e))
3939

4040
@classmethod
4141
def __dict_2_obj(cls, data:any):
@@ -44,11 +44,7 @@ def __dict_2_obj(cls, data:any):
4444
if _type is dict:
4545
obj = object.__new__(cls)
4646
for key in data:
47-
sub_data = data[key]
48-
if type(sub_data) in (list, dict):
49-
setattr(obj, key, cls.__dict_2_obj(sub_data))
50-
else:
51-
setattr(obj, key, sub_data)
47+
setattr(obj, key, cls.__dict_2_obj(data[key]))
5248
return obj
5349
if _type is list:
5450
for i in range(len(data)):

schema_config_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"logging":{
44
"format": str,
55
"datefmt": str
6-
}
6+
},
7+
"obj_list":[{"name":str, "age":int}]
78
}
89
}

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-json-config-parser",
8-
version="1.0.3",
8+
version="1.0.4",
99
author="Bruno Silva de Andrade",
1010
author_email="brunojf.andrade@gmail.com",
1111
description="Project created to given the possibility of create dynamics Json config files",

test_jsonconfigparser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def test_to_access_attr_from_config(self):
1515
config = Config(SIMPLE_SCHEMA_CONFIG)
1616
self.assertEqual(config.core.logging.format, "format")
1717
self.assertEqual(config.core.logging.datefmt, "datefmt")
18+
self.assertEqual(config.core.obj_list[0].name, "bruno")
19+
self.assertEqual(config.core.obj_list[0].age, 24)
1820

1921
def test_access_fake_attr(self):
2022
config = Config(SIMPLE_SCHEMA_CONFIG)

0 commit comments

Comments
 (0)