Beispiel #1
0
 def test20b_add_span_returntarget(self):
     """Add span (return target)"""
     q = fql.Query(Qadd_span_returntarget)
     results = q(self.doc)
     self.assertIsInstance(results[0], fql.SpanSet )
     results = results[0]
     self.assertIsInstance(results[0], folia.Word)
     self.assertEqual(results[0].text(), "hoofdletter")
     self.assertIsInstance(results[1], folia.Word)
     self.assertEqual(results[1].text(), "A")
Beispiel #2
0
    def test06_context(self):
        q = fql.Query(cql.cql2fql(Qcql_context6))
        results = q(self.doc)
        self.assertTrue(len(results) > 0)

        for result in results:
            self.assertIsInstance(result, fql.SpanSet)
            self.assertEqual(len(result), 1)
            self.assertTrue(result[0].pos()[:2] == "VZ"
                            or result[0].pos()[:2] == "VG")
Beispiel #3
0
 def test32_correct_delete(self):
     """Deletion as correction"""
     q = fql.Query(Qcorrect_delete)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Correction)
     self.assertEqual(results[0].cls, "redundantword")
     self.assertEqual(results[0].hastext(), False)
     self.assertEqual(
         results[0].text(
             correctionhandling=folia.CorrectionHandling.ORIGINAL), "een")
Beispiel #4
0
 def test39b_edit_spanrole(self):
     """Editing a spanrole (with ID)"""
     #ID does not exist yet, we add it first:
     q = fql.Query("SELECT hd FOR ID \"WR-P-E-J-0000000001.p.1.s.1.dep.2\"")
     hd = q(self.doc)[0]
     hd.id = "test"
     self.doc.index["test"] = hd
     #now the actual test:
     q = fql.Query(Qedit_spanrole_id)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Dependency)
     self.assertEqual(list(results[0].annotation(folia.Headspan).wrefs()), [
         results[0].doc['WR-P-E-J-0000000001.p.1.s.1.w.3'],
         results[0].doc['WR-P-E-J-0000000001.p.1.s.1.w.4'],
         results[0].doc['WR-P-E-J-0000000001.p.1.s.1.w.5']
     ])
     self.assertEqual(
         results[0].ancestor(folia.AbstractStructureElement).id,
         'WR-P-E-J-0000000001.p.1.s.1')
Beispiel #5
0
 def test3_deletion2(self):
     """Deletion (higher-order)"""
     q = fql.Query(Qdeletion2)
     results = q(self.doc)
     self.assertEqual(len(results), 1)
     self.assertIsInstance(results[0], folia.Correction)
     self.assertEqual(results[0].hastext(), False)
     self.assertEqual(results[0].original().text(), "een")
     self.assertEqual(results[0].previous(None).id, "correctionexample.s.8.w.2")
     self.assertEqual(results[0].next(None).id, "correctionexample.s.8.w.4")
Beispiel #6
0
 def test20e_add_span(self):
     """Add span (using RESPAN and FOR SPAN, immediately respanning)"""
     q = fql.Query(Qadd_span3)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Entity)
     self.assertEqual(results[0].cls, 'misc')
     results = list(results[0].wrefs())
     self.assertIsInstance(results[0], folia.Word)
     self.assertEqual(len(results), 1)
     self.assertEqual(results[0].text(), "A")
Beispiel #7
0
 def test20a_add_span(self):
     """Add span"""
     q = fql.Query(Qadd_span)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Entity)
     self.assertEqual(results[0].cls, 'misc')
     results = list(results[0].wrefs())
     self.assertIsInstance(results[0], folia.Word)
     self.assertEqual(results[0].text(), "hoofdletter")
     self.assertIsInstance(results[1], folia.Word)
     self.assertEqual(results[1].text(), "A")
Beispiel #8
0
 def test27_edit_respan(self):
     """Re-spanning"""
     q = fql.Query(Qrespan)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.SemanticRole)
     self.assertEqual(results[0].cls, "actor")
     results = list(results[0].wrefs())
     self.assertIsInstance(results[0], folia.Word)
     self.assertEqual(results[0].text(), "gaat") #yes, this is not a proper semantic role for class 'actor', I know.. but I had to make up a test
     self.assertIsInstance(results[1], folia.Word)
     self.assertEqual(results[1].text(), "men")
