Skip to content

Commit 2d692f7

Browse files
jkulzickStephen Hoover
authored and
Stephen Hoover
committed
[CIVP-14018] civis_to_csv doesn't return a gzip when requested for include_header=False (#195)
* fix override of compression in civis_to_csv * add test support for gzip compression * gzip.compress is not available in 2.7 * remove local test script * remove new cassettes and tests * update docstring in civis_to_csv
1 parent 4c581ce commit 2d692f7

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

civis/io/_tables.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,9 @@ def civis_to_csv(filename, sql, database, job_name=None, api_key=None,
326326
compression: str, optional
327327
Type of compression to use, if any. One of ``'none'``, ``'zip'``, or
328328
``'gzip'``. Default ``'none'``. ``'gzip'`` currently returns a file
329-
with no compression. In a future release, a ``'gzip'`` compressed
330-
file will be returned.
329+
with no compression unless include_header is set to False. In a
330+
future release, a ``'gzip'`` compressed file will be returned for
331+
all cases.
331332
delimiter: str, optional
332333
Which delimiter to use, if any. One of ``','``, ``'\t'``, or
333334
``'|'``. Default: ``','``.
@@ -368,7 +369,7 @@ def civis_to_csv(filename, sql, database, job_name=None, api_key=None,
368369
# don't fix bug that would cause breaking change for now
369370
# when gzip compression is requested, a gzip file is not actually returned
370371
# instead the gzip file is decompressed during download
371-
if compression == 'gzip':
372+
if compression == 'gzip' and include_header:
372373
compression = 'none'
373374

374375
# don't support parallel unload; the output format
@@ -915,8 +916,9 @@ def _download_file(url, local_path, headers, compression):
915916

916917
# gzipped buffers can be concatenated so write headers as gzip
917918
if compression == 'gzip':
918-
with open(local_path, 'wb') as fout:
919-
fout.write(gzip.compress(headers))
919+
with gzip.open(local_path, 'wb') as fout:
920+
fout.write(headers)
921+
with open(local_path, 'ab') as fout:
920922
shutil.copyfileobj(response.raw, fout, CHUNK_SIZE)
921923

922924
# write headers and decompress the stream

0 commit comments

Comments
 (0)