Google Drive

DriveUtility

class gwrappy.drive.DriveUtility(json_credentials_path, client_id, **kwargs)[source]

Initializes object for interacting with Bigquery API.

Parameters:
  • client_secret_path – File path for client secret JSON file. Only required if credentials are invalid or unavailable.
  • json_credentials_path – File path for automatically generated credentials.
  • client_id – Credentials are stored as a key-value pair per client_id to facilitate multiple clients using the same credentials file. For simplicity, using one’s email address is sufficient.
  • max_retries (integer) – Argument specified with each API call to natively handle retryable errors.
  • chunksize (integer) – Upload/Download chunk size
get_account_info(fields=None)[source]

Abstraction of about().get() method. [https://developers.google.com/drive/v3/reference/about/get]

Parameters:fields (list or ", " delimited string) – Available properties can be found here: https://developers.google.com/drive/v3/reference/about
Returns:Dictionary object representation of About resource.
list_files(max_results=None, **kwargs)[source]

Abstraction of files().list() method with inbuilt iteration functionality. [https://developers.google.com/drive/v3/reference/files/list]

Parameters:
Returns:

List of dictionary objects representing file resources.

get_file(file_id, fields=None)[source]

Get file metadata.

Parameters:
Returns:

Dictionary object representing file resource.

download_file(file_id, write_path, page_num=None, output_type=None)[source]

Downloads object.

Parameters:
  • file_id (string) – Unique file id. Check on UI or by list_files().
  • write_path (string) – Local path to write object to.
  • page_num (integer) – Only applicable to Google Sheets. Check gid param in URL.
  • output_type (string. 'list' or 'dataframe') – Only applicable to Google Sheets. Can be directly downloaded as list or Pandas dataframe.
Returns:

If Google Sheet and output_type specified: result in selected type, DriveResponse object. Else DriveResponse object.

Raises:

HttpError if non-retryable errors are encountered.

upload_file(read_path, overwrite_existing=True, **kwargs)[source]

Creates file if it doesn’t exist, updates if it does.

Parameters:
  • read_path (string) – Local path of object to upload.
  • overwrite_existing (boolean) – Safety flag, would raise ValueError if object exists and overwrite_existing=False
  • kwargs – Key-Value pairs of Request Body params. Reference here: https://developers.google.com/drive/v3/reference/files
Returns:

DriveResponse object.

Misc Classes/Functions

class gwrappy.drive.utils.DriveResponse(description)[source]

Wrapper for Drive upload and download responses, mainly for calculating/parsing job statistics into human readable formats for logging.

Parameters:description – String descriptor for specific function of job.
load_resp(resp, is_download=False)[source]

Loads json response from API.

Parameters:
  • resp (dictionary) – Response from API
  • is_download (boolean) – Calculates time taken based on ‘modifiedTime’ field in response if upload, and based on stop time if download