Example #1
0
    def __init__(self, source, content_type=None):

        tree = etree.parse(source)

        boolean = tree.find(RESULTS_NS_ET + 'boolean')
        results = tree.find(RESULTS_NS_ET + 'results')

        if boolean is not None:
            type_ = 'ASK'
        elif results is not None:
            type_ = 'SELECT'
        else:
            raise ResultException(
                "No RDF result-bindings or boolean answer found!")

        Result.__init__(self, type_)

        if type_ == 'SELECT':
            self.bindings = []
            for result in results:
                r = {}
                for binding in result:
                    r[Variable(binding.get('name'))] = parseTerm(binding[0])
                self.bindings.append(r)

            self.vars = [
                Variable(x.get("name"))
                for x in tree.findall('./%shead/%svariable' %
                                      (RESULTS_NS_ET, RESULTS_NS_ET))
            ]

        else:
            self.askAnswer = boolean.text.lower().strip() == "true"
Example #2
0
    def __init__(self, tree):

        boolean = tree.find(RESULTS_NS_ET + 'boolean')
        results = tree.find(RESULTS_NS_ET + 'results')

        if boolean!=None: 
            type_='ASK'
        elif results!=None:
            type_='SELECT'
        else: 
            raise ResultException("No result-bindings or boolean answer found!")

        Result.__init__(self, type_)
        if type_=='SELECT':
            self.bindings=[]
            for result in results:
                r = {}
                for binding in result:
                    r[binding.get('name')] = parseTerm(binding[0])
                self.bindings.append(r)

            self.vars=[x.get("name") for x in tree.findall(RESULTS_NS_ET+'head')]
            
            
        elif type_=='ASK':
            self.askAnswer=bool(boolean.text)
Example #3
0
    def __init__(self, source):

        xmlstring = source.read()

        if isinstance(xmlstring, text_type):
            xmlstring = xmlstring.encode('utf-8')
        try:
            parser = etree.XMLParser(huge_tree=True)
            tree = etree.parse(BytesIO(xmlstring), parser)
        except TypeError:
            tree = etree.fromstring(xmlstring)
        except Exception as e:
            log.exception("Error parsing XML results: %s" % xmlstring)
            raise e

        boolean = tree.find(RESULTS_NS_ET + 'boolean')
        results = tree.find(RESULTS_NS_ET + 'results')

        if boolean is not None:
            type_ = 'ASK'
        elif results is not None:
            type_ = 'SELECT'
        else:
            g = Graph()
            try:
                g.parse(data=xmlstring)
                if len(g) == 0:
                    raise
                type_ = 'CONSTRUCT'

            except:
                raise ResultException(
                    "No RDF Graph, result-bindings or boolean answer found!")

        Result.__init__(self, type_)
        if type_ == 'SELECT':
            self.bindings = []
            for result in results:
                r = {}
                for binding in result:
                    r[Variable(binding.get('name'))] = parseTerm(binding[0])
                self.bindings.append(r)

            self.vars = [
                Variable(x.get("name"))
                for x in tree.findall('./%shead/%svariable' %
                                      (RESULTS_NS_ET, RESULTS_NS_ET))
            ]

        elif type_ == 'ASK':
            self.askAnswer = boolean.text.lower().strip() == "true"
        elif type_ == 'CONSTRUCT':
            self.graph = g
Example #4
0
    def __init__(self, source):

        xmlstring = source.read()

        if isinstance(xmlstring, str):
            xmlstring = xmlstring.encode('utf-8')
        try:
            tree = ElementTree.fromstring(xmlstring)
        except Exception as e:
            try:
                raise e.__class__("error parsing %r: %s" % (xmlstring, e))
            except:
                raise e

        boolean = tree.find(RESULTS_NS_ET + 'boolean')
        results = tree.find(RESULTS_NS_ET + 'results')

        if boolean != None:
            type_ = 'ASK'
        elif results != None:
            type_ = 'SELECT'
        else:
            g = Graph()
            try:
                g.parse(data=xmlstring)
                if len(g) == 0:
                    raise
                type_ = 'CONSTRUCT'

            except:
                raise ResultException(
                    "No RDF Graph, result-bindings or boolean answer found!")

        Result.__init__(self, type_)
        if type_ == 'SELECT':
            self.bindings = []
            for result in results:
                r = {}
                for binding in result:
                    r[Variable(binding.get('name'))] = parseTerm(binding[0])
                self.bindings.append(r)

            self.vars = [
                Variable(x.get("name"))
                for x in tree.findall('./%shead/%svariable' %
                                      (RESULTS_NS_ET, RESULTS_NS_ET))
            ]

        elif type_ == 'ASK':
            self.askAnswer = boolean.text.lower().strip() == "true"
        elif type_ == 'CONSTRUCT':
            self.graph = g
