-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
54 lines (46 loc) · 1.61 KB
/
config.py
File metadata and controls
54 lines (46 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import configparser, os
import color
from ast import literal_eval
class Config(object):
"""
define a configuration, as it's an object, it will be the same for all plugins
"""
def __init__(self):
"""
initial configuration
"""
print(color.OKBLUE+"Loading configuration file...",color.ENDC)
path = os.path.dirname(os.path.realpath(__file__))
if not os.path.isfile(os.path.join(path,"config.ini")):
print(color.WARNING,"Configuration file not found",color.ENDC)
return
cp = configparser.ConfigParser()
cp.read(os.path.join(path,"config.ini"))
self.settings = dict()
for section in cp:
print(color.OKBLUE+"loading section %s"%(section),color.ENDC)
try:
self.settings.update(cp.items(section))
except Exception as e:
print(color.FAIL+"Error reading the section : %s"%(e),color.ENDC)
print("Loaded the following settings : ")
for k,v in self.settings.items():
print("\t%s = %s"%(k,v))
self.settings[k] = literal_eval(v)
print(color.OKGREEN+"Configuration loaded",color.ENDC)
def get(self,param):
"""
return requested config parameters if exist, else None
"""
param = param.lower()
if param in self.settings:
return self.settings[param]
else:
return None
def set(self,param,value):
"""
set a parameters value
"""
self.settings[param]=value
if __name__ == "__main__":
c = Config()