Beispiel #9
0
 def test28b_suggest_split(self):
     """Split Suggestion for Correction"""
     q = fql.Query(Qsuggest_split)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Correction)
     self.assertEqual(results[0].cls, "runonerror")
     self.assertIsInstance(results[0].suggestions(0)[0], folia.Word)
     self.assertIsInstance(results[0].suggestions(0)[1], folia.Word)
     self.assertEqual(results[0].suggestions(0)[0].text(), "weer")
     self.assertEqual(results[0].suggestions(0)[1].text(), "gegeven")
     self.assertEqual(results[0].current(0).text(), "weergegeven")
Beispiel #10
0
 def test28b_correct_split(self):
     """Split Correction"""
     q = fql.Query(Qcorrect_split)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Correction)
     self.assertEqual(results[0].cls, "runonerror")
     self.assertIsInstance(results[0].new(0), folia.Word)
     self.assertIsInstance(results[0].new(1), folia.Word)
     self.assertEqual(results[0].new(0).text(), "weer")
     self.assertEqual(results[0].new(1).text(), "gegeven")
     self.assertEqual(results[0].original(0).text(), "weergegeven")
Beispiel #11
0
 def test20d_add_span(self):
     """Add span (using SPAN instead of FOR SPAN)"""
     q = fql.Query(Qadd_span2)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Entity)
     self.assertEqual(results[0].cls, 'misc')
     results = list(results[0].wrefs())
     self.assertIsInstance(results[0], folia.Word)
     self.assertEqual(results[0].text(), "hoofdletter")
     self.assertIsInstance(results[1], folia.Word)
     self.assertEqual(results[1].text(), "A")
     self.assertEqual(len(results), 2)
Beispiel #12
0
 def test19_select_span2_returntarget(self):
     """Select span"""
     q = fql.Query(Qselect_span2_returntarget)
     results = q(self.doc)
     self.assertIsInstance(results[0], fql.SpanSet)
     results = results[0]
     self.assertIsInstance(results[0], folia.Word)
     self.assertEqual(results[0].text(), "Maarten")
     self.assertIsInstance(results[1], folia.Word)
     self.assertEqual(results[1].text(), "van")
     self.assertIsInstance(results[2], folia.Word)
     self.assertEqual(results[2].text(), "Gompel")
Beispiel #13
0
 def test18_select_span2(self):
     """Select span"""
     q = fql.Query(Qselect_span2)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Entity)
     results = list(results[0].wrefs())
     self.assertIsInstance(results[0], folia.Word)
     self.assertEqual(results[0].text(), "Maarten")
     self.assertIsInstance(results[1], folia.Word)
     self.assertEqual(results[1].text(), "van")
     self.assertIsInstance(results[2], folia.Word)
     self.assertEqual(results[2].text(), "Gompel")
Beispiel #14
0
 def test38_nested_span(self):
     """Adding a nested span"""
     q = fql.Query(Qadd_nested_span)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.SyntacticUnit)
     self.assertIsInstance(results[0].parent, folia.SyntacticUnit)
     self.assertEqual(results[0].parent.id,
                      "WR-P-E-J-0000000001.p.1.s.1.su.0")
     self.assertEqual(list(results[0].wrefs()), [
         results[0].doc['WR-P-E-J-0000000001.p.1.s.1.w.4'],
         results[0].doc['WR-P-E-J-0000000001.p.1.s.1.w.5']
     ])
Beispiel #15
0
 def test39_edit_spanrole(self):
     """Editing a spanrole"""
     q = fql.Query(Qedit_spanrole)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Dependency)
     self.assertEqual(list(results[0].annotation(folia.Headspan).wrefs()), [
         results[0].doc['WR-P-E-J-0000000001.p.1.s.1.w.3'],
         results[0].doc['WR-P-E-J-0000000001.p.1.s.1.w.4'],
         results[0].doc['WR-P-E-J-0000000001.p.1.s.1.w.5']
     ])
     self.assertEqual(
         results[0].ancestor(folia.AbstractStructureElement).id,
         'WR-P-E-J-0000000001.p.1.s.1')
