Beispiel #1
0
    def parse_inputs(self):

        # TODO: Set by config, can be overridden at runtime
        retain_blanks = True

        fs = FieldStorage(self.request, retain_blanks)

        for n, v in fs.items():

            if not isinstance(v, list):
                v = [v]

            for x in v:

                if type(x.file) == file:

                    tmpfile = self.core.file_handler._temp_from_input(x)

                    try:
                        self.file_input[n] = tmpfile
                    except AttributeError:
                        self.file_input = {n: tmpfile}

                    del fs[n]

        return fs
Beispiel #2
0
def normalize_args(parser, mpreq):
    """
    Normalizer for Python's cgi library.
    """
    args = {}
    fs = FieldStorage(mpreq)

    # Cast down from the FieldStorage object to Python str.
    for name in fs.keys():
        value = fs[name]
        if isinstance(value, str):
            value = str(value)

        elif isinstance(value, Field):
            value = FileUpload(value)

        elif isinstance(value, list):
            for i in range(len(value)):
                if isinstance(value[i], str):
                    value[i] = str(value[i])

                elif isinstance(value[i], Field):
                    value[i] = FileUpload(value[i])

        if name:  # mod_python sometimes gives a None key
            args[name] = value

    return args
Beispiel #3
0
 def getlist(self, name, fce = None):
     """Returns list of values of key from \b GET or \b POST form.
     @param name key
     @param fce function which processed value. For example str or int
     """
     if fce:
         return map(fce, APFieldStorage.getlist(self, name))
     return APFieldStorage.getlist(self, name)
Beispiel #4
0
 def getlist(self, name, fce=None):
     """Returns list of values of key from \b GET or \b POST form.
     @param name key
     @param fce function which processed value. For example str or int
     """
     if fce:
         return map(fce, APFieldStorage.getlist(self, name))
     return APFieldStorage.getlist(self, name)
Beispiel #5
0
 def getfirst(self, name, default = None, fce = None):
     """Returns value of key from \b GET or \b POST form.
     @param name key
     @param default default value if is not set
     @param fce function which processed value. For example str or int
     """
     if fce:
         return fce(APFieldStorage.getfirst(self, name, default))
     return APFieldStorage.getfirst(self, name, default)
Beispiel #6
0
 def getfirst(self, name, default=None, fce=None):
     """Returns value of key from \b GET or \b POST form.
     @param name key
     @param default default value if is not set
     @param fce function which processed value. For example str or int
     """
     if fce:
         return fce(APFieldStorage.getfirst(self, name, default))
     return APFieldStorage.getfirst(self, name, default)
Beispiel #7
0
def handler(req):
    global db, htmlPath, logPath, cheshirePath, xmlp, recordStore
    form = FieldStorage(req)
    try:
        dir = req.uri[1:].rsplit('/')[1]
    except IndexError:
        return apache.HTTP_NOT_FOUND
    remote_host = req.get_remote_host(apache.REMOTE_NOLOOKUP)
    lgr = FileLogger(logPath, remote_host) 
#    lgr.log(req.uri)
#    lgr.log('directory is %s' % dir)
#    if dir == 'index.html' :
#        page = read_file(os.path.join(cheshirePath, 'clic', 'www', 'dickens', 'html', 'index.html'))
#        req.write(page)
#        #req.sendfile(os.path.join(cheshirePath, 'clic', 'www', 'dickens', 'html' + dir))
#        return apache.OK
    if dir in ['css', 'js', 'img', 'images']:
        #raise ValueError(os.path.join(cheshirePath, 'clic', 'www' + req.uri))
        req.sendfile(os.path.join(cheshirePath, 'clic', 'www' + req.uri))
        return apache.OK
    else:        
        try:
            remote_host = req.get_remote_host(apache.REMOTE_NOLOOKUP)     # get the remote host's IP for logging
            os.chdir(htmlPath)                                            # cd to where html fragments are
            lgr = FileLogger(logPath, remote_host)
            # Determine whether to use a sub-handler      
            if form.get('operation', None) =='search':
                handler = SearchHandler(lgr)                                # initialise handler - with logger for this request
            elif form.get('operation', None) =='browse':
                handler = BrowseHandler(lgr)
            else:
                req.content_type = "text/html"
                page = read_file('dickensInterface.html')
                req.write(page)
                #return apache.HTTP_NOT_FOUND
                return apache.OK
            # Handle request
            try:
                handler.handle(req)
            finally:
                # Clean-up
                # Flush all logged strings to disk
                try:
                    lgr.flush()
                except:
                    pass
                # Delete handler to ensure no state info is retained
                del lgr, handler
        except:
            req.content_type = "text/html"
            cgitb.Hook(file = req).handle()                               # give error info
            return apache.HTTP_INTERNAL_SERVER_ERROR
        else:
            return apache.OK
Beispiel #8
0
def _reqArgs(req):
    """
    Returns a dictionary of name->value pairs for request's arguments and fields.
    """
    result = {}
    data = FieldStorage(req, keep_blank_values=1)
    for fieldName in data.keys():
        fieldData = data[fieldName] 
        if type(fieldData) is list:  # on POST requests if there is name collision between a parameter and a field
            result[fieldName] = fieldData[-1].value
        else:
            result[fieldName] = fieldData.value
    return result
Beispiel #9
0
def _reqArgs(req):
    """
    Returns a dictionary of name->value pairs for request's arguments and fields.
    """
    result = {}
    data = FieldStorage(req, keep_blank_values=1)
    for fieldName in data.keys():
        fieldData = data[fieldName] 
        if type(fieldData) is list:  # on POST requests if there is name collision between a parameter and a field
            result[fieldName] = fieldData[-1].value
        else:
            result[fieldName] = fieldData.value
    return result
Beispiel #10
0
 def handle(self, req):
     form = FieldStorage(req)
     mode = form.get('mode', None)
     if (mode == 'compare'):
         page = self.compareIndexes(req)
         self.send_xml(page, req)
     elif (mode == 'index') :
         page = self.getIndexList(req)
         self.send_xml(page, req)
     elif (mode == 'statstable') :
         page = self.getStatsTable(req)
         self.send_xml(page, req)
     elif (mode == 'tfp') :
         page = self.create_TFP(form)
         self.send_xml(page, req)
Beispiel #11
0
    def handle_getConfig(self, req):
        # fetch configuration (eg for editing existing workflow)

        store = FieldStorage(req)
        try:
            objid = store['contextId']
            context = serv.get_object(session, objid)
        except:
            self.send_xml("<fault>Unknown object: %s</fault>" % objid, req);
            return
        try:
            target = store['targetId']
            obj = context.get_object(session, target)
        except:
            raise
            self.send_xml("<fault>Unknown object: %s</fault>" % target, req);
            return

        config = obj.parent.get_config(session, target)
        cfgStr = config.getAttributeNS(None, 'configStore')
        if cfgStr:
            confStore = context.get_object(session, cfgStr)
            try:
                del context.subConfigs[target]
                del context.objects[target]
            except KeyError, e:
                pass

            # now re-retrieve from confStore
            rec = confStore.fetch_record(session, target)            
            dom = rec.get_dom(session)
            config = dom.childNodes[0]
            config.setAttributeNS(None, 'configStore', cfgStr)
            context.subConfigs[target] = config
