8000 Merge pull request #212 from dhermes/six-raise-from · sktt/google-auth-library-python@01efcd4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 01efcd4

Browse files
authored
Merge pull request googleapis#212 from dhermes/six-raise-from
Using `six.raise_from` wherever possible.
2 parents b7b48f1 + 0a93e87 commit 01efcd4

File tree

10 files changed

+86
-46
lines changed

10 files changed

+86
-46
lines changed

google/auth/_default.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import logging
2323
import os
2424

25+
import six
26+
2527
from google.auth import environment_vars
2628
from google.auth import exceptions
2729
import google.auth.transport._http_client
@@ -67,9 +69,11 @@ def _load_credentials_from_file(filename):
6769
with io.open(filename, 'r') as file_obj:
6870
try:
6971
info = json.load(file_obj)
70-
except ValueError as exc:
71-
raise exceptions.DefaultCredentialsError(
72-
'File {} is not a valid json file.'.format(filename), exc)
72+
except ValueError as caught_exc:
73+
new_exc = exceptions.DefaultCredentialsError(
74+
'File {} is not a valid json file.'.format(filename),
75+
caught_exc)
76+
six.raise_from(new_exc, caught_exc)
7377

7478
# The type key should indicate that the file is either a service account
7579
# credentials file or an authorized user credentials file.
@@ -80,10 +84,11 @@ def _load_credentials_from_file(filename):
8084

8185
try:
8286
credentials = _cloud_sdk.load_authorized_user_credentials(info)
83-
except ValueError as exc:
84-
raise exceptions.DefaultCredentialsError(
85-
'Failed to load authorized user credentials from {}'.format(
86-
filename), exc)
87+
except ValueError as caught_exc:
88+
msg = 'Failed to load authorized user credentials from {}'.format(
89+
filename)
90+
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
91+
six.raise_from(new_exc, caught_exc)
8792
# Authorized user credentials do not contain the project ID.
8893
return credentials, None
8994

@@ -93,10 +98,11 @@ def _load_credentials_from_file(filename):
9398
try:
9499
credentials = (
95100
service_account.Credentials.from_service_account_info(info))
96-
except ValueError as exc:
97-
raise exceptions.DefaultCredentialsError(
98-
'Failed to load service account credentials from {}'.format(
99-
filename), exc)
101+
except ValueError as caught_exc:
102+
msg = 'Failed to load service account credentials from {}'.format(
103+
filename)
104+
new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)
105+
six.raise_from(new_exc, caught_exc)
100106
return credentials, info.get('project_id')
101107

102108
else:

google/auth/_oauth2client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
from __future__ import absolute_import
2323

24+
import six
25+
2426
from google.auth import _helpers
2527
import google.auth.app_engine
2628
import google.oauth2.credentials
@@ -30,8 +32,9 @@
3032
import oauth2client.client
3133
import oauth2client.contrib.gce
3234
import oauth2client.service_account
33-
except ImportError:
34-
raise ImportError('oauth2client is not installed.')
35+
except ImportError as caught_exc:
36+
six.raise_from(
37+
ImportError('oauth2client is not installed.'), caught_exc)
3538

3639
try:
3740
import oauth2client.contrib.appengine
@@ -162,5 +165,6 @@ def convert(credentials):
162165

163166
try:
164167
return _CLASS_CONVERSION_MAP[credentials_class](credentials)
165-
except KeyError:
166-
raise ValueError(_CONVERT_ERROR_TMPL.format(credentials_class))
168+
except KeyError as caught_exc:
169+
new_exc = ValueError(_CONVERT_ERROR_TMPL.format(credentials_class))
170+
six.raise_from(new_exc, caught_exc)

google/auth/compute_engine/_metadata.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import logging
2323
import os
2424

25+
import six
2526
from six.moves import http_client
2627
from six.moves.urllib import parse as urlparse
2728