Beispiel #16
0
 def test24b_edit_correctsuggest(self):
     """Add correction as well as suggestions on token annotation"""
     q = fql.Query(Qcorrectsuggest)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Correction)
     self.assertEqual(results[0].cls, "nonworderror")
     self.assertEqual(results[0].confidence, 0.9)
     self.assertIsInstance(results[0].new(0), folia.LemmaAnnotation)
     self.assertEqual(results[0].new(0).cls, "terwijl")
     self.assertIsInstance(results[0].suggestions(0), folia.Suggestion)
     self.assertEqual(results[0].suggestions(0).confidence, 0.1)
     self.assertIsInstance(results[0].suggestions(0)[0], folia.LemmaAnnotation)
     self.assertEqual(results[0].suggestions(0)[0].cls, "gedurende")
Beispiel #17
0
 def test16_context(self):
     """Obtaining all words following 'de'"""
     q = fql.Query(Qcontext)
     results = q(self.doc)
     self.assertTrue(len(results) > 0)
     self.assertEqual(results[0].text(), "historische")
     self.assertEqual(results[1].text(), "naam")
     self.assertEqual(results[2].text(), "verwantschap")
     self.assertEqual(results[3].text(), "handschriften")
     self.assertEqual(results[4].text(), "juiste")
     self.assertEqual(results[5].text(), "laatste")
     self.assertEqual(results[6].text(), "verwantschap")
     self.assertEqual(results[7].text(), "handschriften")
Beispiel #18
0
def query_helper(request,namespace, docid, configuration=None):
    """Does the actual query, called by query() or pub_query(), not directly"""
    flatargs = {
        'customslicesize': request.POST.get('customslicesize',settings.CONFIGURATIONS[configuration].get('customslicesize','50')), #for pagination of search results
    }
    #stupid compatibility stuff
    if sys.version < '3':
        if hasattr(request, 'body'):
            data = json.loads(unicode(request.body,'utf-8')) #pylint: disable=undefined-variable
        else: #older django
            data = json.loads(unicode(request.raw_post_data,'utf-8')) #pylint: disable=undefined-variable
    else:
        if hasattr(request, 'body'):
            data = json.loads(str(request.body,'utf-8'))
        else: #older django
            data = json.loads(str(request.raw_post_data,'utf-8'))

    if not data['queries']:
        return HttpResponseForbidden("No queries to run")

    for query in data['queries']:
        #get document selector and check it doesn't violate the namespace
        docselector, query = getdocumentselector(query)
        if not docselector:
            return HttpResponseForbidden("Query does not start with a valid document selector (USE keyword)!")
        elif docselector[0] != namespace:
            return HttpResponseForbidden("Query would affect a different namespace than your current one, forbidden!")

        if query != "GET" and query[:4] != "CQL " and query[:4] != "META":
            #parse query on this end to catch syntax errors prior to sending, should be fast enough anyway
            try:
                query = fql.Query(query)
            except fql.SyntaxError as e:
                return HttpResponseForbidden("FQL Syntax Error: " + str(e))
            needwritepermission = query.declarations or query.action and query.action.action != "SELECT"
        else:
            needwritepermission = False

    if needwritepermission and configuration != "pub" and not flat.users.models.haswritepermission(request.user.username, namespace, request):
        return HttpResponseForbidden("Permission denied, no write access")

    query = "\n".join(data['queries']) #throw all queries on a big pile to transmit
    try:
        d = flat.comm.query(request, query,**flatargs)
    except Exception as e:
        if sys.version < '3':
            errmsg = docserveerror(e)['fatalerror_text']
            return HttpResponseForbidden("FoLiA Document Server error: ".encode('utf-8') + errmsg.encode('utf-8'))
        else:
            return HttpResponseForbidden("FoLiA Document Server error: " + docserveerror(e)['fatalerror_text'])
    return HttpResponse(json.dumps(d).encode('utf-8'), content_type='application/json')
