ems/scr/ems.py
Philipp Rauch 0d0e49d684 Switch modularized
*the switch is prepared to load different modules
*module database is implemented
*code cleanup
*config can load lists
*add global debug mode
*ems has a method to update the buffer
2013-11-28 12:41:01 +01:00

94 lines
2.4 KiB
Python

'''
Created on 15.11.2013
@author: Philipp Rauch
@version: 0.02
'''
from threading import Thread
from switch import Switch, MYSQL
from config import Config
### LOAD CONFIG ###
c = Config()
conf = c.readConf()
def startSwitch():
swich = Switch(MYSQL)
queue, query = swich.initialisiere()
if conf['config_debug']:
print 'SWITCH-Thread:\t', swich
print '\tEMS-QUERY:\t', query
print '\tEMS-QUEUE:\t', queue
swich.start()
return queue, query
class ems(Thread):
def __init__(self, buf):
Thread.__init__(self)
self.buffer = buf
self.queue, self.query = startSwitch()
if conf['config_debug']:
print '\tEMS-BUFFER:\t', buf
def run(self):
old = None
while True:
new = self.getNewMsg(old)
if conf['config_debug']:
print 'GET:\t', new
self.updateBuffer(new)
old = new
def getNewMsg(self, old):
'''
A blocking Method to get a different Message from Queue
@param old: the old message
'''
tmp = self.queue.get()
while tmp == old:
tmp = self.queue.get()
return tmp
def updateBuffer(self, push):
'''
Method to update the Buffer on the given path
@param push: message to push in the buffer
construction: key is the path
value is the dict
'''
## Test of valid push message ##
if not isinstance(push, dict):
print 'error wrong parameter: Type', push.__class__.__name__, 'expect Type dict'
return
if len(push.keys()) not in [1]:
print 'error wrong number of arguments:', len(push.keys()), 'expect 1'
return
if not isinstance(push.get(push.keys()[0]) ,dict):
print 'error value is not dict'
return
key = push.keys()[0]
value = push[key]
path = key.split('/')
if path[0] == '':
path.remove('')
sys = self.buffer.system
for key in path:
try:
sys = sys[key]
except KeyError:
print 'error wrong path'
return
sys.update(value)
pass
def getRequest(self):
#TODO: get Request from buffer
#TODO: define a request in buffer
pass