Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for nt in utf-8 encoding #447

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions rdflib/plugins/parsers/nt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class NTParser(Parser):
def __init__(self):
super(NTParser, self).__init__()

def parse(self, source, sink, baseURI=None):
def parse(self, source, sink, baseURI=None, encoding="ascii"):
f = source.getByteStream() # TODO getCharacterStream?
parser = NTriplesParser(NTSink(sink))
parser.parse(f)
parser.parse(f, encoding)
f.close()
11 changes: 8 additions & 3 deletions rdflib/plugins/parsers/ntriples.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rdflib.term import BNode as bNode
from rdflib.term import Literal

from rdflib.py3compat import cast_bytes, decodeUnicodeEscape, ascii
from rdflib.py3compat import cast_bytes, decodeUnicodeEscape, ascii, utf8

__all__ = ['unquote', 'uriquote', 'Sink', 'NTriplesParser']

Expand Down Expand Up @@ -120,12 +120,17 @@ def __init__(self, sink=None):
else:
self.sink = Sink()

def parse(self, f):
def parse(self, f, encoding="ascii"):
"""Parse f as an N-Triples file."""
if not hasattr(f, 'read'):
raise ParseError("Item to parse must be a file-like object.")

f = ascii(f)
if encoding == "ascii":
f = ascii(f)
elif encoding == "utf-8":
f = utf8(f)
else:
raise ParseError("Invalid encoding: %s" % encoding)

self.file = f
self.buffer = ''
Expand Down
6 changes: 6 additions & 0 deletions rdflib/py3compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def b(s):
def ascii(stream):
return codecs.getreader('ascii')(stream)

def utf8(stream):
return codecs.getreader('utf-8')(stream)

def bopen(*args, **kwargs):
return open(*args, mode = 'rb', **kwargs)

Expand Down Expand Up @@ -105,6 +108,9 @@ def b(s):
def ascii(stream):
return stream

def utf8(stream):
return stream

bopen = open

bytestype = str
Expand Down