From 8e2e25d7966285d03222c0ed3dee2241907f12fd Mon Sep 17 00:00:00 2001 From: nicholascar Date: Mon, 7 Feb 2022 13:53:43 +1000 Subject: [PATCH 1/7] add jsonld_context() function to DefinedNamespace --- rdflib/namespace/__init__.py | 14 +++- rdflib/term.py | 1 + ...ce_creator.py => test_definednamespace.py} | 80 +++++++++++++++++++ test/test_definednamespace_dir.py | 35 -------- 4 files changed, 93 insertions(+), 37 deletions(-) rename test/{test_definednamespace_creator.py => test_definednamespace.py} (57%) delete mode 100644 test/test_definednamespace_dir.py diff --git a/rdflib/namespace/__init__.py b/rdflib/namespace/__init__.py index b6c405c48..ab0170f2f 100644 --- a/rdflib/namespace/__init__.py +++ b/rdflib/namespace/__init__.py @@ -1,9 +1,9 @@ +import json import logging import warnings +from pathlib import Path from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, Iterable from unicodedata import category - -from pathlib import Path from urllib.parse import urldefrag from urllib.parse import urljoin @@ -239,6 +239,16 @@ def __dir__(cls) -> Iterable[str]: values = {cls[str(x)] for x in cls.__annotations__} return values + def jsonld_context(self, pfx: str) -> str: + """This function creates a JSON-LD 'context' JSON object from this + DefinedNamespace namespace and it's members""" + terms = {pfx: str(self._NS)} + for key, term in self.__annotations__.items(): + if term == URIRef: + terms[key] = f'{pfx}:{key}' + + return json.dumps({'@context': terms}) + class DefinedNamespace(metaclass=DefinedNamespaceMeta): """ diff --git a/rdflib/term.py b/rdflib/term.py index aa1f36a8b..d9c59b61d 100644 --- a/rdflib/term.py +++ b/rdflib/term.py @@ -26,6 +26,7 @@ __all__ = [ "bind", + "_is_valid_uri", "Node", "IdentifiedNode", "Identifier", diff --git a/test/test_definednamespace_creator.py b/test/test_definednamespace.py similarity index 57% rename from test/test_definednamespace_creator.py rename to test/test_definednamespace.py index 7d8ebd55f..8759b51b3 100644 --- a/test/test_definednamespace_creator.py +++ b/test/test_definednamespace.py @@ -1,6 +1,8 @@ import sys import subprocess from pathlib import Path +from rdflib import RDF, SKOS +import json def test_definednamespace_creator_qb(): @@ -92,3 +94,81 @@ def test_definednamespace_creator_bad_ns(): universal_newlines=True, ) assert completed.returncode == 1, "subprocess exited incorrectly (failure expected)" + + +def test_definednamespace_dir(): + x = dir(RDF) + + values = [ + RDF.nil, + RDF.direction, + RDF.first, + RDF.language, + RDF.object, + RDF.predicate, + RDF.rest, + RDF.subject, + RDF.type, + RDF.value, + RDF.Alt, + RDF.Bag, + RDF.CompoundLiteral, + RDF.List, + RDF.Property, + RDF.Seq, + RDF.Statement, + RDF.HTML, + RDF.JSON, + RDF.PlainLiteral, + RDF.XMLLiteral, + RDF.langString, + ] + + assert len(values) == len(x) + + for value in values: + assert value in x + + +def test_definednamespace_jsonld_context(): + expected = json.dumps({ + "@context": { + "skos": "http://www.w3.org/2004/02/skos/core#", + "altLabel": "skos:altLabel", + "broadMatch": "skos:broadMatch", + "broader": "skos:broader", + "broaderTransitive": "skos:broaderTransitive", + "changeNote": "skos:changeNote", + "closeMatch": "skos:closeMatch", + "definition": "skos:definition", + "editorialNote": "skos:editorialNote", + "exactMatch": "skos:exactMatch", + "example": "skos:example", + "hasTopConcept": "skos:hasTopConcept", + "hiddenLabel": "skos:hiddenLabel", + "historyNote": "skos:historyNote", + "inScheme": "skos:inScheme", + "mappingRelation": "skos:mappingRelation", + "member": "skos:member", + "memberList": "skos:memberList", + "narrowMatch": "skos:narrowMatch", + "narrower": "skos:narrower", + "narrowerTransitive": "skos:narrowerTransitive", + "notation": "skos:notation", + "note": "skos:note", + "prefLabel": "skos:prefLabel", + "related": "skos:related", + "relatedMatch": "skos:relatedMatch", + "scopeNote": "skos:scopeNote", + "semanticRelation": "skos:semanticRelation", + "topConceptOf": "skos:topConceptOf", + "Collection": "skos:Collection", + "Concept": "skos:Concept", + "ConceptScheme": "skos:ConceptScheme", + "OrderedCollection": "skos:OrderedCollection" + } + }) + + actual = SKOS.jsonld_context("skos") + + assert actual == expected diff --git a/test/test_definednamespace_dir.py b/test/test_definednamespace_dir.py deleted file mode 100644 index 0c4a6f181..000000000 --- a/test/test_definednamespace_dir.py +++ /dev/null @@ -1,35 +0,0 @@ -from rdflib import RDF - - -def test_definednamespace_dir(): - x = dir(RDF) - - values = [ - RDF.nil, - RDF.direction, - RDF.first, - RDF.language, - RDF.object, - RDF.predicate, - RDF.rest, - RDF.subject, - RDF.type, - RDF.value, - RDF.Alt, - RDF.Bag, - RDF.CompoundLiteral, - RDF.List, - RDF.Property, - RDF.Seq, - RDF.Statement, - RDF.HTML, - RDF.JSON, - RDF.PlainLiteral, - RDF.XMLLiteral, - RDF.langString, - ] - - assert len(values) == len(x) - - for value in values: - assert value in x From ba742cc63f358dc426e5c8de8c4dfb0266f4f36a Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Fri, 11 Feb 2022 10:50:22 +1000 Subject: [PATCH 2/7] Update rdflib/namespace/__init__.py Co-authored-by: Iwan Aucamp --- rdflib/namespace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/namespace/__init__.py b/rdflib/namespace/__init__.py index ab0170f2f..020acb34a 100644 --- a/rdflib/namespace/__init__.py +++ b/rdflib/namespace/__init__.py @@ -247,7 +247,7 @@ def jsonld_context(self, pfx: str) -> str: if term == URIRef: terms[key] = f'{pfx}:{key}' - return json.dumps({'@context': terms}) + return json.dumps({'@context': terms}, sort_keys=True) class DefinedNamespace(metaclass=DefinedNamespaceMeta): From 72cde014aaf7714314174a4237ed4d32b92b81f4 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Fri, 11 Feb 2022 10:50:28 +1000 Subject: [PATCH 3/7] Update test/test_definednamespace.py Co-authored-by: Iwan Aucamp --- test/test_definednamespace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_definednamespace.py b/test/test_definednamespace.py index 8759b51b3..32d0a3d08 100644 --- a/test/test_definednamespace.py +++ b/test/test_definednamespace.py @@ -167,7 +167,7 @@ def test_definednamespace_jsonld_context(): "ConceptScheme": "skos:ConceptScheme", "OrderedCollection": "skos:OrderedCollection" } - }) + }, sort_keys=True) actual = SKOS.jsonld_context("skos") From e453e63bd6670a38cdca0fda1f1fd5e164657b51 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Fri, 11 Feb 2022 10:52:37 +1000 Subject: [PATCH 4/7] Update __init__.py --- rdflib/namespace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/namespace/__init__.py b/rdflib/namespace/__init__.py index 020acb34a..afe17a502 100644 --- a/rdflib/namespace/__init__.py +++ b/rdflib/namespace/__init__.py @@ -244,7 +244,7 @@ def jsonld_context(self, pfx: str) -> str: DefinedNamespace namespace and it's members""" terms = {pfx: str(self._NS)} for key, term in self.__annotations__.items(): - if term == URIRef: + if isinstance(term, URIRef): terms[key] = f'{pfx}:{key}' return json.dumps({'@context': terms}, sort_keys=True) From cfc42ee48e6af319c63c3250da4988c5a07b79c4 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Sun, 13 Feb 2022 21:42:51 +1000 Subject: [PATCH 5/7] Update rdflib/namespace/__init__.py Co-authored-by: Iwan Aucamp --- rdflib/namespace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rdflib/namespace/__init__.py b/rdflib/namespace/__init__.py index afe17a502..31d40fd6d 100644 --- a/rdflib/namespace/__init__.py +++ b/rdflib/namespace/__init__.py @@ -244,7 +244,7 @@ def jsonld_context(self, pfx: str) -> str: DefinedNamespace namespace and it's members""" terms = {pfx: str(self._NS)} for key, term in self.__annotations__.items(): - if isinstance(term, URIRef): + if issubclass(term, URIRef): terms[key] = f'{pfx}:{key}' return json.dumps({'@context': terms}, sort_keys=True) From 0a323bec80340856667a70aff06df360c9697976 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Thu, 31 Mar 2022 00:34:58 +1000 Subject: [PATCH 6/7] change func name to avoid colisions --- rdflib/namespace/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rdflib/namespace/__init__.py b/rdflib/namespace/__init__.py index 8ba438fab..7ef046253 100644 --- a/rdflib/namespace/__init__.py +++ b/rdflib/namespace/__init__.py @@ -239,15 +239,14 @@ def __dir__(cls) -> Iterable[str]: values = {cls[str(x)] for x in cls.__annotations__} return values - def jsonld_context(self, pfx: str) -> str: - """This function creates a JSON-LD 'context' JSON object from this - DefinedNamespace namespace and it's members""" + def as_jsonld_context(self, pfx: str) -> dict: + """Returns this DefinedNamespace as a a JSON-LD 'context' object""" terms = {pfx: str(self._NS)} for key, term in self.__annotations__.items(): if issubclass(term, URIRef): terms[key] = f'{pfx}:{key}' - return json.dumps({'@context': terms}, sort_keys=True) + return {'@context': terms} class DefinedNamespace(metaclass=DefinedNamespaceMeta): From 20d1c746d215707f7ae3ec1a80917970a99aad9d Mon Sep 17 00:00:00 2001 From: Iwan Aucamp Date: Mon, 4 Apr 2022 21:23:18 +0200 Subject: [PATCH 7/7] Fixed unit test for `as_jsonld_context` Method name changed and it now returns a dict instead of a JSON string. --- test/test_definednamespace.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/test_definednamespace.py b/test/test_definednamespace.py index 32d0a3d08..3bf335751 100644 --- a/test/test_definednamespace.py +++ b/test/test_definednamespace.py @@ -131,7 +131,7 @@ def test_definednamespace_dir(): def test_definednamespace_jsonld_context(): - expected = json.dumps({ + expected = { "@context": { "skos": "http://www.w3.org/2004/02/skos/core#", "altLabel": "skos:altLabel", @@ -167,8 +167,7 @@ def test_definednamespace_jsonld_context(): "ConceptScheme": "skos:ConceptScheme", "OrderedCollection": "skos:OrderedCollection" } - }, sort_keys=True) - - actual = SKOS.jsonld_context("skos") + } + actual = SKOS.as_jsonld_context("skos") assert actual == expected