diff --git a/wandio/compressed.py b/wandio/compressed.py index 643ece7..cc0bab4 100644 --- a/wandio/compressed.py +++ b/wandio/compressed.py @@ -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): diff --git a/wandio/file.py b/wandio/file.py index 71c4c61..4597b9a 100644 --- a/wandio/file.py +++ b/wandio/file.py @@ -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 diff --git a/wandio/http.py b/wandio/http.py index d0ca1fd..91b9a01 100644 --- a/wandio/http.py +++ b/wandio/http.py @@ -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 diff --git a/wandio/opener.py b/wandio/opener.py index 0b7c016..5b9b903 100644 --- a/wandio/opener.py +++ b/wandio/opener.py @@ -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: