Skip to content

Commit

Permalink
Internal refactoring.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 268455703
  • Loading branch information
TensorFlow Hub Authors authored and andresusanopinto committed Sep 20, 2019
1 parent 2555031 commit d8d9b88
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions tensorflow_hub/compressed_module_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,9 @@ def __call__(self, handle):

def download(handle, tmp_dir):
"""Fetch a module via HTTP(S), handling redirect and download headers."""
cur_url = handle
request = url.Request(_append_compressed_format_query(handle))

# Look for and handle a special response header. If present, interpret it
# as a redirect to the module download location. This allows publishers
# (if they choose) to provide the same URL for both a module download and
# its documentation.

class LoggingHTTPRedirectHandler(url.HTTPRedirectHandler):

def redirect_request(self, req, fp, code, msg, headers, newurl):
cur_url = newurl # pylint:disable=unused-variable
return url.HTTPRedirectHandler.redirect_request(
self, req, fp, code, msg, headers, newurl)

url_opener = url.build_opener(LoggingHTTPRedirectHandler)
response = url_opener.open(request)
return resolver.DownloadManager(cur_url).download_and_uncompress(
response = self._call_urlopen(request)
return resolver.DownloadManager(handle).download_and_uncompress(
response, tmp_dir)

return resolver.atomic_download(handle, download, module_dir,
Expand All @@ -106,6 +91,10 @@ def _lock_file_timeout_sec(self):
# This method is provided as a convenience to simplify testing.
return LOCK_FILE_TIMEOUT_SEC

def _call_urlopen(self, request):
# Overriding this method allows setting SSL context in Python 3.
return url.urlopen(request)


class GcsCompressedFileResolver(resolver.Resolver):
"""Resolves GCS handles by downloading and decompressing them to local fs."""
Expand Down

0 comments on commit d8d9b88

Please sign in to comment.