Skip to content

radiens_data_catalog.drive

Google Drive API interactions: scanning, file download and upload planning.

Each xdat recording is three files sharing a base_name:

{base_name}_data.xdat
{base_name}.xdat.json
{base_name}_timestamp.xdat
CLASS DESCRIPTION
DriveItem

A single item returned by the Drive API files.list endpoint.

DriveItemEntry

One non-recording Drive item stored in the catalog.

DriveService

The part of the Drive API v3 service this package calls.

RecordingEntry

One xdat recording dataset stored in the catalog.

UploadPlan

What an upload of one local folder will do, worked out before anything is sent.

FUNCTION DESCRIPTION
download_item

Download a Drive item (file or folder) mirroring the Drive path locally.

download_meta

Download only a recording's .xdat.json metadata file to dest_path.

download_recording

Download the xdat files for a recording into a path-mirrored local directory.

local_md5

Return the hex MD5 of a local file, the digest Drive reports as md5Checksum.

plan_upload

Work out how to upload local_dir to {parent_drive_path}/{local_dir.name} without changing Drive.

scan_drive

Recursively scan Drive and return xdat recordings and all other items.

scan_drive_flat

Scan Drive without traversing from a root folder.

xdat_recording_exists_locally

Return True if all xdat files for a recording exist under local_dir.

Classes

DriveItem

Bases: TypedDict

A single item returned by the Drive API files.list endpoint.

DriveItemEntry

Bases: TypedDict

One non-recording Drive item stored in the catalog.

Represents a single file or folder found on Drive that is not part of an xdat recording — for example a logs/ directory, a config file, or a writeup. No structural or semantic classification is applied; the consumer decides what these items mean.

ATTRIBUTE DESCRIPTION
name

The file or folder name as it appears on Drive (e.g. "logs").

TYPE: str

is_folder

True if this item is a Drive folder.

TYPE: bool

drive_path

Slash-joined path from the root folder to the parent folder of this item. Together with name, this uniquely identifies an item.

TYPE: str

drive_id

Google Drive ID of this file or folder.

TYPE: str

mime_type

MIME type as reported by the Drive API.

TYPE: str

local_path

Absolute local path to the downloaded file or folder, or None if not yet downloaded.

TYPE: str | None

upload_time

ISO 8601 createdTime from the Drive API, or None.

TYPE: str | None

size

