7. Interaction with Google Drive

This module contains the constants for the gdrive client.

This module provides the core functions necessary to connect to a google drive, create the necessary directorie and upload the files to the drive.

class ptmd.lib.gdrive.core.GoogleDriveConnector

Bases: object

This is the class that handle connection and interaction with the Google Drive.

connect() None

Connect to the Google Drive.

Returns

A connected Google Drive object

create_directories() tuple[dict, dict]

This function will create the nested directories/folders within the Google Drive.

Returns

A tuple containing the ids of the folders and the ids of the files.

delete_file(file_id: str) str

This function will delete the file from the Google Drive.

Parameters

file_id – The file identifier.

download_file(file_id: str | int, filename: str) str

This function will download the file from the Google Drive.

Parameters
  • file_id – The file identifier.

  • filename – The name of file to be downloaded.

get_filename(file_id: str | int) str | None

This function will return the file name.

Parameters

file_id – The file identifier.

google_drive: GoogleDrive
instance_ = None
lock_file(file_id: str) None

Given a file id, this function change the permissions of the file to read-only for ‘anyone’. Admin keeps the permission to read and write.

Parameters

file_id – The file identifier.

refresh_connection()

This function will refresh the connection to the Google Drive when the token has expired.

update_file(file_id: str, file_path: str, title: str) str

This function will update the file in the Google Drive.

Parameters
  • file_id – The Google Drive file identifier.

  • file_path – The path to the file to be uploaded.

  • title – The title of the file to be uploaded.

upload_file(directory_id: str, file_path: str, title: str = 'SAMPLE_TEST') dict[str, str] | None

This function will upload the file to the Google Drive.

Parameters
  • directory_id – The partner organisation Google Drive folder identifier.

  • file_path – The path to the file to be uploaded.

  • title – The title of the file to be uploaded.

A small utitlise to check if a given directory already exists in the drive.

ptmd.lib.gdrive.utils.find_files_in_folder(google_drive: GoogleDrive, folder_id: str) list | None

Finds all the files in the given directory and return their id and title.

Parameters
  • google_drive – The GoogleDrive object.

  • folder_id – The directory to search in.

Returns

True if the file exists, False otherwise.

ptmd.lib.gdrive.utils.get_file_information(google_drive: GoogleDrive, folder_id: str, filename: str) dict | None

Finds the file in the given directory and return its information.

Parameters
  • google_drive – The GoogleDrive object.

  • folder_id – The directory to search in.

  • filename – The name of the file to search for.

Returns

The data of the file if it exists, None otherwise.

ptmd.lib.gdrive.utils.get_folder_id(google_drive: GoogleDrive, folder_name: str, parent: str = 'root') str | None

Finds if the directory already exists in the drive and in the given directory.

Parameters
  • google_drive – The GoogleDrive object.

  • parent – The directory to search in.

  • folder_name – The name of the directory to search for.

Returns

True if the directory exists, False otherwise.