Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/rdflib_jsonld/util.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 try: | |
2 import json | |
3 assert json # workaround for pyflakes issue #13 | |
4 except ImportError: | |
5 import simplejson as json | |
6 | |
7 from ._compat import IS_PY3 as PY3 | |
8 | |
9 from os import sep | |
10 from os.path import normpath | |
11 if PY3: | |
12 from urllib.parse import urljoin, urlsplit, urlunsplit | |
13 else: | |
14 from urllib.parse import urljoin, urlsplit, urlunsplit | |
15 | |
16 from rdflib.parser import create_input_source | |
17 if PY3: | |
18 from io import StringIO | |
19 | |
20 | |
21 def source_to_json(source): | |
22 # TODO: conneg for JSON (fix support in rdflib's URLInputSource!) | |
23 source = create_input_source(source, format='json-ld') | |
24 | |
25 stream = source.getByteStream() | |
26 try: | |
27 if PY3: | |
28 return json.load(StringIO(stream.read().decode('utf-8'))) | |
29 else: | |
30 return json.load(stream) | |
31 finally: | |
32 stream.close() | |
33 | |
34 | |
35 VOCAB_DELIMS = ('#', '/', ':') | |
36 | |
37 def split_iri(iri): | |
38 for delim in VOCAB_DELIMS: | |
39 at = iri.rfind(delim) | |
40 if at > -1: | |
41 return iri[:at+1], iri[at+1:] | |
42 return iri, None | |
43 | |
44 def norm_url(base, url): | |
45 """ | |
46 >>> norm_url('http://example.org/', '/one') | |
47 'http://example.org/one' | |
48 >>> norm_url('http://example.org/', '/one#') | |
49 'http://example.org/one#' | |
50 >>> norm_url('http://example.org/one', 'two') | |
51 'http://example.org/two' | |
52 >>> norm_url('http://example.org/one/', 'two') | |
53 'http://example.org/one/two' | |
54 >>> norm_url('http://example.org/', 'http://example.net/one') | |
55 'http://example.net/one' | |
56 >>> norm_url('http://example.org/', 'http://example.org//one') | |
57 'http://example.org//one' | |
58 """ | |
59 parts = urlsplit(urljoin(base, url)) | |
60 path = normpath(parts[2]) | |
61 if sep != '/': | |
62 path = '/'.join(path.split(sep)) | |
63 if parts[2].endswith('/') and not path.endswith('/'): | |
64 path += '/' | |
65 result = urlunsplit(parts[0:2] + (path,) + parts[3:]) | |
66 if url.endswith('#') and not result.endswith('#'): | |
67 result += '#' | |
68 return result | |
69 | |
70 def context_from_urlinputsource(source): | |
71 if source.content_type == 'application/json': | |
72 # response_info was added to InputSource in rdflib 4.2 | |
73 try: | |
74 links = source.response_info.getallmatchingheaders('Link') | |
75 except AttributeError: | |
76 return | |
77 for link in links: | |
78 if ' rel="http://www.w3.org/ns/json-ld#context"' in link: | |
79 i, j = link.index('<'), link.index('>') | |
80 if i > -1 and j > -1: | |
81 return urljoin(source.url, link[i+1:j]) |