Beispiel #12
0
    def handle_docs(self, req):
        # retrieve documentation for object
        store = FieldStorage(req)
        try:
            objid = store['contextId']
            context = serv.get_object(session, objid)
            target = store['targetId']
            obj = context.get_object(session, target)
        except:
            obj = None
        if not obj:
            self.send_xml("<fault>Unknown object</fault>", req);
            return

        docs = []
        # look on object for manual docs
        if obj.docstring:
            docs.append('<doc from="%s">' % obj.id)
            docs.append(obj.docstring)
            docs.append("</doc>")
        classList = [obj.__class__]
        done = [];
        while classList:
            cl = classList.pop(0)
            if cl.__module__ != '__builtin__' and cl.__doc__ and not (cl in done):
                docs.append('<doc from="%s.%s">' % (cl.__module__, cl.__name__))
                docs.append(cl.__doc__)
                docs.append("</doc>")
                done.append(cl)
            classList.extend(list(cl.__bases__))
        
        self.send_xml("<docs>" + '\n'.join(docs) + "</docs>", req)
Beispiel #13
0
    def handle_classDocs(self, req):
        # retrieve documentation for object
        store = FieldStorage(req)
        try:
            cls = store['className']
        except:
            self.send_xml("<fault>Missing className</fault>", req);
            return
        try:
            obj = arch.find_class(cls)
        except:
            self.send_xml("<fault>No such class: %s</fault>" % cls, req)

        docs = []
        classList = [obj]
        done = [];
        while classList:
            cl = classList.pop(0)
            if cl.__module__ != '__builtin__' and cl.__doc__ and not (cl in done):
                docs.append('<doc from="%s.%s">' % (cl.__module__, cl.__name__))
                docs.append(cl.__doc__)
                docs.append("</doc>")
                done.append(cl)
            classList.extend(list(cl.__bases__))
        
        self.send_xml("<docs>" + '\n'.join(docs) + "</docs>", req)
Beispiel #14
0
    def dispatch(self, req):
        global configs, oaiDcReader, c3OaiServers
        path = req.uri[1:]
        if (path[-1] == "/"):
            path = path[:-1]

        if configs.has_key(path):
            args = {}
            # parse options out of req
            store = FieldStorage(req)
            for qp in store.list:
                args[qp.name] = qp.value
            try:
                oaixml = MinimalOaiServer(c3OaiServers[path],
                                          c3OaiServers[path].metadataRegistry)
            except KeyError:
                oai = Cheshire3OaiServer(path)
                c3OaiServers[path] = oai
                oaixml = MinimalOaiServer(oai, oai.metadataRegistry)
            try:
                xmlresp = oaixml.handleRequest(args)
            except DatestampError:
                try:
                    raise BadArgumentError('Invalid date format supplied.')
                except:
                    xmlresp = oaixml.handleException(args, sys.exc_info())
            except C3Exception, e:
                xmlresp = '<c3:error xmlns:c3="http://www.cheshire3.org/schemas/error" code="%s">%s</c3:error>' % (
                    str(e.__class__).split('.')[-1], e.reason)
            self.send_xml(xmlresp, req)
Beispiel #15
0
class Args:
    def __init__( self, req ):
        from mod_python.util import FieldStorage
        self.fieldStorage = FieldStorage( req, True )

    # return a list of values for the given key,
    # or None if key not there
    def get( self, var ):
        retVar = self.fieldStorage.getlist( var )
        if len( retVar ) == 0:
            return None
        else:
            return retVar

    # return a list of tuples,
    # (key, value) where value is a list of values
    def items( self ):
        result = [];
        for key in self.fieldStorage.keys():
            result.append( (key, self.fieldStorage.getlist( key ) ) )
        return result
Beispiel #16
0
 def handle(self, req):
     form = FieldStorage(req)        
     mode = form.get('mode', None)
     if (mode == 'search'):
         page = self.search(req)
         self.send_xml(page,req)
     elif (mode=='collocates'):
         page = self.collocates(form)
         self.send_xml(page,req)
     elif (mode=='exportkwic'):
         page = self.exportkwic(form)
         self.send_txt(page, req)
         return
     elif (mode == 'concordance'):
         page = self.concordance(form)
         self.send_xml(page, req)
     elif (mode=='arm'):
         page = self.arm(form)
         self.send_xml(page,req)
     elif (mode=='armtable'):
         page = self.armTable(form)
         self.send_xml(page,req)
     elif (mode=='article'):
         page = self.articleDisplay(form)
         self.send_html(page, req)
     elif (mode=='browse'):
         page = self.articleBrowse(form)
         self.send_html(page, req)
     elif (mode=='sort'):
         page = self.sort(form)
         self.send_xml(page, req)
     elif (mode=='filter'):
         page = self.filter(form)
         self.send_xml(page, req)
     elif (mode=='cfp'):
         page = self.create_cfp(form)
         self.send_xml(page, req)
     else :
         page = read_file('search.html')
         self.send_html(page, req)
Beispiel #17
0
    def handle_listFunctions(self, req):
        # retrieve list of function on object

        store = FieldStorage(req)
        try:
            objid = store['contextId']
            context = serv.get_object(session, objid)
        except:
            self.send_xml("<fault>Unknown object: %s</fault>" % (objid), req)
            return
        try:
            target = store['targetId']
            if target[:8] == "default ":
                # use random object of this type?
                otype = target[8:]
                obj = context.get_path(session, otype)
            else:
                obj = context.get_object(session, target)
            select = store['select']
        except:
            self.send_xml("<fault>Unknown object: %s</fault>" % (target), req)
            return

        properties = dir(obj)
        imt = type(self.handle_listObjects)
        funcs = []
        for p in properties:
            if p[0] != "_" and type(getattr(obj, p)) == imt:
                funcs.append(p)
        xml = [
            '<select xmlns="http://www.w3.org/1999/xhtml" id="functionNameSelect" onchange="set_function()">'
        ]
        xml.append('<option value="">(No Function)</option>')

        boringFns = [
            'add_logging', 'add_auth', 'get_setting', 'get_path',
            'get_default', 'get_object', 'get_config', 'remove_logging',
            'remove_auth'
        ]
        for f in funcs:
            if f in boringFns:
                # emphasize fns not everywhere
                name = '<span style="color:#888888;">%s</span>' % f
            else:
                name = f
            if f == select:
                xml.append('<option value="%s" selected="true">%s</option>' %
                           (f, name))
            else:
                xml.append('<option value="%s">%s</option>' % (f, name))
        xml.append("</select>")
        self.send_xml(''.join(xml), req)
