def finish_connection(self, url): params = parse_url(url) try: # get auth_token auth_token = params["code"] assert self.csrf_token == params["state"] except AssertionError: # csrf mismatch or csrf not found raise exceptions.AuthFailure(self) except KeyError: try: error_code = params["error"] except KeyError: raise exceptions.ProviderOperationFailure(self) if error_code == "invalid_request" or error_code == "unsupported_response_type": raise exceptions.ProviderOperationFailure(self) elif error_code == "access_denied" or error_code == "server_error": raise exceptions.AuthFailure(self) elif error_code == "temporarily_unavailable": raise exceptions.ConnectionFailure(self) else: raise exceptions.ProviderOperationFailure(self) credentials = {} with self.exception_handler(): credentials["access_token"], credentials["refresh_token"] = self.oauth.authenticate(auth_token) self._connect(credentials)
def exception_handler(self): try: yield except BoxOAuthException: raise exceptions.AuthFailure(self) except BoxAPIException: raise exceptions.ProviderOperationFailure(self) except ReadTimeout: raise exceptions.ConnectionFailure(self) except Exception: raise exceptions.ProviderOperationFailure(self) finally: self._persist_tokens()
def exception_handler(self): error_map = { ErrorCode.AccessDenied: exceptions.AuthFailure, ErrorCode.ActivityLimitReached: exceptions.ConnectionFailure, ErrorCode.GeneralException: exceptions.ProviderOperationFailure, ErrorCode.InvalidRange: exceptions.ProviderOperationFailure, ErrorCode.InvalidRequest: exceptions.ProviderOperationFailure, ErrorCode.ItemNotFound: exceptions.ProviderOperationFailure, ErrorCode.Malformed: exceptions.ProviderOperationFailure, ErrorCode.MalwareDetected: exceptions.ProviderOperationFailure, ErrorCode.NameAlreadyExists: exceptions.ProviderOperationFailure, ErrorCode.NotAllowed: exceptions.ProviderOperationFailure, ErrorCode.NotSupported: exceptions.ProviderOperationFailure, ErrorCode.QuotaLimitReached: exceptions.ConnectionFailure, ErrorCode.ResourceModified: exceptions.ProviderOperationFailure, ErrorCode.ResyncRequired: exceptions.ProviderOperationFailure, ErrorCode.ServiceNotAvailable: exceptions.ConnectionFailure, ErrorCode.Unauthenticated: exceptions.AuthFailure } try: yield except OneDriveError as e: logger.error("OneDriveError in OneDriveProvider: %s", e) raise error_map.get(e.message.split()[0], exceptions.ProviderOperationFailure)(self) except Exception as e: logger.error("General error in OneDriveProvider: %s", e) raise exceptions.ProviderOperationFailure(self)
def exception_handler(self): try: yield except requests.ConnectionError: raise exceptions.ConnectionFailure(self) except: raise exceptions.ProviderOperationFailure(self)
def exception_handler(self): try: yield except exceptions.ProviderFailure: raise except HttpError as e: logger.error("HttpError in GoogleDriveProvider: %s", e) if e.resp.status in [401, 403]: raise exceptions.AuthFailure(self) raise exceptions.ProviderOperationFailure(self) except httplib2.ServerNotFoundError as e: logger.error("ServerNotFoundError in GoogleDriveProvider: %s", e) raise exceptions.ConnectionFailure(self) except Exception as e: logger.error("General error in GoogleDriveProvider: %s", e) raise exceptions.ProviderOperationFailure(self)
def wipe(self): translated_root_dir = self._get_translated_filepath("") try: shutil.rmtree(translated_root_dir) os.makedirs(translated_root_dir, DIRECTORY_MODE) except (IOError, OSError): raise exceptions.ProviderOperationFailure(self)
def put(self, filename, data): translated_filepath = self._get_translated_filepath(filename) try: with open(translated_filepath, mode="wb") as target_file: target_file.write(data) except (IOError, OSError): raise exceptions.ProviderOperationFailure(self)
def get(self, filename): translated_filepath = self._get_translated_filepath(filename) try: with open(translated_filepath, mode="rb") as target_file: return target_file.read() except (IOError, OSError): raise exceptions.ProviderOperationFailure(self)
def delete(self, filename): with self.exception_handler(): file_id = self._get_id(filename) if file_id is None: logger.error( "Error in GoogleDriveProvider delete: no file with name %s", filename) raise exceptions.ProviderOperationFailure(self) self.service.files().delete(fileId=file_id).execute()
def _get_id(self, filename): query = "name = '%s' and mimeType = 'text/plain' and trashed = false" % filename files = self.service.files().list( q=query, spaces=self.ROOT_DIR).execute()["files"] if len(files) == 0: logger.error("Error in GoogleDriveProvider: no file with id %s", filename) raise exceptions.ProviderOperationFailure(self) # assume that there is only one file with given name and type return files[0]['id']
def exception_handler(self): try: yield except (dropbox.oauth.NotApprovedException, dropbox.oauth.BadStateException, dropbox.oauth.CsrfException, dropbox.oauth.BadRequestException): raise exceptions.AuthFailure(self) except dropbox.oauth.ProviderException: raise exceptions.ProviderOperationFailure(self) except urllib3.exceptions.MaxRetryError: raise exceptions.ConnectionFailure(self) except dropbox.rest.ErrorResponse as e: if e.status in [401, 400]: raise exceptions.AuthFailure(self) raise exceptions.ProviderOperationFailure(self) except Exception: raise exceptions.ProviderOperationFailure(self)
def exception_handler(self, check_failing=True): print "done" self.state_timer -= 1 if self.state_timer == 0: self.state = TestProviderState.ACTIVE if self.state == TestProviderState.OFFLINE: raise exceptions.ConnectionFailure(self) if self.state == TestProviderState.UNAUTHENTICATED: raise exceptions.AuthFailure(self) if check_failing and self.state == TestProviderState.FAILING: raise exceptions.ProviderOperationFailure(self) yield
def delete(self, filename): translated_filepath = self._get_translated_filepath(filename) try: os.remove(translated_filepath) except (IOError, OSError): raise exceptions.ProviderOperationFailure(self)