Example #5
0
    def __init__(self, source):

        xmlstring = source.read()

        if isinstance(xmlstring, text_type):
            xmlstring = xmlstring.encode('utf-8')
        try:
            parser = etree.XMLParser(huge_tree=True)
            tree = etree.parse(BytesIO(xmlstring), parser)
        except TypeError:
            tree = etree.fromstring(xmlstring)
        except Exception as e:
            log.exception("Error parsing XML results: %s"%xmlstring)
            raise e

        boolean = tree.find(RESULTS_NS_ET + 'boolean')
        results = tree.find(RESULTS_NS_ET + 'results')

        if boolean is not None:
            type_ = 'ASK'
        elif results is not None:
            type_ = 'SELECT'
        else:
            g = Graph()
            try:
                g.parse(data=xmlstring)
                if len(g) == 0:
                    raise
                type_ = 'CONSTRUCT'

            except:
                raise ResultException(
                    "No RDF Graph, result-bindings or boolean answer found!")

        Result.__init__(self, type_)
        if type_ == 'SELECT':
            self.bindings = []
            for result in results:
                r = {}
                for binding in result:
                    r[Variable(binding.get('name'))] = parseTerm(binding[0])
                self.bindings.append(r)

            self.vars = [Variable(x.get("name"))
                         for x in tree.findall(
                         './%shead/%svariable' % (
                             RESULTS_NS_ET, RESULTS_NS_ET))]

        elif type_ == 'ASK':
            self.askAnswer = boolean.text.lower().strip() == "true"
        elif type_ == 'CONSTRUCT':
            self.graph = g
Example #6
0
    def __init__(self, json):
        self.json=json
        if "boolean" in json: 
            type_='ASK'
        elif "results" in json:
            type_='SELECT'
        else: 
            raise ResultException('No boolean or results in json!')
        
        Result.__init__(self,type_)

        if type_=='ASK':
            self.askAnswer=bool(json['boolean'])
        else:
            self.bindings=self._get_bindings()
Example #7
0
    def __init__(self, json):
        self.json = json
        if "boolean" in json:
            type_ = 'ASK'
        elif "results" in json:
            type_ = 'SELECT'
        else:
            raise ResultException('No boolean or results in json!')

        Result.__init__(self, type_)

        if type_ == 'ASK':
            self.askAnswer = bool(json['boolean'])
        else:
            self.bindings = self._get_bindings()
Example #8
0
    def __init__(self, source, **kwargs):

        if not isinstance(source, Graph):
            graph = Graph()
            graph.load(source, **kwargs)
        else:
            graph = source

        rs = graph.value(predicate=RDF.type, object=RS.ResultSet)
        # there better be only one :)

        if rs is None:
            type_ = 'CONSTRUCT'

            # use a new graph
            g = Graph()
            g += graph

        else:

            askAnswer = graph.value(rs, RS.boolean)

            if askAnswer is not None:
                type_ = 'ASK'
            else:
                type_ = 'SELECT'

        Result.__init__(self, type_)

        if type_ == 'SELECT':
            self.vars = [
                Variable(v) for v in graph.objects(rs, RS.resultVariable)
            ]

            self.bindings = []

            for s in graph.objects(rs, RS.solution):
                sol = {}
                for b in graph.objects(s, RS.binding):
                    sol[Variable(graph.value(b, RS.variable))] = graph.value(
                        b, RS.value)
                self.bindings.append(sol)
        elif type_ == 'ASK':
            self.askAnswer = askAnswer.value
            if askAnswer.value == None:
                raise Exception('Malformed boolean in ask answer!')
        elif type_ == 'CONSTRUCT':
            self.graph = g
Example #9
0
    def __init__(self, json):
        self.json = json
        if "boolean" in json:
            type_ = "ASK"
        elif "results" in json:
            type_ = "SELECT"
        else:
            raise ResultException("No boolean or results in json!")

        Result.__init__(self, type_)

        if type_ == "ASK":
            self.askAnswer = bool(json["boolean"])
        else:
            self.bindings = self._get_bindings()
            self.vars = [Variable(x) for x in json["head"]["vars"]]
