Skip to content

Commit

Permalink
Merge pull request #1706 from RDFLib/definednamespace_jsonld
Browse files Browse the repository at this point in the history
Generate JSON-LD context from a DefinedNamespace
  • Loading branch information
nicholascar authored Apr 4, 2022
2 parents 3325ac2 + 20d1c74 commit adca592
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 37 deletions.
13 changes: 11 additions & 2 deletions rdflib/namespace/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json
import logging
import warnings
from pathlib import Path
from functools import lru_cache
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

Expand Down Expand Up @@ -239,6 +239,15 @@ def __dir__(cls) -> Iterable[str]:
values = {cls[str(x)] for x in cls.__annotations__}
return values

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 {'@context': terms}


class DefinedNamespace(metaclass=DefinedNamespaceMeta):
"""
Expand Down
1 change: 1 addition & 0 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

__all__ = [
"bind",
"_is_valid_uri",
"Node",
"IdentifiedNode",
"Identifier",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys
import subprocess
from pathlib import Path
from rdflib import RDF, SKOS
import json


def test_definednamespace_creator_qb():
Expand Down Expand Up @@ -92,3 +94,80 @@ 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 = {
"@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.as_jsonld_context("skos")

assert actual == expected
35 changes: 0 additions & 35 deletions test/test_definednamespace_dir.py

This file was deleted.

0 comments on commit adca592

Please sign in to comment.