def __call__(self, doc, wrap=True,debug=False):
        """Execute the query on the specified document"""

        self.doc = doc

        if debug: print("[FQL EVALUATION DEBUG] Query  - Starting on document ", doc.id,file=sys.stderr)

        if self.declarations:
            for Class, decset, defaults in self.declarations:
                if debug: print("[FQL EVALUATION DEBUG] Processing declaration for ", Class.__name__, "of",str(decset),file=sys.stderr)
                doc.declare(Class,decset,**defaults)

        if self.action:
            targetselector = doc
            if self.targets and not (isinstance(self.targets.targets[0], Selector) and self.targets.targets[0].Class in ("ALL", folia.Text)):
                targetselector = (self.targets, (self, targetselector, True, debug)) #function recipe to get the generator for the targets, (f, *args) (first is always recursive)

            focusselection, targetselection = self.action(self, targetselector, debug) #selecting focus elements further constrains the target selection (if any), return values will be lists

            if self.returntype == "nothing":
                return ""
            elif self.returntype == "focus":
                responseselection = focusselection
            elif self.returntype == "target" or self.returntype == "inner-target":
                responseselection = []
                for e in targetselection:
                    if not any(x is e for x in responseselection): #filter out duplicates
                        responseselection.append(e)
            elif self.returntype == "outer-target":
                raise NotImplementedError
            elif self.returntype == "ancestor" or self.returntype == "ancestor-focus":
                responseselection = []
                try:
                    responseselection.append( next(folia.commonancestors(folia.AbstractStructureElement,*focusselection)) )
                except StopIteration:
                    raise QueryError("No ancestors found for focus: " + str(repr(focusselection)))
            elif self.returntype == "ancestor-target":
                elems = []
                for e in targetselection:
                    if isinstance(e, SpanSet):
                        elems += e
                    else:
                        elems.append(e)
                responseselection = []
                try:
                    responseselection.append( next(folia.commonancestors(folia.AbstractStructureElement,*elems)) )
                except StopIteration:
                    raise QueryError("No ancestors found for targets: " + str(repr(targetselection)))
            else:
                raise QueryError("Invalid return type: " + self.returntype)

        else:
            responseselection = []

        if self.returntype == "nothing": #we're done
            return ""

        #convert response selection to proper format and return
        if self.format.startswith('single'):
            if len(responseselection) > 1:
                raise QueryError("A single response was expected, but multiple are returned")
            if self.format == "single-xml":
                if debug: print("[FQL EVALUATION DEBUG] Query  - Returning single-xml",file=sys.stderr)
                if not responseselection:
                    return ""
                else:
                    if isinstance(responseselection[0], SpanSet):
                            r = "<result>\n"
                            for e in responseselection[0]:
                                r += e.xmlstring(True)
                            r += "</result>\n"
                            return r
                    else:
                        return responseselection[0].xmlstring(True)
            elif self.format == "single-json":
                if debug: print("[FQL EVALUATION DEBUG] Query  - Returning single-json",file=sys.stderr)
                if not responseselection:
                    return "null"
                else:
                    return json.dumps(responseselection[0].json())
            elif self.format == "single-python":
                if debug: print("[FQL EVALUATION DEBUG] Query  - Returning single-python",file=sys.stderr)
                if not responseselection:
                    return None
                else:
                    return responseselection[0]
        else:
            if self.format == "xml":
                if debug: print("[FQL EVALUATION DEBUG] Query  - Returning xml",file=sys.stderr)
                if not responseselection:
                    if wrap:
                        return "<results></results>"
                    else:
                        return ""
                else:
                    if wrap:
                        r = "<results>\n"
                    else:
                        r = ""
                    for e in responseselection:
                        if isinstance(e, SpanSet):
                            r += "<result>\n"
                            for e2 in e:
                                r += "" + e2.xmlstring(True) + "\n"
                            r += "</result>\n"
                        else:
                            r += "<result>\n" + e.xmlstring(True) + "</result>\n"
                    if wrap:
                        r += "</results>\n"
                    return r
            elif self.format == "json":
                if debug: print("[FQL EVALUATION DEBUG] Query  - Returning json",file=sys.stderr)
                if not responseselection:
                    if wrap:
                        return "[]"
                    else:
                        return ""
                else:
                    if wrap:
                        s = "[ "
                    else:
                        s = ""
                    for e in responseselection:
                        if isinstance(e, SpanSet):
                            s += json.dumps([ e2.json() for e2 in e ] ) + ", "
                        else:
                            s += json.dumps(e.json()) + ", "
                    s = s.strip(", ")
                    if wrap:
                        s += "]"
                    return s
            else: #python and undefined formats
                if debug: print("[FQL EVALUATION DEBUG] Query  - Returning python",file=sys.stderr)
                return responseselection

        return QueryError("Invalid format: " + self.format)
