Skip to content

Commit 15fbdc7

Browse files
Merge pull request googleapis#25 from methane/prepare-py3
Work toward Python 3.
2 parents 326cd51 + 0bceb33 commit 15fbdc7

36 files changed

+332
-300
lines changed

googleapiclient/discovery.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
A client library for Google's discovery based APIs.
1818
"""
19+
from __future__ import absolute_import
1920

2021
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
2122
__all__ = [
@@ -48,7 +49,7 @@
4849

4950
# Third-party imports
5051
import httplib2
51-
import mimeparse
52+
from . import mimeparse
5253
import uritemplate
5354

5455
# Local imports
@@ -205,7 +206,7 @@ def build(serviceName,
205206

206207
try:
207208
service = json.loads(content)
208-
except ValueError, e:
209+
except ValueError as e:
209210
logger.error('Failed to parse as JSON: ' + content)
210211
raise InvalidJsonError()
211212

@@ -329,13 +330,13 @@ def _media_size_to_long(maxSize):
329330
The size as an integer value.
330331
"""
331332
if len(maxSize) < 2:
332-
return 0L
333+
return 0
333334
units = maxSize[-2:].upper()
334335
bit_shift = _MEDIA_SIZE_BIT_SHIFTS.get(units)
335336
if bit_shift is not None:
336-
return long(maxSize[:-2]) << bit_shift
337+
return int(maxSize[:-2]) << bit_shift
337338
else:
338-
return long(maxSize)
339+
return int(maxSize)
339340

340341

341342
def _media_path_url_from_info(root_desc, path_url):

googleapiclient/http.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
object supporting an execute() method that does the
1919
actuall HTTP request.
2020
"""
21+
from __future__ import absolute_import
2122

2223
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
2324

@@ -28,7 +29,7 @@
2829
import httplib2
2930
import json
3031
import logging
31-
import mimeparse
32+
from . import mimeparse
3233
import mimetypes
3334
import os
3435
import random
@@ -42,13 +43,13 @@
4243
from email.mime.multipart import MIMEMultipart
4344
from email.mime.nonmultipart import MIMENonMultipart
4445
from email.parser import FeedParser
45-
from errors import BatchError
46-
from errors import HttpError
47-
from errors import InvalidChunkSizeError
48-
from errors import ResumableUploadError
49-
from errors import UnexpectedBodyError
50-
from errors import UnexpectedMethodError
51-
from model import JsonModel
46+
from .errors import BatchError
47+
from .errors import HttpError
48+
from .errors import InvalidChunkSizeError
49+
from .errors import ResumableUploadError
50+
from .errors import UnexpectedBodyError
51+
from .errors import UnexpectedMethodError
52+
from .model import JsonModel
5253
from oauth2client import util
5354

5455

@@ -1330,7 +1331,7 @@ def execute(self, http=None):
13301331
if resp.status >= 300:
13311332
raise HttpError(resp, content, uri=request.uri)
13321333
response = request.postproc(resp, content)
1333-
except HttpError, e:
1334+
except HttpError as e:
13341335
exception = e
13351336

13361337
if callback is not None:

googleapiclient/mimeparse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- best_match(): Choose the mime-type with the highest quality ('q')
2222
from a list of candidates.
2323
"""
24+
from functools import reduce
2425

2526
__version__ = '0.1.3'
2627
__author__ = 'Joe Gregorio'
@@ -68,7 +69,7 @@ def parse_media_range(range):
6869
necessary.
6970
"""
7071
(type, subtype, params) = parse_mime_type(range)
71-
if not params.has_key('q') or not params['q'] or \
72+
if 'q' not in params or not params['q'] or \
7273
not float(params['q']) or float(params['q']) > 1\
7374
or float(params['q']) < 0:
7475
params['q'] = '1'
@@ -99,7 +100,7 @@ def fitness_and_quality_parsed(mime_type, parsed_ranges):
99100
if type_match and subtype_match:
100101
param_matches = reduce(lambda x, y: x + y, [1 for (key, value) in \
101102
target_params.iteritems() if key != 'q' and \
102-
params.has_key(key) and value == params[key]], 0)
103+
key in params and value == params[key]], 0)
103104
fitness = (type == target_type) and 100 or 0
104105
fitness += (subtype == target_subtype) and 10 or 0
105106
fitness += param_matches

googleapiclient/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
for converting between the wire format and the Python
2020
object representation.
2121
"""
22+
from __future__ import absolute_import
2223

2324
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
2425

@@ -27,7 +28,7 @@
2728
import urllib
2829

2930
from googleapiclient import __version__
30-
from errors import HttpError
31+
from .errors import HttpError
3132

