def __init__(self, *args, **kwargs): super(ReaderPlugin, self).__init__(*args, **kwargs) self._rdflib_store = kwargs.get("rdflib_store", "IOMemory") self._rdflib_identifier = kwargs.get("rdflib_identifier") self._commit_pending_transaction_on_close = \ kwargs.get("commit_pending_transaction_on_close", True) self._graph = ConjunctiveGraph(store=self._rdflib_store, identifier=self._rdflib_identifier)
def load_from_source(self, data=None, file=None, location=None, format=None): """ Load the `resource` from a source (uri, file or string rdf data). """ graph = ConjunctiveGraph() if format is None: format = 'application/rdf+xml' elif format in self.formats: format = self.formats[format] graph.parse(data=data, file=file, location=location, format=format) self.set(graph)
def graph(self, direct=True): """ Return an `rdflib` `ConjunctiveGraph` represenation of the current `resource` """ graph = ConjunctiveGraph() self.bind_namespaces_to_graph(graph) graph.add((self.subject, RDF['type'], self.uri)) for predicate in self.__rdf_direct: for value in self.__rdf_direct[predicate]: if isinstance(value, (URIRef, Literal, BNode)): graph.add((self.subject, predicate, value)) if not direct: for predicate in self.__rdf_inverse: for value in self.__rdf_inverse[predicate]: if isinstance(value, (URIRef, Literal, BNode)): graph.add((value, predicate, self.subject)) return graph
def __init__(self, reader, *args, **kwargs): RDFWriter.__init__(self, reader, *args, **kwargs) if isinstance(self.reader, ReaderPlugin): self.__rdflib_store = self.reader.rdflib_store self.__rdflib_identifier = self.reader.rdflib_identifier self.__commit_pending_transaction_on_close = \ self.reader.commit_pending_transaction_on_close self.__graph = self.reader.graph else: self.__rdflib_store = kwargs.get("rdflib_store", "IOMemory") self.__rdflib_identifier = kwargs.get("rdflib_identifier") self.__commit_pending_transaction_on_close = \ kwargs.get("commit_pending_transaction_on_close", True) self.__graph = ConjunctiveGraph( store=self.__rdflib_store, identifier=self.__rdflib_identifier) warnings.warn("Graph is not readable through the reader plugin", UserWarning)
def __deserialize(self, response): ''' serializes the response based on the Content-Type or Accept header ''' content_type = response.getheader('Content-type') content = response.read() format = 'text' if isinstance(content_type, str): for type, mimetype in Sesame2.response_format.items(): if content_type.startswith(mimetype): format = type ser_content = content if format in ['nt', 'xml', 'n3', 'turtle']: graph = ConjunctiveGraph() ser_content = graph.parse(data=content, format=format) elif format in ['sparql']: ser_content = parse_sparql_xml(content) elif format in ['sparql+json']: ser_content = loads(content) return ser_content