Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/rdflib_jsonld/util.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author | shellac |
---|---|
date | Sat, 02 May 2020 07:14:21 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:26e78fe6e8c4 |
---|---|
1 try: | |
2 import json | |
3 assert json # workaround for pyflakes issue #13 | |
4 except ImportError: | |
5 import simplejson as json | |
6 | |
7 from rdflib.py3compat import PY3, format_doctest_out | |
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 @format_doctest_out | |
45 def norm_url(base, url): | |
46 """ | |
47 >>> norm_url('http://example.org/', '/one') | |
48 'http://example.org/one' | |
49 >>> norm_url('http://example.org/', '/one#') | |
50 'http://example.org/one#' | |
51 >>> norm_url('http://example.org/one', 'two') | |
52 'http://example.org/two' | |
53 >>> norm_url('http://example.org/one/', 'two') | |
54 'http://example.org/one/two' | |
55 >>> norm_url('http://example.org/', 'http://example.net/one') | |
56 'http://example.net/one' | |
57 """ | |
58 parts = urlsplit(urljoin(base, url)) | |
59 path = normpath(parts[2]) | |
60 if sep != '/': | |
61 path = '/'.join(path.split(sep)) | |
62 if parts[2].endswith('/') and not path.endswith('/'): | |
63 path += '/' | |
64 result = urlunsplit(parts[0:2] + (path,) + parts[3:]) | |
65 if url.endswith('#') and not result.endswith('#'): | |
66 result += '#' | |
67 return result | |
68 | |
69 def context_from_urlinputsource(source): | |
70 if source.content_type == 'application/json': | |
71 # response_info was added to InputSource in rdflib 4.2 | |
72 try: | |
73 links = source.response_info.getallmatchingheaders('Link') | |
74 except AttributeError: | |
75 return | |
76 for link in links: | |
77 if ' rel="http://www.w3.org/ns/json-ld#context"' in link: | |
78 i, j = link.index('<'), link.index('>') | |
79 if i > -1 and j > -1: | |
80 return urljoin(source.url, link[i+1:j]) |