def __call__ ( self , doc , wrap = True , debug = False ) : self . doc = doc if debug : print ( "[FQL EVALUATION DEBUG] Query  - Starting on document " , doc . id , file = sys . stderr ) if self . declarations : for Class , decset , defaults in self . declarations : if debug : print ( "[FQL EVALUATION DEBUG] Processing declaration for " , Class . __name__ , "of" , str ( decset ) , file = sys . stderr ) doc . declare ( Class , decset , ** defaults ) if self . action : targetselector = doc if self . targets and not ( isinstance ( self . targets . targets [ 0 ] , Selector ) and self . targets . targets [ 0 ] . Class in ( "ALL" , folia . Text ) ) : targetselector = ( self . targets , ( self , targetselector , True , debug ) ) focusselection , targetselection = self . action ( self , targetselector , debug ) if self . returntype == "nothing" : return "" elif self . returntype == "focus" : responseselection = focusselection elif self . returntype == "target" or self . returntype == "inner-target" : responseselection = [ ] for e in targetselection : if not any ( x is e for x in responseselection ) : responseselection . append ( e ) elif self . returntype == "outer-target" : raise NotImplementedError elif self . returntype == "ancestor" or self . returntype == "ancestor-focus" : responseselection = [ ] try : responseselection . append ( next ( folia . commonancestors ( folia . AbstractStructureElement , * focusselection ) ) ) except StopIteration : raise QueryError ( "No ancestors found for focus: " + str ( repr ( focusselection ) ) ) elif self . returntype == "ancestor-target" : elems = [ ] for e in targetselection : if isinstance ( e , SpanSet ) : elems += e else : elems . append ( e ) responseselection = [ ] try : responseselection . append ( next ( folia . commonancestors ( folia . AbstractStructureElement , * elems ) ) ) except StopIteration : raise QueryError ( "No ancestors found for targets: " + str ( repr ( targetselection ) ) ) else : raise QueryError ( "Invalid return type: " + self . returntype ) else : responseselection = [ ] if self . returntype == "nothing" : return "" if self . format . startswith ( 'single' ) : if len ( responseselection ) > 1 : raise QueryError ( "A single response was expected, but multiple are returned" ) if self . format == "single-xml" : if debug : print ( "[FQL EVALUATION DEBUG] Query  - Returning single-xml" , file = sys . stderr ) if not responseselection : return "" else : if isinstance ( responseselection [ 0 ] , SpanSet ) : r = "<result>\n" for e in responseselection [ 0 ] : r += e . xmlstring ( True ) r += "</result>\n" return r else : return responseselection [ 0 ] . xmlstring ( True ) elif self . format == "single-json" : if debug : print ( "[FQL EVALUATION DEBUG] Query  - Returning single-json" , file = sys . stderr ) if not responseselection : return "null" else : return json . dumps ( responseselection [ 0 ] . json ( ) ) elif self . format == "single-python" : if debug : print ( "[FQL EVALUATION DEBUG] Query  - Returning single-python" , file = sys . stderr ) if not responseselection : return None else : return responseselection [ 0 ] else : if self . format == "xml" : if debug : print ( "[FQL EVALUATION DEBUG] Query  - Returning xml" , file = sys . stderr ) if not responseselection : if wrap : return "<results></results>" else : return "" else : if wrap : r = "<results>\n" else : r = "" for e in responseselection : if isinstance ( e , SpanSet ) : r += "<result>\n" for e2 in e : r += "" + e2 . xmlstring ( True ) + "\n" r += "</result>\n" else : r += "<result>\n" + e . xmlstring ( True ) + "</result>\n" if wrap : r += "</results>\n" return r elif self . format == "json" : if debug : print ( "[FQL EVALUATION DEBUG] Query  - Returning json" , file = sys . stderr ) if not responseselection : if wrap : return "[]" else : return "" else : if wrap : s = "[ " else : s = "" for e in responseselection : if isinstance ( e , SpanSet ) : s += json . dumps ( [ e2 . json ( ) for e2 in e ] ) + ", " else : s += json . dumps ( e . json ( ) ) + ", " s = s . strip ( ", " ) if wrap : s += "]" return s else : if debug : print ( "[FQL EVALUATION DEBUG] Query  - Returning python" , file = sys . stderr ) return responseselection return QueryError ( "Invalid format: " + self . format )
Execute the query on the specified document