pydrive Package

apiattr Module

class pydrive.apiattr.ApiAttribute(name)

Bases: object

A data descriptor that sets and returns values.

class pydrive.apiattr.ApiAttributeMixin

Bases: object

Mixin to initialize required global variables to use ApiAttribute.

class pydrive.apiattr.ApiResource(*args, **kwargs)

Bases: dict

Super class of all api resources.

Inherits and behaves as a python dictionary to handle api resources. Save clean copy of metadata in self.metadata as a dictionary. Provides changed metadata elements to efficiently update api resources.

GetChanges()

Returns changed metadata elements to update api resources efficiently.

Returns:dict – changed metadata elements.
UpdateMetadata(metadata=None)

Update metadata and mark all of them to be clean.

class pydrive.apiattr.ApiResourceList(auth=None, metadata=None)

Bases: pydrive.apiattr.ApiAttributeMixin, pydrive.apiattr.ApiResource

Abstract class of all api list resources.

Inherits ApiResource and builds iterator to list any API resource.

GetList()

Get list of API resources.

If ‘maxResults’ is not specified, it will automatically iterate through every resources available. Otherwise, it will make API call once and update ‘pageToken’.

Returns:list – list of API resources.
Reset()

Resets current iteration

_GetList()

Helper function which actually makes API call.

Should be overwritten.

Raises :NotImplementedError
next()

Make API call to list resources and return them.

Auto updates ‘pageToken’ everytime it makes API call and raises StopIteration when it reached the end of iteration.

Returns:list – list of API resources.
Raises :StopIteration

auth Module

exception pydrive.auth.AuthError

Bases: exceptions.Exception

Base error for authentication/authorization errors.

exception pydrive.auth.InvalidCredentialsError

Bases: exceptions.IOError

Error trying to read credentials file.

exception pydrive.auth.AuthenticationRejected

Bases: pydrive.auth.AuthError

User rejected authentication.

exception pydrive.auth.AuthenticationError

Bases: pydrive.auth.AuthError

General authentication error.

exception pydrive.auth.RefreshError

Bases: pydrive.auth.AuthError

Access token refresh error.

static auth.LoadAuth(decoratee)

Decorator to check if the auth is valid and loads auth if not.

static auth.CheckAuth(decoratee)

Decorator to check if it requires OAuth2 flow request.

class pydrive.auth.GoogleAuth(settings_file='settings.yaml')

Bases: pydrive.apiattr.ApiAttributeMixin, object

Wrapper class for oauth2client library in google-api-python-client.

Loads all settings and credentials from one ‘settings.yaml’ file and performs common OAuth2.0 related functionality such as authentication and authorization.

LocalWebserverAuth(host_name='localhost', port_numbers=[8080, 8090])

Authenticate and authorize from user by creating local webserver and retrieving authentication code.

This function is not for webserver application. It creates local webserver for user from standalone application.

Parameters:
  • host_name (str.) – host name of the local webserver.
  • port_numbers (list.) – list of port numbers to be tried to used.
Returns:

str – code returned from local webserver

Raises :

AuthenticationRejected, AuthenticationError

CommandLineAuth()

Authenticate and authorize from user by printing authentication url retrieving authentication code from command-line.

Returns:str – code returned from commandline.
Auth(code)

Authenticate, authorize, and build service.

Parameters:code (str.) – Code for authentication.
Raises :AuthenticationError
Authenticate(code)

Authenticates given authentication code back from user.

Parameters:code (str.) – Code for authentication.
Raises :AuthenticationError
Authorize()

Authorizes and builds service.

Raises :AuthenticationError
GetAuthUrl()

Creates authentication url where user visits to grant access.

Returns:str – Authentication url.
GetFlow()

Gets Flow object from client configuration.

Raises :InvalidConfigError
LoadClientConfig(backend=None)

Loads client configuration according to specified backend.

If you have any specific backend to load client configuration from in mind, don’t use this function and use the corresponding function you want.

Parameters:backend (str.) – backend to load client configuration from.
Raises :InvalidConfigError
LoadClientConfigFile(client_config_file=None)

Loads client configuration file downloaded from APIs console.

Loads client config file from path in settings if not specified.

Parameters:client_config_file (str.) – path of client config file to read.
Raises :InvalidConfigError
LoadClientConfigSettings()

Loads client configuration from settings file.

Raises :InvalidConfigError
LoadCredentials(backend=None)

Loads credentials or create empty credentials if it doesn’t exist.

Parameters:backend (str.) – target backend to save credential to.
Raises :InvalidConfigError
LoadCredentialsFile(credentials_file=None)

Loads credentials or create empty credentials if it doesn’t exist.