Beispiel #18
0
    def handle_listTypedObjects(self, req):
        # get list of typed objects, eg loggers

        storeHash = {}
        collHash = {}
        processHash = {}

        # get start obj from request
        store = FieldStorage(req)
        try:
            objid = store['contextId']
        except:
            self.send_xml("<fault>Missing contextId</fault>", req)
            return
        try:
            obj = serv.get_object(session, objid)
        except:
            self.send_xml("<fault>Unknown object: '%s'</fault>" % objid, req)
            return
        try:
            objType = store['objectType']
        except:
            self.send_xml("<fault>Missing parameter: objectType</fault>")
            return
        try:
            dflt = store['default']
        except:
            dflt = None

        oids = []
        while obj:
            for oid in obj.subConfigs.keys():
                dom = obj.subConfigs[oid]
                t = dom.getAttributeNS(None, 'type')
                if t == objType and oid != dflt:
                    oids.append(oid)
            obj = obj.parent

        oids.sort()
        html = [
            '<select id="loggerObjectSelect" xmlns="http://www.w3.org/1999/xhtml">'
        ]
        if dflt:
            html.append('<option value="%s">%s</option>' % (dflt, dflt))
        html.append('<option value="">Default Logger</option>')

        for o in oids:
            html.append('<option value="%s">%s</option>' % (o, o))
        html.append('</select>')

        xml = '\n'.join(html)
        self.send_xml(xml, req)
    def handle(self, req):
        path = req.uri[5:]
        form = FieldStorage(req)

        strict = form.get('strict', True)
        if strict in ['false', 'False', '0', None, '']:
            strict = False

        mt = form.get('mimeType', '')
        mt = mt.replace(' ', '+')

        if not mt:
            xtn = form.get('extension', '')
            if xtn:
                if not srlzHash.has_key(xtn):
                    # can't continue
                    raise ValueError(xtn)
                else:
                    mt = srlzHash[xtn].mimeType
        
        if not mt:
            try:
                wanted = req.headers_in['Accept']
                mts = conneg.parse(wanted)
                mt = conneg.best(mts, mimeList)
            except:
                mt = ''

        if mt:
            xtn = mimeHash[str(mt)]
        else:
            # default to rdf/xml
            xtn = "rdf.xml"

        srlz = srlzHash[xtn]

        if form.has_key('aggregation'):
            uri = form.get('aggregation')
        else:
            uri = path

        if not uri:
            data = '<html><body>Instructions etc. goes here</body></html>'
            self.send(data, req, ct="text/html");
            return
        elif not protoUriRe.match(uri):
            self.error("Resource Map URI must be a protocol based URI", req)
            return

        try:
            # fetch
            
            rd = ReMDocument(uri)
        except Exception, e:
            self.error("Could not retrieve Resource Map from '%s': %s" % (uri, e.message), req)
            return
Beispiel #20
0
def handler(req):
  req.content_type = "text/plain"
  from mod_python.util import FieldStorage
  FS = FieldStorage(req)
  logfile = StringIO.StringIO()

  if req.filename.find("distl.signal_strength_bcsb")>=0:
    from spotfinder.servers.apache_bcsb import run as run_command
  elif req.filename.find("distl.signal_strength")>0:
    run_command = run
  elif req.filename.find("dials.find_spots")>0:
    from spotfinder.servers.apache_dials import run as run_command

  logfile.write(run_command(args=FS))
  log = logfile.getvalue()
  req.set_content_length(len(log))
  req.write(log)
  return apache.OK
Beispiel #21
0
    def handle_create(self, req):
        # Create a new object  (workflow or otherwise)
        store = FieldStorage(req)
        try:
            objid = store['contextId']
            storeid = store['storeId']
            targetid = store['targetId']
            workflow = store['config']
            replace = int(store['replace'])
            context = serv.get_object(session, objid)
            confStore = context.get_object(session, storeid)
        except:
            context = None
            contStore = None
        if not context or not confStore:
            self.send_xml("<fault>Unknown object</fault>", req);
            return

        try:
            contStore.fetch_record(session, targetid)
            if not replace:
                self.send_xml("<fault>Object exists</fault>", req)
                return
        except:
            # does not exist
            pass

        try:
            p = context.get_object(session, 'SaxParser')
            d = StringDocument(workflow)
            rec = p.process_document(session, d)
            rec.id = targetid
            rec.recordStore = storeid
            confStore.begin_storing(session)
            confStore.store_record(session, rec)
            confStore.commit_storing(session)
        except Exception, e:
            self.send_xml("<fault>Storing failed %s</fault>" % e , req)
            return
Beispiel #22
0
    def compareIndexes(self, req):
        self.logger.log('comparing indexes')
        start = time.time()
        form = FieldStorage(req)
        id = form.get('id','data_grid')
        offset = str(form.get('offset', 0))
        if offset.find('.') != -1:
            startNum = int(offset[:offset.find('.')])
            adjustValue = int(offset[offset.find('.')+1:])
        else :
            startNum = int(offset)
            adjustValue = 0       
        howMany = int(form.get('page_size', 100))
        indexStrings = form.get('index', None)
        baseIdx = db.get_object(session, indexForStats)
        corpusSize = baseIdx.fetch_metadata(session)['nOccs']
        indexList = []
        addTfp = False
        # list means we are comparing indexes otherwise its just one
        # we get the actual index object from the string and store them in indexList
        if (indexStrings.__class__ == list):
            if (indexStrings[0].find('gram') == -1):
                addTfp = True
            for i in range(0, len(indexStrings)):
                if indexStrings[i].find('gram') == -1:
                    compareIndex = db.get_object(session, '%s' % indexStrings[i])
                else: 
                    if indexStrings[i].replace('-idx', '').find('-') == -1:
                        compareIndex = db.get_object(session, 'sentence-idx')
                    else:                   
                        compareIndex = db.get_object(session, '%s-idx' % indexStrings[i][:indexStrings[i].replace('-idx', '').rfind('-')])
                indexList.append((db.get_object(session, '%s' % indexStrings[i]), compareIndex))  
        else :
            if (indexStrings.find('gram') == -1):
                addTfp = True
                compareIndex = db.get_object(session, '%s' % indexStrings)
            else:
                if indexStrings.replace('-idx', '').find('-') == -1:
                    compareIndex = db.get_object(session, 'sentence-idx')
                else:
                    compareIndex = db.get_object(session, '%s-idx' % indexStrings[:indexStrings.replace('-idx', '').rfind('-')])
            indexList.append((db.get_object(session, '%s' % indexStrings), compareIndex))  

