-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiguration.py
61 lines (49 loc) · 1.66 KB
/
configuration.py
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
55
56
57
58
59
60
61
import configparser
import os
class Config:
"""Interact with configuration variables."""
parser = configparser.ConfigParser()
configFilePath = (os.path.join(os.getcwd(), 'config.ini'))
parser.read(configFilePath)
@classmethod
def get(cls, section, key):
"""Get prod values from config.ini."""
try:
return cls.parser.get(section, key)
except configparser.NoOptionError:
return ""
@classmethod
def getAllOptions(cls):
config = {}
for section in cls.parser.sections():
options = {}
for key in cls.parser[section]:
value = cls.parser.get(section, key)
options[key] = value
config[section] = options
return config
@classmethod
def update(cls, section, key, value):
if section not in cls.parser.sections():
cls.parser.add_section(section)
cls.parser.set(section, key, value)
with open(cls.configFilePath, 'w') as configfile:
cls.parser.write(configfile)
@classmethod
def getEnvironmentVariables(cls):
# print(os.environ['NGSI_HOST'])
envMap = {}
env = ['NGSI_ADDRESS', 'SE_HOST', 'SE_PORT', 'SE_CALLBACK']
for v in env:
envMap[v] = Config.getEnvironmentVariable(v)
return envMap
@classmethod
def getEnvironmentVariable(cls, variable):
try:
return os.environ[variable]
except KeyError:
return None
if __name__ == "__main__":
print(Config.get('NGSI', 'host'))
print(Config.getAllOptions())
Config.update("testsection", "testkey", "testvalue")