Skip to content

Commit 0416e93

Browse files
committed
Garden in 'python-modernize' updates to apitools.
Catches up our vendored-in source to commit: 5a1bab1df7a474cff57f7f5cc066b19a1c286a21.
1 parent 71780e3 commit 0416e93

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

_gcloud_vendor/apitools/base/py/http_wrapper.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
"""
77

88
import collections
9-
import httplib
109
import logging
1110
import socket
1211
import time
1312
import urlparse
1413

1514
import httplib2
15+
from six.moves import http_client
16+
from six.moves import range
1617

1718
from _gcloud_vendor.apitools.base.py import exceptions
1819
from _gcloud_vendor.apitools.base.py import util
@@ -28,10 +29,10 @@
2829
RESUME_INCOMPLETE = 308
2930
TOO_MANY_REQUESTS = 429
3031
_REDIRECT_STATUS_CODES = (
31-
httplib.MOVED_PERMANENTLY,
32-
httplib.FOUND,
33-
httplib.SEE_OTHER,
34-
httplib.TEMPORARY_REDIRECT,
32+
http_client.MOVED_PERMANENTLY,
33+
http_client.FOUND,
34+
http_client.SEE_OTHER,
35+
http_client.TEMPORARY_REDIRECT,
3536
RESUME_INCOMPLETE,
3637
)
3738

@@ -129,7 +130,7 @@ def MakeRequest(http, http_request, retries=5, redirections=5):
129130
url_scheme = urlparse.urlsplit(http_request.url).scheme
130131
if url_scheme and url_scheme in http.connections:
131132
connection_type = http.connections[url_scheme]
132-
for retry in xrange(retries + 1):
133+
for retry in range(retries + 1):
133134
# Note that the str() calls here are important for working around
134135
# some funny business with message construction and unicode in
135136
# httplib itself. See, eg,
@@ -140,15 +141,15 @@ def MakeRequest(http, http_request, retries=5, redirections=5):
140141
str(http_request.url), method=str(http_request.http_method),
141142
body=http_request.body, headers=http_request.headers,
142143
redirections=redirections, connection_type=connection_type)
143-
except httplib.BadStatusLine as e:
144+
except http_client.BadStatusLine as e:
144145
logging.error('Caught BadStatusLine from httplib, retrying: %s', e)
145146
exc = e
146147
except socket.error as e:
147148
if http_request.http_method != 'GET':
148149
raise
149150
logging.error('Caught socket error, retrying: %s', e)
150151
exc = e
151-
except httplib.IncompleteRead as e:
152+
except http_client.IncompleteRead as e:
152153
if http_request.http_method != 'GET':
153154
raise
154155
logging.error('Caught IncompleteRead error, retrying: %s', e)
@@ -161,7 +162,7 @@ def MakeRequest(http, http_request, retries=5, redirections=5):
161162
break
162163
logging.info('Retrying request to url <%s> after status code %s.',
163164
response.request_url, response.status_code)
164-
elif isinstance(exc, httplib.IncompleteRead):
165+
elif isinstance(exc, http_client.IncompleteRead):
165166
logging.info('Retrying request to url <%s> after incomplete read.',
166167
str(http_request.url))
167168
else:

_gcloud_vendor/apitools/base/py/transfer.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#!/usr/bin/env python
22
"""Upload and download support for apitools."""
3+
from __future__ import print_function
34

45
import email.generator as email_generator
56
import email.mime.multipart as mime_multipart
67
import email.mime.nonmultipart as mime_nonmultipart
7-
import httplib
88
import io
99
import json
1010
import mimetypes
1111
import os
1212
import StringIO
1313
import threading
1414

15+
from six.moves import http_client
16+
1517
from _gcloud_vendor.apitools.base.py import exceptions
1618
from _gcloud_vendor.apitools.base.py import http_wrapper
1719
from _gcloud_vendor.apitools.base.py import util
@@ -38,7 +40,7 @@ def __init__(self, stream, close_stream=False, chunksize=None,
3840
self.__url = None
3941

4042
self.auto_transfer = auto_transfer
41-
self.chunksize = chunksize or 1048576L
43+
self.chunksize = chunksize or 1048576
4244

4345
def __repr__(self):
4446
return str(self)
@@ -121,10 +123,10 @@ class Download(_Transfer):
121123
chunksize: default chunksize to use for transfers.
122124
"""
123125
_ACCEPTABLE_STATUSES = set((
124-
httplib.OK,
125-
httplib.NO_CONTENT,
126-
httplib.PARTIAL_CONTENT,
127-
httplib.REQUESTED_RANGE_NOT_SATISFIABLE,
126+
http_client.OK,
127+
http_client.NO_CONTENT,
128+
http_client.PARTIAL_CONTENT,
129+
http_client.REQUESTED_RANGE_NOT_SATISFIABLE,
128130
))
129131
_REQUIRED_SERIALIZATION_KEYS = set((
130132
'auto_transfer', 'progress', 'total_size', 'url'))
@@ -242,13 +244,13 @@ def InitializeDownload(self, http_request, http=None, client=None):
242244
@staticmethod
243245
def _ArgPrinter(response, unused_download):
244246
if 'content-range' in response.info:
245-
print 'Received %s' % response.info['content-range']
247+
print('Received %s' % response.info['content-range'])
246248
else:
247-
print 'Received %d bytes' % len(response)
249+
print('Received %d bytes' % len(response))
248250

249251
@staticmethod
250252
def _CompletePrinter(*unused_args):
251-
print 'Download complete'
253+
print('Download complete')
252254