Beispiel #19
0
 def test37_subqueries(self):
     """Adding a complex span annotation with span roles, using subqueries"""
     q = fql.Query(Qadd_span_subqueries)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Dependency)
     self.assertEqual(results[0].cls, "test")
     self.assertEqual(list(results[0].annotation(folia.Headspan).wrefs()),
                      [results[0].doc['WR-P-E-J-0000000001.p.1.s.2.w.7']])
     self.assertEqual(
         list(results[0].annotation(folia.DependencyDependent).wrefs()),
         [results[0].doc['WR-P-E-J-0000000001.p.1.s.2.w.6']])
     self.assertEqual(
         results[0].ancestor(folia.AbstractStructureElement).id,
         'WR-P-E-J-0000000001.p.1.s.2')
Beispiel #20
0
 def test01_context(self):
     q = fql.Query(cql.cql2fql(Qcql_context))
     results = q(self.doc)
     self.assertTrue(len(results) > 0)
     for result in results:
         self.assertIsInstance(result, fql.SpanSet)
         #print("RESULT: ", [w.text() for w in result])
         self.assertEqual(len(result), 3)
         self.assertIsInstance(result[0], folia.Word)
         self.assertIsInstance(result[1], folia.Word)
         self.assertIsInstance(result[2], folia.Word)
         self.assertEqual(result[0].text(), "de")
         self.assertEqual(result[1].pos()[:4], "ADJ(")
         self.assertEqual(result[2].pos()[:2], "N(")
Beispiel #21
0
    def test04_context(self):
        q = fql.Query(cql.cql2fql(Qcql_context4))
        results = q(self.doc)
        self.assertEqual( len(results),2  )

        textresults = []
        for result in results:
            self.assertIsInstance(result, fql.SpanSet)
            textresults.append(  tuple([w.text() for w in result]) )

        #print(textresults,file=sys.stderr)

        self.assertTrue( ('genummerd','en','gedateerd') in textresults )
        self.assertTrue( ('opgenomen','en','worden','weergegeven') in textresults )
Beispiel #22
0
 def test25b_edit_suggest_text(self):
     """Add suggestion for correction on text"""
     q = fql.Query(Qsuggest_text)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Correction)
     self.assertEqual(results[0].cls, "nonworderror")
     self.assertEqual(results[0].parent.text(), "terweil")  #original
     self.assertIsInstance(results[0].suggestions(0), folia.Suggestion)
     self.assertEqual(results[0].suggestions(0).confidence, 0.9)
     self.assertIsInstance(results[0].suggestions(0)[0], folia.TextContent)
     self.assertEqual(results[0].suggestions(0)[0].text(), "terwijl")
     self.assertIsInstance(results[0].suggestions(1), folia.Suggestion)
     self.assertEqual(results[0].suggestions(1).confidence, 0.1)
     self.assertIsInstance(results[0].suggestions(1)[0], folia.TextContent)
     self.assertEqual(results[0].suggestions(1)[0].text(), "gedurende")
Beispiel #23
0
 def test16b_context(self):
     """Obtaining LID ADJ N sequences"""
     q = fql.Query(Qcontext2)
     results = q(self.doc)
     self.assertTrue(len(results) > 0)
     for result in results:
         self.assertIsInstance(result, fql.SpanSet)
         #print("RESULT: ", [w.text() for w in result])
         self.assertEqual(len(result), 3)
         self.assertIsInstance(result[0], folia.Word)
         self.assertIsInstance(result[1], folia.Word)
         self.assertIsInstance(result[2], folia.Word)
         self.assertEqual(result[0].pos()[:4], "LID(")
         self.assertEqual(result[1].pos()[:4], "ADJ(")
         self.assertEqual(result[2].pos()[:2], "N(")