Loads credentials file from path in settings if not specified.

Parameters:credentials_file (str.) – path of credentials file to read.
Raises :InvalidConfigError, InvalidCredentialsError
Refresh()

Refreshes the access_token.

Raises :RefreshError
SaveCredentials(backend=None)

Saves credentials according to specified backend.

If you have any specific credentials backend in mind, don’t use this function and use the corresponding function you want.

Parameters:backend (str.) – backend to save credentials.
Raises :InvalidConfigError
SaveCredentialsFile(credentials_file=None)

Saves credentials to the file in JSON format.

Parameters:credentials_file (str.) – destination to save file to.
Raises :InvalidConfigError, InvalidCredentialsError
access_token_expired

Checks if access token doesn’t exist or is expired.

Returns:bool – True if access token doesn’t exist or is expired.

drive Module

class pydrive.drive.GoogleDrive(auth=None)

Bases: pydrive.apiattr.ApiAttributeMixin, object

Main Google Drive class.

CreateFile(metadata=None)

Create an instance of GoogleDriveFile with auth of this instance.

This method would not upload a file to GoogleDrive.

Parameters:metadata (dict.) – file resource to initialize GoogleDriveFile with.
Returns:pydrive.files.GoogleDriveFile – initialized with auth of this instance.
ListFile(param=None)

Create an instance of GoogleDriveFileList with auth of this instance.

This method will not fetch from Files.List().

Parameters:param (dict.) – parameter to be sent to Files.List().
Returns:pydrive.files.GoogleDriveFileList – initialized with auth of this instance.

files Module

exception pydrive.files.FileNotUploadedError

Bases: exceptions.RuntimeError

Error trying to access metadata of file that is not uploaded.

exception pydrive.files.ApiRequestError

Bases: exceptions.IOError

Error while making any API requests.

exception pydrive.files.FileNotDownloadableError

Bases: exceptions.RuntimeError

Error trying to download file that is not downloadable.

static files.LoadMetadata(decoratee)

Decorator to check if the file has metadata and fetches it if not.

Raises :ApiRequestError, FileNotUploadedError
class pydrive.files.GoogleDriveFileList(auth=None, param=None)

Bases: pydrive.apiattr.ApiResourceList

Google Drive FileList instance.

Equivalent to Files.list() in Drive APIs.

_GetList()

Overwritten method which actually makes API call to list files.

Returns:list – list of pydrive.files.GoogleDriveFile.
class pydrive.files.GoogleDriveFile(auth=None, metadata=None, uploaded=False)

Bases: pydrive.apiattr.ApiAttributeMixin, pydrive.apiattr.ApiResource

Google Drive File instance.

Inherits ApiResource which inherits dict. Can access and modify metadata like dictionary.

FetchMetadata()

Download file’s metadata from id using Files.get().

Raises :ApiRequestError, FileNotUploadedError
FetchContent()

Download file’s content from download_url.

Raises :ApiRequestError, FileNotUploadedError, FileNotDownloadableError
GetContentFile(filename)

Save content of this file as a local file.

Parameters:filename (str.) – name of the file to write to.
Raises :ApiRequestError, FileNotUploadedError, FileNotDownloadableError
GetContentString()

Get content of this file as a string.

Returns:str – utf-8 decoded content of the file
Raises :ApiRequestError, FileNotUploadedError, FileNotDownloadableError
SetContentFile(filename)

Set content of this file from a file.

Opens the file specified by this method. Will be read, uploaded, and closed by Upload() method. Sets metadata ‘title’ and ‘mimeType’ automatically if not specified.

Parameters:filename (str.) – name of the file to be uploaded.
SetContentString(content)

Set content of this file to be a string.

Creates io.BytesIO instance of utf-8 encoded string. Sets mimeType to be ‘text/plain’ if not specified.

Parameters:content (str.) – content of the file in string.
Upload(param=None)

Upload/update file by choosing the most efficient method.

Parameters:param (dict.) – additional parameter to upload file.
Raises :ApiRequestError

settings Module

exception pydrive.settings.SettingsError

Bases: exceptions.IOError

Error while loading/saving settings

exception pydrive.settings.InvalidConfigError

Bases: exceptions.IOError

Error trying to read client configuration.

static settings.LoadSettingsFile(filename='settings.yaml')

Loads settings file in yaml format given file name.

Parameters:filename (str.) – path for settings file. ‘settings.yaml’ by default.
Raises :SettingsError
static settings.ValidateSettings(data)

Validates if current settings is valid.

Parameters:data (dict.) – dictionary containing all settings.
Raises :InvalidConfigError

Table Of Contents

Previous topic

File listing made easy

This Page