#            indexList.append(db.get_object(session, '%s' % indexStrings))
        # 
        output = []
        firstIndex = indexList[0][0]
        
        firstTotal = indexList[0][1].fetch_metadata(session)['nOccs']
        q = qf.get_query(session, 'idx-foo any "bar"')
        appending = True
        if startNum < 0 :
            appending = False
            startNum = startNum/-1
        
        idxLength = firstIndex.fetch_metadata(session)['nTerms']
        completed = False
        cycles = 0
        firstStart = startNum
        while len(output) < howMany and completed == False:     
            if appending:
                startNum = int(firstStart+(howMany*cycles))
            else:
                startNum = int(startNum-(howMany*cycles))
            cycles += 1
            if appending and idxLength-(startNum) <= howMany:
                completed = True
            if appending:
                termList = firstIndex.fetch_termFrequencies(session, 'occ', startNum, min(howMany, idxLength-(startNum)), '>')
            else:
                termList = firstIndex.fetch_termFrequencies(session, 'occ', startNum, min(howMany, startNum), '<')         
            for i, t in enumerate(termList):                
                cells = []
                word = firstIndex.fetch_termById(session, t[1])
                q.term.value = word
                percentage = round((float(t[2]) / float(firstTotal) * normalizationBase), 2)
                firstIndexName = indexList[0][0].id[:indexList[0][0].id.find('-idx')]
                
                if appending:
                    cells.append('<td>%d</td>' % (i + 1 + startNum))
                else:                   
                    cells.append('<td>%d</td>' % (startNum + 1 - i))
                # This try/except/else deals with whether we are viewing one
                # index or more than one
                try:
                    indexList[1]
                except:
                    # A single index     
                    if addTfp == True and tfp == True:                                       
                        cells.append('<td><a href="javascript:searchFor(\'%s\', \'%s\')">%s</a></td><td><a href="javascript:tfpFor(\'%s\', \'%s\')">tfp</a></td><td>%s</td>' % (word, firstIndexName, word, word, firstIndexName, percentage))                     
                    else :
                        cells.append('<td><a href="javascript:searchFor(\'%s\', \'%s\')">%s</a></td><td>%s</td>' % (word, firstIndexName, word, percentage))                     
                    cells.append('<td>%s</td>' % t[2])     
                # more than one index
                else:                    
                    if addTfp == True and tfp == True:
                        cells.append('<td>&lt;a href="javascript:searchFor(\'%s\', \'%s\')">%s&lt;/a></td><td>&lt;a href="javascript:tfpFor(\'%s\', \'%s\')">tfp&lt;/a></td><td>%s</td>' % (word, firstIndexName, word, word, firstIndexName, percentage))                     
                    else :
                        cells.append('<td>&lt;a href="javascript:searchFor(\'%s\', \'%s\')">%s&lt;/a></td><td>%s</td>' % (word, firstIndexName, word, percentage))                     
                    othersTotal = 0
                    othersHits = 0 
                    self.logger.log(cells)              
                    for j in range(1, len(indexList)):
                        total = indexList[j][1].fetch_metadata(session)['nOccs']
                        othersTotal += total
                        occs = indexList[j][0].scan(session, q, 1)    
                        
                        if (occs[0][0] == word):
                            othersHits += occs[0][1][2]
                            #add each cell
                            normalisedOccs = round((float(occs[0][1][2]) / float(total) * normalizationBase), 2)
                            cells.append('<td>%s</td>' % normalisedOccs)
                        else :
                            cells.append('<td>0</td>')                        
                    if z :
                        zstat = zscore(othersHits, t[2], othersTotal, indexList[0][1].fetch_metadata(session)['nOccs'])
                        if zstat >= zstatSig:
                            cells.append('<td>%s</td>' % zstat)
                        else :
                            continue      
                output.append('<tr>%s</tr>' % ''.join(cells))

            if not appending:
                output.reverse()
           # output = output[adjustValue:]
        (mins, secs) = divmod(time.time()-start, 60)
        self.logger.log('scanning complete: %s' % secs) 
        return '<ajax-response><response type="object" id="%s_updater"><rows update_ui="true">%s</rows></response></ajax-response>' % (id, ''.join(output))
    def handle_workflowRequest(self, config, req):
        postdata = FieldStorage(req)

        xmlstr = postdata.get('requestxml', None)

        if not (xmlstr):
            # throw some sensible error
            time.sleep(1)
            req.write('ERROR : No request XML submitted\n')
            return