Beispiel #24
0
 def test24a_edit_suggest(self):
     """Add suggestions for correction on token annotation"""
     q = fql.Query(Qsuggest1)
     results = q(self.doc)
     self.assertIsInstance(results[0], folia.Correction)
     self.assertEqual(results[0].cls, "nonworderror")
     self.assertEqual(results[0].parent.lemma(),"terweil")
     self.assertIsInstance(results[0].suggestions(0), folia.Suggestion)
     self.assertEqual(results[0].suggestions(0).confidence, 0.9)
     self.assertIsInstance(results[0].suggestions(0)[0], folia.LemmaAnnotation)
     self.assertEqual(results[0].suggestions(0)[0].cls, "terwijl")
     self.assertIsInstance(results[0].suggestions(1), folia.Suggestion)
     self.assertEqual(results[0].suggestions(1).confidence, 0.1)
     self.assertIsInstance(results[0].suggestions(1)[0], folia.LemmaAnnotation)
     self.assertEqual(results[0].suggestions(1)[0].cls, "gedurende")
Beispiel #25
0
    def test03_context(self):
        q = fql.Query(cql.cql2fql(Qcql_context3))
        results = q(self.doc)
        self.assertEqual(len(results), 2)

        textresults = []
        for result in results:
            self.assertIsInstance(result, fql.SpanSet)
            self.assertEqual(len(result), 2)
            textresults.append(tuple([w.text() for w in result]))

        #print(textresults,file=sys.stderr)

        self.assertTrue(('naam', 'stemma') in textresults)
        self.assertTrue(('stemma', 'codicum') in textresults)
Beispiel #26
0
    def test02_context(self):
        q = fql.Query(cql.cql2fql(Qcql_context2))
        results = q(self.doc)
        self.assertTrue(len(results) > 0)

        textresults = []
        for result in results:
            self.assertIsInstance(result, fql.SpanSet)
            textresults.append(tuple([w.text() for w in result]))

        self.assertTrue(('het', 'alfabet') in textresults)
        self.assertTrue(('vierkante', 'haken') in textresults)
        self.assertTrue(('plaats', ) in textresults)
        self.assertTrue(('het', 'originele', 'handschrift') in textresults)
        self.assertTrue(('Een', 'volle', 'lijn') in textresults)
