Skip to content

Commit 2d709f8

Browse files
committed
fix: raise an exception when serialization format does not support quads
- fixes <#2393>
1 parent 8c9608b commit 2d709f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+926
-109
lines changed

rdflib/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@
118118

119119
del sys
120120

121+
#: Checking if this works ...
122+
CHECKME_A = 1
123+
121124

122125
NORMALIZE_LITERALS = True
123126
"""

rdflib/util.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import logging
4+
35
"""
46
Some utility functions.
57
@@ -614,3 +616,31 @@ def _iri2uri(iri: str) -> str:
614616
uri += "#"
615617

616618
return uri
619+
620+
621+
def _has_non_default_graphs(graph: rdflib.graph.ConjunctiveGraph) -> bool:
622+
"""
623+
Check if the container passed as `graph` contains graphs other than the
624+
default graph.
625+
626+
The intent of this is to detect if the value passed can be serialized using
627+
formats which do not support named graphs like N-Triples and Turtle.
628+
629+
Ideally this function would check if the supplied value contains any named
630+
graphs, but RDFLib assigns a name to the default graph, so the best that can
631+
be done is to check if the supplied graph contains any graphs other than the
632+
default graph.
633+
634+
If the supplied value contains only the default graph and other graphs, this
635+
function will return `False`, otherwise if the value passed contains at
636+
least one graph other than the default graph it will return `True`.
637+
"""
638+
default_context = graph.default_context
639+
# logging.debug("default_context.identifier = %s", default_context.identifier)
640+
for context_index, context in enumerate(graph.contexts()):
641+
# logging.debug("contexts[%s].identifier = %s", context_index, context.identifier)
642+
if context.identifier != default_context.identifier:
643+
return True
644+
if context_index > 0:
645+
return True
646+
return False

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ exclude =
1414
test/data/suites/, # does not contain python
1515
test/jsonld/1.1/, # does not contain python
1616
test/jsonld/test-suite/, # does not contain python
17-
test/data/variants/, # does not contain python
1817
test/data/translate_algebra/, # does not contain python
1918
docs/rdf_terms.rst, # This file is causing an error on GitHub actions
2019
extend-ignore =

test/data.py renamed to test/data/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from rdflib import URIRef
44
from rdflib.graph import Graph
55

6-
TEST_DIR = Path(__file__).parent
6+
TEST_DIR = Path(__file__).parent.parent
77
TEST_DATA_DIR = TEST_DIR / "data"
88

99
alice_uri = URIRef("http://example.org/alice")
@@ -22,7 +22,7 @@
2222
context2 = URIRef("urn:example:context-2")
2323

2424

25-
simple_triple_graph = Graph().add(
25+
SIMPLE_TRIPLE_GRAPH = Graph().add(
2626
(
2727
URIRef("http://example.org/subject"),
2828
URIRef("http://example.org/predicate"),

test/data/variants/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# multi variant graphs
22

3-
This directory containts variants of the same graph encoded in different
3+
This directory contains variants of the same graph encoded in different
44
formats, or differently in the same format.
55

6-
The graph that a specific file is a variant of is determined by it's filename.
7-
Files that differ only in file extention but have the same basename are
6+
The graph that a specific file is a variant of is determined by its filename.
7+
Files that differ only in file extensions but have the same basename are
88
considered variants of the same graph. Additionally, any suffix that matches
9-
`-variant-[^/]*` is excluded when determening the graph key, so the following
9+
`-variant-[^/]*` is excluded when determining the graph key, so the following
1010
files are all considered variants of the same graph:
1111

1212
```
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"quad_count": 6,
3-
"exact_match": true
2+
"quad_count": 6
43
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exact_match": true
3+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"quad_count": 10,
3-
"exact_match": true
2+
"quad_count": 10
43
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exact_match": true
3+
}

test/data/variants/diverse_quads.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from test.utils.namespace import EGDC, EGSCHEME, EGURN
2+
3+
from rdflib.graph import ConjunctiveGraph, Graph
4+
from rdflib.namespace import XSD
5+
from rdflib.term import Literal
6+
7+
8+
def populate_graph(graph: Graph) -> None:
9+
assert isinstance(graph, ConjunctiveGraph)
10+
11+
graph.add((EGSCHEME.subject, EGSCHEME.predicate, EGSCHEME.object))
12+
graph.add((EGDC.subject, EGDC.predicate, Literal("typeless")))
13+
graph.add((EGURN.subject, EGURN.predicate, EGURN.object))
14+
15+
egscheme_graph = graph.get_context(EGSCHEME.graph)
16+
egscheme_graph.add((EGDC.subject, EGDC.predicate, Literal("日本語の表記体系", lang="jpx")))
17+
egscheme_graph.add((EGURN.subject, EGSCHEME.predicate, EGSCHEME.subject))
18+
egscheme_graph.add((EGSCHEME.subject, EGSCHEME.predicate, EGSCHEME.object))
19+
egscheme_graph.add((EGSCHEME.subject, EGSCHEME.predicate, Literal(12)))
20+
21+
egurn_graph = graph.get_context(EGURN.graph)
22+
egurn_graph.add((EGSCHEME.subject, EGSCHEME.predicate, EGSCHEME.object))
23+
egurn_graph.add((EGSCHEME.subject, EGDC.predicate, EGDC.object))
24+
egurn_graph.add(
25+
(EGSCHEME.subject, EGDC.predicate, Literal("XSD string", datatype=XSD.string))
26+
)
27+
28+
29+
__all__ = ["populate_graph"]

0 commit comments

Comments
 (0)