#!/usr/bin/env python
import types, logging
import cElementTree as ElementTree
import cherrypy
import DBSManager
from XMLToDict import *
URL = "http://localhost:8080"
DBS = DBSManager.DBSManager()
DBS_services = DBSManager.DBSManager.__dict__.keys()
DBS_services.sort()
def makeDict(id):
"""
Helper function. Return dictionary {'id':value} out of input id value.
"""
d = {}
d['id']=id
return d
class Main:
"""
DBS server main class. It defines the content of server web page.
"""
def index(self):
"""
Create server 'index.html' web page. Currently, we list all DBS services with their
documention (taken from their API implementation description).
"""
msg = "
DBS Server home page
"
msg+= ""
msg+= "List of available services:
"
msg+= "
"
for item in DBS_services:
if string.find(item,"__")!=-1: continue
msg+= "- %s"%(URL,item)
msg+= "
"
if DBSManager.DBSManager.__dict__[item].__doc__:
msg+= DBSManager.DBSManager.__dict__[item].__doc__
else:
msg+="Documentation is not available"
msg+= "
"
msg+= "
"
return msg
index.exposed = True
class DBSAccess:
"""
DBS server access class. It provides access to all available DBS services.
"""
def __init__(self,verbose=0):
self.logger = logging.getLogger("DBS server")
self.logger.setLevel(verbose)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(verbose)
# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
self.logger.addHandler(ch)
def index(self):
"""
Create class 'index.html' web page.
"""
return '''
'''%'test'
index.exposed = True
def execute(self, iMethod):
"""
DBS server access class. It provides access to all available DBS services.
Particular service can be accessed via %s/dbs/execute?iMethod=
by sending to this URL the XML instruction. For example, to create primary
dataset with name='client_datasetName' we use the following content:
client_datasetName
and send it to %s/dbs/execute?iMethod=createPrimaryDataset
"""%(URL,URL)
self.logger.info(iMethod)
xml_string = cherrypy.request.body.read()
self.logger.debug(xml_string)
xmldict = XmlDictConfig(ElementTree.XML(xml_string))
self.logger.info(xmldict)
if len(xmldict.keys())==1 and xmldict.has_key('pattern'):
res = DBSManager.DBSManager.__dict__[iMethod](DBS,xmldict['pattern'])
else:
res = DBSManager.DBSManager.__dict__[iMethod](DBS,xmldict)
self.logger.info(res)
if res is types.DictType:
return makeXML(res)
elif type(res) is types.ListType:
# Loop over all entries in a list and create XML output
# for testing, we return first element in the list
print "############ ",res
return makeXML(res)
elif type(res) is types.IntType:
return makeXML(makeDict(res))
else:
msg = "Uknown data type returned: "
msg+=type(res)
raise msg
execute.exposed = True
cherrypy.root = Main()
cherrypy.root.dbs = DBSAccess(logging.INFO)
if __name__ == '__main__':
cherrypy.config.update(file="CherryServer.conf")
cherrypy.server.start()