From 5b51feddd4bbae243b99c88049715ac75cfc6b6b Mon Sep 17 00:00:00 2001 From: nicholascar Date: Sun, 2 Jan 2022 17:14:11 +1000 Subject: [PATCH 1/5] all RDF Media Types in Accept header --- rdflib/parser.py | 26 ++++++++++++++++------- test/test_graph_http.py | 46 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/rdflib/parser.py b/rdflib/parser.py index 8437a2e72..93356d563 100644 --- a/rdflib/parser.py +++ b/rdflib/parser.py @@ -181,23 +181,35 @@ def __init__(self, system_id=None, format=None): # copy headers to change myheaders = dict(headers) - if format == "application/rdf+xml": + if format == "xml": myheaders["Accept"] = "application/rdf+xml, */*;q=0.1" elif format == "n3": myheaders["Accept"] = "text/n3, */*;q=0.1" - elif format == "turtle": - myheaders["Accept"] = "text/turtle,application/x-turtle, */*;q=0.1" + elif format in ["turtle" or "ttl"]: + myheaders["Accept"] = "text/turtle, application/x-turtle, */*;q=0.1" elif format == "nt": myheaders["Accept"] = "text/plain, */*;q=0.1" + elif format == "trig": + myheaders["Accept"] = "application/trig, */*;q=0.1" + elif format == "trix": + myheaders["Accept"] = "application/trix, */*;q=0.1" elif format == "json-ld": myheaders[ "Accept" ] = "application/ld+json, application/json;q=0.9, */*;q=0.1" else: - myheaders["Accept"] = ( - "application/rdf+xml,text/rdf+n3;q=0.9," - + "application/xhtml+xml;q=0.5, */*;q=0.1" - ) + # if format not given, create an Accept header from all registered + # parser Media Types + from rdflib.parser import Parser + from rdflib.plugin import plugins + + acc = [] + for p in plugins(): + if p.kind == Parser: # only get parsers + if "/" in p.name: # all Media Types known have a / in them + acc.append(p.name) + + myheaders["Accept"] = ", ".join(acc) req = Request(system_id, None, myheaders) diff --git a/test/test_graph_http.py b/test/test_graph_http.py index 96ef805ec..46a362e0b 100644 --- a/test/test_graph_http.py +++ b/test/test_graph_http.py @@ -30,6 +30,24 @@ nttestdoc = " .\n" +ttltestdoc = """@prefix : . + + :a :b :c . + """ + +jsonldtestdoc = """ + [ + { + "@id": "http://example.org/a", + "http://example.org/b": [ + { + "@id": "http://example.org/c" + } + ] + } + ] + """ + class ContentNegotiationHandler(BaseHTTPRequestHandler): def do_GET(self): @@ -40,7 +58,7 @@ def do_GET(self): acs = self.headers["Accept"].split(",") acq = [x.split(";") for x in acs if ";" in x] acn = [(x, "q=1") for x in acs if ";" not in x] - acs = [(x[0], float(x[1].strip()[2:])) for x in acq + acn] + acs = [(x[0].strip(), float(x[1].strip()[2:])) for x in acq + acn] ac = sorted(acs, key=lambda x: x[1]) ct = ac[-1] @@ -50,9 +68,18 @@ def do_GET(self): elif "text/n3" in ct: rct = "text/n3" content = n3testdoc - elif "text/plain" in ct: + elif 'application/trig' in ct: + rct = "application/trig" + content = ttltestdoc + elif "text/plain" in ct or "application/n-triples" in ct: rct = "text/plain" content = nttestdoc + elif "application/ld+json" in ct: + rct = "application/ld+json" + content = jsonldtestdoc + else: # "text/turtle" in ct: + rct = "text/turtle" + content = ttltestdoc self.send_header("Content-type", rct) self.end_headers() @@ -66,7 +93,7 @@ class TestGraphHTTP(unittest.TestCase): def test_content_negotiation(self) -> None: EG = Namespace("http://example.org/") expected = Graph() - expected.add((EG["a"], EG["b"], EG["c"])) + expected.add((EG.a, EG.b, EG.c)) expected_triples = GraphHelper.triple_set(expected) with ctx_http_server(ContentNegotiationHandler) as server: @@ -77,6 +104,19 @@ def test_content_negotiation(self) -> None: graph.parse(url, format=format) self.assertEqual(expected_triples, GraphHelper.triple_set(graph)) + def test_content_negotiation_no_format(self) -> None: + EG = Namespace("http://example.org/") + expected = Graph() + expected.add((EG.a, EG.b, EG.c)) + expected_triples = GraphHelper.triple_set(expected) + + with ctx_http_server(ContentNegotiationHandler) as server: + (host, port) = server.server_address + url = f"http://{host}:{port}/foo" + graph = Graph() + graph.parse(url) + self.assertEqual(expected_triples, GraphHelper.triple_set(graph)) + def test_source(self) -> None: EG = Namespace("http://example.org/") expected = Graph() From 6076edbe65b259ec9f6014f1099668e072f35c78 Mon Sep 17 00:00:00 2001 From: nicholascar Date: Sun, 2 Jan 2022 17:18:11 +1000 Subject: [PATCH 2/5] blacked --- test/test_graph_http.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_graph_http.py b/test/test_graph_http.py index 46a362e0b..8ffb358f0 100644 --- a/test/test_graph_http.py +++ b/test/test_graph_http.py @@ -68,7 +68,7 @@ def do_GET(self): elif "text/n3" in ct: rct = "text/n3" content = n3testdoc - elif 'application/trig' in ct: + elif "application/trig" in ct: rct = "application/trig" content = ttltestdoc elif "text/plain" in ct or "application/n-triples" in ct: From 397fc296a30b2fac165a8c4e22f3ecd889d71c53 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Sun, 2 Jan 2022 18:34:33 +1000 Subject: [PATCH 3/5] Update parser.py --- rdflib/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/parser.py b/rdflib/parser.py index 93356d563..8116877c5 100644 --- a/rdflib/parser.py +++ b/rdflib/parser.py @@ -185,7 +185,7 @@ def __init__(self, system_id=None, format=None): myheaders["Accept"] = "application/rdf+xml, */*;q=0.1" elif format == "n3": myheaders["Accept"] = "text/n3, */*;q=0.1" - elif format in ["turtle" or "ttl"]: + elif format in ["turtle", "ttl"]: myheaders["Accept"] = "text/turtle, application/x-turtle, */*;q=0.1" elif format == "nt": myheaders["Accept"] = "text/plain, */*;q=0.1" From bee1525285dfc24189060caee353ca5702cb4566 Mon Sep 17 00:00:00 2001 From: nicholascar Date: Sun, 2 Jan 2022 21:39:05 +1000 Subject: [PATCH 4/5] fix mypy error --- test/test_rdfxml.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/test_rdfxml.py b/test/test_rdfxml.py index d77d5ee8c..697fc3368 100644 --- a/test/test_rdfxml.py +++ b/test/test_rdfxml.py @@ -117,11 +117,6 @@ def _testPositive(uri, manifest): write(inDoc) write("' failed with") raise pe - try: - write(type(pe)) - except: - write("sorry could not dump out error.") - result = 1 else: if not store.isomorphic(expected): write("""Failed: '%s'""" % uri) From 79acf2fe793b2226bca01f94663fb7dabb2c9bb6 Mon Sep 17 00:00:00 2001 From: nicholascar Date: Sun, 2 Jan 2022 21:46:17 +1000 Subject: [PATCH 5/5] improved use of plugins() --- rdflib/parser.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rdflib/parser.py b/rdflib/parser.py index 8116877c5..46aea760b 100644 --- a/rdflib/parser.py +++ b/rdflib/parser.py @@ -204,10 +204,9 @@ def __init__(self, system_id=None, format=None): from rdflib.plugin import plugins acc = [] - for p in plugins(): - if p.kind == Parser: # only get parsers - if "/" in p.name: # all Media Types known have a / in them - acc.append(p.name) + for p in plugins(kind=Parser): # only get parsers + if "/" in p.name: # all Media Types known have a / in them + acc.append(p.name) myheaders["Accept"] = ", ".join(acc)