#        else:
#            self.log.write(xmlstr)

        doc = document.StringDocument(xmlstr)
        rec = mdp.process_document(session, doc)

        self.log.write('rec.get_xml():\n%s\n' % rec.get_xml(session))

        dom = rec.get_dom(session)
        #now do some clever dynamic object stuff
        wfNode = dom.childNodes[0]
        wfNode = dom.getElementsByTagName('workflow')[0]
        wfConf = self._generateWorkflowConfigNode(wfNode)
        wfobj = SimpleWorkflow(session, wfConf, serv)
        inputs = dom.getElementsByTagName('inputs')[0]

        time.sleep(1)
        if not (wfobj):
            req.write('ERROR : Junk XML - must contain workflow element\n')
            return
        elif not (inputs):
            req.write('ERROR : Junk XML - must contain inputs element\n')
            return

        iCount = 1
        wfmsgs = []
        self.log.write('inputs:%s\n' % inputs.toxml())
        for input in inputs.childNodes:
            self.log.write('input:%s\n' % input.toxml())
            if (input.nodeType == elementType):
                objectType = input.getAttribute('type')
            else:
                continue

            if objectType == 'document.StringDocument':
                try:
                    f = postdata.get('file', None)
                    data = f.value
                except:
                    data = None
            else:
                data = input.firstChild.toxml()

            if not data:
                req.write('ERROR : No input data provided\n')
                return

            time.sleep(5)
            #req.write('PROGRESS : %s\n' % (objectType))
            modName = objectType[:objectType.rfind('.')]
            clName = objectType[objectType.rfind('.') + 1:]
            mod = dynamic.globalImport(modName, [])
            cl = getattr(mod, clName)
            inobj = cl(data)
            try:
                msg = wfobj.process(session, inobj)
            except ObjectAlreadyExistsException, e:
                time.sleep(1)
                req.write(
                    'ERROR : One or more records in input %d already exist in the database : %s\n'
                    % (iCount, e))
                continue
            except Exception, e:
                time.sleep(1)
                msg = e
                req.write(
                    'ERROR : Something went wrong while processing input %d : %s\n'
                    % (iCount, e))
                continue
         result = self.call(xreq)
     except AttributeError, err:
         # result = self.processUnknownOperation(classname, err, config)
         self.send_fault(
             Fault(Fault.Client,
                   'Unknown Operation (%s)' % str(err)), req)
         return
     except Exception, err:
         self.send_fault(
             Fault(Fault.Client,
                   'Broken request %s: (%s)' % (err, data)), req)
         return
 elif (req.method == "GET"):
     # parse options out of req
     config = config['http://www.loc.gov/zing/srw/']
     store = FieldStorage(req)
     opts = {}
     for qp in store.list:
         opts[qp.name] = [qp.value]
     if not opts:
         opts = {
             'operation': ['explain'],
             'version': ['1.1'],
             'recordPacking': ['xml']
         }
     if not opts.has_key('operation'):
         # Be rigourous and error ... somehow
         err = SRWDiagnostics.Diagnostic7()
         err.uri = err.uri
         err.message = "Mandatory 'operation' parameter not supplied"
         err.details = "operation"
 def dispatch(self, req):
     path = req.uri.strip('/')
     if path not in configs:
         # Unknown endpoint
         # No specification
         xml = ['<databases numberOfDatabases="%d">' % (len(configs))]
         for k in sorted(configs.keys()):
             xml.append("<database><path>%s</path></database>" % k)
         xml.append('</databases>')
         txt = ''.join(xml)
         self.send_xml(txt, req)
     else:
         dbconf = configs[path]
         if isinstance(dbconf, tuple):
             dbid = dbconf[0]
             db = serv.get_object(session, dbid)
             config = db.get_object(
                 session,
                 dbconf[1]['http://www.loc.gov/zing/srw/']
             )
         else:
             config = dbconf['http://www.loc.gov/zing/srw/']
         # Check db hasn't changed since instantiated
         db = config.parent
         # Attempt to find filepath for db metadata
         fp = db.get_path(session, 'metadataPath')
         if os.stat(fp).st_mtime > db.initTime:
             # rediscover objects
             dbid = db.id
             del db
             try:
                 del serv.objects[dbid]
             except KeyError:
                 pass
             try:
                 del serv.databases[dbid]
             except KeyError:
                 pass
             db = serv.get_object(session, dbid)
         session.path = "http://%s/%s" % (req.hostname, path)
         session.config = config
         store = FieldStorage(req)
         opts = {}
         for qp in store.list:
             if qp.value.isdigit():
                 opts[qp.name] = int(qp.value)
             else:
                 opts[qp.name] = qp.value
         if not opts:
             opts = {
                 'operation': 'explain',
                 'version': '1.2',
                 'recordPacking': 'xml'
                 }
         if not 'operation' in opts:
             err = self.diagnostic(7,
                                   msg="Mandatory parameter not supplied",
                                   details='operation')
             result = self.processUnknownOperation(err, config)
         elif not opts['operation'] in ['explain',
                                        'searchRetrieve',
                                        'scan']:
             err = self.diagnostic(4,
                                   msg="Unsupported Operation",
                                   details=opts['operation'])
             result = self.processUnknownOperation(err, config)
         else:
             respName = "%sResponse" % opts['operation']
             result = getattr(elemFac, respName)()
             v = elemFac.version('1.2')
             result.append(v)
             if not 'version' in opts:
                 err = self.diagnostic(
                     7,
                     msg="Mandatory parameter not supplied",
                     details='version'
                 )
                 dx = self.diagnosticToXml(err)
                 x = elemFac.diagnostics()
                 x.append(dx)
                 result.append(x)
             else:
                 fn = getattr(self, 'process_%s' % opts['operation'])
                 try:
                     fn(opts, result)
                 except cqlParser.Diagnostic as d:
                     diags = elemFac.diagnostics(self.diagnosticToXml(d))
                     result.append(diags)
                 result.append(self.echoedQuery(opts))
                 self.extraData('response', opts, result)
                 session.currentResultSet = None
         text = etree.tostring(result, pretty_print=True)
         if 'stylesheet' in opts:
             text = (u'<?xml version="1.0"?>\n'
                     u'<?xml-stylesheet type="text/xsl" href="%s"?>\n'
                     u'%s' % (opts['stylesheet'], text))
         else:
             text = u'<?xml version="1.0"?>\n%s' % text
         self.send_xml(text, req)
         if len(serv.databaseConfigs) >= 25:
             # cleanup memory
             try:
                 del serv.objects[config.parent.id]
             except KeyError:
                 pass
    def handle_workflowRequest(self, config, req):
        postdata = FieldStorage(req)
            
        xmlstr = postdata.get('requestxml', None)
        
        if not (xmlstr):
            # throw some sensible error
            time.sleep(1)
            req.write('ERROR : No request XML submitted\n')
            return
#        else:
#            self.log.write(xmlstr)
            
        doc = document.StringDocument(xmlstr)
        rec = mdp.process_document(session, doc)
        
        self.log.write('rec.get_xml():\n%s\n' % rec.get_xml(session))
        
        dom = rec.get_dom(session)
        #now do some clever dynamic object stuff
        wfNode = dom.childNodes[0]
        wfNode = dom.getElementsByTagName('workflow')[0]
        wfConf = self._generateWorkflowConfigNode(wfNode)
        wfobj = SimpleWorkflow(session, wfConf, serv)
        inputs = dom.getElementsByTagName('inputs')[0]
                        
        time.sleep(1);                      
        if not (wfobj): req.write('ERROR : Junk XML - must contain workflow element\n'); return
        elif not (inputs): req.write('ERROR : Junk XML - must contain inputs element\n'); return
        
        iCount = 1
        wfmsgs = []
        self.log.write('inputs:%s\n' % inputs.toxml())
        for input in inputs.childNodes:
            self.log.write('input:%s\n' % input.toxml())
            if (input.nodeType == elementType):
                objectType = input.getAttribute('type')
            else:
                continue
            
            if objectType == 'document.StringDocument':
                try: 
                    f = postdata.get('file', None)
                    data = f.value
                except: 
                    data = None
            else: 
                data = input.firstChild.toxml()
                
            if not data: req.write('ERROR : No input data provided\n'); return
                
            time.sleep(5);
            #req.write('PROGRESS : %s\n' % (objectType))
            modName = objectType[:objectType.rfind('.')]
            clName = objectType[objectType.rfind('.')+1:]
            mod = dynamic.globalImport(modName, [])
            cl = getattr(mod, clName)
            inobj = cl(data)
            try:
                msg = wfobj.process(session, inobj)
            except ObjectAlreadyExistsException, e:
                time.sleep(1);
                req.write('ERROR : One or more records in input %d already exist in the database : %s\n' % (iCount, e))
                continue
            except Exception, e:
                time.sleep(1);
                msg = e
                req.write('ERROR : Something went wrong while processing input %d : %s\n' % (iCount, e))
                continue
