refactor other helpers into helpers lib, consolidate

This commit is contained in:
Taylor Wilsdon
2025-08-19 14:15:04 -04:00
parent 25dddda9b8
commit 737b493878
3 changed files with 258 additions and 270 deletions

View File

@@ -3,7 +3,8 @@ Google Drive Helper Functions
Shared utilities for Google Drive operations including permission checking.
"""
from typing import List, Dict, Any
import re
from typing import List, Dict, Any, Optional
def check_public_link_permission(permissions: List[Dict[str, Any]]) -> bool:
@@ -50,4 +51,60 @@ def get_drive_image_url(file_id: str) -> str:
Returns:
str: URL for embedding Drive images
"""
return f"https://drive.google.com/uc?export=view&id={file_id}"
return f"https://drive.google.com/uc?export=view&id={file_id}"
# Precompiled regex patterns for Drive query detection
DRIVE_QUERY_PATTERNS = [
re.compile(r'\b\w+\s*(=|!=|>|<)\s*[\'"].*?[\'"]', re.IGNORECASE), # field = 'value'
re.compile(r'\b\w+\s*(=|!=|>|<)\s*\d+', re.IGNORECASE), # field = number
re.compile(r'\bcontains\b', re.IGNORECASE), # contains operator
re.compile(r'\bin\s+parents\b', re.IGNORECASE), # in parents
re.compile(r'\bhas\s*\{', re.IGNORECASE), # has {properties}
re.compile(r'\btrashed\s*=\s*(true|false)\b', re.IGNORECASE), # trashed=true/false
re.compile(r'\bstarred\s*=\s*(true|false)\b', re.IGNORECASE), # starred=true/false
re.compile(r'[\'"][^\'"]+[\'"]\s+in\s+parents', re.IGNORECASE), # 'parentId' in parents
re.compile(r'\bfullText\s+contains\b', re.IGNORECASE), # fullText contains
re.compile(r'\bname\s*(=|contains)\b', re.IGNORECASE), # name = or name contains
re.compile(r'\bmimeType\s*(=|!=)\b', re.IGNORECASE), # mimeType operators
]
def build_drive_list_params(
query: str,
page_size: int,
drive_id: Optional[str] = None,
include_items_from_all_drives: bool = True,
corpora: Optional[str] = None,
) -> Dict[str, Any]:
"""
Helper function to build common list parameters for Drive API calls.
Args:
query: The search query string
page_size: Maximum number of items to return
drive_id: Optional shared drive ID
include_items_from_all_drives: Whether to include items from all drives
corpora: Optional corpus specification
Returns:
Dictionary of parameters for Drive API list calls
"""
list_params = {
"q": query,
"pageSize": page_size,
"fields": "nextPageToken, files(id, name, mimeType, webViewLink, iconLink, modifiedTime, size)",
"supportsAllDrives": True,
"includeItemsFromAllDrives": include_items_from_all_drives,
}
if drive_id:
list_params["driveId"] = drive_id
if corpora:
list_params["corpora"] = corpora
else:
list_params["corpora"] = "drive"
elif corpora:
list_params["corpora"] = corpora
return list_params