sadrive.helpers.drive module#

Provides Google Drive client wrapper using service accounts for authentication.

Includes: - PatchedDrive subclass for typing - SADrive class for Drive operations - Authentication - File listing, creation, upload, rename, delete - Sharing/unsharing - Search and space usage - Bulk deletion

Constants: - PARENT_ID: Root folder ID from config

class sadrive.helpers.drive.PatchedDrive(auth=None)[source]#

Bases: GoogleDrive

Subclass of GoogleDrive with explicit auth attribute for type checking.

auth#

GoogleAuth instance used for authentication.

Type:

pydrive2.auth.GoogleAuth

auth: GoogleAuth#
class sadrive.helpers.drive.SADrive(service_account_num: str)[source]#

Bases: object

Service-account-driven Google Drive client.

Uses pydrive2 and googleapiclient to perform common operations.

authorise() PatchedDrive[source]#

Authenticates using a service account JSON.

Uses pydrive2.GoogleAuth with service backend settings.

Returns:

An authenticated PatchedDrive instance.

create_folder(subfolder_name: str, parent_folder_id: str = 'root') str[source]#

Creates a new folder in Drive.

Parameters:
  • subfolder_name – Name for the new folder.

  • parent_folder_id – ID of the parent folder (default ‘root’).

Returns:

The ID of the created folder.

delete_all_files()[source]#

Permanently deletes all non-trashed files owned by the account.

Iterates pages of up to 1000 items, printing progress to stdout.

delete_file(file_id: str)[source]#

Deletes a file in Drive.

Parameters:

file_id – ID of the file to remove.

list_files(parent_id: str = 'root')[source]#

Lists non-trashed files under a given folder.

Parameters:

parent_id – Drive folder ID (default: ‘root’).

Returns:

List of GoogleDriveFile instances.

rename(fileid: str, new_name: str)[source]#

Renames an existing Drive file.

Parameters:
  • fileid – ID of the file to rename.

  • new_name – New title for the file.

Returns:

The updated GoogleDriveFile instance.

search(name: str)[source]#

Searches for files whose titles contain a substring.

Parameters:

name – Substring to match in file titles.

Returns:

List of dict representations of matched files.

share(fileid: str)[source]#

Publishes a file by granting ‘reader’ permission to anyone.

Parameters:

fileid – ID of the file to share.

Returns:

The file’s alternateLink.

unshare(file_id: str)[source]#

Revokes ‘anyone’ permission from a file.

Parameters:

file_id – ID of the file to unshare.

upload_file(filename: str, parent_folder_id: str, stream_bytes: IOBase)[source]#

Uploads a file stream to Drive with resumable media.

Parameters:
  • filename – Name to assign in Drive.

  • parent_folder_id – ID of the destination folder.

  • stream_bytes – File-like object providing binary data.

Yields:

Progress percentage integers until upload completes.

Returns:

The upload response dict from the Drive API.

used_space()[source]#

Retrieves the total bytes used by the authenticated account.

Returns:

Bytes used as an integer.