Size in bytes as reported by Drive, or None for folders (Drive doesn't report a size for them) and for files Drive has no size for (e.g. Google Workspace-native Docs/Sheets/Slides).

TYPE: int | None

DriveService

Bases: Protocol

The part of the Drive API v3 service this package calls.

METHOD DESCRIPTION
files

Return the files resource.

Functions

files
files() -> _DriveFilesResource

Return the files resource.

RecordingEntry

Bases: TypedDict

One xdat recording dataset stored in the catalog.

Represents a single neural recording. The three xdat files (_data.xdat, .xdat.json, _timestamp.xdat) share a common base_name stem and are treated as a single unit.

ATTRIBUTE DESCRIPTION
base_name

Shared filename stem across all three xdat files. Note: base_name is not globally unique; the pair (drive_path, base_name) uniquely identifies a recording.

TYPE: str

drive_path

Slash-joined path from the root folder to the folder containing the recording files.

TYPE: str

drive_file_ids

Maps file type labels ("data", "meta", "timestamp") to their Google Drive file IDs.

TYPE: dict[str, str]

local_path

Absolute path to the local directory where the recording has been downloaded, or None if not yet downloaded.

TYPE: str | None

upload_time

ISO 8601 createdTime from the Drive API, or None.

TYPE: str | None

size

Combined size in bytes of the constituent xdat files that reported a size (summed from whichever of the three files Drive reported a size for). None only if none of them did.

TYPE: int | None

UploadPlan dataclass

What an upload of one local folder will do, worked out before anything is sent.

Drive paths are relative to the catalog's root folder, like drive_path in the catalog entries.

ATTRIBUTE DESCRIPTION
drive_path

Drive path the local folder lands at.

TYPE: str

folder_ids

Drive ID of each folder on the way that already exists, keyed by Drive path ("" is the root).

TYPE: dict[str, str]

folders_to_create

Drive paths of missing folders, each after its parent.

TYPE: list[str]

files_to_upload

Each local file to send, with the Drive path of its folder.

TYPE: list[tuple[Path, str]]

files_skipped

Local files already on Drive with the same MD5.

TYPE: list[Path]

Functions

download_item

download_item(
    service: DriveService,
    item: DriveItemEntry,
    local_data_dir: str,
    quiet: bool = False,
    progress_callback: ProgressCallback | None = None,
) -> str

Download a Drive item (file or folder) mirroring the Drive path locally.

Items are stored under {local_data_dir}/{drive_path}/{name}, keeping them in the same local directory tree as recordings.

For folder items the entire subtree is downloaded recursively.

PARAMETER DESCRIPTION
service

Authenticated Drive API service.

TYPE: DriveService

item

The :class:DriveItemEntry to download.

TYPE: DriveItemEntry

local_data_dir

Root local data directory.

TYPE: str

quiet

If True, suppress tqdm progress bars.

TYPE: bool DEFAULT: False

progress_callback

If given, called with the bytes received since the previous chunk across all files in this item.

TYPE: ProgressCallback | None DEFAULT: None

RETURNS DESCRIPTION
str

The absolute path to the downloaded file or folder as a string.

download_meta

download_meta(
    service: DriveService, recording: RecordingEntry, dest_path: Path
) -> None

Download only a recording's .xdat.json metadata file to dest_path.

RAISES DESCRIPTION
KeyError

If the recording has no "meta" file on Drive.

download_recording

download_recording(
    service: DriveService,
    recording: RecordingEntry,
    local_data_dir: str,
    quiet: bool = False,
    progress_callback: ProgressCallback | None = None,
) -> str

Download the xdat files for a recording into a path-mirrored local directory.

Files are stored under {local_data_dir}/{drive_path}/, mirroring the Drive folder hierarchy.

PARAMETER DESCRIPTION
service

Authenticated Drive API service.

TYPE: DriveService

recording

The :class:RecordingEntry whose files should be downloaded.

TYPE: RecordingEntry

local_data_dir

Root local data directory.

TYPE: str

quiet

If True, suppress tqdm progress bars.

TYPE: bool DEFAULT: False

progress_callback

If given, called with the bytes received since the previous chunk across all of this recording's files.

TYPE: ProgressCallback | None DEFAULT: None

RETURNS DESCRIPTION
str

The absolute path to the directory where the files were written.

local_md5

local_md5(path: Path) -> str

Return the hex MD5 of a local file, the digest Drive reports as md5Checksum.

plan_upload

plan_upload(
    service: DriveService, root_folder_id: str, local_dir: Path, parent_drive_path: str
) -> UploadPlan

Work out how to upload local_dir to {parent_drive_path}/{local_dir.name} without changing Drive.

Missing folders on the way, parent_drive_path included, are planned for creation. Where the target folder already exists, each local file is compared with the remote file at the same path: absent means upload, the same MD5 means skip, and anything else is a conflict. Files named in SKIPPED_UPLOAD_NAMES are left out.

PARAMETER DESCRIPTION
service

Authenticated Drive API service.

TYPE: DriveService

root_folder_id

Drive ID of the catalog's root folder.

TYPE: str

local_dir

Local folder to upload.

TYPE: Path

parent_drive_path

Drive path, from the root, of the folder that receives local_dir; "" for the root itself.

TYPE: str

RETURNS DESCRIPTION
UploadPlan

The plan; nothing on Drive has changed.

RAISES DESCRIPTION
NotADirectoryError

If local_dir isn't a directory.

UploadConflictError

If anything at a target path differs from the local files, or a folder on the way is ambiguous. Lists every conflict.

scan_drive

scan_drive(
    service: DriveService,
    root_folder_id: str,
    *,
    drive_path: str = "",
    quiet: bool = False,
) -> tuple[list[RecordingEntry], list[DriveItemEntry]]

Recursively scan Drive and return xdat recordings and all other items.

Traverses the full subtree rooted at root_folder_id. The folder itself gets no entry.

Recording collection: xdat files are recognized at any depth by their suffix and merged by (drive_path, base_name) into :class:RecordingEntry records. The three component files are treated as a single unit.

Item collection: Every non-xdat file and every folder (at any depth) is cataloged as a :class:DriveItemEntry. No depth-based filtering or structural classification is applied — the consumer decides what items mean.

PARAMETER DESCRIPTION
service

Authenticated Drive API service.

TYPE: DriveService

root_folder_id

Google Drive folder ID of the root folder to scan.

TYPE: str

drive_path

Path of root_folder_id from the catalog's root folder, prefixed to every entry's drive_path. Pass it to scan one subtree; leave it empty when root_folder_id is the catalog's root.

TYPE: str DEFAULT: ''

quiet

Has no effect; this scanner shows no progress. It is accepted so both scanners take the same arguments.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
tuple[list[RecordingEntry], list[DriveItemEntry]]

A tuple (recordings, items) where recordings is a list of :class:RecordingEntry dicts and items is a list of :class:DriveItemEntry dicts. All entries have local_path=None.

scan_drive_flat

scan_drive_flat(
    service: DriveService, root_folder_id: str, *, quiet: bool = False
) -> tuple[list[RecordingEntry], list[DriveItemEntry]]

Scan Drive without traversing from a root folder.

Flat-lists every file in the root's Shared Drive, or every file visible to the account when the root is in a My Drive, reconstructs folder paths via the parents field, and applies the same classification rules as :func:scan_drive: xdat files become :class:RecordingEntry records; everything else becomes a :class:DriveItemEntry.

PARAMETER DESCRIPTION
service

Authenticated Drive API service.

TYPE: DriveService

root_folder_id

The logical root folder ID — used as the stop sentinel when resolving paths.

TYPE: str

quiet

Suppress the progress output.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
tuple[list[RecordingEntry], list[DriveItemEntry]]

Same (recordings, items) tuple as :func:scan_drive. In a Shared Drive, items outside the root folder are left out; in a My Drive, an item whose parent chain can't be followed to the root is cataloged as if it sat directly under it.

xdat_recording_exists_locally

xdat_recording_exists_locally(recording: RecordingEntry, local_dir: str | Path) -> bool

Return True if all xdat files for a recording exist under local_dir.

PARAMETER DESCRIPTION
recording

The catalog entry to check.

TYPE: RecordingEntry

local_dir

Directory expected to contain the three xdat files.

TYPE: str | Path

RETURNS DESCRIPTION
bool

True if every file in XDAT_SUFFIXES exists at local_dir/{base_name}{suffix}; False if any are missing.

RAISES DESCRIPTION
ValueError

If drive_file_ids contains an unrecognized file type.