Example #10
0
    def __init__(self, source, **kwargs):

        if not isinstance(source, Graph):
            graph = Graph()
            graph.load(source, **kwargs)
        else:
            graph = source

        rs = graph.value(predicate=RDF.type, object=RS.ResultSet)
                         # there better be only one :)

        if rs is None:
            type_ = 'CONSTRUCT'

            # use a new graph
            g = Graph()
            g += graph

        else:

            askAnswer = graph.value(rs, RS.boolean)

            if askAnswer is not None:
                type_ = 'ASK'
            else:
                type_ = 'SELECT'

        Result.__init__(self, type_)

        if type_ == 'SELECT':
            self.vars = [Variable(v) for v in graph.objects(rs,
                                                            RS.resultVariable)]

            self.bindings = []

            for s in graph.objects(rs, RS.solution):
                sol = {}
                for b in graph.objects(s, RS.binding):
                    sol[Variable(graph.value(
                        b, RS.variable))] = graph.value(b, RS.value)
                self.bindings.append(sol)
        elif type_ == 'ASK':
            self.askAnswer = askAnswer.value
            if askAnswer.value == None:
                raise Exception('Malformed boolean in ask answer!')
        elif type_ == 'CONSTRUCT':
            self.graph = g
Example #11
0
    def __init__(self, source, content_type: Optional[str] = None):

        try:
            # try use as if etree is from lxml, and if not use it as normal.
            parser = etree.XMLParser(huge_tree=True)  # type: ignore[call-arg]
            tree = etree.parse(source, parser)
        except TypeError:
            tree = etree.parse(source)

        boolean = tree.find(RESULTS_NS_ET + "boolean")
        results = tree.find(RESULTS_NS_ET + "results")

        if boolean is not None:
            type_ = "ASK"
        elif results is not None:
            type_ = "SELECT"
        else:
            raise ResultException("No RDF result-bindings or boolean answer found!")

        Result.__init__(self, type_)

        if type_ == "SELECT":
            self.bindings = []
            for result in results:  # type: ignore[union-attr]
                r = {}
                for binding in result:
                    r[Variable(binding.get("name"))] = parseTerm(binding[0])
                self.bindings.append(r)

            self.vars = [
                Variable(x.get("name"))
                for x in tree.findall(
                    "./%shead/%svariable" % (RESULTS_NS_ET, RESULTS_NS_ET)
                )
            ]

        else:
            self.askAnswer = boolean.text.lower().strip() == "true"  # type: ignore[union-attr]
Example #12
0
    def __init__(self, source, content_type=None):

        try:
            parser = etree.XMLParser(huge_tree=True)
            tree = etree.parse(source, parser)
        except TypeError:
            tree = etree.parse(source)

        boolean = tree.find(RESULTS_NS_ET + 'boolean')
        results = tree.find(RESULTS_NS_ET + 'results')

        if boolean is not None:
            type_ = 'ASK'
        elif results is not None:
            type_ = 'SELECT'
        else:
            raise ResultException(
                "No RDF result-bindings or boolean answer found!")

        Result.__init__(self, type_)

        if type_ == 'SELECT':
            self.bindings = []
            for result in results:
                r = {}
                for binding in result:
                    r[Variable(binding.get('name'))] = parseTerm(binding[0])
                self.bindings.append(r)

            self.vars = [Variable(x.get("name"))
                         for x in tree.findall(
                         './%shead/%svariable' % (
                             RESULTS_NS_ET, RESULTS_NS_ET))]

        else:
            self.askAnswer = boolean.text.lower().strip() == "true"
Example #13
0
 def __init__(self, inner_result):
     if type(inner_result) is EagerIterator:
         Result.__init__(self, "SELECT")
         self._eagerIterator = inner_result
         self.vars = inner_result.vars
     elif type(inner_result) is bool:
         Result.__init__(self, "ASK")
         self.askAnswer = inner_result
     elif type(inner_result) is Graph:
         Result.__init__(self, "CONSTRUCT")
         self.graph = inner_result
     else:
         raise ValueError("Unrecognized inner_result %r" % inner_result)
Example #14
0
 def __init__(self, inner_result):
     if type(inner_result) is EagerIterator:
         Result.__init__(self, "SELECT")
         self._eagerIterator = inner_result
         self.vars = inner_result.vars
     elif type(inner_result) is bool:
         Result.__init__(self, "ASK")
         self.askAnswer = inner_result
     elif type(inner_result) is Graph:
         Result.__init__(self, "CONSTRUCT")
         self.graph = inner_result
     else:
         raise ValueError("Unrecognized inner_result %r" % inner_result)
Example #15
0
 def __init__(self, res):
     Result.__init__(self, res["type_"])
     self.vars = res.get("vars_")
     self.bindings = res.get("bindings")
     self.askAnswer = res.get("askAnswer")
     self.graph = res.get("graph")
Example #16
0
 def __init__(self, res):
     Result.__init__(self, res["type_"])
     self.vars = res.get("vars_")
     self.bindings = res.get("bindings")
     self.askAnswer = res.get("askAnswer")
     self.graph = res.get("graph")