Beispiel #27
0
 def search(self, req):
     global db, idxStore, resultSetStore
   
     self.logger.log('search called')
     start = time.time()
     form = FieldStorage(req)
     type_ = form.get('type', None)
     terms = form.get('terms', None)  
     book = form.get('book', 'all') 
     csCheckbox = form.get('caseSensitive', None)
     caseSensitive = csCheckbox and "s" or "i"
     id_ = form.get('id', None)
     span = int(form.get('span', 0))
     wordWindow = int(form.get('windowsize', 10))
     gid = form.get('gid', None)
     if id_:
         # remove the 'kwic_grid_' that comes from LiveGrid id 
         self.logger.log('ID SUPPLIED DISPLAYING LINES') 
         id_ = id_[10:]
         start = int(form.get('offset', 0))
         howmany = int(form.get('page_size', 100))            
         return self.kwicDisplay(id_, start, howmany)
     elif (gid != None):
         start = int(form.get('offset', 0))
         howmany = int(form.get('page_size', 100))            
         return self.kwicDisplay(gid, start, howmany)                   
     else:
         if (terms == None):
             self.logger.log('no terms') 
            
         id_ = '%s|%s|%s|%d|%d|%s|%s|' % (form.get('context', None), type_, multiReplace(terms, {'"' : '*', ' ' : '_', '<' : '(', '>' : ')'}), span, wordWindow, caseSensitive, book) 
         try:
             rs = resultSetStore.fetch_resultSet(session, id_)
         except cheshire3.exceptions.ObjectDoesNotExistException:
             if type_ == 'CQL':
                 queryString = terms
             else:
                 (queryString, idx) = self.build_query(id_) 
                                         
             query = qf.get_query(session, queryString)
             (mins, secs) = divmod(time.time() - start, 60)
             self.logger.log('%s\nquery parsed: %s' % (queryString, secs))
             rs = db.search(session, query)
             
             (mins, secs) = divmod(time.time() - start, 60)
             self.logger.log('db searched: %s' % secs)
             
             # Save ResultSet
             resultSetStore.begin_storing(session)
             rs.id = id_                   
             resultSetStore.store_resultSet(session, rs)
             resultSetStore.commit_storing(session) 
         try:
             totalOccs = rs.totalOccs
         except:
             totalOccs = 'unavailable'
         if totalOccs == 0:
             totalOccs = 'unavailable' 
       
         (mins, secs) = divmod(time.time() - start, 60)
         (hours, mins) = divmod(mins, 60) 
         self.logger.log('search complete: %d:%d:%d' % (hours, mins, secs))
         output = '<results><rsid>%s</rsid><totalDocs>%i</totalDocs><totalOccs>%s</totalOccs></results>' % (id_, len(rs), str(totalOccs))        
     return output