3233

3334
dump_request_response = False

samples-index.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
2929
The rest of the file is ignored when it comes to building the index.
3030
"""
31+
from __future__ import print_function
3132

3233
import httplib2
3334
import itertools
@@ -239,7 +240,7 @@ def main():
239240
</tr>""" % context)
240241
page.append('</table>\n')
241242

242-
print ''.join(page)
243+
print(''.join(page))
243244

244245

245246
if __name__ == '__main__':

samples/adexchangeseller/generate_report.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
Tags: reports.generate
2222
"""
23+
from __future__ import print_function
2324

2425
__author__ = 'sgomes@google.com (Sérgio Gomes)'
2526

@@ -68,14 +69,14 @@ def main(argv):
6869
sys.exit(1)
6970
# Display headers.
7071
for header in result['headers']:
71-
print '%25s' % header['name'],
72-
print
72+
print('%25s' % header['name'], end=' ')
73+
print()
7374

7475
# Display results.
7576
for row in result['rows']:
7677
for column in row:
77-
print '%25s' % column,
78-
print
78+
print('%25s' % column, end=' ')
79+
print()
7980

8081
except client.AccessTokenRefreshError:
8182
print ('The credentials have been revoked or expired, please re-run the '

samples/adexchangeseller/generate_report_with_paging.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
2525
Tags: reports.generate
2626
"""
27+
from __future__ import print_function
2728

2829
__author__ = 'sgomes@google.com (Sérgio Gomes)'
2930

@@ -70,14 +71,14 @@ def main(argv):
7071
# If this is the first page, display the headers.
7172
if start_index == 0:
7273
for header in result['headers']:
73-
print '%25s' % header['name'],
74-
print
74+
print('%25s' % header['name'], end=' ')
75+
print()
7576

7677
# Display results for this page.
7778
for row in result['rows']:
7879
for column in row:
79-
print '%25s' % column,
80-
print
80+
print('%25s' % column, end=' ')
81+
print()
8182

8283
start_index += len(result['rows'])
8384

samples/adexchangeseller/get_all_ad_clients.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
1919
Tags: adclients.list
2020
"""
21+
from __future__ import print_function
2122

2223
__author__ = 'sgomes@google.com (Sérgio Gomes)'
2324

@@ -43,11 +44,11 @@ def main(argv):
4344
result = request.execute()
4445
ad_clients = result['items']
4546
for ad_client in ad_clients:
46-
print ('Ad client for product "%s" with ID "%s" was found. '
47-
% (ad_client['productCode'], ad_client['id']))
47+
print(('Ad client for product "%s" with ID "%s" was found. '
48+
% (ad_client['productCode'], ad_client['id'])))
4849

49-
print ('\tSupports reporting: %s' %
50-
(ad_client['supportsReporting'] and 'Yes' or 'No'))
50+
print(('\tSupports reporting: %s' %
51+
(ad_client['supportsReporting'] and 'Yes' or 'No')))
5152

5253
request = service.adclients().list_next(request, result)
5354

samples/adexchangeseller/get_all_ad_units.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
Tags: adunits.list
2222
"""
23+
from __future__ import print_function
2324

2425
__author__ = 'sgomes@google.com (Sérgio Gomes)'
2526

@@ -54,8 +55,8 @@ def main(argv):
5455
result = request.execute()
5556
ad_units = result['items']
5657
for ad_unit in ad_units:
57-
print ('Ad unit with code "%s", name "%s" and status "%s" was found. ' %
58-
(ad_unit['code'], ad_unit['name'], ad_unit['status']))
58+
print(('Ad unit with code "%s", name "%s" and status "%s" was found. ' %
59+
(ad_unit['code'], ad_unit['name'], ad_unit['status'])))
5960

6061
request = service.adunits().list_next(request, result)
6162

samples/adexchangeseller/get_all_ad_units_for_custom_channel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
Tags: customchannels.adunits.list
2222
"""
23+
from __future__ import print_function
2324

2425
__author__ = 'sgomes@google.com (Sérgio Gomes)'
2526

@@ -59,8 +60,8 @@ def main(argv):
5960
result = request.execute()
6061
ad_units = result['items']
6162
for ad_unit in ad_units:
62-
print ('Ad unit with code "%s", name "%s" and status "%s" was found. ' %
63-
(ad_unit['code'], ad_unit['name'], ad_unit['status']))
63+
print(('Ad unit with code "%s", name "%s" and status "%s" was found. ' %
64+
(ad_unit['code'], ad_unit['name'], ad_unit['status'])))
6465

6566
request = service.adunits().list_next(request, result)
6667

0 commit comments

Comments
 (0)