fixed error handling for python 2.7+

This commit is contained in:
Rob Weber
2019-08-19 15:23:48 -05:00
parent d0b1d6bb34
commit 4513eb67f9
6 changed files with 18 additions and 36 deletions

View File

@ -156,7 +156,7 @@ class GoogleAuth(ApiAttributeMixin, object):
port_number = port
try:
httpd = ClientRedirectServer((host_name, port), ClientRedirectHandler)
except socket.error, e:
except socket.error as e:
pass
else:
success = True
@ -164,26 +164,16 @@ class GoogleAuth(ApiAttributeMixin, object):
if success:
oauth_callback = 'http://%s:%s/' % (host_name, port_number)
else:
print 'Failed to start a local webserver. Please check your firewall'
print 'settings and locally running programs that may be blocking or'
print 'using configured ports. Default ports are 8080 and 8090.'
raise AuthenticationError()
self.flow.redirect_uri = oauth_callback
authorize_url = self.GetAuthUrl()
webbrowser.open(authorize_url, new=1, autoraise=True)
print 'Your browser has been opened to visit:'
print
print ' ' + authorize_url
print
httpd.handle_request()
if 'error' in httpd.query_params:
print 'Authentication request was rejected'
raise AuthenticationRejected('User rejected authentication')
if 'code' in httpd.query_params:
return httpd.query_params['code']
else:
print 'Failed to find "code" in the query parameters of the redirect.'
print 'Try command-line authentication'
raise AuthenticationError('No code found in redirect')
@CheckAuth
@ -195,10 +185,6 @@ class GoogleAuth(ApiAttributeMixin, object):
"""
self.flow.redirect_uri = OOB_CALLBACK_URN
authorize_url = self.GetAuthUrl()
print 'Go to the following link in your browser:'
print
print ' ' + authorize_url
print
return raw_input('Enter verification code: ').strip()
def LoadCredentials(self, backend=None):
@ -309,7 +295,7 @@ class GoogleAuth(ApiAttributeMixin, object):
client_config_file = self.settings['client_config_file']
try:
client_type, client_info = clientsecrets.loadfile(client_config_file)
except clientsecrets.InvalidClientSecretsError, error:
except clientsecrets.InvalidClientSecretsError as error:
raise InvalidConfigError('Invalid client secrets file %s' % error)
if not client_type in (clientsecrets.TYPE_WEB,
clientsecrets.TYPE_INSTALLED):
@ -334,7 +320,6 @@ class GoogleAuth(ApiAttributeMixin, object):
self.client_config[config] = self.settings['client_config'][config]
except KeyError:
print config
raise InvalidConfigError('Insufficient client config in settings')
def GetFlow(self):
@ -374,7 +359,7 @@ class GoogleAuth(ApiAttributeMixin, object):
self.http = httplib2.Http()
try:
self.credentials.refresh(self.http)
except AccessTokenRefreshError, error:
except AccessTokenRefreshError as error:
raise RefreshError('Access token refresh failed: %s' % error)
def GetAuthUrl(self, keys = None):
@ -414,9 +399,8 @@ class GoogleAuth(ApiAttributeMixin, object):
self.GetFlow()
try:
self.credentials = self.flow.step2_exchange(code)
except FlowExchangeError, e:
except FlowExchangeError as e:
raise AuthenticationError('OAuth2 code exchange failed: %s' % e)
print 'Authentication successful.'
def Authorize(self):
"""Authorizes and builds service.

View File

@ -108,7 +108,7 @@ class GoogleDriveFile(ApiAttributeMixin, ApiResource):
"""
try:
return dict.__getitem__(self, key)
except KeyError, e:
except KeyError as e:
if self.uploaded:
raise KeyError(e)
if self.get('id'):
@ -180,7 +180,7 @@ class GoogleDriveFile(ApiAttributeMixin, ApiResource):
if file_id:
try:
metadata = self.auth.service.files().get(fileId=file_id).execute()
except errors.HttpError, error:
except errors.HttpError as error:
raise ApiRequestError(error)
else:
self.uploaded = True
@ -244,7 +244,7 @@ class GoogleDriveFile(ApiAttributeMixin, ApiResource):
if self.dirty['content']:
param['media_body'] = self._BuildMediaBody()
metadata = self.auth.service.files().insert(**param).execute()
except errors.HttpError, error:
except errors.HttpError as error:
raise ApiRequestError(error)
else:
self.uploaded = True
@ -268,7 +268,7 @@ class GoogleDriveFile(ApiAttributeMixin, ApiResource):
if self.dirty['content']:
param['media_body'] = self._BuildMediaBody()
metadata = self.auth.service.files().update(**param).execute()
except errors.HttpError, error:
except errors.HttpError as error:
raise ApiRequestError(error)
else:
self.uploaded = True
@ -290,7 +290,7 @@ class GoogleDriveFile(ApiAttributeMixin, ApiResource):
param['fileId'] = self.metadata.get('id')
try:
metadata = self.auth.service.files().patch(**param).execute()
except errors.HttpError, error:
except errors.HttpError as error:
raise ApiRequestError(error)
else:
self.UpdateMetadata(metadata)

View File

@ -120,7 +120,7 @@ def LoadSettingsFile(filename=SETTINGS_FILE):
try:
stream = file(filename, 'r')
data = load(stream, Loader=Loader)
except (YAMLError, IOError), e:
except (YAMLError, IOError) as e:
print e
raise SettingsError(e)
return data