Beispiel #27
0
    def test05_context(self):
        q = fql.Query(cql.cql2fql(Qcql_context5))
        results = q(self.doc)
        self.assertTrue(len(results) > 0)

        textresults = []
        for result in results:
            self.assertIsInstance(result, fql.SpanSet)
            textresults.append(tuple([w.text() for w in result]))

        #print(textresults,file=sys.stderr)

        self.assertTrue(('en', 'gedateerd', 'zodat') in textresults)
        self.assertTrue(('en', 'worden', 'weergegeven', 'door') in textresults)
        self.assertTrue(('zodat', 'ze') in textresults)
        self.assertTrue(('en', 'worden', 'tussen') in textresults)
        self.assertTrue(('terweil', 'een') in textresults)
    def query(self, **kwargs):
        """Query method, all FQL queries arrive here"""

        if 'X-Sessionid' in cherrypy.request.headers:
            sid = cherrypy.request.headers['X-Sessionid']
        else:
            sid = 'NOSID'

        if 'query' in kwargs:
            rawqueries = kwargs['query'].split("\n")
        else:
            cl = cherrypy.request.headers['Content-Length']
            rawqueries = cherrypy.request.body.read(int(cl)).split("\n")

        if self.debug:
            for i,rawquery in enumerate(rawqueries):
                log("[QUERY INCOMING #" + str(i+1) + ", SID=" +sid + "] " + rawquery)

        #Get parameters for FLAT-specific return format
        flatargs = getflatargs(cherrypy.request.params)
        flatargs['debug'] = self.debug
        flatargs['logfunction'] = log
        flatargs['version'] = VERSION

        prevdocsel = None
        sessiondocsel = None
        queries = []
        metachanges = {}
        for rawquery in rawqueries:
            try:
                docsel, rawquery = getdocumentselector(rawquery)
                if not docsel: docsel = prevdocsel
                self.docstore.use(docsel)
                if self.debug >= 2: log("[acquired lock " + "/".join(docsel)+"]")
                if not sessiondocsel: sessiondocsel = docsel
                if rawquery == "GET":
                    query = "GET"
                elif rawquery == "PROBE":
                    query = "PROBE" #gets no content data at all, but allows returning associated metadata used by FLAT, forces FLAT format
                else:
                    if rawquery[:4] == "CQL ":
                        if rawquery.find('FORMAT') != -1:
                            end = rawquery.find('FORMAT')
                            format = rawquery[end+7:]
                        else:
                            end = 9999
                            format = 'xml'
                        try:
                            query = fql.Query(cql.cql2fql(rawquery[4:end]))
                            query.format = format
                        except cql.SyntaxError as e :
                            raise fql.SyntaxError("Error in CQL query: " + str(e))
                    elif rawquery[:5] == "META ":
                        try:
                            key, value = rawquery[5:].split('=',maxsplit=1)
                        except ValueError:
                            raise fql.SyntaxError("Expected key=value after META keyword")
                        key = key.strip()
                        value = value.strip()
                        metachanges[key] = value
                        query = None
                    else:
                        query = fql.Query(rawquery)
                    if query and query.format == "python":
                        query.format = "xml"
                    if query and query.action and not docsel:
                        raise fql.SyntaxError("Document Server requires USE statement prior to FQL query")
            except fql.SyntaxError as e:
                log("[QUERY ON " + "/".join(docsel)  + "] " + str(rawquery))
                log("[QUERY FAILED] FQL Syntax Error: " + str(e))
                raise cherrypy.HTTPError(404, "FQL syntax error: " + str(e))
            finally:
                if self.debug >= 2: log("[releasing lock " + "/".join(docsel))
                self.docstore.done(docsel)

            if query:
                queries.append( (query, rawquery))
            prevdocsel = docsel


        if metachanges:
            try:
                doc = self.docstore[docsel]
            except NoSuchDocument:
                log("[QUERY FAILED] No such document")
                raise cherrypy.HTTPError(404, "Document not found: " + docsel[0] + "/" + docsel[1])
            except Exception as e:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                traceback.print_tb(exc_traceback, limit=50, file=sys.stderr)
                print("[QUERY FAILED] FoLiA Error in " + "/".join(docsel) + ": [" + e.__class__.__name__ + "] " + str(e), file=sys.stderr)
                log("[QUERY FAILED] FoLiA Error in " + "/".join(docsel) + ": [" + e.__class__.__name__ + "] " + str(e))
                if logfile: traceback.print_tb(exc_traceback, limit=50, file=logfile)
                raise cherrypy.HTTPError(404, "FoLiA error in " + "/".join(docsel) + ": [" + e.__class__.__name__ + "] " + str(e) + "\n\nQuery was: " + rawquery)

            if doc.metadatatype == folia.MetaDataType.NATIVE:
                doc.changed = True
                self.docstore.lastaccess[docsel][sid] = time.time()
                log("[METADATA EDIT ON " + "/".join(docsel)  + "]")
                for key, value in metachanges.items():
                    if value == 'NONE':
                        del doc.metadata[key]
                    else:
                        doc.metadata[key] = value
            else:
                raise cherrypy.HTTPError(404, "Unable to edit metadata on document with non-native metadata type (" + "/".join(docsel)+")")
        else:
            doc = None #initialize document only if not already initialized by metadta changes


        results = [] #stores all results
        xresults = [] #stores results that should be transferred to other sessions as well, i.e. results of adds/edits
        prevdocid = None
        multidoc = False #are the queries over multiple distinct documents?
        format = None
        for query, rawquery in queries:
            try:
                doc = self.docstore[docsel]
                self.docstore.lastaccess[docsel][sid] = time.time()
                log("[QUERY ON " + "/".join(docsel)  + "] " + str(rawquery))
                if isinstance(query, fql.Query):
                    if prevdocid and doc.id != prevdocid:
                        multidoc = True
                    result =  query(doc,False,self.debug >= 2)
                    results.append(result) #False = nowrap
                    if query.action and query.action.action in ('EDIT','ADD','DELETE', 'SUBSTITUTE','PREPEND','APPEND'):
                        #results of edits should be transferred to other open sessions
                        xresults.append(result)
                    if self.debug:
                        log("[QUERY RESULT] " + repr(result))
                    format = query.format
                    if query.action and query.action.action != "SELECT":
                        doc.changed = True
                        self.addtochangelog(doc, query, docsel)
                elif query == "GET":
                    results.append(doc.xmlstring())
                    format = "single-xml"
                elif query == "PROBE":
                    #no queries to perform
                    format = "flat"
                else:
                    raise Exception("Invalid query")
            except NoSuchDocument:
                if self.docstore.fail and not self.docstore.ignorefail:
                    log("[QUERY FAILED] Document server is in lockdown due to earlier failure. Restart required!")
                    raise cherrypy.HTTPError(403, "Document server is in lockdown due to earlier failure. Contact your FLAT administrator")
                else:
                    log("[QUERY FAILED] No such document")
                    raise cherrypy.HTTPError(404, "Document not found: " + docsel[0] + "/" + docsel[1])
            except fql.QueryError as e:
                log("[QUERY FAILED] FQL Query Error: " + str(e))
                raise cherrypy.HTTPError(404, "FQL query error: " + str(e))
            except Exception as e:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                traceback.print_tb(exc_traceback, limit=50, file=sys.stderr)
                log("[QUERY FAILED] FoLiA Error in " + "/".join(docsel) + ": [" + e.__class__.__name__ + "] " + str(e))
                print("[QUERY FAILED] FoLiA Error in " + "/".join(docsel) + ": [" + e.__class__.__name__ + "] " + str(e), file=sys.stderr)
                if logfile: traceback.print_tb(exc_traceback, limit=50, file=logfile)
                raise cherrypy.HTTPError(404, "FoLiA error in " + "/".join(docsel) + ": [" + e.__class__.__name__ + "] " + str(e) + "\n\nQuery was: " + rawquery)
            prevdocid = doc.id

        if not format:
            if metachanges:
                return "{\"version\":\"" + VERSION + "\"}"
            else:
                raise cherrypy.HTTPError(404, "No queries given")
        if format.endswith('xml'):
            cherrypy.response.headers['Content-Type']= 'text/xml'
        elif format.endswith('json'):
            cherrypy.response.headers['Content-Type']= 'application/json'


        if format == "xml":
            out = "<results>" + "\n".join(results) + "</results>"
        elif format == "json":
            out = "[" + ",".join(results) + "]"
        elif format == "flat":
            if sid != 'NOSID' and sessiondocsel:
                self.setsession(sessiondocsel[0],sessiondocsel[1],sid, xresults)
            cherrypy.response.headers['Content-Type']= 'application/json'
            if multidoc:
                raise "{\"version\":\""+VERSION +"\"} //multidoc response, not producing results"
            elif doc:
                log("[Parsing results for FLAT]")
                out =  parseresults(results, doc, **flatargs)
        else:
            if len(results) > 1:
                raise cherrypy.HTTPError(404, "Multiple results were obtained but format dictates only one can be returned!")
            out = results[0]


        if docsel[0] == "testflat":
            testresult = self.docstore.save(docsel) #won't save, will run tests instead
            log("Test result: " +str(repr(testresult)))


            if format == "flat":
                out = json.loads(str(out,'utf-8'))
                out['testresult'] = testresult[0]
                out['testmessage'] = testresult[1]
                out['queries'] = rawqueries
                out = json.dumps(out)

            #unload the document, we want a fresh copy every time
            del self.docstore.data[('testflat','testflat')]

        if self.debug:
            if isinstance(out,bytes):
                log("[FINAL RESULTS] " + str(out,'utf-8'))
            else:
                log("[FINAL RESULTS] " + out)

        if isinstance(out,str):
            return out.encode('utf-8')
        else:
            return out
Beispiel #29
0
 def test31d_tfor(self):
     q = fql.Query(
         "SELECT t FOR w ID \"WR-P-E-J-0000000001.sandbox.2.s.1.w.2\"")
     results = q(self.doc)
     self.assertEqual(len(results), 3)  #includes t under morpheme
Beispiel #30
0
 def test31e_tin(self):
     q = fql.Query(
         "SELECT t IN w ID \"WR-P-E-J-0000000001.sandbox.2.s.1.w.2\"")
     results = q(self.doc)
     self.assertEqual(len(results), 1)