コード例 #1
0
ファイル: xmlresults.py プロジェクト: scossu/rdflib
    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"
コード例 #2
0
    def contexts(self, triple=None):
        """
        Iterates over results to "SELECT ?NAME { GRAPH ?NAME { ?s ?p ?o } }"
        or "SELECT ?NAME { GRAPH ?NAME {} }" if triple is `None`.

        Returns instances of this store with the SPARQL wrapper
        object updated via addNamedGraph(?NAME).

        This causes a named-graph-uri key / value  pair to be sent over
        the protocol.

        Please note that some SPARQL endpoints are not able to find empty named
        graphs.
        """
        self.resetQuery()

        if triple:
            nts = self.node_to_sparql
            s, p, o = triple
            params = (nts(s if s else Variable('s')),
                      nts(p if p else Variable('p')),
                      nts(o if o else Variable('o')))
            self.setQuery('SELECT ?name WHERE { GRAPH ?name { %s %s %s }}' % params)
        else:
            self.setQuery('SELECT ?name WHERE { GRAPH ?name {} }')

        with contextlib.closing(SPARQLWrapper.query(self).response) as res:
            doc = etree.parse(res)

        return (
            rt.get(Variable("name"))
            for rt, vars in _traverse_sparql_result_dom(
                doc, as_dictionary=True, node_from_result=self.node_from_result)
        )
コード例 #3
0
    def contexts(self, triple=None):
        """
        Iterates over results to "SELECT ?NAME { GRAPH ?NAME { ?s ?p ?o } }"
        or "SELECT ?NAME { GRAPH ?NAME {} }" if triple is `None`.

        Returns instances of this store with the SPARQL wrapper
        object updated via addNamedGraph(?NAME).

        This causes a named-graph-uri key / value  pair to be sent over
        the protocol.

        Please note that some SPARQL endpoints are not able to find empty named
        graphs.
        """
        self.resetQuery()

        if triple:
            nts = self.node_to_sparql
            s, p, o = triple
            params = (nts(s if s else Variable('s')),
                      nts(p if p else Variable('p')),
                      nts(o if o else Variable('o')))
            self.setQuery('SELECT ?name WHERE { GRAPH ?name { %s %s %s }}' %
                          params)
        else:
            self.setQuery('SELECT ?name WHERE { GRAPH ?name {} }')

        with contextlib.closing(SPARQLWrapper.query(self).response) as res:
            doc = etree.parse(res)

        return (rt.get(
            Variable("name")
        ) for rt, vars in _traverse_sparql_result_dom(
            doc, as_dictionary=True, node_from_result=self.node_from_result))
コード例 #4
0
ファイル: xmlresults.py プロジェクト: wufanjames/rdflib
    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
コード例 #5
0
ファイル: xmlresults.py プロジェクト: drewp/rdflib
    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
コード例 #6
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]
コード例 #7
0
ファイル: xmlresults.py プロジェクト: joernhees/rdflib
    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"
コード例 #8
0
    def __len__(self, context=None):
        if not self.sparql11:
            raise NotImplementedError("For performance reasons, this is not" +
                                      "supported for sparql1.0 endpoints")
        else:
            self.resetQuery()
            q = "SELECT (count(*) as ?c) WHERE {?s ?p ?o .}"
            if self._is_contextual(context):
                self.addParameter("default-graph-uri", context.identifier)
            self.setQuery(q)

            with contextlib.closing(SPARQLWrapper.query(self).response) as res:
                doc = etree.parse(res)

            rt, vars = iter(
                _traverse_sparql_result_dom(
                    doc,
                    as_dictionary=True,
                    node_from_result=self.node_from_result)).next()
            return int(rt.get(Variable("c")))
コード例 #9
0
    def __len__(self, context=None):
        if not self.sparql11:
            raise NotImplementedError(
                "For performance reasons, this is not" +
                "supported for sparql1.0 endpoints")
        else:
            self.resetQuery()
            q = "SELECT (count(*) as ?c) WHERE {?s ?p ?o .}"
            if self._is_contextual(context):
                self.addParameter("default-graph-uri", context.identifier)
            self.setQuery(q)

            with contextlib.closing(SPARQLWrapper.query(self).response) as res:
                doc = etree.parse(res)

            rt, vars = iter(
                _traverse_sparql_result_dom(
                    doc,
                    as_dictionary=True,
                    node_from_result=self.node_from_result
                )
            ).next()
            return int(rt.get(Variable("c")))
