unterstütung zum einleis von dictionaries
zeilen die mit einem ' in der ems.conf beginnen werden als dict interpretiert. vorbereitung für mehrdimensionale dictionaries
This commit is contained in:
parent
c08f86cf8f
commit
053dff064e
2 changed files with 91 additions and 38 deletions
101
src/config.py
101
src/config.py
|
|
@ -31,42 +31,95 @@ class Config():
|
|||
cls, *args, **kwargs)
|
||||
return cls._instance
|
||||
|
||||
def printConf(self):
|
||||
'''
|
||||
Print the config only if debug is enable.
|
||||
'''
|
||||
if self._confDic['config_debug']:
|
||||
mes = 'config:\t{'
|
||||
for x in self._confDic:
|
||||
mes = '%s\r %s \t: %s' % (mes, x, self._confDic[x])
|
||||
mes = '%s\r }' % mes
|
||||
print mes
|
||||
|
||||
def makeList(self, arg):
|
||||
for a in range(len(arg)):
|
||||
arg[a] = arg[a].strip()
|
||||
arg[a] = True if arg[a] == 'True' else arg[a]
|
||||
arg[a] = False if arg[a] == 'False' else arg[a]
|
||||
return arg
|
||||
|
||||
def getElement(self, line):
|
||||
tmp = line.split("=")
|
||||
key = tmp[0].strip()
|
||||
val = tmp[1].split("#") #cut off comments, comment is on val[1]
|
||||
val = str(val[0].strip()) #overwrite val with string
|
||||
|
||||
if key == '':
|
||||
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)
|
||||
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)
|
||||
return None, None
|
||||
return key, arg
|
||||
|
||||
def readConf(self):
|
||||
if self._confDic['config_read']:
|
||||
if self._confDic['config_read']: #prevent re-read
|
||||
return self._confDic
|
||||
|
||||
try:
|
||||
confFile = open("config\ems.conf", "r")
|
||||
confFile = open(config, "r")
|
||||
except IOError:
|
||||
self._confDic.update({'error' : 'config/ems.conf not found'})
|
||||
self._confDic.update({'error' : '%s not found' % config})
|
||||
stderr.write('config_error %s not found\r' % config)
|
||||
return self._confDic
|
||||
|
||||
# master = None
|
||||
|
||||
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()
|
||||
|
||||
val = ident[1].split("#") #cut off comments
|
||||
#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)
|
||||
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)
|
||||
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
|
||||
self.makeList(arg)
|
||||
arg = arg if len(arg) > 1 else arg[0]
|
||||
|
||||
self._confDic[key] = arg
|
||||
|
||||
self._confDic[ident[0]] = arg if len(arg) > 1 else arg[0]
|
||||
confFile.close()
|
||||
self._confDic['config_read'] = True
|
||||
if self._confDic['config_debug']:
|
||||
print 'config:\t{'
|
||||
for x in self._confDic:
|
||||
print ' ', x, '\t:', self._confDic[x]
|
||||
print ' }'
|
||||
|
||||
self.printConf()
|
||||
return self._confDic
|
||||
|
||||
#c = Config()
|
||||
#conf = c.readConf()
|
||||
c = Config()
|
||||
conf = c.readConf()
|
||||
|
|
|
|||
|
|
@ -19,17 +19,17 @@ config_debug = True
|
|||
#possible Baud Rates: 100k, 125k, 250k, 500k, 1000k
|
||||
can_baudrate = 250k
|
||||
|
||||
#can_messages = dc_battery, dc_grid, dc_pv, dc_charger
|
||||
#'dc_battery = id:0x3A4, length:8, time:50
|
||||
#'dc_grid = id:0x3A5, length:8, time:50
|
||||
#'dc_pv = id:0x3A6, length:8, time:50
|
||||
#'dc_charger = id:0x3A7, length:8, time:50
|
||||
#
|
||||
#can_signales = current, voltage, capacity, isMaster, isFeed, isCharging, isOn
|
||||
#'current = begin:0, length:16, offset:0, scaling:0.001, data:0
|
||||
#'voltage = begin:16, length:16, offset:0, scaling:0.01, data:0
|
||||
#'capacity = begin:32, length:11, offset:0, scaling:0.05, data:0
|
||||
#'isMaster = begin:56, length:1, offset:0, scaling:1, data:0
|
||||
#'isFeed = begin:57, length:1, offset:0, scaling:1, data:0
|
||||
#'isCharging = begin:57, length:1, offset:0, scaling:1, data:0
|
||||
#'isOn = begin:58, length:1, offset:0, scaling:1, data:0
|
||||
can_messages = dc_battery, dc_grid, dc_pv, dc_charger
|
||||
'dc_battery = id:0x3A4, length:8, time:50
|
||||
'dc_grid = id:0x3A5, length:8, time:50
|
||||
'dc_pv = id:0x3A6, length:8, time:50
|
||||
'dc_charger = id:0x3A7, length:8, time:50
|
||||
|
||||
can_signales = current, voltage, capacity, isMaster, isFeed, isCharging, isOn
|
||||
'current = begin:0, length:16, offset:0, scaling:0.001, data:0
|
||||
'voltage = begin:16, length:16, offset:0, scaling:0.01, data:0
|
||||
'capacity = begin:32, length:11, offset:0, scaling:0.05, data:0
|
||||
'isMaster = begin:56, length:1, offset:0, scaling:1, data:0
|
||||
'isFeed = begin:57, length:1, offset:0, scaling:1, data:0
|
||||
'isCharging = begin:57, length:1, offset:0, scaling:1, data:0
|
||||
'isOn = begin:58, length:1, offset:0, scaling:1, data:0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue