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

Correct behaviour of compute_qname for URNs #1274

Merged
merged 2 commits into from
Mar 10, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions rdflib/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ def absolutize(self, uri, defrag=1):
NAME_START_CATEGORIES = ["Ll", "Lu", "Lo", "Lt", "Nl"]
SPLIT_START_CATEGORIES = NAME_START_CATEGORIES + ["Nd"]
NAME_CATEGORIES = NAME_START_CATEGORIES + ["Mc", "Me", "Mn", "Lm", "Nd"]
ALLOWED_NAME_CHARS = ["\u00B7", "\u0387", "-", ".", "_", ":", "%"]
ALLOWED_NAME_CHARS = ["\u00B7", "\u0387", "-", ".", "_", "%"]


# http://www.w3.org/TR/REC-xml-names/#NT-NCName
Expand All @@ -914,7 +914,7 @@ def is_ncname(name):
for i in range(1, len(name)):
c = name[i]
if not category(c) in NAME_CATEGORIES:
if c != ":" and c in ALLOWED_NAME_CHARS:
if c in ALLOWED_NAME_CHARS:
continue
return 0
# if in compatibility area
Expand Down
11 changes: 10 additions & 1 deletion test/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def test_compute_qname(self):
g.compute_qname(URIRef("http://foo/bar/")),
("ns1", URIRef("http://foo/bar/"), ""),
)
# should compute qnames of URNs correctly as well
self.assertEqual(
g.compute_qname(URIRef("urn:ISSN:0167-6423")),
("ns5", URIRef("urn:ISSN:"), "0167-6423"),
)
self.assertEqual(
g.compute_qname(URIRef("urn:ISSN:")),
("ns5", URIRef("urn:ISSN:"), ""),
)

def test_reset(self):
data = (
Expand Down Expand Up @@ -123,4 +132,4 @@ def test_contains_method(self):
self.assertFalse(ref in RDFS, "ClosedNamespace(RDFS) includes out-of-ns member rdfs:example")

ref = URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
self.assertTrue(ref in RDF, "_RDFNamespace does not include rdf:type")
self.assertTrue(ref in RDF, "_RDFNamespace does not include rdf:type")
12 changes: 11 additions & 1 deletion test/test_turtle_serialize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rdflib import Graph, URIRef, BNode, RDF, Literal, Namespace
from rdflib import Graph, URIRef, BNode, RDF, RDFS, Literal, Namespace
from rdflib.collection import Collection
from rdflib.plugins.serializers.turtle import TurtleSerializer

Expand Down Expand Up @@ -80,13 +80,22 @@ def test_turtle_namespace():
graph.bind("GENO", "http://purl.obolibrary.org/obo/GENO_")
graph.bind("RO", "http://purl.obolibrary.org/obo/RO_")
graph.bind("RO_has_phenotype", "http://purl.obolibrary.org/obo/RO_0002200")
graph.bind("SERIAL", "urn:ISSN:")
graph.add(
(
URIRef("http://example.org"),
URIRef("http://purl.obolibrary.org/obo/RO_0002200"),
URIRef("http://purl.obolibrary.org/obo/GENO_0000385"),
)
)
graph.add(
(
URIRef("urn:ISSN:0167-6423"),
RDFS.label,
Literal("Science of Computer Programming"),

)
)
output = [
val
for val in graph.serialize(format="turtle").splitlines()
Expand All @@ -95,6 +104,7 @@ def test_turtle_namespace():
output = " ".join(output)
assert "RO_has_phenotype:" in output
assert "GENO:0000385" in output
assert "SERIAL:0167-6423" in output


if __name__ == "__main__":
Expand Down