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