Tidy up pyLD

main
Bob Mottram 2020-10-11 17:39:30 +01:00
parent ae0b6d32c5
commit 65f4e3ad41
1 changed files with 87 additions and 75 deletions

View File

@ -468,7 +468,7 @@ def prepend_base(base, iri):
# per RFC3986 5.2.2
transform = {
'scheme': base.scheme
};
}
if rel.authority is not None:
transform['authority'] = rel.authority
@ -479,7 +479,7 @@ def prepend_base(base, iri):
if rel.path == '':
transform['path'] = base.path
if rel.query != None:
if rel.query is not None:
transform['query'] = rel.query
else:
transform['query'] = base.query
@ -491,7 +491,8 @@ def prepend_base(base, iri):
# merge paths
path = base.path
# append relative path to the end of the last directory from base
# append relative path to the end of the last
# directory from base
if rel.path != '':
path = path[0:path.rfind('/') + 1]
if len(path) > 0 and not path.endswith('/'):
@ -571,6 +572,7 @@ def remove_base(base, iri):
ParsedUrl = namedtuple(
'ParsedUrl', ['scheme', 'authority', 'path', 'query', 'fragment'])
def parse_url(url):
# regex from RFC 3986
p = r'^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?'
@ -652,7 +654,7 @@ class JsonLdProcessor(object):
options.setdefault('skipExpansion', False)
options.setdefault('activeCtx', False)
options.setdefault('documentLoader', _default_document_loader)
options.setdefault('link', False);
options.setdefault('link', False)
if options['link']:
# force skip expansion when linking, "link" is not part of the
# public API, it should only be called from framing
@ -1246,7 +1248,8 @@ class JsonLdProcessor(object):
JsonLdProcessor.add_value(subject, property, v, options)
elif property in subject:
# check if subject already has value if duplicates not allowed
has_value = (not options['allowDuplicate'] and
has_value = \
(not options['allowDuplicate'] and
JsonLdProcessor.has_value(subject, property, value))
# make property an array if value not present or always an array
@ -1469,12 +1472,16 @@ class JsonLdProcessor(object):
triple['object'] = {'type': 'blank node', 'value': match[4]}
else:
triple['object'] = {'type': 'literal'}
unescaped = (match[5]
.replace('\\"', '\"')
.replace('\\t', '\t')
.replace('\\n', '\n')
.replace('\\r', '\r')
.replace('\\\\', '\\'))
replacements = {
'\\"': '\"',
'\\t': '\t',
'\\n': '\n',
'\\r': '\r',
'\\\\': '\\'
}
unescaped = match[5]
for match, repl in replacements.items():
unescaped = unescaped.replace(match, repl)
if match[6] is not None:
triple['object']['datatype'] = match[6]
elif match[7] is not None:
@ -1580,12 +1587,16 @@ class JsonLdProcessor(object):
else:
quad += o['value']
else:
escaped = (o['value']
.replace('\\', '\\\\')
.replace('\t', '\\t')
.replace('\n', '\\n')
.replace('\r', '\\r')
.replace('\"', '\\"'))
replacements = {
'\\': '\\\\',
'\t': '\\t',
'\n': '\\n',
'\r': '\\r',
'\"': '\\"'
}
escaped = o['value']
for match, repl in replacements.items():
escaped = escaped.replace(match, repl)
quad += '"' + escaped + '"'
if o['datatype'] == RDF_LANGSTRING:
if o['language']:
@ -1765,7 +1776,8 @@ class JsonLdProcessor(object):
JsonLdProcessor.add_value(rval, alias, expanded_value)
continue
# skip array processing for keywords that aren't @graph or @list
# skip array processing for keywords that aren't
# @graph or @list
if(expanded_property != '@graph' and
expanded_property != '@list' and
_is_keyword(expanded_property)):
@ -1856,7 +1868,8 @@ class JsonLdProcessor(object):
# @container is @set or @list, value is an empty
# array, or key is @graph
is_array = (not options['compactArrays'] or
container == '@set' or container == '@list' or
container == '@set' or
container == '@list' or
(_is_array(compacted_item) and
len(compacted_item) == 0) or
expanded_property == '@list' or
@ -2030,10 +2043,9 @@ class JsonLdProcessor(object):
# properties double-reversed
if '@reverse' in expanded_value:
for rproperty, rvalue in (
expanded_value['@reverse'].items()):
for rprop, rvalue in expanded_value['@reverse'].items():
JsonLdProcessor.add_value(
rval, rproperty, rvalue,
rval, rprop, rvalue,
{'propertyIsArray': True})
# merge in all reversed properties
@ -2564,7 +2576,6 @@ class JsonLdProcessor(object):
# reset to initial context
if ctx is None:
rval = active_ctx = self._get_initial_context(options)
must_clone = False
continue
# dereference @context key if present
@ -3382,7 +3393,6 @@ class JsonLdProcessor(object):
for i, parent in enumerate(embed['parent']):
if JsonLdProcessor.compare_values(parent, subject):
embed['parent'][i] = subject
foo = True
break
else:
# replace subject with reference
@ -3470,7 +3480,7 @@ class JsonLdProcessor(object):
idx = options['link'][id_].index(input_)
# already visited
return options['link'][id_][idx]
except:
except BaseException:
# prevent circular visitation
options['link'][id_].append(input_)
else:
@ -3839,7 +3849,8 @@ class JsonLdProcessor(object):
# select curie if it is shorter or the same length but
# lexicographically less than the current choice
if (is_usable_curie and (candidate is None or
_compare_shortest_least(curie, candidate) < 0)):
_compare_shortest_least(curie,
candidate) < 0)):
candidate = curie
# return curie candidate
@ -3887,7 +3898,8 @@ class JsonLdProcessor(object):
# 2. there is no default language or @value is not a string or
# the key has a mapping with a null @language
key_count = len(value)
is_value_only_key = (key_count == 1 or (key_count == 2 and
is_value_only_key = \
(key_count == 1 or (key_count == 2 and
'@index' in value and not preserve_index))
has_default_language = '@language' in active_ctx
is_value_string = _is_string(value['@value'])