Skip to content

Commit

Permalink
Merge pull request #41 from 4dn-dcic/gzip_for_jh
Browse files Browse the repository at this point in the history
Gzip for jh
  • Loading branch information
carlvitzthum authored Jun 6, 2019
2 parents a502576 + 46938c8 commit f232ee2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dcicutils/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Version information."""

# The following line *must* be the last in the module, exactly as formatted:
__version__ = "0.7.1"
__version__ = "0.7.2"
30 changes: 20 additions & 10 deletions dcicutils/jh_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import gzip
import types
import os
import sys
Expand Down Expand Up @@ -247,23 +248,32 @@ def open_4dn_file(obj_id, format=None, local=True):
"""
file_info = find_valid_file_or_extra_file(obj_id, format)
# this will be the base case in the future...
gz = False
if not local:
# this seems to handle both binary and non-binary files
ff_file = use_urllib.urlopen(file_info['full_href'])
if file_info.get('full_href', '').endswith('.gz'):
gz = True
else:
ff_file = open(file_info['full_path'])
# see if the file is binary (needs to opened with 'rb' mode)
# try to read a line from the file; if it is read, reset with seek()
try:
ff_file.readline()
except UnicodeDecodeError:
ff_file = open(file_info['full_path'], 'rb')
if file_info.get('full_path', '').endswith('.gz'):
ff_file = gzip.open(file_info['full_path'])
else:
ff_file.seek(0)
ff_file = open(file_info['full_path'])
# see if the file is binary (needs to opened with 'rb' mode)
# try to read a line from the file; if it is read, reset with seek()
try:
ff_file.readline()
except UnicodeDecodeError:
ff_file = open(file_info['full_path'], 'rb')
else:
ff_file.seek(0)
f = gzip.open(ff_file, 'rt') if gz else ff_file
try:
yield ff_file
yield f
finally:
ff_file.close()
if gz:
ff_file.close()
f.close()


# LASTLY, do setup that requires the above functions to be defined
Expand Down

0 comments on commit f232ee2

Please sign in to comment.