radiens_drive_catalog.drive
drive.py
Google Drive API interactions: recursive scanning and file download.
xdat dataset file naming convention (3 files per base_name): {base_name}_data.xdat {base_name}.xdat.json {base_name}_timestamp.xdat
Classes
AssetEntry
Bases: TypedDict
One non-xdat asset record stored in the catalog.
Represents a single file or folder found on Drive that is not an xdat
dataset — for example a logs/ directory, a PowerPoint, or a writeup.
Attributes:
| Name | Type | Description |
|---|---|---|
asset_name |
str
|
The file or folder name as it appears on Drive
(e.g. |
asset_type |
Literal['folder', 'file']
|
|
drive_path |
str
|
Slash-joined path from the root folder to the parent
folder of this asset (e.g. |
drive_id |
str
|
Google Drive ID of this file or folder. |
mime_type |
str
|
MIME type as reported by the Drive API. |
local_path |
str | None
|
Absolute local path to the downloaded file or folder, or
|
upload_time |
str | None
|
ISO 8601 |
DatasetEntry
Bases: TypedDict
One xdat dataset record stored in the catalog.
Represents a single neural recording dataset. The three xdat files
(_data.xdat, .xdat.json, _timestamp.xdat) share a common
base_name stem.
Attributes:
| Name | Type | Description |
|---|---|---|
base_name |
str
|
Shared filename stem across all three xdat files.
Note: |
drive_path |
str
|
Slash-joined path from the root folder to the folder
containing the dataset files (e.g.
|
drive_file_ids |
dict[str, str]
|
Maps file type labels ( |
local_path |
str | None
|
Absolute path to the local directory where the dataset has
been downloaded, or |
upload_time |
str | None
|
ISO 8601 |
DriveItem
Bases: TypedDict
A single item returned by the Drive API files.list endpoint.
id, name, and mimeType are always present in well-formed
responses. parents is only populated when explicitly requested
in the fields parameter (used by the flat-scan fallback).
createdTime is populated when requested; absent on older API responses.
Functions
download_asset
Download an asset (file or folder) into the assets subdirectory.
Assets are stored under {local_data_dir}/assets/{drive_path}/{asset_name},
keeping them separate from the xdat dataset files.
For folder assets the entire subtree is downloaded recursively, mirroring the Drive folder structure under the asset's local directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
DriveService
|
Authenticated Drive API service. |
required |
asset
|
AssetEntry
|
The :class: |
required |
local_data_dir
|
str
|
Root local data directory (same as used for datasets). |
required |
quiet
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The absolute path to the downloaded file or folder as a string. |
download_dataset
Download the xdat files for a dataset into a path-mirrored local directory.
Files are stored under {local_data_dir}/{drive_path}/, mirroring the
Drive folder hierarchy:
{local_data_dir}/{drive_path}/{base_name}_data.xdat
{local_data_dir}/{drive_path}/{base_name}.xdat.json
{local_data_dir}/{drive_path}/{base_name}_timestamp.xdat
The directory is created if it does not already exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
DriveService
|
Authenticated Drive API service. |
required |
dataset
|
DatasetEntry
|
The |
required |
local_data_dir
|
str
|
Root local data directory. |
required |
quiet
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The absolute path to the directory where the files were written. |
scan_drive
Recursively scan Drive and return xdat datasets and non-xdat assets.
Traverses the full subtree rooted at root_folder_id.
Dataset collection: xdat files are recognized at any depth by their
suffix and merged by (drive_path, base_name) into
:class:DatasetEntry records.
Asset collection: Non-xdat content is cataloged according to depth:
- Folders encountered inside an experiment folder (depth ≥ 3 from root)
are cataloged as
"folder"assets and also recursed into so that any xdat files they contain are still found. - Non-xdat files encountered inside a date folder or experiment folder
(depths 2-3 from root) are cataloged as
"file"assets. - Content deeper than the experiment level is not separately cataloged (it is part of a parent folder asset and downloaded with it).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
DriveService
|
Authenticated Drive API service. |
required |
root_folder_id
|
str
|
Google Drive folder ID of the root folder to scan. |
required |
Returns:
| Type | Description |
|---|---|
list[DatasetEntry]
|
A tuple |
list[AssetEntry]
|
class: |
tuple[list[DatasetEntry], list[AssetEntry]]
|
class: |
scan_drive_flat
Scan Drive without traversing from a root folder.
This is the fallback used when the service account cannot access
root_folder_id (404 / 403). It flat-lists every file visible to the
account, reconstructs folder paths via the parents field, and applies
the same depth-based classification rules as :func:scan_drive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
DriveService
|
Authenticated Drive API service. |
required |
root_folder_id
|
str
|
The logical root folder ID. Not used for API calls — only as a stop sentinel when resolving paths so that depth assignments match the recursive scan. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[DatasetEntry], list[AssetEntry]]
|
Same |
xdat_dataset_exists_locally
Return True if all xdat files for a dataset exist under local_dir.
Checks for each of the three canonical xdat suffixes (_data.xdat,
.xdat.json, _timestamp.xdat). If drive_file_ids contains a
file-type label that is not one of the known types, raises rather than
silently returning False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
DatasetEntry
|
The catalog entry to check. |
required |
local_dir
|
str | Path
|
Directory expected to contain the three xdat files. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |