Skip to content

Commit 3afebf2

Browse files
jdsikarmessaou
authored andcommitted
fix(generators): normalize trailing newline in Turtle serialization
rdflib's Turtle serializer always emits a trailing double newline. Normalize to single newline in deterministic_turtle() and the rdflib fallback path in canonicalize_rdf_graph() for consistent file endings. Note: CLI print() still adds a newline after serialize()'s trailing newline. Callers capturing stdout should strip trailing blank lines (e.g. via sed). Signed-off-by: Carlo van Driesten <carlo.van-driesten@bmw.de>
1 parent 0c87aba commit 3afebf2

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

packages/linkml/src/linkml/generators/jsonldcontextgen.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ def end_schema(
237237
json.dump(frame, f, indent=2, ensure_ascii=False)
238238

239239
if self.deterministic:
240-
return self._deterministic_context_json(json.loads(str(as_json(context))), indent=3) + "\n"
241-
return str(as_json(context)) + "\n"
240+
return self._deterministic_context_json(json.loads(str(as_json(context))), indent=3)
241+
return str(as_json(context))
242242

243243
@staticmethod
244244
def _deterministic_context_json(data: dict, indent: int = 3) -> str:

packages/linkml/src/linkml/utils/generator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ def _to_rdflib(term):
307307
if pfx_s and any(iri.startswith(ns_s) for iri in used_iris):
308308
result_graph.bind(pfx_s, ns_s)
309309

310-
return result_graph.serialize(format="turtle")
310+
# rdflib's Turtle serializer always emits a trailing double newline;
311+
# normalize to a single newline for consistent file endings.
312+
return result_graph.serialize(format="turtle").rstrip("\n") + "\n"
311313

312314

313315
def deterministic_json(obj: object, indent: int = 3, preserve_list_order_keys: frozenset[str] | None = None) -> str:

packages/linkml/src/linkml/utils/rdf_canonicalize.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ def canonicalize_rdf_graph(
146146
"pyoxigraph does not support format %r; falling back to rdflib serializer",
147147
output_format,
148148
)
149-
return graph.serialize(format=output_format)
149+
# rdflib's Turtle serializer emits a trailing double newline;
150+
# normalize to single newline for consistent file endings.
151+
data = graph.serialize(format=output_format)
152+
return data.rstrip("\n") + "\n" if data.endswith("\n") else data
150153

151154
# 1. Transfer rdflib graph to pyoxigraph via N-Triples.
152155
nt_data = graph.serialize(format="nt")

0 commit comments

Comments
 (0)