Skip to content

Commit 8bac63a

Browse files
committed
add ConfigManager class
ConfigManager - a class, managing the application configuration defaults
1 parent c6a6547 commit 8bac63a

File tree

3 files changed

+143
-45
lines changed

3 files changed

+143
-45
lines changed

source/confParser.py

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def checkTLSsettings(args):
4747
def getSettings(argv):
4848
settings = {}
4949
msg = ''
50+
defaults = ConfigManager().defaults
5051
args, msg = parse_cmd_args(argv)
51-
defaults = parse_defaults_from_config_file()
5252
if args and defaults:
5353
settings = merge_defaults_and_args(defaults, args)
5454
elif args:
@@ -71,19 +71,70 @@ def merge_defaults_and_args(defaults, args):
7171
return brConfig
7272

7373

74-
def parse_defaults_from_config_file(fileName='config.ini'):
75-
'''parse default parameters from a config file'''
76-
defaults = {}
77-
dirname, filename = os.path.split(os.path.abspath(__file__))
78-
conf_file = os.path.join(dirname, fileName)
79-
if os.path.isfile(conf_file):
80-
config = configparser.ConfigParser()
81-
config.optionxform = str
82-
config.read(conf_file)
83-
for sect in config.sections():
84-
for name, value in config.items(sect):
74+
class Singleton(type):
75+
_inst = {}
76+
77+
def __call__(clazz, *args, **kwargs):
78+
if clazz not in clazz._inst:
79+
clazz._inst[clazz] = super(Singleton, clazz).__call__(*args, **kwargs)
80+
return clazz._inst[clazz]
81+
82+
83+
class ConfigManager(object, metaclass=Singleton):
84+
''' A singleton class managing the application configuration defaults '''
85+
86+
def __init__(self):
87+
self.__sectOptions = {}
88+
self.__defaults = {}
89+
self.configFiles = ['config.ini']
90+
91+
@property
92+
def options(self):
93+
if not self.__sectOptions:
94+
self.__sectOptions = self.reload()
95+
return self.__sectOptions
96+
97+
@property
98+
def defaults(self):
99+
if not self.__defaults:
100+
self.__defaults = self.parse_defaults()
101+
return self.__defaults
102+
103+
def reload(self):
104+
options = {}
105+
self.__sectOptions = {}
106+
if self.configFiles:
107+
for config in self.configFiles:
108+
options.update(self.readConfigFile(config))
109+
return options
110+
111+
def readConfigFile(self, fileName):
112+
'''parse config file and store values in a dict {section:{ key: value}}'''
113+
options = {}
114+
dirname, filename = os.path.split(os.path.abspath(__file__))
115+
conf_file = os.path.join(dirname, fileName)
116+
if os.path.isfile(conf_file):
117+
try:
118+
config = configparser.ConfigParser()
119+
config.optionxform = str
120+
config.read(conf_file)
121+
for sect in config.sections():
122+
options[sect] = {}
123+
for name, value in config.items(sect):
124+
options[sect][name] = value
125+
except Exception as e:
126+
print(f"cannot read config file {fileName} Exception {e}")
127+
else:
128+
print(f"cannot find config file {fileName} in {dirname}")
129+
return options
130+
131+
def parse_defaults(self):
132+
'''parse all sections parameters to a simple key:value dict'''
133+
defaults = {}
134+
for sect_name, sect_values in self.options.items():
135+
for name, value in sect_values.items():
85136
defaults[name] = value
86-
return defaults
137+
return defaults
87138

88139

89140
def parse_cmd_args(argv):

tests/test_configManager.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from source.confParser import ConfigManager, Singleton
2+
from nose.tools import with_setup
3+
4+
5+
def my_setup():
6+
global a
7+
a = 1
8+
9+
10+
def test_case01():
11+
cm = ConfigManager()
12+
result = cm.readConfigFile('config.ini')
13+
assert isinstance(result, dict)
14+
15+
16+
def test_case02():
17+
cm = ConfigManager()
18+
result = cm.readConfigFile('config.ini')
19+
assert len(result.keys()) > 0
20+
21+
22+
def test_case03():
23+
cm = ConfigManager()
24+
result = cm.readConfigFile('config.ini')
25+
assert 'tls' in result.keys()
26+
27+
28+
def test_case04():
29+
cm = ConfigManager()
30+
result = cm.readConfigFile('config.ini')
31+
connection = result['connection']
32+
assert len(connection) > 0
33+
assert isinstance(connection, dict)
34+
assert len(connection) > 0
35+
assert 'port' in connection.keys()
36+
37+
38+
def test_case05():
39+
cm = ConfigManager()
40+
result = cm.parse_defaults()
41+
assert isinstance(result, dict)
42+
43+
44+
def test_case06():
45+
cm = ConfigManager()
46+
result = cm.parse_defaults()
47+
assert len(result.keys()) > 0
48+
49+
50+
def test_case07():
51+
cm = ConfigManager()
52+
result = cm.parse_defaults()
53+
result1 = cm.defaults
54+
assert len(result) == len(result1)
55+
56+
57+
def test_case08():
58+
cm = ConfigManager()
59+
result = cm.defaults
60+
elements = list(result.keys())
61+
mandatoryItems = ['port', 'serverPort']
62+
assert all(item in elements for item in mandatoryItems)
63+
64+
65+
def test_case09():
66+
cm = ConfigManager()
67+
result = cm.defaults
68+
value = int(result['port'])
69+
assert value == 4242
70+
71+
72+
def test_case10():
73+
cm = ConfigManager()
74+
result = cm.defaults
75+
assert int(result['port']) == 4242 and int(result['serverPort']) == 9084

tests/test_params.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,24 @@
1-
from source.confParser import parse_defaults_from_config_file, merge_defaults_and_args, parse_cmd_args
1+
from source.confParser import ConfigManager, Singleton, merge_defaults_and_args, parse_cmd_args
22
from nose.tools import with_setup
33

44

55
def my_setup():
66
global a, b, c, d, e
7-
a = parse_defaults_from_config_file()
7+
a = ConfigManager().defaults
88
b, c = parse_cmd_args([])
99
d, e = parse_cmd_args(['-p', '8443', '-t', '/etc/my_tls'])
1010

1111

12-
def test_case01():
13-
result = parse_defaults_from_config_file()
14-
assert isinstance(result, dict)
15-
16-
17-
def test_case02():
18-
result = parse_defaults_from_config_file()
19-
assert len(result.keys()) > 0
20-
21-
22-
def test_case03():
23-
result = parse_defaults_from_config_file()
24-
elements = list(result.keys())
25-
mandatoryItems = ['port', 'serverPort']
26-
assert all(item in elements for item in mandatoryItems)
27-
28-
29-
def test_case04():
30-
result = parse_defaults_from_config_file()
31-
value = int(result['port'])
32-
assert value == 4242
33-
34-
35-
def test_case05():
36-
result = parse_defaults_from_config_file()
37-
assert int(result['port']) == 4242 and int(result['serverPort']) == 9084
38-
39-
4012
@with_setup(my_setup)
41-
def test_case06():
13+
def test_case01():
4214
result = merge_defaults_and_args(a, b)
4315
assert len(result.keys()) > 0
4416
assert 'port' in result.keys()
4517
assert 'serverPort' in result.keys()
4618

4719

4820
@with_setup(my_setup)
49-
def test_case07():
21+
def test_case02():
5022
result = merge_defaults_and_args(a, d)
5123
assert len(result.keys()) > 0
5224
assert 'port' in result.keys()

0 commit comments

Comments
 (0)