Beispiel #28
0
    def handle_listObjects(self, req):
        # List existing objects in context

        storeHash = {}
        collHash = {}
        processHash = {}

        # get start obj from request
        store = FieldStorage(req)
        try:
            objid = store['contextId']
        except:
            self.send_xml("<fault>Missing contextId</fault>", req);
            return
        try:
            obj = serv.get_object(session, objid)
            origObj = obj
        except:
            raise
            self.send_xml("<fault>Unknown object: '%s'</fault>" % objid, req);
            return;

        while obj:
            # first, recheck any configStores
            for csid in obj._includeConfigStores:
                confStore = obj.get_object(session, csid)
                if confStore != None:
                    for rec in confStore:
                        # do something with config
                        if (not (rec.id in obj.subConfigs.keys())):
                            node = rec.get_dom(session)
                            node= node.childNodes[0]
                            nid = node.getAttributeNS(None, 'id')
                            node.setAttributeNS(None, 'configStore', confStore.id)
                            obj.subConfigs[nid] = node
                            ntype = node.getAttributeNS(None, 'type')
                            if ntype == 'index':
                                obj.indexConfigs[nid] = node
                            elif ntype == 'protocolMap':
                                obj.protocolMapConfigs[nid] = node
                            elif ntype == 'database':
                                obj.databaseConfigs[nid] = node
                            elif ntype == '':
                                raise ConfigFileException("Object must have a type attribute: %s  -- in configStore %s" % (nid, csid))
            sys.stderr.flush()
            for oid in obj.subConfigs.keys():
                dom = obj.subConfigs[oid]
                t = dom.getAttributeNS(None, 'type')
                if t in storeTypes:
                    try:
                        storeHash[t].append(oid)
                    except:
                        storeHash[t] = [oid]
                elif t in collTypes:
                    try:
                        collHash[t].append(oid)
                    except:
                        collHash[t] = [oid]
                elif t in processTypes:
                    try:
                        processHash[t].append(oid)
                    except:
                        processHash[t] = [oid]
            obj = obj.parent

        html = ["""<div xmlns="http://www.w3.org/1999/xhtml"><ul id="ObjectList" style="padding-left: 5px; border-left: none;">"""]

        html.append("<li>Process Objects")
        html.append("<ul>")
        keys = processHash.keys()
        keys.sort()
        for ot in keys:
            html.append("<li>" + ot + "<ul>")
            oids = processHash[ot]
            oids.sort()
            if (origObj.get_path(session, ot)):
                html.append('<li> <a class="item" href="javascript:none()" onclick="create_ellipse(\'default %s\', \'%s\')"><i>default %s</i></a></li>' % (ot, ot, ot))
            for oid in oids:
                html.append("""
<li>
<a class="item" href="javascript:none()" onclick="create_ellipse('%s', '%s')">%s</a> 
<a class="item" href="javascript:none()" onclick="help(event, '%s')" onmouseout="unhelp()">?</a>
</li>""" % (oid, ot, oid, oid))
            html.append("</ul></li>")
        html.append("</ul></li>")


        html.append("<li>Store Objects")
        html.append("<ul>")
        keys = storeHash.keys()
        keys.sort()
        for ot in keys:
            html.append("<li>" + ot + "<ul>")
            oids = storeHash[ot]
            oids.sort()
            if (origObj.get_path(session, ot)):
                html.append('<li> <a class="item" href="javascript:none()" onclick="create_store(\'default %s\', \'%s\')"><i>default %s</i></a></li>' % (ot, ot, ot))
            if ot == 'configStore':
                for oid in oids:
                    html.append("""
<li>
<a class="item" href="javascript:none()" onclick="create_store('%s', '%s')">%s</a> 
<a class="item" href="javascript:none()" onclick="help(event, '%s')" onmouseout="unhelp()">?</a>
<a class="item" href="javascript:none()" onclick="setConfigStore('%s')">(set)</a>
</li>""" % (oid, ot, oid, oid, oid))
            else:
                for oid in oids:
                    html.append("""
<li>
<a class="item" href="javascript:none()" onclick="create_store('%s', '%s')">%s</a> 
<a class="item" href="javascript:none()" onclick="help(event, '%s')" onmouseout="unhelp()">?</a>
</li>""" % (oid, ot, oid, oid))
            html.append("</ul></li>")
        html.append("</ul></li>")

        html.append("<li>Collection Objects")
        html.append("<ul>")
        keys = collHash.keys()
        keys.sort()
        for ot in keys:
            html.append("<li>" + ot + "<ul>")
            oids  = collHash[ot]
            oids.sort()
            if (origObj.get_path(session, ot)):
                html.append('<li> <a class="item" href="javascript:none()" onclick="create_rect(\'default %s\', \'%s\')"><i>default %s</i></a></li>' % (ot, ot, ot))
            if ot == 'database':
                for oid in oids:
                    html.append("""
<li>
<a class="item" href="javascript:none()" onclick="create_rect('%s', '%s')">%s</a> 
<a class="item" href="javascript:none()" onclick="help(event, '%s')" onmouseout="unhelp()">?</a>
<a class="item" href="javascript:none()" onclick="setContext('%s')">(set)</a>
</li>""" % (oid, ot, oid, oid, oid))
            elif ot == 'workflow':
                for oid in oids:
                    html.append("""
<li>
<a class="item" href="javascript:none()" onclick="create_rect('%s', '%s')">%s</a> 
<a class="item" href="javascript:none()" onclick="help(event, '%s')" onmouseout="unhelp()">?</a>
<a class="item" href="javascript:none()" onclick="loadWorkflow('%s')">(edit)</a>
</li>""" % (oid, ot, oid, oid, oid))

            else:
                for oid in oids:
                    html.append("""
<li>
<a class="item" href="javascript:none()" onclick="create_rect('%s', '%s')">%s</a> 
<a class="item" href="javascript:none()" onclick="help(event, '%s')" onmouseout="unhelp()">?</a>
</li>""" % (oid, ot, oid, oid))

            html.append("</ul></li>")

        html.append("</ul></li>")
        html.append('<li><a class="item" href="javascript:none()" onclick="create_line();">Create Line</a></li>')
        html.append('<li><a class="item" href="javascript:none()" onclick="create_log();">Create Log Entry</a></li>')
        html.append('<li><a class="item" href="javascript:none()" onclick="create_assign();">Create Assignment</a></li>')

        html.append('<li>Create: <a class="item" href="javascript:none()" onclick="create_fork();">Fork</a>, ')
        html.append('<a class="item" href="javascript:none()" onclick="create_foreach();">ForEach</a>, ')
        html.append('<a class="item" href="javascript:none()" onclick="create_try();">TryExcept</a></li>')

        html.append('<li>Create: <a class="item" href="javascript:none()" onclick="create_return();">Return</a>, ')
        html.append('<a class="item" href="javascript:none()" onclick="create_raise();">Raise</a>, ')
        html.append('<a class="item" href="javascript:none()" onclick="create_continue();">Continue</a>, ')
        html.append('<a class="item" href="javascript:none()" onclick="create_break();">Break</a></li>')

        html.append("</ul>")

        html.append('<ul style="padding-left: 5px; border-left: none;">')

        html.append('<li><a class="item" href="javascript:none()" onclick="zoom(1);">Zoom Out</a>  (Current: <span id="zoomLevel">1</span>:1)</li>')
        html.append('<li><a class="item" href="javascript:none()" onclick="zoom(-1);">Zoom In</a></li>')

        html.append('<li><a class="item" href="javascript:none()" onclick="panX(1);">Move Left</a> (Current: <span id="panLevel">0 0</span>)</li>')
        html.append('<li><a class="item" href="javascript:none()" onclick="panX(-1);">Move Right</a></li>')
        html.append('<li><a class="item" href="javascript:none()" onclick="panY(1);">Move Up</a></li>')
        html.append('<li><a class="item" href="javascript:none()" onclick="panY(-1);">Move Down</a></li>')


        html.append('<li><br/><a class="item" href="index2.xml">Create New Object</a></li>')


        html.append('<li><br/><a class="item" href="javascript:none()" onclick="clear_flow();">Clear Workflow</a></li>')
        html.append('<li><a class="item" href="javascript:none()" onclick="show_flow();">Preview Workflow</a></li>')
        html.append('<li><a class="item" href="javascript:none()" onclick="send_flow();">Save Workflow</a></li>')

        html.append('<li><br/><table cellpadding="0" cellspacing="0" border="0"><tr><td>Context:</td><td><span id="contextId">%s</span></td></tr><tr><td>Workflow:</td><td><input type="text" value="test-flow" id="workflowId"/></td></tr><tr><td>ConfigStore:</td><td><input type="text" value="defaultConfigStore" id="configStoreId"/></td></tr><tr><td>Replace:</td><td><select id="replace"><option value="0">No</option><option value="1">Yes</option></select></td></tr></table></li>' % objid);

        html.append("</ul></div>")

        xml = '\n'.join(html)
        self.send_xml(xml, req)
Beispiel #29
0
 def __init__( self, req ):
     from mod_python.util import FieldStorage
     self.fieldStorage = FieldStorage( req, True )