@@ -118,10 +119,11 @@ def get(request, path, root=_METADATA_ROOT, recursive=False):
118119
if response.headers['content-type'] == 'application/json':
119120
try:
120121
return json.loads(content)
121-
except ValueError:
122-
raise exceptions.TransportError(
122+
except ValueError as caught_exc:
123+
new_exc = exceptions.TransportError(
123124
'Received invalid JSON from the Google Compute Engine'
124125
'metadata service: {:.20}'.format(content))
126+
six.raise_from(new_exc, caught_exc)
125127
else:
126128
return content
127129
else:

google/auth/compute_engine/credentials.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
2020
"""
2121

22+
import six
23+
2224
from google.auth import credentials
2325
from google.auth import exceptions
2426
from google.auth.compute_engine import _metadata
@@ -89,8 +91,9 @@ def refresh(self, request):
8991
self.token, self.expiry = _metadata.get_service_account_token(
9092
request,
9193
service_account=self._service_account_email)
92-
except exceptions.TransportError as exc:
93-
raise exceptions.RefreshError(exc)
94+
except exceptions.TransportError as caught_exc:
95+
new_exc = exceptions.RefreshError(caught_exc)
96+
six.raise_from(new_exc, caught_exc)
9497

9598
@property
9699
def service_account_email(self):

google/auth/jwt.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import json
4848

4949
import cachetools
50+
import six
5051
from six.moves import urllib
5152

5253
from google.auth import _helpers
@@ -101,8 +102,9 @@ def _decode_jwt_segment(encoded_section):
101102
section_bytes = _helpers.padded_urlsafe_b64decode(encoded_section)
102103
try:
103104
return json.loads(section_bytes.decode('utf-8'))
104-
except ValueError:
105-
raise ValueError('Can\'t parse segment: {0}'.format(section_bytes))
105+
except ValueError as caught_exc:
106+
new_exc = ValueError('Can\'t parse segment: {0}'.format(section_bytes))
107+
six.raise_from(new_exc, caught_exc)
106108

107109

108110
def _unverified_decode(token):

google/auth/transport/_http_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818
import socket
1919

20+
import six
2021
from six.moves import http_client
2122
from six.moves import urllib
2223

@@ -104,8 +105,9 @@ def __call__(self, url, method='GET', body=None, headers=None,
104105
response = connection.getresponse()
105106
return Response(response)
106107

107-
E377 except (http_client.HTTPException, socket.error) as exc:
108-
raise exceptions.TransportError(exc)
108+
except (http_client.HTTPException, socket.error) as caught_exc:
109+
new_exc = exceptions.TransportError(caught_exc)
110+
six.raise_from(new_exc, caught_exc)
109111

110112
finally:
111113
connection.close()

google/auth/transport/grpc.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
from __future__ import absolute_import
1818

19+
import six
1920
try:
2021
import grpc
21-
except ImportError: # pragma: NO COVER
22-
raise ImportError(
23-
'gRPC is not installed, please install the grpcio package to use the '
24-
'gRPC transport.')
25-
import six
22+
except ImportError as caught_exc: # pragma: NO COVER
23+
six.raise_from(
24+
ImportError(
25+
'gRPC is not installed, please install the grpcio package '
26+
'to use the gRPC transport.'
27+
),
28+
caught_exc,
29+
)
2630

2731

2832
class AuthMetadataPlugin(grpc.AuthMetadataPlugin):

google/auth/transport/requests.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@
2020

2121
try:
2222
import requests
23-
except ImportError: # pragma: NO COVER
24-
raise ImportError(
25-
'The requests library is not installed, please install the requests '
26-
'package to use the requests transport.')
27-
import requests.exceptions< F438 /span>
23+
except ImportError as caught_exc: # pragma: NO COVER
24+
import six
25+
six.raise_from(
26+
ImportError(
27+
'The requests library is not installed, please install the '
28+
'requests package to use the requests transport.'
29+
),
30+
caught_exc,
31+
)
32+
import requests.exceptions # pylint: disable=ungrouped-imports
33+
import six # pylint: disable=ungrouped-imports
2834

2935
from google.auth import exceptions
3036
from google.auth import transport
@@ -111,8 +117,9 @@ def __call__(self, url, method='GET', body=None, headers=None,
111117
method, url, data=body, headers=headers, timeout=timeout,
112118
**kwargs)
113119
return _Response(response)
114-
except requests.exceptions.RequestException as exc:
115-
raise exceptions.TransportError(exc)
120+
except requests.exceptions.RequestException as caught_exc:
121+
new_exc = exceptions.TransportError(caught_exc)
122+
six.raise_from(new_exc, caught_exc)
116123

117124

118125
class AuthorizedSession(requests.Session):

google/auth/transport/urllib3.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@
3232

3333
try:
3434
import urllib3
35-
except ImportError: # pragma: NO COVER
36-
raise ImportError(
37-
'The urllib3 library is not installed, please install the urllib3 '
38-
'package to use the urllib3 transport.')
39-
import urllib3.exceptions
35+
except ImportError as caught_exc: # pragma: NO COVER
36+
import six
37+
six.raise_from(
38+
ImportError(
39+
'The urllib3 library is not installed, please install the '
40+
'urllib3 package to use the urllib3 transport.'
41+
),
42+
caught_exc,
43+
)
44+
import six
45+
import urllib3.exceptions # pylint: disable=ungrouped-imports
4046

4147
from google.auth import exceptions
4248
from google.auth import transport
@@ -126,8 +132,9 @@ def __call__(self, url, method='GET', body=None, headers=None,
126132
response = self.http.request(
127133
method, url, body=body, headers=headers, **kwargs)
128134
return _Response(response)
129-
except urllib3.exceptions.HTTPError as exc:
130-
raise exceptions.TransportError(exc)
135+
except urllib3.exceptions.HTTPError as caught_exc:
136+
new_exc = exceptions.TransportError(caught_exc)
137+
six.raise_from(new_exc, caught_exc)
131138

132139

133140
def _make_default_http():

google/oauth2/_client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import datetime
2727
import json
2828

29+
import six
2930
from six.moves import http_client
3031
from six.moves import urllib
3132

@@ -144,9 +145,10 @@ def jwt_grant(request, token_uri, assertion):
144145

145146
try:
146147
access_token = response_data['access_token']
147-
except KeyError:
148-
raise exceptions.RefreshError(
148+
except KeyError as caught_exc:
149+
new_exc = exceptions.RefreshError(
149150
'No access token in response.', response_data)
151+
six.raise_from(new_exc, caught_exc)
150152

151153
expiry = _parse_expiry(response_data)
152154

@@ -190,9 +192,10 @@ def refresh_grant(request, token_uri, refresh_token, client_id, client_secret):
190192

191193
try:
192194
access_token = response_data['access_token']
193-
except KeyError:
194-
raise exceptions.RefreshError(
195+
except KeyError as caught_exc:
196+
new_exc = exceptions.RefreshError(
195197
'No access token in response.', response_data)
198+
six.raise_from(new_exc, caught_exc)
196199

197200
refresh_token = response_data.get('refresh_token', refresh_token)
198201
expiry = _parse_expiry(response_data)

0 commit comments

Comments
 (0)
0