Skip to content
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
6 changes: 4 additions & 2 deletions rdflib/plugins/serializers/trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def preprocess(self) -> None:
if len(context) == 0:
continue
self.store = context
self.getQName(context.identifier)
# Don't generate a new prefix for a graph URI if one already exists
self.getQName(context.identifier, False)
self._subjects = {}

for triple in context:
Expand Down Expand Up @@ -97,7 +98,8 @@ def serialize(
if isinstance(store.identifier, BNode):
iri = store.identifier.n3()
else:
iri = self.getQName(store.identifier)
# Show the full graph URI if a prefix for it doesn't already exist
iri = self.getQName(store.identifier, False)
if iri is None:
# type error: "IdentifiedNode" has no attribute "n3"
iri = store.identifier.n3() # type: ignore[attr-defined]
Expand Down
10 changes: 5 additions & 5 deletions test/test_trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def test_remember_namespace():
# prefix for the graph but later serialize() calls would work.
first_out = g.serialize(format="trig", encoding="latin-1")
second_out = g.serialize(format="trig", encoding="latin-1")
assert b"@prefix ns1: <http://example.com/> ." in second_out
assert b"@prefix ns1: <http://example.com/> ." in first_out
assert b"@prefix ns1: <http://example.com/> ." not in second_out
assert b"@prefix ns1: <http://example.com/> ." not in first_out


def test_graph_qname_syntax():
g = rdflib.ConjunctiveGraph()
g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),))
out = g.serialize(format="trig", encoding="latin-1")
assert b"ns1:graph1 {" in out
assert b"ns1:graph1 {" not in out


def test_graph_uri_syntax():
Expand Down Expand Up @@ -178,9 +178,9 @@ def test_prefixes():
cg.parse(data=data, format="trig")
data = cg.serialize(format="trig", encoding="latin-1")

assert "ns2: <http://ex.org/docs/".encode("latin-1") in data, data
assert "ns2: <http://ex.org/docs/".encode("latin-1") not in data, data
assert "<ns2:document1>".encode("latin-1") not in data, data
assert "ns2:document1".encode("latin-1") in data, data
assert "ns2:document1".encode("latin-1") not in data, data


def test_issue_2154():
Expand Down