Skip to content

Commit 3c00211

Browse files
Add tests and test aux files, fix the README and fix jsoncoonfigparser
1 parent 12f436f commit 3c00211

6 files changed

Lines changed: 90 additions & 17 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ except ConfigException as e:
6666
6767
#to access your config you need just:
6868
logging.getLogger(__name__)
69-
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
69+
70+
logging.basicConfig(
71+
format=config.core.logging.format, #look this, at this point I'm already using the config variable
72+
datefmt=config.core.logging.datefmt, #here again
73+
level=20
74+
)
7075
7176
#the list of object example:
7277

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"core":{
3+
"logging":{
4+
"format": "format",
5+
"datefmt": "datefmt"
6+
}
7+
}
8+
}

jsonconfigparser.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,36 @@ class Config(object):
66

77
__instance = None
88

9-
def __new__(cls,schema:dict = None, path_file:str = "config.json"):
10-
if cls.__instance is None:
9+
def __new__(cls, schema:dict = None, path_file:str = "config.json"):
10+
if cls.__instance is None or schema is not None:
1111
cls.__create_new_instace(schema, path_file)
1212
return cls.__instance
1313

1414
@classmethod
15-
def __create_new_instace(cls,schema:dict, path_file:str):
16-
if not path.isfile(path_file):
17-
raise ConfigException("Wasn't possible find the config file: {}".format(path_file))
18-
19-
with open(path_file, "r") as f:
20-
file_buff = f.read()
21-
f.close()
15+
def __create_new_instace(cls, schema, path_file:str):
16+
if type(schema) is not dict:
17+
raise ConfigError("The first config's element should be a Map")
18+
19+
file_buff = cls.__get_file_buff(path_file)
2220

2321
try:
2422
config = Schema(schema).validate(json.loads(file_buff))
25-
if type(config) is not dict:
26-
raise ConfigException("The first config's element should be a Map")
2723
cls.__instance = cls.__dict_2_obj(config)
2824
except json.JSONDecodeError:
29-
raise ConfigException("Wasn't possible to decode the json file:{}".format(path_file))
25+
raise ConfigFileDecodeError("Wasn't possible to decode the json file:{}".format(path_file))
3026
except SchemaError as e:
31-
raise ConfigException(str(e))
27+
raise ConfigFileModelError(str(e))
28+
29+
@classmethod
30+
def __get_file_buff(cls, path_file:str):
31+
if not path.isfile(path_file):
32+
raise ConfigFileNotFoundError("Wasn't find the config file: {}".format(path_file))
33+
34+
try:
35+
with open(path_file, "r") as f:
36+
return f.read()
37+
except Exception as e:
38+
raise
3239

3340
@classmethod
3441
def __dict_2_obj(cls, data:any):
@@ -50,6 +57,21 @@ def __dict_2_obj(cls, data:any):
5057
else:
5158
return data
5259

53-
class ConfigException(Exception):
54-
def __init__(self, *args, **kwargs):
55-
super().__init__(*args, **kwargs)
60+
61+
class ConfigError(Exception):
62+
pass
63+
64+
class ConfigFileModelError(ConfigError):
65+
pass
66+
67+
class ConfigFileDecodeError(ConfigError):
68+
pass
69+
70+
class ConfigSchemaModelError(ConfigError):
71+
pass
72+
73+
class ConfigFileOpenReadError(ConfigError):
74+
pass
75+
76+
class ConfigFileNotFoundError(ConfigError):
77+
pass

schema_config_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SIMPLE_SCHEMA_CONFIG = {
2+
"core":{
3+
"logging":{
4+
"format": str,
5+
"datefmt": str
6+
}
7+
}
8+
}

test_jsonconfigparser.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import unittest
2+
from jsonconfigparser import *
3+
from schema_config_test import SIMPLE_SCHEMA_CONFIG
4+
5+
6+
class ConfigTestCase(unittest.TestCase):
7+
8+
def test_config_without_file(self):
9+
self.assertRaises(ConfigFileNotFoundError, Config, SIMPLE_SCHEMA_CONFIG, "some_non_exists_file.json")
10+
11+
def test_config_with_wrong_json_model(self):
12+
self.assertRaises(ConfigFileModelError, Config, SIMPLE_SCHEMA_CONFIG, "wrong_model.json")
13+
14+
def test_to_access_attr_from_config(self):
15+
config = Config(SIMPLE_SCHEMA_CONFIG)
16+
self.assertEqual(config.core.logging.format, "format")
17+
self.assertEqual(config.core.logging.datefmt, "datefmt")
18+
19+
def test_access_fake_attr(self):
20+
config = Config(SIMPLE_SCHEMA_CONFIG)
21+
self.assertRaises(AttributeError, lambda:config.fake_attr)
22+

wrong_model.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"core":{
3+
"logging":{
4+
"format": 1,
5+
"datefmt": ""
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)