Skip to content
Draft
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
5 changes: 4 additions & 1 deletion wandio/compressed.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def readline(self):
break
if not len(res) and not len(self.buf) and self.eof:
return None
return res.decode("utf-8")
if self.decode:
return res.decode(self.encoding)
else:
return res


class CompressedWriter(wandio.file.GenericWriter):
Expand Down
4 changes: 3 additions & 1 deletion wandio/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ class GenericReader(object):
Wraps a file-like object
"""

def __init__(self, fh):
def __init__(self, fh, decode=True, encoding="utf-8"):
self.fh = fh
self.decode = decode
self.encoding = encoding

def __enter__(self):
return self
Expand Down
6 changes: 5 additions & 1 deletion wandio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ def __init__(self, url):
super(HttpReader, self).__init__(urlopen(self.url))

def __next__(self):
return next(self.fh).decode("utf-8")
res = next(self.fh)
if self.decode:
return res.decode(self.encoding)
else:
return res
7 changes: 5 additions & 2 deletions wandio/opener.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ def __init__(self, filename, options=None):
super(Writer, self).__init__(fh)


def wandio_open(filename, mode="r"):
def wandio_open(filename, mode="r", decode=True, encoding="utf-8"):
if mode == "r":
return Reader(filename)
reader = Reader(filename)
reader.fh.decode = decode
reader.fh.encoding = encoding
return reader
elif mode == "w":
return Writer(filename)
else:
Expand Down