Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions lib/pyld/nquads.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,27 @@
RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
RDF_LANGSTRING = RDF + 'langString'

def parse_nquads(input_):
def escape(value: str):
return (
value.replace("\\", "\\\\")
.replace("\t", "\\t")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace('"', '\\"')
)


def unescape(value: str):
return (
value.replace('\\"', '"')
.replace("\\t", "\t")
.replace("\\n", "\n")
.replace("\\r", "\r")
.replace("\\\\", "\\")
)


def parse_nquads(input_: str):
"""
Parses RDF in the form of N-Quads.

Expand All @@ -15,14 +35,13 @@ def parse_nquads(input_):
"""
# define partial regexes
iri = '(?:<([^:]+:[^>]*)>)'
bnode = '(_:(?:[A-Za-z][A-Za-z0-9]*))'
bnode = '(_:(?:[A-Za-z0-9_][A-Za-z0-9_.-]*))'
plain = '"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"'
datatype = '(?:\\^\\^' + iri + ')'
language = '(?:@([a-zA-Z]+(?:-[a-zA-Z0-9]+)*))'
literal = '(?:' + plain + '(?:' + datatype + '|' + language + ')?)'
ws = '[ \\t]+'
wso = '[ \\t]*'
eoln = r'(?:\r\n)|(?:\n)|(?:\r)'
empty = r'^' + wso + '$'

# define quad part regexes
Expand All @@ -45,7 +64,7 @@ def parse_nquads(input_):
dataset = {}

# split N-Quad input into lines
lines = re.split(eoln, input_)
lines = input_.splitlines(True)
line_number = 0
for line in lines:
line_number += 1
Expand All @@ -57,7 +76,7 @@ def parse_nquads(input_):
# parse quad
match = re.search(quad, line)
if match is None:
raise ParserError('Error while parsing N-Quads invalid quad.', line_number=line_number)
raise ParserError(f'Error while parsing N-Quads invalid quad {line} at line {line_number}.', line_number=line_number)
match = match.groups()

# create RDF triple
Expand All @@ -79,13 +98,7 @@ def parse_nquads(input_):
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('\\\\', '\\'))
unescaped = unescape(match[5])
if match[6] is not None:
triple['object']['datatype'] = match[6]
elif match[7] is not None:
Expand Down Expand Up @@ -176,16 +189,10 @@ def serialize_nquad(triple, graph_name=None):
elif(o['type'] == 'blank node'):
quad += o['value']
else:
escaped = (
o['value']
.replace('\\', '\\\\')
.replace('\t', '\\t')
.replace('\n', '\\n')
.replace('\r', '\\r')
.replace('\"', '\\"'))
escaped = escape(o['value'])
quad += '"' + escaped + '"'
if o['datatype'] == RDF_LANGSTRING:
if o['language']:
if o.get('language'):
quad += '@' + o['language']
elif o['datatype'] != XSD_STRING:
quad += '^^<' + o['datatype'] + '>'
Expand Down
1 change: 0 additions & 1 deletion tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,6 @@ def write(self, filename):
},
'skip': {
'idRegex': [
'.*manifest-urdna2015#test059$',
'.*manifest-urdna2015#test060$',
]
},
Expand Down