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

Sort Turtle output #1978

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
20 changes: 16 additions & 4 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,7 @@ def serialize(
format: str,
base: Optional[str],
encoding: str,
sort: bool = ...,
**args: Any,
) -> bytes:
...
Expand All @@ -1246,6 +1247,7 @@ def serialize(
base: Optional[str] = ...,
*,
encoding: str,
sort: bool = ...,
**args: Any,
) -> bytes:
...
Expand All @@ -1258,6 +1260,7 @@ def serialize(
format: str = ...,
base: Optional[str] = ...,
encoding: None = ...,
sort: bool = ...,
**args: Any,
) -> str:
...
Expand All @@ -1270,6 +1273,7 @@ def serialize(
format: str = ...,
base: Optional[str] = ...,
encoding: Optional[str] = ...,
sort: bool = ...,
**args: Any,
) -> "Graph":
...
Expand All @@ -1282,6 +1286,7 @@ def serialize(
format: str = ...,
base: Optional[str] = ...,
encoding: Optional[str] = ...,
sort: bool = ...,
**args: Any,
) -> Union[bytes, str, "Graph"]:
...
Expand All @@ -1292,6 +1297,7 @@ def serialize(
format: str = "turtle",
base: Optional[str] = None,
encoding: Optional[str] = None,
sort: bool = ...,
**args: Any,
) -> Union[bytes, str, _GraphT]:
"""
Expand Down Expand Up @@ -1335,14 +1341,20 @@ def serialize(
if destination is None:
stream = BytesIO()
if encoding is None:
serializer.serialize(stream, base=base, encoding="utf-8", **args)
serializer.serialize(
stream, base=base, encoding="utf-8", sort=sort, **args
)
return stream.getvalue().decode("utf-8")
else:
serializer.serialize(stream, base=base, encoding=encoding, **args)
serializer.serialize(
stream, base=base, encoding=encoding, sort=sort, **args
)
return stream.getvalue()
if hasattr(destination, "write"):
stream = cast(IO[bytes], destination)
serializer.serialize(stream, base=base, encoding=encoding, **args)
serializer.serialize(
stream, base=base, encoding=encoding, sort=sort, **args
)
else:
if isinstance(destination, pathlib.PurePath):
os_path = str(destination)
Expand All @@ -1358,7 +1370,7 @@ def serialize(
else:
os_path = location
with open(os_path, "wb") as stream:
serializer.serialize(stream, encoding=encoding, **args)
serializer.serialize(stream, encoding=encoding, sort=sort, **args)
return self

def print(
Expand Down
19 changes: 14 additions & 5 deletions rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

from collections import defaultdict
from functools import cmp_to_key
from typing import Dict, List

from rdflib.exceptions import Error
from rdflib.namespace import RDF, RDFS
from rdflib.serializer import Serializer
from rdflib.term import BNode, Literal, URIRef
from rdflib.term import BNode, IdentifiedNode, Literal, URIRef

__all__ = ["RecursiveSerializer", "TurtleSerializer"]

Expand Down Expand Up @@ -73,9 +74,9 @@ def isDone(self, subject):
"""Return true if subject is serialized"""
return subject in self._serialized

def orderSubjects(self):
seen = {}
subjects = []
def orderSubjects(self) -> List[IdentifiedNode]:
seen: Dict[IdentifiedNode, bool] = {}
subjects: List[IdentifiedNode] = []

for classURI in self.topClasses:
members = list(self.store.subjects(RDF.type, classURI))
Expand Down Expand Up @@ -223,7 +224,15 @@ def reset(self):
self._started = False
self._ns_rewrite = {}

def serialize(self, stream, base=None, encoding=None, spacious=None, **args):
def serialize(
self,
stream,
base=None,
encoding=None,
spacious=None,
sort: bool = False,
**args,
) -> None:
self.reset()
self.stream = stream
# if base is given here, use that, if not and a base is set for the graph use that
Expand Down
3 changes: 2 additions & 1 deletion rdflib/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


class Serializer:
def __init__(self, store: "Graph"):
def __init__(self, store: "Graph") -> None:
self.store: "Graph" = store
self.encoding: str = "utf-8"
self.base: Optional[str] = None
Expand All @@ -31,6 +31,7 @@ def serialize(
stream: IO[bytes],
base: Optional[str] = None,
encoding: Optional[str] = None,
sort: bool = False,
**args,
) -> None:
"""Abstract method"""
Expand Down
92 changes: 92 additions & 0 deletions test/test_turtle_sort_issue1890.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python3

# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
# of their official duties. Pursuant to title 17 Section 105 of the
# United States Code this software is not subject to copyright
# protection and is in the public domain. NIST assumes no
# responsibility whatsoever for its use by other parties, and makes
# no guarantees, expressed or implied, about its quality,
# reliability, or any other characteristic.
#
# We would appreciate acknowledgement if the software is used.

import random
from collections import defaultdict
from typing import DefaultDict, List

from rdflib import RDFS, BNode, Graph, Literal, Namespace, URIRef


def test_sort_semiblank_graph() -> None:
"""
This test reviews whether the output of the Turtle form is
consistent when involving repeated generates with blank nodes.
"""

EX = Namespace("http://example.org/ex/")

serialization_counter: DefaultDict[str, int] = defaultdict(int)

first_graph_text: str = ""

# Use a fixed sequence of once-but-no-longer random values for more
# consistent test results.
nonrandom_shuffler = random.Random(1234)
for x in range(1, 10):
graph = Graph()
graph.bind("ex", EX)
graph.bind("rdfs", RDFS)

graph.add((EX.A, RDFS.comment, Literal("Thing A")))
graph.add((EX.B, RDFS.comment, Literal("Thing B")))
graph.add((EX.C, RDFS.comment, Literal("Thing C")))

nodes: List[URIRef] = [EX.A, EX.B, EX.C, EX.B]
nonrandom_shuffler.shuffle(nodes)
for node in nodes:
# Instantiate one bnode per URIRef node.
graph.add((BNode(), RDFS.seeAlso, node))

nesteds: List[URIRef] = [EX.A, EX.B, EX.C]
nonrandom_shuffler.shuffle(nesteds)
for nested in nesteds:
# Instantiate a nested node reference.
outer_node = BNode()
inner_node = BNode()
graph.add((outer_node, EX.has, inner_node))
graph.add((inner_node, RDFS.seeAlso, nested))

graph_text = graph.serialize(format="turtle", sort=True)
if first_graph_text == "":
first_graph_text = graph_text

serialization_counter[graph_text] += 1

expected_serialization = """
@prefix ex: <http://example.org/ex/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

ex:A rdfs:comment "Thing A" .

ex:B rdfs:comment "Thing B" .

ex:C rdfs:comment "Thing C" .

[] ex:has [ rdfs:seeAlso ex:A ] .

[] ex:has [ rdfs:seeAlso ex:B ] .

[] ex:has [ rdfs:seeAlso ex:C ] .

[] rdfs:seeAlso ex:A .

[] rdfs:seeAlso ex:B .

[] rdfs:seeAlso ex:B .

[] rdfs:seeAlso ex:C .
"""

assert expected_serialization.strip() == first_graph_text.strip()
assert 1 == len(serialization_counter)