2013-11-21 18:06:51 +01:00
|
|
|
'''
|
|
|
|
|
Created on 21.11.2013
|
|
|
|
|
|
|
|
|
|
@author: rauchp
|
|
|
|
|
'''
|
2013-11-22 14:25:12 +01:00
|
|
|
class Config():
|
|
|
|
|
_instance = None
|
2013-11-21 18:06:51 +01:00
|
|
|
|
2013-11-22 14:25:12 +01:00
|
|
|
_confDic = {
|
|
|
|
|
'mySQL_server': 'localhost',
|
|
|
|
|
'mySQL_port': '3306',
|
|
|
|
|
'mySQL_user': 'smoke',
|
|
|
|
|
'mySQL_pass': 'KiWujcafAlor',
|
|
|
|
|
'mySQL_database': 'smoke_test',
|
2013-11-28 12:41:01 +01:00
|
|
|
#'mySQL_table': 'battery',
|
2013-11-22 16:01:43 +01:00
|
|
|
'mySQL_speed' : '0.1',
|
2013-11-22 14:25:12 +01:00
|
|
|
'flask_server': '0.0.0.0',
|
|
|
|
|
'flask_port': '5000',
|
|
|
|
|
'flask_debug': False,
|
|
|
|
|
'config_debug' : False,
|
|
|
|
|
'config_read' : False
|
|
|
|
|
}
|
2013-11-21 18:11:38 +01:00
|
|
|
|
2013-11-22 14:25:12 +01:00
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
|
# http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern
|
|
|
|
|
if not cls._instance:
|
|
|
|
|
cls._instance = super(Config, cls).__new__(
|
|
|
|
|
cls, *args, **kwargs)
|
|
|
|
|
return cls._instance
|
2013-11-21 18:11:38 +01:00
|
|
|
|
2013-11-22 14:25:12 +01:00
|
|
|
def readConf(self):
|
|
|
|
|
if self._confDic['config_read']:
|
|
|
|
|
return self._confDic
|
2013-11-21 18:11:38 +01:00
|
|
|
|
2013-11-22 14:25:12 +01:00
|
|
|
try:
|
|
|
|
|
confFile = open("config\ems.conf", "r")
|
|
|
|
|
except IOError:
|
|
|
|
|
self._confDic.update({'error' : 'config/ems.conf not found'})
|
|
|
|
|
return self._confDic
|
2013-11-21 18:06:51 +01:00
|
|
|
|
2013-11-22 14:25:12 +01:00
|
|
|
for line in confFile:
|
|
|
|
|
line = line.strip()
|
|
|
|
|
if len(line) > 0 and not line[0] == "#":
|
|
|
|
|
ident = line.split("=")
|
|
|
|
|
for i in range(len(ident)):
|
|
|
|
|
ident[i] = ident[i].strip()
|
2013-11-28 12:41:01 +01:00
|
|
|
|
2013-11-22 14:25:12 +01:00
|
|
|
val = ident[1].split("#") #cut off comments
|
2013-11-28 12:41:01 +01:00
|
|
|
#comment is on val[1]
|
|
|
|
|
argument = str(val[0].strip()).split(',')
|
|
|
|
|
arg = []
|
|
|
|
|
for a in argument:
|
|
|
|
|
a.strip()
|
|
|
|
|
a = True if a == 'True' else a
|
|
|
|
|
a = False if a == 'False' else a
|
|
|
|
|
arg.append(a)
|
|
|
|
|
|
|
|
|
|
self._confDic[ident[0]] = arg if len(arg) > 1 else arg[0]
|
2013-11-22 14:25:12 +01:00
|
|
|
confFile.close()
|
|
|
|
|
self._confDic['config_read'] = True
|
|
|
|
|
if self._confDic['config_debug']:
|
2013-11-28 12:41:01 +01:00
|
|
|
print 'config:\t{'
|
|
|
|
|
for x in self._confDic:
|
|
|
|
|
print ' ', x, '\t:', self._confDic[x]
|
|
|
|
|
print ' }'
|
2013-11-22 16:01:43 +01:00
|
|
|
return self._confDic
|
2013-11-28 12:41:01 +01:00
|
|
|
|
|
|
|
|
#c = Config()
|
|
|
|
|
#conf = c.readConf()
|