-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.py
71 lines (59 loc) · 2 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
62
63
64
65
66
67
68
69
70
71
import configparser
import os
import sys
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
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):
# envMap = {}
# env = ['NGSI_ADDRESS', 'SE_HOST', 'SE_PORT', 'SE_CALLBACK']
# for v in env:
# envMap[v] = Config.getEnvironmentVariable(v)
# return envMap
return os.environ
@classmethod
def getEnvironmentVariable(cls, variable, default=None):
try:
return os.environ[variable]
except KeyError:
return default
@classmethod
def showEnvironmentVariables(cls):
for key in os.environ:
eprint("ENV:", key, "=", os.environ[key])
@classmethod
def setEnvironmentVariable(cls, variable, newValue):
os.environ[variable] = newValue
if __name__ == "__main__":
print(Config.get('NGSI', 'host'))
print(Config.getAllOptions())
Config.update("testsection", "testkey", "testvalue")