autodrive.connection

Module Contents

Classes

FileUpload

Use FileUploads when you need more complicated file upload instructions.

DriveConnection

Provides a connection to the Google Drive api, and methods to send requests

SheetsConnection

Provides a connection to the Google Sheets api, and methods to send requests

class autodrive.connection.FileUpload(path, to_folder_id=None, convert=False, name_override=None)

Use FileUploads when you need more complicated file upload instructions.

Parameters
  • path (Path, str) – The pathlike to the file.

  • to_folder_id (str, optional) – A folder id, if you want to upload this file to a specific folder in Google Drive. Defaults to None.

  • convert (bool, optional) – Set to True if you want to convert the file to a Google Drive format. The format will be automatically determined based on Google Drive’s rules for conversion. Defaults to False.

  • name_override (str | None) –

property do_conv: bool
Return type

bool

class autodrive.connection.DriveConnection(*, auth_config=None, api_version='v3')

Provides a connection to the Google Drive api, and methods to send requests to it.

This class is documented primarily for reference, if you want to connect to Google Drive, it’s easier to just use a Drive instance.

Parameters
  • auth_config (AuthConfig, optional) – Optional custom AuthConfig object, defaults to None.

  • api_version (str, optional) – The version of the Drive api to connect to, defaults to “v3”.

get_import_formats()
Returns

The mapping between standard MIMEtypes and the

MIMEtypes for Google Drive’s types (Docs, Sheets, etc) as provided by the Google Drive API.

Return type

Dict[str, str]

find_object(obj_name, obj_type=None, shared_drive_id=None)

Searches for a Google Drive Object via the connected api.

Parameters
  • obj_name (str) – The name of the object, or part of its name.

  • obj_type (Literal["sheet", "folder", "file"], optional) – The type of object to restrict the search to.

  • shared_drive_id (str, optional) – The id of a Shared Drive to search within, if desired, defaults to None.

Returns

A list of object properties, if any matches are found.

Return type

List[Dict[str, Any]]

create_object(obj_name, obj_type, parent_id=None)

Creates a file or folder via the Google Drive connection.

Parameters
  • obj_name (str) – The desired name of the object to create.

  • obj_type (Literal["sheet", "folder"]) – The type of object to create.

  • parent_id (str, optional) – The id of the folder or shared drive to create the object within, defaults to None.

Returns

The new id of the created object.

Return type

str

delete_object(object_id)

Deletes the passed Google object id from the connected Google Drive.

Parameters

object_id (str) – A Google object id.

Return type

None

upload_files(*filepaths)

Uploads files to the root drive or to a folder.

Note

This method will cause a request to be posted to the relevant Google API immediately.

Parameters

*filepaths (Path | str | FileUpload) – An arbitrary number of Path objects, path strings, or FileUpload objects (for more complex cases).

Returns

The names of the uploaded files and their new ids in

Google Drive.

Return type

Dict[str, str]

detect_conv_format(p)

Detects the MIMEtype for the passed filepath and determines its corresponding Google Drive format.

Parameters

p (Path) – Any filepath.

Raises
  • ValueError – If the MIMEtype cannot be determined from the path at all.

  • ValueError – If the MIMEtype cannot be converted to a Google Drive format MIMEtype.

Returns

The Google Drive format MIMEtype for the file.

Return type

str

class autodrive.connection.SheetsConnection(*, auth_config=None, api_version='v4')

Provides a connection to the Google Sheets api, and methods to send requests to it.

This class is documented primarily for reference, if you want to connect to Google Sheets, it’s easier to just use a View.

Parameters
  • auth_config (AuthConfig, optional) – Optional custom AuthConfig object, defaults to None.

  • api_version (str, optional) – The version of the Sheets api to connect to, defaults to “v4”.

execute_requests(spreadsheet_id, requests)

Sends the passed list of request dictionaries to the Sheets api to be applied to the spreadsheet_id via batch update.

Parameters
  • spreadsheet_id (str) – The id of the Google Sheet to update.

  • requests (List[Dict[str, Any]]) – A list of dictionaries formatted as requests.

Returns

The resulting response from the Sheets api as a dictionary.

Return type

Dict[str, Any]

get_properties(spreadsheet_id)

Gets the metadata properties of the indicated Google Sheet.

Parameters

spreadsheet_id (str) – The id of the Google Sheet to collect properties from.

Returns

A dictionary of the Google Sheet’s properties.

Return type

Dict[str, Any]

get_data(spreadsheet_id, ranges=None)

Collects data from cells in the passed spreadsheet.

Parameters
  • spreadsheet_id (str) – The id of the Google Sheet to collect data from.

  • ranges (List[str], optional) – A list of range strings (e.g. Sheet1!A1:C3), defaults to None, which prompts get_data to fetch all data from all cells.

Returns

The collected data from the spreadsheet, raw and unparsed.

Return type

Dict[str, Any]