コード例 #10
0
            query = query + ' LIMIT %s' % int(getattr(context, LIMIT))
        except (ValueError, TypeError, AttributeError):
            pass
        try:
            query = query + ' OFFSET %s' % int(getattr(context, OFFSET))
        except (ValueError, TypeError, AttributeError):
            pass

        self.resetQuery()
        if self._is_contextual(context):
            self.addParameter("default-graph-uri", context.identifier)
        self.timeout = self._timeout
        self.setQuery(query)

        with contextlib.closing(SPARQLWrapper.query(self).response) as res:
            doc = etree.parse(res)

        # ElementTree.dump(doc)
        for rt, vars in _traverse_sparql_result_dom(
                doc, as_dictionary=True,
                node_from_result=self.node_from_result):
            yield (rt.get(s, s), rt.get(p, p), rt.get(o, o)), None

    def triples_choices(self, (subject, predicate, object_), context=None):
        """
        A variant of triples that can take a list of terms instead of a
        single term in any slot.  Stores can implement this to optimize
        the response time from the import default 'fallback' implementation,
        which will iterate over each term in the list and dispatch to
        triples.
        """
コード例 #11
0
            query = query + ' LIMIT %s' % int(getattr(context, LIMIT))
        except (ValueError, TypeError, AttributeError):
            pass
        try:
            query = query + ' OFFSET %s' % int(getattr(context, OFFSET))
        except (ValueError, TypeError, AttributeError):
            pass

        self.resetQuery()
        if self._is_contextual(context):
            self.addParameter("default-graph-uri", context.identifier)
        self.timeout = self._timeout
        self.setQuery(query)

        with contextlib.closing(SPARQLWrapper.query(self).response) as res:
            doc = etree.parse(res)

        # ElementTree.dump(doc)
        for rt, vars in _traverse_sparql_result_dom(
                doc,
                as_dictionary=True,
                node_from_result=self.node_from_result):
            yield (rt.get(s, s),
                   rt.get(p, p),
                   rt.get(o, o)), None

    def triples_choices(self, (subject, predicate, object_), context=None):
        """
        A variant of triples that can take a list of terms instead of a
        single term in any slot.  Stores can implement this to optimize
        the response time from the import default 'fallback' implementation,
コード例 #12
0
    def triples(self, xxx_todo_changeme2, context=None):
        """
        - tuple **(s, o, p)**
            the triple used as filter for the SPARQL select.
            (None, None, None) means anything.
        - context **context**
            the graph effectively calling this method.

        Returns a tuple of triples executing essentially a SPARQL like
        SELECT ?subj ?pred ?obj WHERE { ?subj ?pred ?obj }

        **context** may include three parameter
        to refine the underlying query:
         * LIMIT: an integer to limit the number of results
         * OFFSET: an integer to enable paging of results
         * ORDERBY: an instance of Variable('s'), Variable('o') or Variable('p')
        or, by default, the first 'None' from the given triple

        .. warning::
        - Using LIMIT or OFFSET automatically include ORDERBY otherwise this is
        because the results are retrieved in a not deterministic way (depends on
        the walking path on the graph)
        - Using OFFSET without defining LIMIT will discard the first OFFSET - 1
        results

        ``
        a_graph.LIMIT = limit
        a_graph.OFFSET = offset
        triple_generator = a_graph.triples(mytriple):
            #do something
        #Removes LIMIT and OFFSET if not required for the next triple() calls
        del a_graph.LIMIT
        del a_graph.OFFSET
        ``
        """
        (s, p, o) = xxx_todo_changeme2
        vars = []
        if not s:
            s = Variable('s')
            vars.append(s)

        if not p:
            p = Variable('p')
            vars.append(p)
        if not o:
            o = Variable('o')
            vars.append(o)

        if vars:
            v = ' '.join([term.n3() for term in vars])
        else:
            v = '*'

        nts = self.node_to_sparql
        query = "SELECT %s WHERE { %s %s %s }" % (v, nts(s), nts(p), nts(o))

        # The ORDER BY is necessary
        if hasattr(context, LIMIT) or hasattr(context, OFFSET) \
                or hasattr(context, ORDERBY):
            var = None
            if isinstance(s, Variable):
                var = s
            elif isinstance(p, Variable):
                var = p
            elif isinstance(o, Variable):
                var = o
            elif hasattr(context, ORDERBY) \
                    and isinstance(getattr(context, ORDERBY), Variable):
                var = getattr(context, ORDERBY)
            query = query + ' %s %s' % (ORDERBY, var.n3())

        try:
            query = query + ' LIMIT %s' % int(getattr(context, LIMIT))
        except (ValueError, TypeError, AttributeError):
            pass
        try:
            query = query + ' OFFSET %s' % int(getattr(context, OFFSET))
        except (ValueError, TypeError, AttributeError):
            pass

        self.resetQuery()
        if self._is_contextual(context):
            self.addParameter("default-graph-uri", context.identifier)
        self.timeout = self._timeout
        self.setQuery(query)

        with contextlib.closing(SPARQLWrapper.query(self).response) as res:
            doc = etree.parse(res)

        # ElementTree.dump(doc)
        for rt, vars in _traverse_sparql_result_dom(
                doc, as_dictionary=True,
                node_from_result=self.node_from_result):
            yield (rt.get(s, s), rt.get(p, p), rt.get(o, o)), None