#!/usr/bin/python2.4 # -*- coding: utf-8 -*- """ Main module Will do all the database connectivity and method dispatching. """ __author__ = "Niels Hoffmann (hoffmnie@fh-brandenburg.de)" import cgi import cgitb; cgitb.enable() import os, StringIO,string from props import * from toqe import tools from toqe import methods from toqe.Errors import * from toqe.db import datasource from toqe.BasicTypes import Concept from toqe.BasicTypes import Relation #from toqe.config import configuration #cfgObj = configuration.getConfigObj() def normalizeFieldStorage(fieldStorage): """Parses the FieldStorage for the needed data.""" # KLUDGE: this is a hack, need the dict to exist # otherwise no checking for not signing dict = {'params' : 'true'} params = fieldStorage for key in params.keys(): dict[key] = params.getvalue(key) if 'method' not in dict: raise NotWellFormedError() method = dict.pop('method') return method, dict #def getCfgObj(): # return cfgObj def dispatcher(method, parameters = None): """expects a method that is defined in methods. calls that method with parameters and returns the result""" try: # get a reference to the method and raise # if not possible methodClass = getattr(methods, method) except: raise NoMethodError() #_cfgObj = getCfgObj() #print parameters thesaurusName = parameters.get('thesaurus','Fauna Europaea') os.chdir('/home/patricia/workspace/thesaurus/log') fichier=open("pouet.txt",'a') fichier.write("thesaurus name %s \n" %thesaurusName) fichier.close() try: propertiesFile = props('/home/patricia/workspace/thesaurus/thesaurusList.cfg') prop = propertiesFile.getProperty configFile=prop(thesaurusName,'file') except Exception,e: os.chdir('/home/patricia/workspace/thesaurus/log') fichier=open("pouet.txt",'a') fichier.write("Exception for %s \n" %e) fichier.close() configFile="/home/patricia/workspace/thesaurus/toqe/config/fauna_euro.xml" myDS = datasource.Datasource(configFile) methodInstance = methodClass(myDS) recursive = parameters.get('recursive',False) if parameters: if recursive: try: return methodInstance.getDataRecursive(parameters) except: return methodInstance.getData(parameters) else: return methodInstance.getData(parameters) else: if recursive: try: return methodInstance.getDataRecursive() except: return methodInstance.getData() else: return methodInstance.getData() def response(success, payload): """Generate the response XML.""" contentType = 'Content-type: text/xml; charset=utf-8\n\n' xmlHeader = '\n' startResponse = '' % (success and 'true' or 'false') endResponse = '' document = '%s%s%s%s%s\n' %(contentType, xmlHeader, startResponse, payload, endResponse) #unicode(document) try: return unicode(document,'latin-1').encode('utf-8') except: return(document) #return document.encode('utf-8') def main(): """Execute the queries send to TOQE.""" parameters = cgi.FieldStorage() try: method, parameterDict = normalizeFieldStorage(parameters) myPayload = dispatcher(method, parameterDict) mySuccess = True except Exception, e: myPayload = e mySuccess = False print response(success = mySuccess, payload = myPayload) # begin cgi output if __name__ == "__main__": main()