Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializer: use store prefixes and base #61

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions oxrdflib/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ class _OxigraphSerializer(Serializer, ABC):
def serialize(
self,
stream: IO[bytes],
_base: Optional[str] = None,
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
base_iri = base or self.store.base
prefixes = dict(self.store.namespaces())
if isinstance(self.store.store, OxigraphStore):
self.store.store._inner.dump(
stream,
format=self._format,
from_graph=None if isinstance(self.store, Dataset) else to_ox(self.store.identifier),
base_iri=base_iri,
prefixes=prefixes,
)
else:
serialize((to_ox(q) for q in self.store), stream, format=self._format)
serialize((to_ox(q) for q in self.store), stream, format=self._format, base_iri=base_iri, prefixes=prefixes)

@property
@abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Database :: Database Engines/Servers",
]
dependencies = ["pyoxigraph~=0.4.0", "rdflib>=6.3,<8.0"]
dependencies = ["pyoxigraph~=0.4.2", "rdflib>=6.3,<8.0"]
dynamic = ["version"]
license = { text = "BSD-3-Clause" }
name = "oxrdflib"
Expand Down
1 change: 0 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def test_parse_graph(self):
transactional=transactional,
)
self.assertEqual(list(graph), [(s, p, o)])
# TODO: pyoxigraph 0.4.2: test that prefixes are properly loaded

@unittest.skipIf(rdflib_version < (7, 1), "only works in rdflib 7.1+")
def test_parse_dataset(self):
Expand Down
27 changes: 13 additions & 14 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,29 @@ 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-turtle", "<s> v:p <o> .\n"),
("ox-ttl", "<s> v:p <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-n3", "<s> v:p <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-trig", "<s> v:p <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:about="s">
<v:p rdf:resource="o"/>
</rdf:Description>
</rdf:RDF>""",
),
):
with self.subTest(store=store, format=fmt):
graph = Graph(store=store)
graph = Graph(store=store, base="http://example.com/")
graph.add((s, p, o))
graph.store.add((o, p, s), context=g) # Should not be serialized
self.assertEqual(graph.serialize(format=fmt), serialization)
# TODO: pyoxigraph 0.4.2: test that prefixes and base are properly used
graph.bind("v", "http://example.com/vocab#")
self.assertTrue(graph.serialize(format=fmt).endswith(serialization))

def test_serialize_dataset(self):
for store in ("default", "oxigraph"):
Expand All @@ -47,14 +46,14 @@ def test_serialize_dataset(self):
),
(
"ox-trig",
"<http://example.com/g> {\n\t<http://example.com/s> "
"<http://example.com/vocab#p> <http://example.com/o> .\n}\n",
"<g> {\n\t<s> v:p <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)
dataset.bind("v", "http://example.com/vocab#")
self.assertTrue(dataset.serialize(format=fmt, base="http://example.com/").endswith(serialization))


if __name__ == "__main__":
Expand Down