-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows using Oxigraph native serializers
- Loading branch information
Showing
3 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import IO, Any, Optional | ||
|
||
from pyoxigraph import RdfFormat, serialize | ||
from rdflib.serializer import Serializer | ||
|
||
from oxrdflib._converter import to_ox | ||
from oxrdflib.store import OxigraphStore | ||
|
||
__all__ = [ | ||
"OxigraphN3Serializer", | ||
"OxigraphTurtleSerializer", | ||
"OxigraphNTriplesSerializer", | ||
"OxigraphRdfXmlSerializer", | ||
"OxigraphTriGSerializer", | ||
"OxigraphNQuadsSerializer", | ||
] | ||
|
||
|
||
class _OxigraphSerializer(Serializer, ABC): | ||
def serialize( | ||
self, | ||
stream: IO[bytes], | ||
_base: Optional[str] = None, | ||
encoding: Optional[str] = None, | ||
**kwargs: Any, # noqa: ARG002 | ||
) -> None: | ||
if encoding not in (None, "utf-8"): | ||
raise ValueError(f"RDF files are always utf-8 encoded, I was passed: {encoding}") | ||
# TODO: base and prefixes | ||
if isinstance(self.store, OxigraphStore): | ||
self.store._inner.dump(stream, format=self._format) | ||
else: | ||
serialize((to_ox(q) for q in self.store), stream, format=self._format) | ||
|
||
@property | ||
@abstractmethod | ||
def _format(self) -> RdfFormat: | ||
pass | ||
|
||
|
||
class OxigraphN3Serializer(_OxigraphSerializer): | ||
_format = RdfFormat.N3 | ||
|
||
|
||
class OxigraphTurtleSerializer(_OxigraphSerializer): | ||
_format = RdfFormat.TURTLE | ||
|
||
|
||
class OxigraphNTriplesSerializer(_OxigraphSerializer): | ||
_format = RdfFormat.N_TRIPLES | ||
|
||
|
||
class OxigraphRdfXmlSerializer(_OxigraphSerializer): | ||
_format = RdfFormat.RDF_XML | ||
|
||
|
||
class OxigraphNQuadsSerializer(_OxigraphSerializer): | ||
_format = RdfFormat.N_QUADS | ||
|
||
|
||
class OxigraphTriGSerializer(_OxigraphSerializer): | ||
_format = RdfFormat.TRIG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import unittest | ||
|
||
from rdflib import Dataset, Graph, URIRef | ||
|
||
s = URIRef("http://example.com/s") | ||
p = URIRef("http://example.com/vocab#p") | ||
o = URIRef("http://example.com/o") | ||
g = URIRef("http://example.com/g") | ||
|
||
|
||
class TestSerializer(unittest.TestCase): | ||
def test_serialize_graph(self): | ||
for store in ("default", "oxigraph"): | ||
for fmt, serialization in ( | ||
("ox-turtle", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"), | ||
("ox-ttl", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"), | ||
("ox-ntriples", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"), | ||
("ox-n3", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"), | ||
("ox-nquads", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"), | ||
("ox-nt", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"), | ||
("ox-nt11", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"), | ||
("ox-trig", "<http://example.com/s> <http://example.com/vocab#p> <http://example.com/o> .\n"), | ||
( | ||
"ox-xml", | ||
"""<?xml version="1.0" encoding="UTF-8"?> | ||
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> | ||
<rdf:Description rdf:about="http://example.com/s"> | ||
<p xmlns="http://example.com/vocab#" rdf:resource="http://example.com/o"/> | ||
</rdf:Description> | ||
</rdf:RDF>""", | ||
), | ||
): | ||
with self.subTest(store=store, format=fmt): | ||
graph = Graph(store=store) | ||
graph.add((s, p, o)) | ||
graph.store.add((o, p, s), context=g) # Should not be serialized | ||
self.assertEqual(graph.serialize(format=fmt), serialization) | ||
|
||
def test_serialize_dataset(self): | ||
for store in ("default", "oxigraph"): | ||
for fmt, serialization in ( | ||
( | ||
"ox-nquads", | ||
"<http://example.com/s> <http://example.com/vocab#p> " | ||
"<http://example.com/o> <http://example.com/g> .\n", | ||
), | ||
( | ||
"ox-trig", | ||
"<http://example.com/g> {\n\t<http://example.com/s> " | ||
"<http://example.com/vocab#p> <http://example.com/o> .\n}\n", | ||
), | ||
): | ||
with self.subTest(store=store, format=fmt): | ||
dataset = Dataset(store=store) | ||
dataset.add((s, p, o, Graph(identifier=g))) | ||
self.assertEqual(dataset.serialize(format=fmt), serialization) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |