diff options
author | Ashley Sommer <Ashley.Sommer@csiro.au> | 2021-07-07 16:47:21 +1000 |
---|---|---|
committer | Ashley Sommer <Ashley.Sommer@csiro.au> | 2021-07-07 16:47:21 +1000 |
commit | ec0c7c18e2a5d3d8652cf676c273eb7d7b97a48e (patch) | |
tree | 0fe4f6b6f936214e36ffbd78da94d1a98c0e3cda /rdflib/plugins/shared/jsonld/util.py | |
parent | 538446e08f992b7fc333151367dd216611dcd8e3 (diff) | |
download | rdflib-ec0c7c18e2a5d3d8652cf676c273eb7d7b97a48e.tar.gz |
integrate jsonld code into rdflib core
Diffstat (limited to 'rdflib/plugins/shared/jsonld/util.py')
-rw-r--r-- | rdflib/plugins/shared/jsonld/util.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/rdflib/plugins/shared/jsonld/util.py b/rdflib/plugins/shared/jsonld/util.py new file mode 100644 index 00000000..50b34b54 --- /dev/null +++ b/rdflib/plugins/shared/jsonld/util.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +# https://github.com/RDFLib/rdflib-jsonld/blob/feature/json-ld-1.1/rdflib_jsonld/util.py + +try: + import json + + assert json # workaround for pyflakes issue #13 +except ImportError: + import simplejson as json + +from os import sep +from os.path import normpath + +from urllib.parse import urljoin, urlsplit, urlunsplit + +from rdflib.parser import create_input_source + +from io import StringIO + + +def source_to_json(source): + # TODO: conneg for JSON (fix support in rdflib's URLInputSource!) + source = create_input_source(source, format="json-ld") + + stream = source.getByteStream() + try: + return json.load(StringIO(stream.read().decode("utf-8"))) + finally: + stream.close() + + +VOCAB_DELIMS = ("#", "/", ":") + + +def split_iri(iri): + for delim in VOCAB_DELIMS: + at = iri.rfind(delim) + if at > -1: + return iri[: at + 1], iri[at + 1 :] + return iri, None + + +def norm_url(base, url): + """ + >>> norm_url('http://example.org/', '/one') + 'http://example.org/one' + >>> norm_url('http://example.org/', '/one#') + 'http://example.org/one#' + >>> norm_url('http://example.org/one', 'two') + 'http://example.org/two' + >>> norm_url('http://example.org/one/', 'two') + 'http://example.org/one/two' + >>> norm_url('http://example.org/', 'http://example.net/one') + 'http://example.net/one' + >>> norm_url('http://example.org/', 'http://example.org//one') + 'http://example.org//one' + """ + parts = urlsplit(urljoin(base, url)) + path = normpath(parts[2]) + if sep != "/": + path = "/".join(path.split(sep)) + if parts[2].endswith("/") and not path.endswith("/"): + path += "/" + result = urlunsplit(parts[0:2] + (path,) + parts[3:]) + if url.endswith("#") and not result.endswith("#"): + result += "#" + return result + + +def context_from_urlinputsource(source): + if source.content_type == "application/json": + # response_info was added to InputSource in rdflib 4.2 + try: + links = source.response_info.getallmatchingheaders("Link") + except AttributeError: + return + for link in links: + if ' rel="http://www.w3.org/ns/json-ld#context"' in link: + i, j = link.index("<"), link.index(">") + if i > -1 and j > -1: + return urljoin(source.url, link[i + 1 : j]) |