Beispiel #30
0
    def handle_getConfigParams(self, req):
        store = FieldStorage(req)
        try:
            cls = store['className']
        except:
            self.send_xml("<fault>Missing className</fault>", req);
            return
        try:
            classObj = arch.find_class(cls)
        except:
            self.send_xml("<fault>No such class: %s</fault>" % cls, req)

        try:
            (paths, setts, defs) = arch.find_params(classObj)
        except:
            self.send_xml("<fault>Not a configurable object?</fault>", req)


        # This is stupid...
        classes = arch.discover_classes()
        otype = "unknown"
        for c in classes:
            if c[1] == classObj:
                otype = c[0]
                otype = otype[:2].lower() + otype[2:]
        

        html = ["""<div xmlns="http://www.w3.org/1999/xhtml">""",
                """<center><i>Basic Information</i></center>""",
                """<table width="100%%">
                <tr><td width="20%%"><b>Identifier</b></td><td><input id="identifierInput" type="text" name="identifier" size="30"/></td><td width="5%%"></td></tr>
                <tr><td><b>Class</b></td><td>%s</td><td></td></tr>
                <tr><td><b>Object Type</b></td><td>%s</td><td></td></tr>
                </table>
                <input type="hidden" name="className" id="classNameInput" value="%s"/>
                <input type="hidden" name="objectType" id="objectTypeInput" value="%s"/>
                <br/>
                <center><i>Paths</i></center>""" % (cls, otype, cls, otype)]

        html.append('<table width="100%">')
        items = paths.items()
        items.sort()
        for (p, info) in items:
            html.append("<tr><td width=\"20%%\"><b>%s</b></td><td>" % p)
            if info.has_key('options'):
                opts = info['options'].split('|')
                html.append("""<select name="path_%s">""" % p)
                html.append("""<option value="">(default)</option>""")
                for o in opts:
                    html.append("""<option value="%s">%s</option>""" % (o,o))
                html.append("""</select>""")
            else:
                html.append("""<input type="text" name="path_%s" size="30"/>""" % (p))
            html.append("""</td><td width=\"5%%\"><a href="javascript:none()" onclick="config_help('%s')"><img src="http://sca.lib.liv.ac.uk/images/whatisthis.gif"/></a></td></tr>""" % info['docs'].replace("'", "\\'"))
        html.append('</table>')

        if setts:
            html.append("<br/><center><i>Settings</i></center>")
            html.append('<table width="100%">')
            items = setts.items()
            items.sort()
            for (p, info) in items:
                html.append("<tr><td width=\"20%%\"><b>%s</b></td><td>" % p)
                if info.has_key('options'):
                    opts = info['options'].split('|')
                    html.append("""<select name="setting_%s">""" % p)
                    html.append("""<option value="">(default)</option>""")
                    for o in opts:
                        html.append("""<option value="%s">%s</option>""" % (o,o))
                    html.append("""</select>""")
                else:
                    html.append("""<input type="text" name="setting_%s" size="30"/>""" % (p))
                html.append("""</td><td width=\"5%%\"><a href="javascript:none()" onclick="config_help('%s')"><img src="http://sca.lib.liv.ac.uk/images/whatisthis.gif"/></a></td></tr>""" % info['docs'].replace("'", "\\'"))
            html.append('</table>')


        if defs:
            html.append("<br/><center><i>Defaults</i></center>")
            html.append('<table width="100%">')
            items = defs.items()
            items.sort()
            for (p, info) in items:
                html.append("<tr><td width=\"20%%\"><b>%s</b></td><td>" % p)
                if info.has_key('options'):
                    opts = info['options'].split('|')
                    html.append("""<select name="default_%s">""" % p)
                    html.append("""<option value="">(default)</option>""")
                    for o in opts:
                        html.append("""<option value="%s">%s</option>""" % (o,o))
                    html.append("""</select>""")
                else:
                    html.append("""<input type="text" name="default_%s" size="30"/>""" % (p))
                html.append("""</td><td width=\"5%%\"><a href="javascript:none()" onclick="config_help('%s')"><img src="http://sca.lib.liv.ac.uk/images/whatisthis.gif"/></a></td></tr>""" % info['docs'].replace("'", "\\'"))
            html.append('</table>')

        html.append("</div>")

        xml = "\n".join(html)
        self.send_xml(xml, req)
 def handle(self, req):
     form = FieldStorage(req, True)
     tmpl = unicode(read_file(self.baseTemplatePath))
     title = ' :: '.join(self.htmlTitle)
     navbar = ' '.join(self.htmlNav)
     replaceAll = {
         "%TITLE%": title,
         "%NAVBAR%": navbar,
         "%USERNAME%": session.user.username,
         "%REALNAME%": session.user.realName
     }
     tmpl = multiReplace(tmpl, replaceAll)
     path = req.uri[1:]
     path = path[path.rfind('/') + 1:]
     content = None
     operation = form.get('operation', None)
     if path.endswith('.js'):
         self.send_response(read_file(abspath('../js/{0}'.format(path))),
                            req,
                            content_type='text/javascript',
                            code=200
                            )
         return apache.OK
     elif path == 'users.html':
         if (operation):
             if (operation == 'findcontacts'):
                 content = self.get_contactDetails(form)
                 self.send_xml(content, req)
                 return
             else:
                 if (operation == 'adduser'):
                     content = self.add_user(form)
                 elif (operation == 'addinstitution'):
                     content = self.add_inst(form)
                 elif (operation == 'editinstitution'):
                     content = self.edit_inst(form)
                 elif (operation == 'deleteinst'):
                     content = self.delete_inst(form)
                 elif (operation == 'deleteuser'):
                     content = self.delete_user(form)
                 elif operation in ['edit', 'edituser']:
                     content = self.edit_user(form)
                 else:
                     content = self.show_adminMenu()
         else:
             content = self.show_adminMenu()
     elif path == 'admin':
         # redirect to make sure later relative links work correctly
         redirect(req, 'admin/',
                  permanent=False, # TODO: make me True
                  text=("To prevent URL collisions caused by internal "
                        "relative, this service must be accessed at "
                        "admin/"
                        )
                  )
         return
     else:
         content = self.show_adminMenu()
     content = tmpl.replace('%CONTENT%', content)
     # send the display
     self.send_html(content, req)
Beispiel #32
0
    def handle(self, req):
        path = req.uri[5:]
        form = FieldStorage(req)

        strict = form.get('strict', True)
        if strict in ['false', 'False', '0', None, '']:
            strict = False

        mt = form.get('mimeType', '')
        mt = mt.replace(' ', '+')

        if not mt:
            xtn = form.get('extension', '')
            if xtn:
                if xtn not in srlzHash:
                    # can't continue
                    raise ValueError(xtn)
                else:
                    mt = srlzHash[xtn].mimeType

        if not mt:
            try:
                wanted = req.headers_in['Accept']
                mts = conneg.parse(wanted)
                mt = conneg.best(mts, mimeList)
            except:
                mt = ''

        if mt:
            xtn = mimeHash[str(mt)]
        else:
            # default to rdf/xml
            xtn = "rdf.xml"

        srlz = srlzHash[xtn]

        if 'aggregation' in form:
            uri = form.get('aggregation')
        else:
            uri = path

        if not uri:
            data = '<html><body>Instructions etc. goes here</body></html>'
            self.send(data, req, ct="text/html")
            return
        elif not protoUriRe.match(uri):
            self.error("Resource Map URI must be a protocol based URI", req)
            return

        try:
            # fetch

            rd = ReMDocument(uri)
        except Exception as e:
            self.error(
                "Could not retrieve Resource Map from '%s': %s" %
                (uri, e.message), req)
            return

        try:
            # parse
            if rd.format == 'atom':
                parser = ap
            elif rd.format == 'rdfa':
                parser = rdfap
            else:
                parser = p
            if not strict:
                parser.strict = False
            try:
                rem = parser.parse(rd)
                parser.strict = True
            except:
                parser.strict = True
                raise

        except OreException as e:
            # get exception message
            self.error("Resource Map Invalid: %s" % e.message, req)
            return
        except SAXParseException as e:
            self.error(
                "Could not parse XML: %s (line %s, column %s)" %
                (e.getMessage(), e.getLineNumber(), e.getColumnNumber()), req)
            return
        except:
            raise

        try:
            # serialize
            rem2 = rem._aggregation_.register_serialization(
                srlz, 'http://foresite.cheshire3.org/%s#rem' % req.uri)
            rd = rem2.get_serialization()
            data = rd.data
            if srlz == srlzHash['rdfa.html']:
                data = '<xhtml xmlns="http://www.w3.org/1999/xhtml"><body><i>Invisible RDFa resource map follows, it must have validated okay. [view source] :)</i>' + data + "</body></xhtml>"

        except Exception as e:
            self.error(
                "Could not serialize Aggregation to Resource Map: %s" %
                e.message, req)
            return

        self.send(data, req, ct=srlz.mimeType)