Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Added support for compressed file-like objects
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed Aug 16, 2019
1 parent 570b151 commit c665c02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tabulator/stream.py
Expand Up @@ -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]
Expand Down
17 changes: 17 additions & 0 deletions tests/test_stream.py
Expand Up @@ -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:
Expand All @@ -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():
Expand Down

0 comments on commit c665c02

Please sign in to comment.