253255
def __NormalizeStartEnd(self, start, end=None):
254256
if end is not None:
@@ -290,10 +292,10 @@ def __ProcessResponse(self, response):
290292
"""Process this response (by updating self and writing to self.stream)."""
291293
if response.status_code not in self._ACCEPTABLE_STATUSES:
292294
raise exceptions.TransferInvalidError(response.content)
293-
if response.status_code in (httplib.OK, httplib.PARTIAL_CONTENT):
295+
if response.status_code in (http_client.OK, http_client.PARTIAL_CONTENT):
294296
self.stream.write(response.content)
295297
self.__progress += len(response)
296-
elif response.status_code == httplib.NO_CONTENT:
298+
elif response.status_code == http_client.NO_CONTENT:
297299
# It's important to write something to the stream for the case
298300
# of a 0-byte download to a file, as otherwise python won't
299301
# create the file.
@@ -348,7 +350,7 @@ def StreamInChunks(self, callback=None, finish_callback=None,
348350
additional_headers=additional_headers)
349351
response = self.__ProcessResponse(response)
350352
self._ExecuteCallback(callback, response)
351-
if (response.status_code == httplib.OK or
353+
if (response.status_code == http_client.OK or
352354
self.progress >= self.total_size):
353355
break
354356
self._ExecuteCallback(finish_callback, response)
@@ -591,7 +593,7 @@ def _RefreshResumableUploadState(self):
591593
self.http, refresh_request, redirections=0)
592594
range_header = refresh_response.info.get(
593595
'Range', refresh_response.info.get('range'))
594-
if refresh_response.status_code in (httplib.OK, httplib.CREATED):
596+
if refresh_response.status_code in (http_client.OK, http_client.CREATED):
595597
self.__complete = True
596598
elif refresh_response.status_code == http_wrapper.RESUME_INCOMPLETE:
597599
if range_header is None:
@@ -619,7 +621,7 @@ def InitializeUpload(self, http_request, http=None, client=None):
619621
http_request.url = client.FinalizeTransferUrl(http_request.url)
620622
self.EnsureUninitialized()
621623
http_response = http_wrapper.MakeRequest(http, http_request)
622-
if http_response.status_code != httplib.OK:
624+
if http_response.status_code != http_client.OK:
623625
raise exceptions.HttpError.FromResponse(http_response)
624626

625627
self.__server_chunk_granularity = http_response.info.get(
@@ -651,11 +653,11 @@ def __ValidateChunksize(self, chunksize=None):
651653

652654
@staticmethod
653655
def _ArgPrinter(response, unused_upload):
654-
print 'Sent %s' % response.info['range']
656+
print('Sent %s' % response.info['range'])
655657

656658
@staticmethod
657659
def _CompletePrinter(*unused_args):
658-
print 'Upload complete'
660+
print('Upload complete')
659661

660662
def StreamInChunks(self, callback=None, finish_callback=None,
661663
additional_headers=None):
@@ -674,7 +676,7 @@ def StreamInChunks(self, callback=None, finish_callback=None,
674676
while not self.complete:
675677
response = self.__SendChunk(self.stream.tell(),
676678
additional_headers=additional_headers)
677-
if response.status_code in (httplib.OK, httplib.CREATED):
679+
if response.status_code in (http_client.OK, http_client.CREATED):
678680
self.__complete = True
679681
break
680682
self.__progress = self.__GetLastByte(response.info['range'])
@@ -703,10 +705,10 @@ def __SendChunk(self, start, additional_headers=None, data=None):
703705
request.headers.update(additional_headers)
704706

705707
response = http_wrapper.MakeRequest(self.bytes_http, request)
706-
if response.status_code not in (httplib.OK, httplib.CREATED,
708+
if response.status_code not in (http_client.OK, http_client.CREATED,
707709
http_wrapper.RESUME_INCOMPLETE):
708710
raise exceptions.HttpError.FromResponse(response)
709-
if response.status_code in (httplib.OK, httplib.CREATED):
711+
if response.status_code in (http_client.OK, http_client.CREATED):
710712
return response
711713
# TODO(craigcitro): Add retries on no progress?
712714
last_byte = self.__GetLastByte(response.info['range'])

_gcloud_vendor/apitools/base/py/util.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"""Assorted utilities shared between parts of apitools."""
33

44
import collections
5-
import httplib
65
import os
76
import random
8-
import types
97
import urllib
108
import urllib2
119

10+
import six
11+
from six.moves import http_client
12+
1213
from _gcloud_vendor.apitools.base.py import exceptions
1314

1415
__all__ = [
@@ -46,13 +47,13 @@ def DetectGce():
4647
o = urllib2.urlopen('http://metadata.google.internal')
4748
except urllib2.URLError:
4849
return False
49-
return (o.getcode() == httplib.OK and
50+
return (o.getcode() == http_client.OK and
5051
o.headers.get('metadata-flavor') == 'Google')
5152

5253

5354
def NormalizeScopes(scope_spec):
5455
"""Normalize scope_spec to a set of strings."""
55-
if isinstance(scope_spec, types.StringTypes):
56+
if isinstance(scope_spec, six.string_types):
5657
return set(scope_spec.split(' '))
5758
elif isinstance(scope_spec, collections.Iterable):
5859
return set(scope_spec)
@@ -99,7 +100,7 @@ def ExpandRelativePath(method_config, params, relative_path=None):
99100
raise exceptions.InvalidUserInputError(
100101
'Request missing required parameter %s' % param)
101102
try:
102-
if not isinstance(value, basestring):
103+
if not isinstance(value, six.string_types):
103104
value = str(value)
104105
path = path.replace(param_template,
105106
urllib.quote(value.encode('utf_8'), reserved_chars))

0 commit comments

Comments
 (0)