summaryrefslogtreecommitdiff
path: root/rdflib/plugins/parsers/notation3.py
diff options
context:
space:
mode:
Diffstat (limited to 'rdflib/plugins/parsers/notation3.py')
-rwxr-xr-xrdflib/plugins/parsers/notation3.py102
1 files changed, 54 insertions, 48 deletions
diff --git a/rdflib/plugins/parsers/notation3.py b/rdflib/plugins/parsers/notation3.py
index c427f153..d866977d 100755
--- a/rdflib/plugins/parsers/notation3.py
+++ b/rdflib/plugins/parsers/notation3.py
@@ -139,10 +139,13 @@ def join(here, there):
return here + frag
# join('mid:foo@example', '../foo') bzzt
- if here[bcolonl + 1: bcolonl + 2] != "/":
- raise ValueError("Base <%s> has no slash after " "colon - with relative '%s'." % (here, there))
+ if here[bcolonl + 1 : bcolonl + 2] != "/":
+ raise ValueError(
+ "Base <%s> has no slash after "
+ "colon - with relative '%s'." % (here, there)
+ )
- if here[bcolonl + 1: bcolonl + 3] == "//":
+ if here[bcolonl + 1 : bcolonl + 3] == "//":
bpath = here.find("/", bcolonl + 3)
else:
bpath = bcolonl + 1
@@ -502,14 +505,14 @@ class SinkParser:
"""
assert tok[0] not in _notNameChars # not for punctuation
- if argstr[i: i + 1] == "@":
+ if argstr[i : i + 1] == "@":
i = i + 1
else:
if tok not in self.keywords:
return -1 # No, this has neither keywords declaration nor "@"
if (
- argstr[i: i + len(tok)] == tok
+ argstr[i : i + len(tok)] == tok
and (argstr[i + len(tok)] in _notKeywordsChars)
or (colon and argstr[i + len(tok)] == ":")
):
@@ -526,7 +529,7 @@ class SinkParser:
assert tok[0] not in _notNameChars # not for punctuation
- if argstr[i: i + len(tok)].lower() == tok.lower() and (
+ if argstr[i : i + len(tok)].lower() == tok.lower() and (
argstr[i + len(tok)] in _notQNameChars
):
i = i + len(tok)
@@ -794,23 +797,23 @@ class SinkParser:
res.append(("->", RDF_type))
return j
- if argstr[i: i + 2] == "<=":
+ if argstr[i : i + 2] == "<=":
if self.turtle:
self.BadSyntax(argstr, i, "Found '<=' in Turtle mode. ")
res.append(("<-", self._store.newSymbol(Logic_NS + "implies")))
return i + 2
- if argstr[i: i + 1] == "=":
+ if argstr[i : i + 1] == "=":
if self.turtle:
self.BadSyntax(argstr, i, "Found '=' in Turtle mode")
- if argstr[i + 1: i + 2] == ">":
+ if argstr[i + 1 : i + 2] == ">":
res.append(("->", self._store.newSymbol(Logic_NS + "implies")))
return i + 2
res.append(("->", DAML_sameAs))
return i + 1
- if argstr[i: i + 2] == ":=":
+ if argstr[i : i + 2] == ":=":
if self.turtle:
self.BadSyntax(argstr, i, "Found ':=' in Turtle mode")
@@ -823,7 +826,7 @@ class SinkParser:
res.append(("->", r[0]))
return j
- if argstr[i: i + 2] == ">-" or argstr[i: i + 2] == "<-":
+ if argstr[i : i + 2] == ">-" or argstr[i : i + 2] == "<-":
self.BadSyntax(argstr, j, ">- ... -> syntax is obsolete.")
return -1
@@ -844,8 +847,8 @@ class SinkParser:
if j < 0:
return j # nope
- while argstr[j: j + 1] in "!^": # no spaces, must follow exactly (?)
- ch = argstr[j: j + 1]
+ while argstr[j : j + 1] in "!^": # no spaces, must follow exactly (?)
+ ch = argstr[j : j + 1]
subj = res.pop()
obj = self.blankNode(uri=self.here(j))
j = self.node(argstr, j + 1, res)
@@ -879,7 +882,7 @@ class SinkParser:
if j < 0:
return j # eof
i = j
- ch = argstr[i: i + 1] # Quick 1-character checks first:
+ ch = argstr[i : i + 1] # Quick 1-character checks first:
if ch == "[":
bnodeID = self.here(i)
@@ -887,7 +890,7 @@ class SinkParser:
if j < 0:
self.BadSyntax(argstr, i, "EOF after '['")
# Hack for "is" binding name to anon node
- if argstr[j: j + 1] == "=":
+ if argstr[j : j + 1] == "=":
if self.turtle:
self.BadSyntax(
argstr, j, "Found '[=' or '[ =' when in turtle mode."
@@ -905,7 +908,7 @@ class SinkParser:
self.BadSyntax(
argstr, i, "EOF when objectList expected after [ = "
)
- if argstr[j: j + 1] == ";":
+ if argstr[j : j + 1] == ";":
j = j + 1
else:
self.BadSyntax(argstr, i, "objectList expected after [= ")
@@ -922,7 +925,7 @@ class SinkParser:
self.BadSyntax(
argstr, i, "EOF when ']' expected after [ <propertyList>"
)
- if argstr[j: j + 1] != "]":
+ if argstr[j : j + 1] != "]":
self.BadSyntax(argstr, j, "']' expected")
res.append(subj)
return j + 1
@@ -931,7 +934,7 @@ class SinkParser:
# if self.turtle:
# self.BadSyntax(argstr, i,
# "found '{' while in Turtle mode, Formulas not supported!")
- ch2 = argstr[i + 1: i + 2]
+ ch2 = argstr[i + 1 : i + 2]
if ch2 == "$":
# a set
i += 1
@@ -942,12 +945,12 @@ class SinkParser:
i = self.skipSpace(argstr, j)
if i < 0:
self.BadSyntax(argstr, i, "needed '$}', found end.")
- if argstr[i: i + 2] == "$}":
+ if argstr[i : i + 2] == "$}":
j = i + 2
break
if not first_run:
- if argstr[i: i + 1] == ",":
+ if argstr[i : i + 1] == ",":
i += 1
else:
self.BadSyntax(argstr, i, "expected: ','")
@@ -982,7 +985,7 @@ class SinkParser:
if i < 0:
self.BadSyntax(argstr, i, "needed '}', found end.")
- if argstr[i: i + 1] == "}":
+ if argstr[i : i + 1] == "}":
j = i + 1
break
@@ -1001,7 +1004,7 @@ class SinkParser:
if ch == "(":
thing_type = self._store.newList
- ch2 = argstr[i + 1: i + 2]
+ ch2 = argstr[i + 1 : i + 2]
if ch2 == "$":
thing_type = self._store.newSet
i += 1
@@ -1012,7 +1015,7 @@ class SinkParser:
i = self.skipSpace(argstr, j)
if i < 0:
self.BadSyntax(argstr, i, "needed ')', found end.")
- if argstr[i: i + 1] == ")":
+ if argstr[i : i + 1] == ")":
j = i + 1
break
@@ -1065,7 +1068,7 @@ class SinkParser:
break
i = j + 1
- if argstr[j: j + 2] == ":-":
+ if argstr[j : j + 2] == ":-":
if self.turtle:
self.BadSyntax(argstr, j, "Found in ':-' in Turtle mode")
i = j + 2
@@ -1095,7 +1098,7 @@ class SinkParser:
j = self.skipSpace(argstr, i)
if j < 0:
self.BadSyntax(argstr, j, "EOF found in list of objects")
- if argstr[i: i + 1] != ";":
+ if argstr[i : i + 1] != ";":
return i
i = i + 1 # skip semicolon and continue
@@ -1116,7 +1119,7 @@ class SinkParser:
j = self.skipSpace(argstr, i)
if j < 0:
return j # eof
- ch = argstr[j: j + 1]
+ ch = argstr[j : j + 1]
if ch != ",":
if ch != ".":
return -1
@@ -1133,7 +1136,7 @@ class SinkParser:
j = self.skipSpace(argstr, i)
if j < 0:
self.BadSyntax(argstr, j, "EOF found after object")
- if argstr[j: j + 1] != ",":
+ if argstr[j : j + 1] != ",":
return j # Found something else!
i = self.object(argstr, j + 1, res)
if i < 0:
@@ -1143,11 +1146,11 @@ class SinkParser:
j = self.skipSpace(argstr, i)
if j < 0:
return j # eof
- if argstr[j: j + 1] == ".":
+ if argstr[j : j + 1] == ".":
return j + 1 # skip
- if argstr[j: j + 1] == "}":
+ if argstr[j : j + 1] == "}":
return j # don't skip it
- if argstr[j: j + 1] == "]":
+ if argstr[j : j + 1] == "]":
return j
self.BadSyntax(argstr, j, "expected '.' or '}' or ']' at end of statement")
@@ -1212,7 +1215,7 @@ class SinkParser:
assert (
":" in uref
), "With no base URI, cannot deal with relative URIs"
- if argstr[i - 1: i] == "#" and not uref[-1:] == "#":
+ if argstr[i - 1 : i] == "#" and not uref[-1:] == "#":
uref = uref + "#" # She meant it! Weirdness in urlparse?
symb = self._store.newSymbol(uref)
if symb in self._variables:
@@ -1261,7 +1264,7 @@ class SinkParser:
if j < 0:
return -1
- if argstr[j: j + 1] != "?":
+ if argstr[j : j + 1] != "?":
return -1
j = j + 1
i = j
@@ -1419,7 +1422,7 @@ class SinkParser:
i = j
if argstr[i] in self.string_delimiters:
- if argstr[i: i + 3] == argstr[i] * 3:
+ if argstr[i : i + 3] == argstr[i] * 3:
delim = argstr[i] * 3
else:
delim = argstr[i]
@@ -1467,7 +1470,7 @@ class SinkParser:
# return -1 ## or fall through?
if argstr[i] in self.string_delimiters:
- if argstr[i: i + 3] == argstr[i] * 3:
+ if argstr[i : i + 3] == argstr[i] * 3:
delim = argstr[i] * 3
else:
delim = argstr[i]
@@ -1476,7 +1479,7 @@ class SinkParser:
dt = None
j, s = self.strconst(argstr, i, delim)
lang = None
- if argstr[j: j + 1] == "@": # Language?
+ if argstr[j : j + 1] == "@": # Language?
m = langcode.match(argstr, j + 1)
if m is None:
raise BadSyntax(
@@ -1487,9 +1490,9 @@ class SinkParser:
"Bad language code syntax on string " + "literal, after @",
)
i = m.end()
- lang = argstr[j + 1: i]
+ lang = argstr[j + 1 : i]
j = i
- if argstr[j: j + 2] == "^^":
+ if argstr[j : j + 2] == "^^":
res2 = []
j = self.uri_ref2(argstr, j + 2, res2) # Read datatype URI
dt = res2[0]
@@ -1522,15 +1525,15 @@ class SinkParser:
if (
delim == delim3
): # done when delim is """ or ''' and, respectively ...
- if argstr[j: j + 5] == delim5: # ... we have "" or '' before
+ if argstr[j : j + 5] == delim5: # ... we have "" or '' before
i = j + 5
ustr = ustr + delim2
return i, ustr
- if argstr[j: j + 4] == delim4: # ... we have " or ' before
+ if argstr[j : j + 4] == delim4: # ... we have " or ' before
i = j + 4
ustr = ustr + delim1
return i, ustr
- if argstr[j: j + 3] == delim3: # current " or ' is part of delim
+ if argstr[j : j + 3] == delim3: # current " or ' is part of delim
i = j + 3
return i, ustr
@@ -1542,8 +1545,8 @@ class SinkParser:
m = interesting.search(argstr, j) # was argstr[j:].
# Note for pos param to work, MUST be compiled ... re bug?
assert m, "Quote expected in string at ^ in %s^%s" % (
- argstr[j - 20: j],
- argstr[j: j + 20],
+ argstr[j - 20 : j],
+ argstr[j : j + 20],
) # at least need a quote
i = m.start()
@@ -1589,7 +1592,7 @@ class SinkParser:
elif ch == "\\":
j = i + 1
- ch = argstr[j: j + 1] # Will be empty if string ends
+ ch = argstr[j : j + 1] # Will be empty if string ends
if not ch:
raise BadSyntax(
self._thisDoc,
@@ -1620,14 +1623,14 @@ class SinkParser:
self._thisDoc, startline, argstr, i, "unterminated string literal(3)"
)
try:
- return i + n, reg.sub(unicodeExpand, "\\" + prefix + argstr[i: i + n])
+ return i + n, reg.sub(unicodeExpand, "\\" + prefix + argstr[i : i + n])
except:
raise BadSyntax(
self._thisDoc,
startline,
argstr,
i,
- "bad string literal hex escape: " + argstr[i: i + n],
+ "bad string literal hex escape: " + argstr[i : i + n],
)
def uEscape(self, argstr, i, startline):
@@ -1672,7 +1675,7 @@ class BadSyntax(SyntaxError):
self._why,
pre,
argstr[st:i],
- argstr[i: i + 60],
+ argstr[i : i + 60],
post,
)
@@ -1896,8 +1899,11 @@ class TurtleParser(Parser):
baseURI = graph.absolutize(source.getPublicId() or source.getSystemId() or "")
p = SinkParser(sink, baseURI=baseURI, turtle=turtle)
-
- p.loadStream(source.getByteStream())
+ # N3 parser prefers str stream
+ stream = source.getCharacterStream()
+ if not stream:
+ stream = source.getByteStream()
+ p.loadStream(stream)
for prefix, namespace in p._bindings.items():
graph.bind(prefix, namespace)