From c665c0297168a319d949b94bc5048ea4046f319a Mon Sep 17 00:00:00 2001 From: roll Date: Fri, 16 Aug 2019 14:55:40 +0300 Subject: [PATCH] Added support for compressed file-like objects --- tabulator/stream.py | 4 +++- tests/test_stream.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tabulator/stream.py b/tabulator/stream.py index 8643a6d3..0ff24ec0 100644 --- a/tabulator/stream.py +++ b/tabulator/stream.py @@ -231,7 +231,9 @@ def open(self): # Gzip compression elif compression == 'gz' and six.PY3: - name = self.__source.replace('.gz', '') + name = '' + if isinstance(self.__source, str): + name = self.__source.replace('.gz', '') self.__source = gzip.open(self.__loader.load(self.__source, mode='b')) self.__loader = StreamLoader(bytes_sample_size=self.__bytes_sample_size) format = self.__format or helpers.detect_scheme_and_format(name)[1] diff --git a/tests/test_stream.py b/tests/test_stream.py index 8c1e71d6..5966ed99 100644 --- a/tests/test_stream.py +++ b/tests/test_stream.py @@ -640,6 +640,7 @@ def test_stream_local_csv_zip(): assert stream.headers is None assert stream.read() == [['id', 'name'], ['1', 'english'], ['2', '中国人']] + @pytest.mark.skipif(six.PY2, reason='Support only for Python3') def test_stream_local_csv_zip_multiple_files(): with Stream('data/2-files.zip', filename = 'table.csv') as stream: @@ -657,6 +658,22 @@ def test_stream_local_csv_gz(): assert stream.read() == [['id', 'name'], ['1', 'english'], ['2', '中国人']] +@pytest.mark.skipif(six.PY2, reason='Support only for Python3') +def test_stream_filelike_csv_zip(): + with open('data/table.csv.zip', 'rb') as file: + with Stream(file, format='csv', compression='zip') as stream: + assert stream.headers is None + assert stream.read() == [['id', 'name'], ['1', 'english'], ['2', '中国人']] + + +@pytest.mark.skipif(six.PY2, reason='Support only for Python3') +def test_stream_filelike_csv_gz(): + with open('data/table.csv.gz', 'rb') as file: + with Stream(file, format='csv', compression='gz') as stream: + assert stream.headers is None + assert stream.read() == [['id', 'name'], ['1', 'english'], ['2', '中国人']] + + # Issues def test_stream_reset_on_close_issue_190():