144 lines
5.3 KiB
Python
144 lines
5.3 KiB
Python
from syslab.whiteboard.WhiteBoardEntry import WhiteBoardEntry
|
|
|
|
class CommModule():
|
|
__BASE_URL = 'http://whiteboard.syslab.dk/'
|
|
|
|
def __init__(self, namespace):
|
|
if not type(namespace) is str or len(namespace) == 0: raise TypeError('Namespace should be an non-empty string, found "{0}"'.format(type(namespace)))
|
|
|
|
self.__namespace = namespace
|
|
self.__entries = {}
|
|
|
|
def appendValue(self, key, value):
|
|
values = self.getList(key)
|
|
|
|
if values == None:
|
|
self.publishToWhiteBoardServer(key, '[{0}]'.format(str(value)))
|
|
else:
|
|
values.append(value)
|
|
self.publishList(key, values)
|
|
|
|
def getList(self, key):
|
|
from ast import literal_eval
|
|
if not type(key) is str: raise TypeError('Key should be a string, found {0}'.format(type(key)))
|
|
|
|
entry = self.getFromWhiteBoardServer(key)
|
|
|
|
values = None
|
|
if entry is not None:
|
|
values_str = str(entry.value)
|
|
values_str = values_str.replace('[','["').replace(']','"]'.replace(',','","'))
|
|
values = literal_eval(values_str)
|
|
if type(values) is not list:
|
|
values = [values, ]
|
|
return values
|
|
|
|
def getAllEntries(self):
|
|
return self.__entries
|
|
|
|
def update(self):
|
|
from requests import get, auth
|
|
import re
|
|
|
|
url = CommModule.__BASE_URL + 'wbget.php'
|
|
r = get(url, auth=auth.HTTPBasicAuth('twinPV99', 'twinPV99'))
|
|
entries = r.text.split('\n')
|
|
|
|
result = {}
|
|
for entry in entries:
|
|
entry = entry.rstrip().lstrip()
|
|
g = re.match('SYSLAB@(\d+):{0}::(\w+)=(.+);'.format(self.__namespace), entry)
|
|
if g is None:
|
|
continue
|
|
else:
|
|
g = g.groups()
|
|
self.__entries[g[1]] = WhiteBoardEntry(g[1], g[2], g[0])
|
|
|
|
def printEntries(self):
|
|
if len(self.__entries)==0:
|
|
print('No WhiteBoard entries found for namespace: {0}'.format(self.__namespace))
|
|
return
|
|
|
|
for e in self.__entries:
|
|
print(e)
|
|
|
|
def publishList(self, key, values):
|
|
from requests import post, auth
|
|
if not type(key) is str: raise TypeError('Key should be a string, found {0}'.format(type(key)))
|
|
if not type(values) is list: raise TypeError('Values should be represented by a list'.format(type(values)))
|
|
|
|
values_str = "[{0}]".format(",".join(map(str,values)))
|
|
|
|
url = '{0}{1}?source=SYSLAB&{2}::{3}={4}'.format(CommModule.__BASE_URL, 'wbset.php', self.__namespace, str(key), values_str)
|
|
post(url, auth=auth.HTTPBasicAuth('twinPV99', 'twinPV99'))
|
|
|
|
def getFromWhiteBoardServer(self, key):
|
|
"""
|
|
Go in and read/write the value from a key from the whiteboard. The value
|
|
should use decimal dot, not comma!
|
|
"""
|
|
from requests import get, auth
|
|
import re
|
|
|
|
if not type(key) is str: raise TypeError('Key should be a string, found {0}'.format(type(key)))
|
|
if key is '': raise TypeError('Key should be an empty string')
|
|
|
|
url = CommModule.__BASE_URL+'wbget.php'
|
|
r = get(url, auth=auth.HTTPBasicAuth('twinPV99', 'twinPV99'))
|
|
entries = r.text.split('\n')
|
|
|
|
result = None
|
|
|
|
for entry in entries:
|
|
entry = entry.rstrip().lstrip()
|
|
g = re.match('SYSLAB@(\d+):{0}::{1}=(.+);'.format(self.__namespace, key), entry)
|
|
if g is None:
|
|
continue
|
|
else:
|
|
g = g.groups()
|
|
result = WhiteBoardEntry(key, g[1], g[0])
|
|
break
|
|
|
|
return result
|
|
|
|
def publishToWhiteBoardServer(self, key, value):
|
|
from requests import post, auth
|
|
if not type(key) is str: raise TypeError('Key should be a string, found {0}'.format(type(key)))
|
|
if type(value) is list or type(value) is dict or type(value) is tuple: raise TypeError('This function only supports single values, found {0}'.format(type(value)))
|
|
|
|
url = '{0}{1}?source=SYSLAB&{2}::{3}={4}'.format(CommModule.__BASE_URL, 'wbset.php', self.__namespace, str(key), str(value))
|
|
post(url, auth=auth.HTTPBasicAuth('twinPV99', 'twinPV99'))
|
|
|
|
def getPoolKeys(self):
|
|
from requests import get, auth
|
|
import re
|
|
|
|
url = CommModule.__BASE_URL+'wbget.php'
|
|
r = get(url, auth=auth.HTTPBasicAuth('twinPV99', 'twinPV99'))
|
|
entries = r.text.split('\n')
|
|
|
|
result = {}
|
|
for entry in entries:
|
|
entry = entry.rstrip().lstrip()
|
|
g = re.match('SYSLAB@(\d+):{0}::(\w+)=(.+)'.format(self.__namespace), entry)
|
|
if g is None:
|
|
continue
|
|
else:
|
|
g = g.groups()
|
|
result[g[1]] = g[2]
|
|
|
|
return result
|
|
|
|
def clearKey(self, key):
|
|
from requests import post, auth
|
|
if not type(key) is str: raise TypeError('Key should be a string, found {0}'.format(type(key)))
|
|
|
|
url = '{0}{1}?source=SYSLAB&key={2}::{3}'.format(CommModule.__BASE_URL, 'wbclean.php', self.__namespace, str(key))
|
|
post(url, auth=auth.HTTPBasicAuth('twinPV99', 'twinPV99'))
|
|
|
|
def clearAllKeys(self):
|
|
entries = self.getPoolKeys()
|
|
|
|
for key in entries.keys():
|
|
self.clearKey(key)
|