From 74cb04475830e9c0cfab6931b10759e1c9818b0f Mon Sep 17 00:00:00 2001 From: Mingwei Zhang Date: Tue, 26 May 2020 10:46:45 -0700 Subject: [PATCH 1/2] allow setting default codec or disabling decode --- wandio/compressed.py | 5 ++++- wandio/file.py | 4 +++- wandio/http.py | 6 +++++- 3 files changed, 12 insertions(+), 3 deletions(-) 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 From cc91cda2cc2ea4d5bdcf2981d257e161172b7bfd Mon Sep 17 00:00:00 2001 From: Mingwei Zhang Date: Tue, 26 May 2020 11:24:43 -0700 Subject: [PATCH 2/2] update wanio_open to support decode changes --- wandio/opener.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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: