code aufgeräumt
This commit is contained in:
parent
bd216d5222
commit
25e406ed8c
6 changed files with 62 additions and 78 deletions
|
|
@ -3,7 +3,7 @@ Created on 21.11.2013
|
|||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
from sys import stderr
|
||||
from sys import stderr, exit
|
||||
|
||||
config = "config\ems.conf"
|
||||
|
||||
|
|
@ -11,19 +11,19 @@ class Config():
|
|||
_instance = None
|
||||
|
||||
_confDic = {
|
||||
'mySQL_server': 'localhost',
|
||||
'mySQL_server': '',
|
||||
'mySQL_port': '3306',
|
||||
'mySQL_user': 'smoke',
|
||||
'mySQL_pass': 'KiWujcafAlor',
|
||||
'mySQL_database': 'smoke_test',
|
||||
#'mySQL_table': 'battery',
|
||||
'mySQL_speed' : '0.1',
|
||||
'mySQL_user': '',
|
||||
'mySQL_pass': '',
|
||||
'mySQL_database': '',
|
||||
'mySQL_speed' : '0.02',
|
||||
'flask_server': '0.0.0.0',
|
||||
'flask_port': '5000',
|
||||
'flask_debug': False,
|
||||
'config_debug' : False,
|
||||
'config_read' : False,
|
||||
'config_dictionary' : 'config'
|
||||
'config_dictionary' : 'config',
|
||||
'can_baudrate' : '500k',
|
||||
'can_symfile' : ''
|
||||
}
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
|
|
@ -41,7 +41,7 @@ class Config():
|
|||
mes = 'config:\t{'
|
||||
for x in self._confDic:
|
||||
mes = '%s\r %s \t: %s' % (mes, x, self._confDic[x])
|
||||
mes = '%s\r }' % mes
|
||||
mes = '%s\r }\n' % mes
|
||||
print mes
|
||||
|
||||
def makeList(self, arg):
|
||||
|
|
@ -58,17 +58,17 @@ class Config():
|
|||
val = str(val[0].strip()) #overwrite val with string
|
||||
|
||||
if key == '':
|
||||
stderr.write('config_error there is an empty key\r')
|
||||
stderr.write('config_error: there is an empty key\r')
|
||||
return None, None
|
||||
if val == '':
|
||||
stderr.write('config_error %s has no value\r' % key)
|
||||
stderr.write('config_error: %s has no value\r' % key)
|
||||
return None, None
|
||||
|
||||
arg = val.split(',')
|
||||
while arg.count('') > 0:
|
||||
arg.remove('')
|
||||
if len(arg) == 0:
|
||||
stderr.write('config_error %s has an empty argument\r' % key)
|
||||
stderr.write('config_error: %s has an empty argument\r' % key)
|
||||
return None, None
|
||||
return key, arg
|
||||
|
||||
|
|
@ -83,46 +83,36 @@ class Config():
|
|||
stderr.write('config_error %s not found\r' % config)
|
||||
return self._confDic
|
||||
|
||||
# master = None
|
||||
_linenumber = 0
|
||||
|
||||
for line in confFile:
|
||||
_linenumber += 1
|
||||
line = line.strip()
|
||||
|
||||
if len(line) == 0 or line[0] == "#": #remove empty lines and comments
|
||||
# master = None
|
||||
continue
|
||||
|
||||
if line[0] == '\'':
|
||||
line = line.lstrip('\'')
|
||||
# if master is not None:
|
||||
key, arg = self.getElement(line)
|
||||
if key == None:
|
||||
continue #skip errors
|
||||
res = []
|
||||
for a in arg:
|
||||
tmp = a.split(':')
|
||||
for t in tmp:
|
||||
res.append(t.strip())
|
||||
arg = dict(res[i:i+2] for i in range(0, len(res), 2))
|
||||
### prepared for more dimensional dictionaries ###
|
||||
# elif line.find(':') != -1:
|
||||
# master = line.split(':')[0]
|
||||
# key = master
|
||||
# arg = None
|
||||
# pass
|
||||
elif line.find("=") != -1:
|
||||
key, arg = self.getElement(line)
|
||||
if key == None:
|
||||
continue #skip errors
|
||||
self.makeList(arg)
|
||||
arg = arg if len(arg) > 1 else arg[0]
|
||||
if line.find("=") == -1:
|
||||
exit('config_error: an error has occurred in line %s\r' % _linenumber)
|
||||
|
||||
key, arg = self.getElement(line)
|
||||
if key == None:
|
||||
continue #skip errors
|
||||
self.makeList(arg)
|
||||
arg = arg if len(arg) > 1 else arg[0]
|
||||
|
||||
self._confDic[key] = arg
|
||||
|
||||
confFile.close()
|
||||
self._confDic['config_read'] = True
|
||||
|
||||
self.printConf()
|
||||
for key in self._confDic:
|
||||
if self._confDic[key] == '':
|
||||
exit('please select a value for {0}'.format(key))
|
||||
|
||||
if self._confDic['config_debug']:
|
||||
self.printConf()
|
||||
|
||||
return self._confDic
|
||||
|
||||
c = Config()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue