Skip to content

Commit 24b1ce6

Browse files
committed
Adjust lib to resolve function
1 parent c620522 commit 24b1ce6

File tree

5 files changed

+24
-93
lines changed

5 files changed

+24
-93
lines changed

lib/pyld/context_resolver.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from frozendict import frozendict
1212
from c14n.Canonicalize import canonicalize
13-
from pyld import jsonld
13+
from pyld import jsonld, iri_resolver
1414
from .resolved_context import ResolvedContext
1515

1616
MAX_CONTEXT_URLS = 10
@@ -104,7 +104,7 @@ def _cache_resolved_context(self, key, resolved, tag):
104104

105105
def _resolve_remote_context(self, active_ctx, url, base, cycles):
106106
# resolve relative URL and fetch context
107-
url = jsonld.prepend_base(base, url)
107+
url = iri_resolver.resolve(url, base)
108108
context, remote_doc = self._fetch_context(active_ctx, url, cycles)
109109

110110
# update base according to remote document and resolve any relative URLs
@@ -194,13 +194,13 @@ def _resolve_context_urls(self, context, base):
194194
ctx = context.get('@context')
195195

196196
if isinstance(ctx, str):
197-
context['@context'] = jsonld.prepend_base(base, ctx)
197+
context['@context'] = iri_resolver.resolve(ctx, base)
198198
return
199199

200200
if isinstance(ctx, list):
201201
for num, element in enumerate(ctx):
202202
if isinstance(element, str):
203-
ctx[num] = jsonld.prepend_base(base, element)
203+
ctx[num] = iri_resolver.resolve(element, base)
204204
elif isinstance(element, dict) or isinstance(element, frozendict):
205205
self. _resolve_context_urls({'@context': element}, base)
206206
return

lib/pyld/documentloader/aiohttp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import threading
1414
import urllib.parse as urllib_parse
1515

16+
from pyld import iri_resolver
1617
from pyld.jsonld import (JsonLdError, parse_link_header, LINK_HEADER_REL)
1718

1819

@@ -114,7 +115,7 @@ async def async_loader(url, headers):
114115
linked_alternate.get('type') == 'application/ld+json' and
115116
not re.match(r'^application\/(\w*\+)?json$', content_type)):
116117
doc['contentType'] = 'application/ld+json'
117-
doc['documentUrl'] = jsonld.prepend_base(url, linked_alternate['target'])
118+
doc['documentUrl'] = iri_resolver.resolve(linked_alternate['target'], url)
118119

119120
return doc
120121
except JsonLdError as e:

lib/pyld/documentloader/requests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
.. moduleauthor:: Tim McNamara <tim.mcnamara@okfn.org>
1010
.. moduleauthor:: Olaf Conradi <olaf@conradi.org>
1111
"""
12+
import re
1213
import string
1314
import urllib.parse as urllib_parse
1415

16+
from pyld import iri_resolver
1517
from pyld.jsonld import (JsonLdError, parse_link_header, LINK_HEADER_REL)
1618

1719

@@ -92,7 +94,7 @@ def loader(url, options={}):
9294
linked_alternate.get('type') == 'application/ld+json' and
9395
not re.match(r'^application\/(\w*\+)?json$', content_type)):
9496
doc['contentType'] = 'application/ld+json'
95-
doc['documentUrl'] = jsonld.prepend_base(url, linked_alternate['target'])
97+
doc['documentUrl'] = iri_resolver.resolve(linked_alternate['target'], url)
9698
return doc
9799
except JsonLdError as e:
98100
raise e

lib/pyld/jsonld.py

Lines changed: 10 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from numbers import Integral, Real
3232
from frozendict import frozendict
3333
from pyld.__about__ import (__copyright__, __license__, __version__)
34+
from .iri_resolver import resolve
3435

3536
def cmp(a, b):
3637
return (a > b) - (a < b)
@@ -443,80 +444,6 @@ def unregister_rdf_parser(content_type):
443444
del _rdf_parsers[content_type]
444445

445446

446-
def prepend_base(base, iri):
447-
"""
448-
Prepends a base IRI to the given relative IRI.
449-
450-
:param base: the base IRI.
451-
:param iri: the relative IRI.
452-
453-
:return: the absolute IRI.
454-
"""
455-
# skip IRI processing
456-
if base is None:
457-
return iri
458-
459-
# already an absolute iri
460-
if _is_absolute_iri(iri):
461-
return iri
462-
463-
# parse IRIs
464-
base = parse_url(base)
465-
rel = parse_url(iri)
466-
467-
# per RFC3986 5.2.2
468-
transform = {
469-
'scheme': base.scheme
470-
}
471-
472-
if rel.authority is not None:
473-
transform['authority'] = rel.authority
474-
transform['path'] = rel.path
475-
transform['query'] = rel.query
476-
else:
477-
transform['authority'] = base.authority
478-
479-
if rel.path == '':
480-
transform['path'] = base.path
481-
if rel.query is not None:
482-
transform['query'] = rel.query
483-
else:
484-
transform['query'] = base.query
485-
else:
486-
if rel.path.startswith('/'):
487-
# IRI represents an absolute path
488-
transform['path'] = rel.path
489-
else:
490-
# merge paths
491-
path = base.path
492-
493-
# append relative path to the end of the last directory from
494-
# base
495-
path = path[0:path.rfind('/') + 1]
496-
if (len(path) > 0 or base.authority) and not path.endswith('/'):
497-
path += '/'
498-
path += rel.path
499-
500-
transform['path'] = path
501-
502-
transform['query'] = rel.query
503-
504-
if rel.path != '':
505-
# normalize path
506-
transform['path'] = remove_dot_segments(transform['path'])
507-
508-
transform['fragment'] = rel.fragment
509-
510-
# construct URL
511-
rval = unparse_url(transform)
512-
513-
# handle empty base case
514-
if rval == '':
515-
rval = './'
516-
517-
return rval
518-
519-
520447
def remove_base(base, iri):
521448
"""
522449
Removes a base IRI from the given absolute IRI.
@@ -3188,10 +3115,10 @@ def _process_context(self, active_ctx, local_ctx, options,
31883115
base = ctx['@base']
31893116
if base is None:
31903117
base = None
3191-
elif _is_absolute_iri(base):
3118+
elif _is_absolute_iri(base) or (_is_relative_iri(base) and active_ctx.get('@base') is None):
31923119
base = base
3193-
elif _is_relative_iri(base):
3194-
base = prepend_base(active_ctx.get('@base'), base)
3120+
elif _is_relative_iri(base) and active_ctx.get('@base') is not None:
3121+
base = resolve(base, active_ctx.get('@base'))
31953122
else:
31963123
raise JsonLdError(
31973124
'Invalid JSON-LD syntax; the value of "@base" in a '
@@ -3295,7 +3222,7 @@ def _process_context(self, active_ctx, local_ctx, options,
32953222
process = True
32963223

32973224
if _is_string(key_ctx):
3298-
url = prepend_base(options['base'], key_ctx)
3225+
url = resolve(key_ctx, options['base'])
32993226
if url in cycles:
33003227
process = False
33013228
else:
@@ -4887,7 +4814,7 @@ def _compact_iri(
48874814
if active_ctx['@base'] is None:
48884815
return iri
48894816
else:
4890-
return remove_base(prepend_base(base, active_ctx['@base']), iri)
4817+
return remove_base(resolve(active_ctx['@base'], base), iri)
48914818
else:
48924819
return remove_base(base, iri)
48934820

@@ -5510,9 +5437,10 @@ def _expand_iri(
55105437
if base and '@base' in active_ctx:
55115438
# The None case preserves rval as potentially relative
55125439
if active_ctx['@base'] is not None:
5513-
rval = prepend_base(prepend_base(base, active_ctx['@base']), rval)
5440+
resolved_base = active_ctx['@base'] if _is_absolute_iri(active_ctx['@base']) else resolve(active_ctx['@base'], base)
5441+
rval = resolve(rval, resolved_base)
55145442
elif base:
5515-
rval = prepend_base(base, rval)
5443+
rval = resolve(rval, base)
55165444

55175445
return rval
55185446

@@ -6642,7 +6570,7 @@ def load_html(input, url, profile, options):
66426570
# use either specified base, or document location
66436571
effective_base = options.get('base', url)
66446572
if effective_base:
6645-
html_base = prepend_base(effective_base, html_base[0])
6573+
html_base = resolve(html_base[0], effective_base)
66466574
options['base'] = html_base
66476575

66486576
url_elements = parse_url(url)

tests/runtests.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from unittest import TextTestResult
2121

2222
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'lib'))
23-
from pyld import jsonld
23+
from pyld import jsonld, iri_resolver
2424

2525
__copyright__ = 'Copyright (c) 2011-2013 Digital Bazaar, Inc.'
2626
__license__ = 'New BSD license'
@@ -97,9 +97,9 @@ def main(self):
9797
# default to find known sibling test dirs
9898
test_targets = []
9999
sibling_dirs = [
100-
'../json-ld-api/tests/',
101-
'../json-ld-framing/tests/',
102-
'../normalization/tests/',
100+
'./specifications/json-ld-api/tests/',
101+
'./specifications/json-ld-framing/tests/',
102+
'./specifications/normalization/tests/',
103103
]
104104
for dir in sibling_dirs:
105105
if os.path.exists(dir):
@@ -528,7 +528,7 @@ def load_locally(url):
528528
linked_alternate.get('type') == 'application/ld+json' and
529529
not re.match(r'^application\/(\w*\+)?json$', content_type)):
530530
doc['contentType'] = 'application/ld+json'
531-
doc['documentUrl'] = jsonld.prepend_base(url, linked_alternate['target'])
531+
doc['documentUrl'] = iri_resolver.resolve(linked_alternate['target'], url)
532532
global ROOT_MANIFEST_DIR
533533
if doc['documentUrl'].find(':') == -1:
534534
filename = os.path.join(ROOT_MANIFEST_DIR, doc['documentUrl'])

0 commit comments

Comments
 (0)