gws-cli 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gws/__init__.py +3 -0
- gws/__main__.py +9 -0
- gws/auth/__init__.py +14 -0
- gws/auth/oauth.py +211 -0
- gws/auth/provider.py +97 -0
- gws/auth/scopes.py +47 -0
- gws/auth/server.py +700 -0
- gws/cli.py +1123 -0
- gws/commands/__init__.py +1 -0
- gws/commands/_account.py +55 -0
- gws/commands/calendar.py +688 -0
- gws/commands/contacts.py +291 -0
- gws/commands/convert.py +122 -0
- gws/commands/docs.py +1432 -0
- gws/commands/drive.py +580 -0
- gws/commands/gmail.py +885 -0
- gws/commands/sheets.py +1550 -0
- gws/commands/slides.py +1107 -0
- gws/config.py +451 -0
- gws/context.py +14 -0
- gws/crypto.py +217 -0
- gws/exceptions.py +58 -0
- gws/output.py +111 -0
- gws/services/__init__.py +1 -0
- gws/services/base.py +47 -0
- gws/services/calendar.py +1377 -0
- gws/services/contacts.py +812 -0
- gws/services/convert.py +879 -0
- gws/services/docs.py +3723 -0
- gws/services/drive.py +1807 -0
- gws/services/gmail.py +2001 -0
- gws/services/sheets.py +3689 -0
- gws/services/slides.py +2845 -0
- gws/utils/__init__.py +1 -0
- gws/utils/colors.py +25 -0
- gws/utils/diagrams.py +220 -0
- gws/utils/retry.py +111 -0
- gws/utils/security.py +23 -0
- gws_cli-1.0.0.dist-info/METADATA +283 -0
- gws_cli-1.0.0.dist-info/RECORD +43 -0
- gws_cli-1.0.0.dist-info/WHEEL +4 -0
- gws_cli-1.0.0.dist-info/entry_points.txt +2 -0
- gws_cli-1.0.0.dist-info/licenses/LICENSE +21 -0
gws/services/drive.py
ADDED
|
@@ -0,0 +1,1807 @@
|
|
|
1
|
+
"""Google Drive service operations."""
|
|
2
|
+
|
|
3
|
+
import mimetypes
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
|
|
8
|
+
from googleapiclient.errors import HttpError
|
|
9
|
+
import io
|
|
10
|
+
|
|
11
|
+
from gws.services.base import BaseService
|
|
12
|
+
from gws.output import output_success, output_error
|
|
13
|
+
from gws.exceptions import ExitCode
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DriveService(BaseService):
|
|
17
|
+
"""Google Drive operations."""
|
|
18
|
+
|
|
19
|
+
SERVICE_NAME = "drive"
|
|
20
|
+
VERSION = "v3"
|
|
21
|
+
|
|
22
|
+
# Common fields to request for file metadata
|
|
23
|
+
FILE_FIELDS = (
|
|
24
|
+
"id, name, mimeType, webViewLink, webContentLink, parents, "
|
|
25
|
+
"createdTime, modifiedTime, size, description, starred, trashed"
|
|
26
|
+
)
|
|
27
|
+
LIST_FIELDS = (
|
|
28
|
+
"nextPageToken, files(id, name, mimeType, webViewLink, parents, "
|
|
29
|
+
"createdTime, modifiedTime, size)"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
# MIME type mappings for common extensions
|
|
33
|
+
MIME_TYPES = {
|
|
34
|
+
".json": "application/json",
|
|
35
|
+
".txt": "text/plain",
|
|
36
|
+
".md": "text/markdown",
|
|
37
|
+
".html": "text/html",
|
|
38
|
+
".htm": "text/html",
|
|
39
|
+
".css": "text/css",
|
|
40
|
+
".js": "application/javascript",
|
|
41
|
+
".pdf": "application/pdf",
|
|
42
|
+
".png": "image/png",
|
|
43
|
+
".jpg": "image/jpeg",
|
|
44
|
+
".jpeg": "image/jpeg",
|
|
45
|
+
".gif": "image/gif",
|
|
46
|
+
".svg": "image/svg+xml",
|
|
47
|
+
".zip": "application/zip",
|
|
48
|
+
".csv": "text/csv",
|
|
49
|
+
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
50
|
+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
51
|
+
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
# Export formats for Google native types
|
|
55
|
+
EXPORT_FORMATS = {
|
|
56
|
+
"application/vnd.google-apps.document": "application/pdf",
|
|
57
|
+
"application/vnd.google-apps.spreadsheet": "text/csv",
|
|
58
|
+
"application/vnd.google-apps.presentation": "application/pdf",
|
|
59
|
+
"application/vnd.google-apps.drawing": "image/png",
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
def _detect_mime_type(self, file_path: str) -> str:
|
|
63
|
+
"""Detect MIME type from file extension."""
|
|
64
|
+
ext = Path(file_path).suffix.lower()
|
|
65
|
+
if ext in self.MIME_TYPES:
|
|
66
|
+
return self.MIME_TYPES[ext]
|
|
67
|
+
# Fall back to mimetypes module
|
|
68
|
+
mime_type, _ = mimetypes.guess_type(file_path)
|
|
69
|
+
return mime_type or "application/octet-stream"
|
|
70
|
+
|
|
71
|
+
def _file_to_dict(self, file: Any) -> dict[str, Any]:
|
|
72
|
+
"""Convert Google Drive file object to dictionary."""
|
|
73
|
+
return {
|
|
74
|
+
"id": file.get("id"),
|
|
75
|
+
"name": file.get("name"),
|
|
76
|
+
"mime_type": file.get("mimeType"),
|
|
77
|
+
"web_view_link": file.get("webViewLink"),
|
|
78
|
+
"web_content_link": file.get("webContentLink"),
|
|
79
|
+
"parents": file.get("parents"),
|
|
80
|
+
"created_time": file.get("createdTime"),
|
|
81
|
+
"modified_time": file.get("modifiedTime"),
|
|
82
|
+
"size": file.get("size"),
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
def upload(
|
|
86
|
+
self,
|
|
87
|
+
file_path: str,
|
|
88
|
+
folder_id: str | None = None,
|
|
89
|
+
name: str | None = None,
|
|
90
|
+
mime_type: str | None = None,
|
|
91
|
+
) -> dict[str, Any]:
|
|
92
|
+
"""Upload a file to Google Drive."""
|
|
93
|
+
path = Path(file_path)
|
|
94
|
+
if not path.exists():
|
|
95
|
+
output_error(
|
|
96
|
+
error_code="FILE_NOT_FOUND",
|
|
97
|
+
operation="drive.upload",
|
|
98
|
+
message=f"File not found: {file_path}",
|
|
99
|
+
)
|
|
100
|
+
raise SystemExit(ExitCode.OPERATION_FAILED)
|
|
101
|
+
|
|
102
|
+
file_name = name or path.name
|
|
103
|
+
content_type = mime_type or self._detect_mime_type(file_path)
|
|
104
|
+
|
|
105
|
+
file_metadata: dict[str, Any] = {"name": file_name}
|
|
106
|
+
if folder_id:
|
|
107
|
+
file_metadata["parents"] = [folder_id]
|
|
108
|
+
|
|
109
|
+
media = MediaFileUpload(file_path, mimetype=content_type, resumable=True)
|
|
110
|
+
|
|
111
|
+
try:
|
|
112
|
+
result = self.execute(
|
|
113
|
+
self.service.files()
|
|
114
|
+
.create(
|
|
115
|
+
body=file_metadata,
|
|
116
|
+
media_body=media,
|
|
117
|
+
fields=self.FILE_FIELDS,
|
|
118
|
+
supportsAllDrives=True,
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
output_success(
|
|
123
|
+
operation="drive.upload",
|
|
124
|
+
file=self._file_to_dict(result),
|
|
125
|
+
)
|
|
126
|
+
return result
|
|
127
|
+
except HttpError as e:
|
|
128
|
+
output_error(
|
|
129
|
+
error_code="API_ERROR",
|
|
130
|
+
operation="drive.upload",
|
|
131
|
+
message=f"Google Drive API error: {e.reason}",
|
|
132
|
+
)
|
|
133
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
134
|
+
|
|
135
|
+
def download(self, file_id: str, output_path: str) -> dict[str, Any]:
|
|
136
|
+
"""Download a file from Google Drive."""
|
|
137
|
+
try:
|
|
138
|
+
# Get file metadata first
|
|
139
|
+
file = self.execute(self.service.files().get(
|
|
140
|
+
fileId=file_id, fields="id, name, mimeType", supportsAllDrives=True
|
|
141
|
+
))
|
|
142
|
+
|
|
143
|
+
# Handle Google native formats (need export)
|
|
144
|
+
if file.get("mimeType", "").startswith("application/vnd.google-apps."):
|
|
145
|
+
return self.export(
|
|
146
|
+
file_id=file_id,
|
|
147
|
+
output_path=output_path,
|
|
148
|
+
export_mime_type=self.EXPORT_FORMATS.get(file["mimeType"], "application/pdf"),
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
# Regular file download
|
|
152
|
+
request = self.service.files().get_media(fileId=file_id)
|
|
153
|
+
fh = io.BytesIO()
|
|
154
|
+
downloader = MediaIoBaseDownload(fh, request)
|
|
155
|
+
|
|
156
|
+
done = False
|
|
157
|
+
while not done:
|
|
158
|
+
_, done = downloader.next_chunk()
|
|
159
|
+
|
|
160
|
+
# Write to file
|
|
161
|
+
Path(output_path).write_bytes(fh.getvalue())
|
|
162
|
+
|
|
163
|
+
output_success(
|
|
164
|
+
operation="drive.download",
|
|
165
|
+
file_id=file_id,
|
|
166
|
+
output_path=output_path,
|
|
167
|
+
name=file.get("name"),
|
|
168
|
+
mime_type=file.get("mimeType"),
|
|
169
|
+
)
|
|
170
|
+
return {"file_id": file_id, "output_path": output_path}
|
|
171
|
+
except HttpError as e:
|
|
172
|
+
output_error(
|
|
173
|
+
error_code="API_ERROR",
|
|
174
|
+
operation="drive.download",
|
|
175
|
+
message=f"Google Drive API error: {e.reason}",
|
|
176
|
+
)
|
|
177
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
178
|
+
|
|
179
|
+
def export(
|
|
180
|
+
self,
|
|
181
|
+
file_id: str,
|
|
182
|
+
output_path: str,
|
|
183
|
+
export_mime_type: str = "application/pdf",
|
|
184
|
+
) -> dict[str, Any]:
|
|
185
|
+
"""Export a Google native file (Docs, Sheets, Slides) to a different format."""
|
|
186
|
+
try:
|
|
187
|
+
request = self.service.files().export_media(
|
|
188
|
+
fileId=file_id, mimeType=export_mime_type
|
|
189
|
+
)
|
|
190
|
+
fh = io.BytesIO()
|
|
191
|
+
downloader = MediaIoBaseDownload(fh, request)
|
|
192
|
+
|
|
193
|
+
done = False
|
|
194
|
+
while not done:
|
|
195
|
+
_, done = downloader.next_chunk()
|
|
196
|
+
|
|
197
|
+
Path(output_path).write_bytes(fh.getvalue())
|
|
198
|
+
|
|
199
|
+
output_success(
|
|
200
|
+
operation="drive.export",
|
|
201
|
+
file_id=file_id,
|
|
202
|
+
output_path=output_path,
|
|
203
|
+
export_mime_type=export_mime_type,
|
|
204
|
+
)
|
|
205
|
+
return {"file_id": file_id, "output_path": output_path}
|
|
206
|
+
except HttpError as e:
|
|
207
|
+
output_error(
|
|
208
|
+
error_code="API_ERROR",
|
|
209
|
+
operation="drive.export",
|
|
210
|
+
message=f"Google Drive API error: {e.reason}",
|
|
211
|
+
)
|
|
212
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
213
|
+
|
|
214
|
+
def list_files(
|
|
215
|
+
self,
|
|
216
|
+
folder_id: str | None = None,
|
|
217
|
+
max_results: int = 100,
|
|
218
|
+
page_token: str | None = None,
|
|
219
|
+
) -> dict[str, Any]:
|
|
220
|
+
"""List files in Drive or a specific folder."""
|
|
221
|
+
try:
|
|
222
|
+
query_parts = ["trashed = false"]
|
|
223
|
+
if folder_id:
|
|
224
|
+
query_parts.append(f"'{folder_id}' in parents")
|
|
225
|
+
|
|
226
|
+
result = self.execute(
|
|
227
|
+
self.service.files()
|
|
228
|
+
.list(
|
|
229
|
+
q=" and ".join(query_parts),
|
|
230
|
+
pageSize=max_results,
|
|
231
|
+
pageToken=page_token,
|
|
232
|
+
fields=self.LIST_FIELDS,
|
|
233
|
+
supportsAllDrives=True,
|
|
234
|
+
includeItemsFromAllDrives=True,
|
|
235
|
+
)
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
files = [self._file_to_dict(f) for f in result.get("files", [])]
|
|
239
|
+
|
|
240
|
+
output_success(
|
|
241
|
+
operation="drive.list",
|
|
242
|
+
folder_id=folder_id,
|
|
243
|
+
files=files,
|
|
244
|
+
next_page_token=result.get("nextPageToken"),
|
|
245
|
+
count=len(files),
|
|
246
|
+
)
|
|
247
|
+
return result
|
|
248
|
+
except HttpError as e:
|
|
249
|
+
output_error(
|
|
250
|
+
error_code="API_ERROR",
|
|
251
|
+
operation="drive.list",
|
|
252
|
+
message=f"Google Drive API error: {e.reason}",
|
|
253
|
+
)
|
|
254
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
255
|
+
|
|
256
|
+
def search(
|
|
257
|
+
self,
|
|
258
|
+
query: str,
|
|
259
|
+
max_results: int = 100,
|
|
260
|
+
page_token: str | None = None,
|
|
261
|
+
) -> dict[str, Any]:
|
|
262
|
+
"""Search files with a query."""
|
|
263
|
+
try:
|
|
264
|
+
# Add trashed filter if not already in query
|
|
265
|
+
full_query = query if "trashed" in query else f"{query} and trashed = false"
|
|
266
|
+
|
|
267
|
+
result = self.execute(
|
|
268
|
+
self.service.files()
|
|
269
|
+
.list(
|
|
270
|
+
q=full_query,
|
|
271
|
+
pageSize=max_results,
|
|
272
|
+
pageToken=page_token,
|
|
273
|
+
fields=self.LIST_FIELDS,
|
|
274
|
+
supportsAllDrives=True,
|
|
275
|
+
includeItemsFromAllDrives=True,
|
|
276
|
+
)
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
files = [self._file_to_dict(f) for f in result.get("files", [])]
|
|
280
|
+
|
|
281
|
+
output_success(
|
|
282
|
+
operation="drive.search",
|
|
283
|
+
query=query,
|
|
284
|
+
files=files,
|
|
285
|
+
next_page_token=result.get("nextPageToken"),
|
|
286
|
+
count=len(files),
|
|
287
|
+
)
|
|
288
|
+
return result
|
|
289
|
+
except HttpError as e:
|
|
290
|
+
output_error(
|
|
291
|
+
error_code="API_ERROR",
|
|
292
|
+
operation="drive.search",
|
|
293
|
+
message=f"Google Drive API error: {e.reason}",
|
|
294
|
+
)
|
|
295
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
296
|
+
|
|
297
|
+
def get_metadata(self, file_id: str) -> dict[str, Any]:
|
|
298
|
+
"""Get detailed file metadata."""
|
|
299
|
+
try:
|
|
300
|
+
fields = (
|
|
301
|
+
"id, name, mimeType, webViewLink, webContentLink, parents, "
|
|
302
|
+
"createdTime, modifiedTime, size, description, starred, trashed, "
|
|
303
|
+
"owners, permissions"
|
|
304
|
+
)
|
|
305
|
+
file = self.execute(self.service.files().get(
|
|
306
|
+
fileId=file_id, fields=fields, supportsAllDrives=True
|
|
307
|
+
))
|
|
308
|
+
|
|
309
|
+
# Format owners and permissions
|
|
310
|
+
owners = [
|
|
311
|
+
{"email": o.get("emailAddress"), "name": o.get("displayName")}
|
|
312
|
+
for o in file.get("owners", [])
|
|
313
|
+
]
|
|
314
|
+
permissions = [
|
|
315
|
+
{
|
|
316
|
+
"id": p.get("id"),
|
|
317
|
+
"type": p.get("type"),
|
|
318
|
+
"role": p.get("role"),
|
|
319
|
+
"email": p.get("emailAddress"),
|
|
320
|
+
}
|
|
321
|
+
for p in file.get("permissions", [])
|
|
322
|
+
]
|
|
323
|
+
|
|
324
|
+
file_data = self._file_to_dict(file)
|
|
325
|
+
file_data["description"] = file.get("description")
|
|
326
|
+
file_data["starred"] = file.get("starred")
|
|
327
|
+
file_data["trashed"] = file.get("trashed")
|
|
328
|
+
file_data["owners"] = owners
|
|
329
|
+
file_data["permissions"] = permissions
|
|
330
|
+
|
|
331
|
+
output_success(operation="drive.get_metadata", file=file_data)
|
|
332
|
+
return file
|
|
333
|
+
except HttpError as e:
|
|
334
|
+
output_error(
|
|
335
|
+
error_code="API_ERROR",
|
|
336
|
+
operation="drive.get_metadata",
|
|
337
|
+
message=f"Google Drive API error: {e.reason}",
|
|
338
|
+
)
|
|
339
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
340
|
+
|
|
341
|
+
def create_folder(
|
|
342
|
+
self, name: str, parent_id: str | None = None
|
|
343
|
+
) -> dict[str, Any]:
|
|
344
|
+
"""Create a new folder."""
|
|
345
|
+
try:
|
|
346
|
+
file_metadata: dict[str, Any] = {
|
|
347
|
+
"name": name,
|
|
348
|
+
"mimeType": "application/vnd.google-apps.folder",
|
|
349
|
+
}
|
|
350
|
+
if parent_id:
|
|
351
|
+
file_metadata["parents"] = [parent_id]
|
|
352
|
+
|
|
353
|
+
result = self.execute(
|
|
354
|
+
self.service.files()
|
|
355
|
+
.create(
|
|
356
|
+
body=file_metadata,
|
|
357
|
+
fields="id, name, mimeType, webViewLink, parents, createdTime",
|
|
358
|
+
supportsAllDrives=True,
|
|
359
|
+
)
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
output_success(
|
|
363
|
+
operation="drive.create_folder",
|
|
364
|
+
folder={
|
|
365
|
+
"id": result.get("id"),
|
|
366
|
+
"name": result.get("name"),
|
|
367
|
+
"web_view_link": result.get("webViewLink"),
|
|
368
|
+
"parents": result.get("parents"),
|
|
369
|
+
"created_time": result.get("createdTime"),
|
|
370
|
+
},
|
|
371
|
+
)
|
|
372
|
+
return result
|
|
373
|
+
except HttpError as e:
|
|
374
|
+
output_error(
|
|
375
|
+
error_code="API_ERROR",
|
|
376
|
+
operation="drive.create_folder",
|
|
377
|
+
message=f"Google Drive API error: {e.reason}",
|
|
378
|
+
)
|
|
379
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
380
|
+
|
|
381
|
+
def move(self, file_id: str, folder_id: str) -> dict[str, Any]:
|
|
382
|
+
"""Move a file to a different folder."""
|
|
383
|
+
try:
|
|
384
|
+
# Get current parents
|
|
385
|
+
file = self.execute(self.service.files().get(
|
|
386
|
+
fileId=file_id, fields="parents", supportsAllDrives=True
|
|
387
|
+
))
|
|
388
|
+
previous_parents = ",".join(file.get("parents", []))
|
|
389
|
+
|
|
390
|
+
result = self.execute(
|
|
391
|
+
self.service.files()
|
|
392
|
+
.update(
|
|
393
|
+
fileId=file_id,
|
|
394
|
+
addParents=folder_id,
|
|
395
|
+
removeParents=previous_parents,
|
|
396
|
+
fields="id, name, parents, webViewLink",
|
|
397
|
+
supportsAllDrives=True,
|
|
398
|
+
)
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
output_success(
|
|
402
|
+
operation="drive.move",
|
|
403
|
+
file={
|
|
404
|
+
"id": result.get("id"),
|
|
405
|
+
"name": result.get("name"),
|
|
406
|
+
"parents": result.get("parents"),
|
|
407
|
+
"web_view_link": result.get("webViewLink"),
|
|
408
|
+
},
|
|
409
|
+
)
|
|
410
|
+
return result
|
|
411
|
+
except HttpError as e:
|
|
412
|
+
output_error(
|
|
413
|
+
error_code="API_ERROR",
|
|
414
|
+
operation="drive.move",
|
|
415
|
+
message=f"Google Drive API error: {e.reason}",
|
|
416
|
+
)
|
|
417
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
418
|
+
|
|
419
|
+
def share(
|
|
420
|
+
self,
|
|
421
|
+
file_id: str,
|
|
422
|
+
email: str | None = None,
|
|
423
|
+
role: str = "reader",
|
|
424
|
+
share_type: str | None = None,
|
|
425
|
+
domain: str | None = None,
|
|
426
|
+
) -> dict[str, Any]:
|
|
427
|
+
"""Share a file with a user, group, domain, or make public.
|
|
428
|
+
|
|
429
|
+
Args:
|
|
430
|
+
file_id: The file ID to share.
|
|
431
|
+
email: Email address for user/group sharing.
|
|
432
|
+
role: Permission role (reader, writer, commenter).
|
|
433
|
+
share_type: Type of sharing (user, group, domain, anyone).
|
|
434
|
+
domain: Domain for domain-wide sharing (e.g., 'company.com').
|
|
435
|
+
Auto-sets share_type to 'domain' if provided.
|
|
436
|
+
"""
|
|
437
|
+
try:
|
|
438
|
+
# Auto-detect type from domain parameter
|
|
439
|
+
if domain:
|
|
440
|
+
if share_type and share_type != "domain":
|
|
441
|
+
output_error(
|
|
442
|
+
error_code="INVALID_ARGUMENT",
|
|
443
|
+
operation="drive.share",
|
|
444
|
+
message=f"Cannot use --domain with --type {share_type}. Domain sharing requires type 'domain'.",
|
|
445
|
+
)
|
|
446
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
447
|
+
share_type = "domain"
|
|
448
|
+
|
|
449
|
+
# Validate domain sharing has a domain
|
|
450
|
+
if share_type == "domain" and not domain:
|
|
451
|
+
output_error(
|
|
452
|
+
error_code="INVALID_ARGUMENT",
|
|
453
|
+
operation="drive.share",
|
|
454
|
+
message="Domain sharing requires --domain parameter (e.g., --domain company.com).",
|
|
455
|
+
)
|
|
456
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
457
|
+
|
|
458
|
+
# Determine permission type
|
|
459
|
+
perm_type = share_type or ("user" if email else "anyone")
|
|
460
|
+
|
|
461
|
+
permission: dict[str, Any] = {"type": perm_type, "role": role}
|
|
462
|
+
if email and perm_type == "user":
|
|
463
|
+
permission["emailAddress"] = email
|
|
464
|
+
if domain and perm_type == "domain":
|
|
465
|
+
permission["domain"] = domain
|
|
466
|
+
|
|
467
|
+
result = self.execute(
|
|
468
|
+
self.service.permissions()
|
|
469
|
+
.create(
|
|
470
|
+
fileId=file_id,
|
|
471
|
+
body=permission,
|
|
472
|
+
fields="id, type, role, emailAddress, domain",
|
|
473
|
+
supportsAllDrives=True,
|
|
474
|
+
)
|
|
475
|
+
)
|
|
476
|
+
|
|
477
|
+
# Get updated file info with sharing link
|
|
478
|
+
file = self.execute(
|
|
479
|
+
self.service.files()
|
|
480
|
+
.get(fileId=file_id, fields="webViewLink, webContentLink", supportsAllDrives=True)
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
output_success(
|
|
484
|
+
operation="drive.share",
|
|
485
|
+
permission={
|
|
486
|
+
"id": result.get("id"),
|
|
487
|
+
"type": result.get("type"),
|
|
488
|
+
"role": result.get("role"),
|
|
489
|
+
"email": result.get("emailAddress"),
|
|
490
|
+
"domain": result.get("domain"),
|
|
491
|
+
},
|
|
492
|
+
web_view_link=file.get("webViewLink"),
|
|
493
|
+
web_content_link=file.get("webContentLink"),
|
|
494
|
+
)
|
|
495
|
+
return result
|
|
496
|
+
except HttpError as e:
|
|
497
|
+
output_error(
|
|
498
|
+
error_code="API_ERROR",
|
|
499
|
+
operation="drive.share",
|
|
500
|
+
message=f"Google Drive API error: {e.reason}",
|
|
501
|
+
)
|
|
502
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
503
|
+
|
|
504
|
+
def copy(
|
|
505
|
+
self,
|
|
506
|
+
file_id: str,
|
|
507
|
+
name: str | None = None,
|
|
508
|
+
folder_id: str | None = None,
|
|
509
|
+
) -> dict[str, Any]:
|
|
510
|
+
"""Copy a file."""
|
|
511
|
+
try:
|
|
512
|
+
file_metadata: dict[str, Any] = {}
|
|
513
|
+
if name:
|
|
514
|
+
file_metadata["name"] = name
|
|
515
|
+
if folder_id:
|
|
516
|
+
file_metadata["parents"] = [folder_id]
|
|
517
|
+
|
|
518
|
+
result = self.execute(
|
|
519
|
+
self.service.files()
|
|
520
|
+
.copy(
|
|
521
|
+
fileId=file_id,
|
|
522
|
+
body=file_metadata,
|
|
523
|
+
fields="id, name, mimeType, webViewLink, parents, createdTime",
|
|
524
|
+
supportsAllDrives=True,
|
|
525
|
+
)
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
output_success(
|
|
529
|
+
operation="drive.copy",
|
|
530
|
+
file=self._file_to_dict(result),
|
|
531
|
+
)
|
|
532
|
+
return result
|
|
533
|
+
except HttpError as e:
|
|
534
|
+
output_error(
|
|
535
|
+
error_code="API_ERROR",
|
|
536
|
+
operation="drive.copy",
|
|
537
|
+
message=f"Google Drive API error: {e.reason}",
|
|
538
|
+
)
|
|
539
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
540
|
+
|
|
541
|
+
def update(
|
|
542
|
+
self,
|
|
543
|
+
file_id: str,
|
|
544
|
+
file_path: str,
|
|
545
|
+
name: str | None = None,
|
|
546
|
+
) -> dict[str, Any]:
|
|
547
|
+
"""Update a file's content."""
|
|
548
|
+
path = Path(file_path)
|
|
549
|
+
if not path.exists():
|
|
550
|
+
output_error(
|
|
551
|
+
error_code="FILE_NOT_FOUND",
|
|
552
|
+
operation="drive.update",
|
|
553
|
+
message=f"File not found: {file_path}",
|
|
554
|
+
)
|
|
555
|
+
raise SystemExit(ExitCode.OPERATION_FAILED)
|
|
556
|
+
|
|
557
|
+
try:
|
|
558
|
+
file_metadata: dict[str, Any] = {}
|
|
559
|
+
if name:
|
|
560
|
+
file_metadata["name"] = name
|
|
561
|
+
|
|
562
|
+
mime_type = self._detect_mime_type(file_path)
|
|
563
|
+
media = MediaFileUpload(file_path, mimetype=mime_type, resumable=True)
|
|
564
|
+
|
|
565
|
+
result = self.execute(
|
|
566
|
+
self.service.files()
|
|
567
|
+
.update(
|
|
568
|
+
fileId=file_id,
|
|
569
|
+
body=file_metadata if file_metadata else None,
|
|
570
|
+
media_body=media,
|
|
571
|
+
fields="id, name, mimeType, webViewLink, modifiedTime, size",
|
|
572
|
+
supportsAllDrives=True,
|
|
573
|
+
)
|
|
574
|
+
)
|
|
575
|
+
|
|
576
|
+
output_success(
|
|
577
|
+
operation="drive.update",
|
|
578
|
+
file=self._file_to_dict(result),
|
|
579
|
+
)
|
|
580
|
+
return result
|
|
581
|
+
except HttpError as e:
|
|
582
|
+
output_error(
|
|
583
|
+
error_code="API_ERROR",
|
|
584
|
+
operation="drive.update",
|
|
585
|
+
message=f"Google Drive API error: {e.reason}",
|
|
586
|
+
)
|
|
587
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
588
|
+
|
|
589
|
+
def delete(self, file_id: str, permanent: bool = False) -> dict[str, Any]:
|
|
590
|
+
"""Delete a file (trash or permanent)."""
|
|
591
|
+
try:
|
|
592
|
+
if permanent:
|
|
593
|
+
self.execute(self.service.files().delete(fileId=file_id, supportsAllDrives=True))
|
|
594
|
+
else:
|
|
595
|
+
self.execute(self.service.files().update(
|
|
596
|
+
fileId=file_id, body={"trashed": True}, supportsAllDrives=True
|
|
597
|
+
))
|
|
598
|
+
|
|
599
|
+
output_success(
|
|
600
|
+
operation="drive.delete",
|
|
601
|
+
file_id=file_id,
|
|
602
|
+
permanent=permanent,
|
|
603
|
+
)
|
|
604
|
+
return {"file_id": file_id, "permanent": permanent}
|
|
605
|
+
except HttpError as e:
|
|
606
|
+
output_error(
|
|
607
|
+
error_code="API_ERROR",
|
|
608
|
+
operation="drive.delete",
|
|
609
|
+
message=f"Google Drive API error: {e.reason}",
|
|
610
|
+
)
|
|
611
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
612
|
+
|
|
613
|
+
# =========================================================================
|
|
614
|
+
# COMMENTS
|
|
615
|
+
# =========================================================================
|
|
616
|
+
|
|
617
|
+
def list_comments(
|
|
618
|
+
self,
|
|
619
|
+
file_id: str,
|
|
620
|
+
include_deleted: bool = False,
|
|
621
|
+
max_results: int = 20,
|
|
622
|
+
) -> dict[str, Any]:
|
|
623
|
+
"""List comments on a file.
|
|
624
|
+
|
|
625
|
+
Args:
|
|
626
|
+
file_id: The file ID.
|
|
627
|
+
include_deleted: Include deleted comments.
|
|
628
|
+
max_results: Maximum comments to return.
|
|
629
|
+
"""
|
|
630
|
+
try:
|
|
631
|
+
result = self.execute(
|
|
632
|
+
self.service.comments()
|
|
633
|
+
.list(
|
|
634
|
+
fileId=file_id,
|
|
635
|
+
includeDeleted=include_deleted,
|
|
636
|
+
pageSize=max_results,
|
|
637
|
+
fields="comments(id,content,author,createdTime,modifiedTime,resolved,replies)",
|
|
638
|
+
)
|
|
639
|
+
)
|
|
640
|
+
|
|
641
|
+
comments = []
|
|
642
|
+
for comment in result.get("comments", []):
|
|
643
|
+
comments.append({
|
|
644
|
+
"comment_id": comment.get("id"),
|
|
645
|
+
"content": comment.get("content"),
|
|
646
|
+
"author": comment.get("author", {}).get("displayName"),
|
|
647
|
+
"created_time": comment.get("createdTime"),
|
|
648
|
+
"resolved": comment.get("resolved", False),
|
|
649
|
+
"reply_count": len(comment.get("replies", [])),
|
|
650
|
+
})
|
|
651
|
+
|
|
652
|
+
output_success(
|
|
653
|
+
operation="drive.list_comments",
|
|
654
|
+
file_id=file_id,
|
|
655
|
+
comment_count=len(comments),
|
|
656
|
+
comments=comments,
|
|
657
|
+
)
|
|
658
|
+
return {"comments": comments}
|
|
659
|
+
except HttpError as e:
|
|
660
|
+
output_error(
|
|
661
|
+
error_code="API_ERROR",
|
|
662
|
+
operation="drive.list_comments",
|
|
663
|
+
message=f"Google Drive API error: {e.reason}",
|
|
664
|
+
)
|
|
665
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
666
|
+
|
|
667
|
+
def add_comment(
|
|
668
|
+
self,
|
|
669
|
+
file_id: str,
|
|
670
|
+
content: str,
|
|
671
|
+
) -> dict[str, Any]:
|
|
672
|
+
"""Add a comment to a file.
|
|
673
|
+
|
|
674
|
+
Args:
|
|
675
|
+
file_id: The file ID.
|
|
676
|
+
content: The comment text.
|
|
677
|
+
"""
|
|
678
|
+
try:
|
|
679
|
+
result = self.execute(
|
|
680
|
+
self.service.comments()
|
|
681
|
+
.create(
|
|
682
|
+
fileId=file_id,
|
|
683
|
+
body={"content": content},
|
|
684
|
+
fields="id,content,author,createdTime",
|
|
685
|
+
)
|
|
686
|
+
)
|
|
687
|
+
|
|
688
|
+
output_success(
|
|
689
|
+
operation="drive.add_comment",
|
|
690
|
+
file_id=file_id,
|
|
691
|
+
comment_id=result.get("id"),
|
|
692
|
+
content=result.get("content"),
|
|
693
|
+
)
|
|
694
|
+
return result
|
|
695
|
+
except HttpError as e:
|
|
696
|
+
output_error(
|
|
697
|
+
error_code="API_ERROR",
|
|
698
|
+
operation="drive.add_comment",
|
|
699
|
+
message=f"Google Drive API error: {e.reason}",
|
|
700
|
+
)
|
|
701
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
702
|
+
|
|
703
|
+
def resolve_comment(
|
|
704
|
+
self,
|
|
705
|
+
file_id: str,
|
|
706
|
+
comment_id: str,
|
|
707
|
+
) -> dict[str, Any]:
|
|
708
|
+
"""Resolve a comment on a file by creating a resolve reply.
|
|
709
|
+
|
|
710
|
+
Args:
|
|
711
|
+
file_id: The file ID.
|
|
712
|
+
comment_id: The comment ID.
|
|
713
|
+
"""
|
|
714
|
+
try:
|
|
715
|
+
# Google Drive API requires creating a reply with action="resolve" to resolve a comment
|
|
716
|
+
result = self.execute(
|
|
717
|
+
self.service.replies()
|
|
718
|
+
.create(
|
|
719
|
+
fileId=file_id,
|
|
720
|
+
commentId=comment_id,
|
|
721
|
+
body={"action": "resolve"},
|
|
722
|
+
fields="id,action",
|
|
723
|
+
)
|
|
724
|
+
)
|
|
725
|
+
|
|
726
|
+
output_success(
|
|
727
|
+
operation="drive.resolve_comment",
|
|
728
|
+
file_id=file_id,
|
|
729
|
+
comment_id=comment_id,
|
|
730
|
+
reply_id=result.get("id"),
|
|
731
|
+
)
|
|
732
|
+
return result
|
|
733
|
+
except HttpError as e:
|
|
734
|
+
output_error(
|
|
735
|
+
error_code="API_ERROR",
|
|
736
|
+
operation="drive.resolve_comment",
|
|
737
|
+
message=f"Google Drive API error: {e.reason}",
|
|
738
|
+
)
|
|
739
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
740
|
+
|
|
741
|
+
def delete_comment(
|
|
742
|
+
self,
|
|
743
|
+
file_id: str,
|
|
744
|
+
comment_id: str,
|
|
745
|
+
) -> dict[str, Any]:
|
|
746
|
+
"""Delete a comment from a file.
|
|
747
|
+
|
|
748
|
+
Args:
|
|
749
|
+
file_id: The file ID.
|
|
750
|
+
comment_id: The comment ID.
|
|
751
|
+
"""
|
|
752
|
+
try:
|
|
753
|
+
self.execute(self.service.comments().delete(
|
|
754
|
+
fileId=file_id, commentId=comment_id
|
|
755
|
+
))
|
|
756
|
+
|
|
757
|
+
output_success(
|
|
758
|
+
operation="drive.delete_comment",
|
|
759
|
+
file_id=file_id,
|
|
760
|
+
comment_id=comment_id,
|
|
761
|
+
)
|
|
762
|
+
return {"file_id": file_id, "comment_id": comment_id}
|
|
763
|
+
except HttpError as e:
|
|
764
|
+
output_error(
|
|
765
|
+
error_code="API_ERROR",
|
|
766
|
+
operation="drive.delete_comment",
|
|
767
|
+
message=f"Google Drive API error: {e.reason}",
|
|
768
|
+
)
|
|
769
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
770
|
+
|
|
771
|
+
def reply_to_comment(
|
|
772
|
+
self,
|
|
773
|
+
file_id: str,
|
|
774
|
+
comment_id: str,
|
|
775
|
+
content: str,
|
|
776
|
+
) -> dict[str, Any]:
|
|
777
|
+
"""Reply to a comment on a file.
|
|
778
|
+
|
|
779
|
+
Args:
|
|
780
|
+
file_id: The file ID.
|
|
781
|
+
comment_id: The comment ID to reply to.
|
|
782
|
+
content: The reply text.
|
|
783
|
+
"""
|
|
784
|
+
try:
|
|
785
|
+
result = self.execute(
|
|
786
|
+
self.service.replies()
|
|
787
|
+
.create(
|
|
788
|
+
fileId=file_id,
|
|
789
|
+
commentId=comment_id,
|
|
790
|
+
body={"content": content},
|
|
791
|
+
fields="id,content,author,createdTime",
|
|
792
|
+
)
|
|
793
|
+
)
|
|
794
|
+
|
|
795
|
+
output_success(
|
|
796
|
+
operation="drive.reply_to_comment",
|
|
797
|
+
file_id=file_id,
|
|
798
|
+
comment_id=comment_id,
|
|
799
|
+
reply_id=result.get("id"),
|
|
800
|
+
)
|
|
801
|
+
return result
|
|
802
|
+
except HttpError as e:
|
|
803
|
+
output_error(
|
|
804
|
+
error_code="API_ERROR",
|
|
805
|
+
operation="drive.reply_to_comment",
|
|
806
|
+
message=f"Google Drive API error: {e.reason}",
|
|
807
|
+
)
|
|
808
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
809
|
+
|
|
810
|
+
# =========================================================================
|
|
811
|
+
# REVISIONS
|
|
812
|
+
# =========================================================================
|
|
813
|
+
|
|
814
|
+
def list_revisions(
|
|
815
|
+
self,
|
|
816
|
+
file_id: str,
|
|
817
|
+
max_results: int = 20,
|
|
818
|
+
) -> dict[str, Any]:
|
|
819
|
+
"""List revisions of a file.
|
|
820
|
+
|
|
821
|
+
Args:
|
|
822
|
+
file_id: The file ID.
|
|
823
|
+
max_results: Maximum revisions to return.
|
|
824
|
+
"""
|
|
825
|
+
try:
|
|
826
|
+
result = self.execute(
|
|
827
|
+
self.service.revisions()
|
|
828
|
+
.list(
|
|
829
|
+
fileId=file_id,
|
|
830
|
+
pageSize=max_results,
|
|
831
|
+
fields="revisions(id,mimeType,modifiedTime,lastModifyingUser,originalFilename,size)",
|
|
832
|
+
)
|
|
833
|
+
)
|
|
834
|
+
|
|
835
|
+
revisions = []
|
|
836
|
+
for rev in result.get("revisions", []):
|
|
837
|
+
revisions.append({
|
|
838
|
+
"revision_id": rev.get("id"),
|
|
839
|
+
"modified_time": rev.get("modifiedTime"),
|
|
840
|
+
"last_modifying_user": rev.get("lastModifyingUser", {}).get("displayName"),
|
|
841
|
+
"original_filename": rev.get("originalFilename"),
|
|
842
|
+
"size": rev.get("size"),
|
|
843
|
+
})
|
|
844
|
+
|
|
845
|
+
output_success(
|
|
846
|
+
operation="drive.list_revisions",
|
|
847
|
+
file_id=file_id,
|
|
848
|
+
revision_count=len(revisions),
|
|
849
|
+
revisions=revisions,
|
|
850
|
+
)
|
|
851
|
+
return {"revisions": revisions}
|
|
852
|
+
except HttpError as e:
|
|
853
|
+
output_error(
|
|
854
|
+
error_code="API_ERROR",
|
|
855
|
+
operation="drive.list_revisions",
|
|
856
|
+
message=f"Google Drive API error: {e.reason}",
|
|
857
|
+
)
|
|
858
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
859
|
+
|
|
860
|
+
def get_revision(
|
|
861
|
+
self,
|
|
862
|
+
file_id: str,
|
|
863
|
+
revision_id: str,
|
|
864
|
+
) -> dict[str, Any]:
|
|
865
|
+
"""Get metadata for a specific revision.
|
|
866
|
+
|
|
867
|
+
Args:
|
|
868
|
+
file_id: The file ID.
|
|
869
|
+
revision_id: The revision ID.
|
|
870
|
+
"""
|
|
871
|
+
try:
|
|
872
|
+
result = self.execute(
|
|
873
|
+
self.service.revisions()
|
|
874
|
+
.get(
|
|
875
|
+
fileId=file_id,
|
|
876
|
+
revisionId=revision_id,
|
|
877
|
+
fields="id,mimeType,modifiedTime,lastModifyingUser,originalFilename,size,keepForever,publishAuto,published",
|
|
878
|
+
)
|
|
879
|
+
)
|
|
880
|
+
|
|
881
|
+
output_success(
|
|
882
|
+
operation="drive.get_revision",
|
|
883
|
+
file_id=file_id,
|
|
884
|
+
revision_id=revision_id,
|
|
885
|
+
modified_time=result.get("modifiedTime"),
|
|
886
|
+
last_modifying_user=result.get("lastModifyingUser", {}).get("displayName"),
|
|
887
|
+
keep_forever=result.get("keepForever", False),
|
|
888
|
+
)
|
|
889
|
+
return result
|
|
890
|
+
except HttpError as e:
|
|
891
|
+
output_error(
|
|
892
|
+
error_code="API_ERROR",
|
|
893
|
+
operation="drive.get_revision",
|
|
894
|
+
message=f"Google Drive API error: {e.reason}",
|
|
895
|
+
)
|
|
896
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
897
|
+
|
|
898
|
+
def delete_revision(
|
|
899
|
+
self,
|
|
900
|
+
file_id: str,
|
|
901
|
+
revision_id: str,
|
|
902
|
+
) -> dict[str, Any]:
|
|
903
|
+
"""Delete a revision (cannot delete the last remaining revision).
|
|
904
|
+
|
|
905
|
+
Args:
|
|
906
|
+
file_id: The file ID.
|
|
907
|
+
revision_id: The revision ID to delete.
|
|
908
|
+
"""
|
|
909
|
+
try:
|
|
910
|
+
self.execute(self.service.revisions().delete(
|
|
911
|
+
fileId=file_id, revisionId=revision_id, supportsAllDrives=True
|
|
912
|
+
))
|
|
913
|
+
|
|
914
|
+
output_success(
|
|
915
|
+
operation="drive.delete_revision",
|
|
916
|
+
file_id=file_id,
|
|
917
|
+
revision_id=revision_id,
|
|
918
|
+
)
|
|
919
|
+
return {"file_id": file_id, "revision_id": revision_id}
|
|
920
|
+
except HttpError as e:
|
|
921
|
+
output_error(
|
|
922
|
+
error_code="API_ERROR",
|
|
923
|
+
operation="drive.delete_revision",
|
|
924
|
+
message=f"Google Drive API error: {e.reason}",
|
|
925
|
+
)
|
|
926
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
927
|
+
|
|
928
|
+
# =========================================================================
|
|
929
|
+
# TRASH MANAGEMENT
|
|
930
|
+
# =========================================================================
|
|
931
|
+
|
|
932
|
+
def list_trash(
|
|
933
|
+
self,
|
|
934
|
+
max_results: int = 20,
|
|
935
|
+
) -> dict[str, Any]:
|
|
936
|
+
"""List files in trash.
|
|
937
|
+
|
|
938
|
+
Args:
|
|
939
|
+
max_results: Maximum files to return.
|
|
940
|
+
"""
|
|
941
|
+
try:
|
|
942
|
+
result = self.execute(
|
|
943
|
+
self.service.files()
|
|
944
|
+
.list(
|
|
945
|
+
q="trashed=true",
|
|
946
|
+
pageSize=max_results,
|
|
947
|
+
fields="files(id,name,mimeType,trashedTime,trashingUser)",
|
|
948
|
+
supportsAllDrives=True,
|
|
949
|
+
includeItemsFromAllDrives=True,
|
|
950
|
+
)
|
|
951
|
+
)
|
|
952
|
+
|
|
953
|
+
files = []
|
|
954
|
+
for f in result.get("files", []):
|
|
955
|
+
files.append({
|
|
956
|
+
"file_id": f.get("id"),
|
|
957
|
+
"name": f.get("name"),
|
|
958
|
+
"mime_type": f.get("mimeType"),
|
|
959
|
+
"trashed_time": f.get("trashedTime"),
|
|
960
|
+
"trashing_user": f.get("trashingUser", {}).get("displayName"),
|
|
961
|
+
})
|
|
962
|
+
|
|
963
|
+
output_success(
|
|
964
|
+
operation="drive.list_trash",
|
|
965
|
+
file_count=len(files),
|
|
966
|
+
files=files,
|
|
967
|
+
)
|
|
968
|
+
return {"files": files}
|
|
969
|
+
except HttpError as e:
|
|
970
|
+
output_error(
|
|
971
|
+
error_code="API_ERROR",
|
|
972
|
+
operation="drive.list_trash",
|
|
973
|
+
message=f"Google Drive API error: {e.reason}",
|
|
974
|
+
)
|
|
975
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
976
|
+
|
|
977
|
+
def restore_from_trash(
|
|
978
|
+
self,
|
|
979
|
+
file_id: str,
|
|
980
|
+
) -> dict[str, Any]:
|
|
981
|
+
"""Restore a file from trash.
|
|
982
|
+
|
|
983
|
+
Args:
|
|
984
|
+
file_id: The file ID to restore.
|
|
985
|
+
"""
|
|
986
|
+
try:
|
|
987
|
+
result = self.execute(
|
|
988
|
+
self.service.files()
|
|
989
|
+
.update(
|
|
990
|
+
fileId=file_id,
|
|
991
|
+
body={"trashed": False},
|
|
992
|
+
fields="id,name,mimeType,webViewLink",
|
|
993
|
+
supportsAllDrives=True,
|
|
994
|
+
)
|
|
995
|
+
)
|
|
996
|
+
|
|
997
|
+
output_success(
|
|
998
|
+
operation="drive.restore_from_trash",
|
|
999
|
+
file_id=file_id,
|
|
1000
|
+
name=result.get("name"),
|
|
1001
|
+
)
|
|
1002
|
+
return result
|
|
1003
|
+
except HttpError as e:
|
|
1004
|
+
output_error(
|
|
1005
|
+
error_code="API_ERROR",
|
|
1006
|
+
operation="drive.restore_from_trash",
|
|
1007
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1008
|
+
)
|
|
1009
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1010
|
+
|
|
1011
|
+
def empty_trash(self) -> dict[str, Any]:
|
|
1012
|
+
"""Permanently delete all files in trash."""
|
|
1013
|
+
try:
|
|
1014
|
+
self.execute(self.service.files().emptyTrash())
|
|
1015
|
+
|
|
1016
|
+
output_success(
|
|
1017
|
+
operation="drive.empty_trash",
|
|
1018
|
+
status="Trash emptied",
|
|
1019
|
+
)
|
|
1020
|
+
return {"status": "trash_emptied"}
|
|
1021
|
+
except HttpError as e:
|
|
1022
|
+
output_error(
|
|
1023
|
+
error_code="API_ERROR",
|
|
1024
|
+
operation="drive.empty_trash",
|
|
1025
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1026
|
+
)
|
|
1027
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1028
|
+
|
|
1029
|
+
# =========================================================================
|
|
1030
|
+
# PERMISSIONS
|
|
1031
|
+
# =========================================================================
|
|
1032
|
+
|
|
1033
|
+
def list_permissions(
|
|
1034
|
+
self,
|
|
1035
|
+
file_id: str,
|
|
1036
|
+
) -> dict[str, Any]:
|
|
1037
|
+
"""List all permissions on a file.
|
|
1038
|
+
|
|
1039
|
+
Args:
|
|
1040
|
+
file_id: The file ID.
|
|
1041
|
+
"""
|
|
1042
|
+
try:
|
|
1043
|
+
result = self.execute(
|
|
1044
|
+
self.service.permissions()
|
|
1045
|
+
.list(
|
|
1046
|
+
fileId=file_id,
|
|
1047
|
+
fields="permissions(id,type,role,emailAddress,displayName,domain,expirationTime,deleted)",
|
|
1048
|
+
supportsAllDrives=True,
|
|
1049
|
+
)
|
|
1050
|
+
)
|
|
1051
|
+
|
|
1052
|
+
permissions = []
|
|
1053
|
+
for perm in result.get("permissions", []):
|
|
1054
|
+
permissions.append({
|
|
1055
|
+
"permission_id": perm.get("id"),
|
|
1056
|
+
"type": perm.get("type"),
|
|
1057
|
+
"role": perm.get("role"),
|
|
1058
|
+
"email_address": perm.get("emailAddress"),
|
|
1059
|
+
"display_name": perm.get("displayName"),
|
|
1060
|
+
"domain": perm.get("domain"),
|
|
1061
|
+
"expiration_time": perm.get("expirationTime"),
|
|
1062
|
+
"deleted": perm.get("deleted", False),
|
|
1063
|
+
})
|
|
1064
|
+
|
|
1065
|
+
output_success(
|
|
1066
|
+
operation="drive.list_permissions",
|
|
1067
|
+
file_id=file_id,
|
|
1068
|
+
permission_count=len(permissions),
|
|
1069
|
+
permissions=permissions,
|
|
1070
|
+
)
|
|
1071
|
+
return {"permissions": permissions}
|
|
1072
|
+
except HttpError as e:
|
|
1073
|
+
output_error(
|
|
1074
|
+
error_code="API_ERROR",
|
|
1075
|
+
operation="drive.list_permissions",
|
|
1076
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1077
|
+
)
|
|
1078
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1079
|
+
|
|
1080
|
+
def get_permission(
|
|
1081
|
+
self,
|
|
1082
|
+
file_id: str,
|
|
1083
|
+
permission_id: str,
|
|
1084
|
+
) -> dict[str, Any]:
|
|
1085
|
+
"""Get a specific permission.
|
|
1086
|
+
|
|
1087
|
+
Args:
|
|
1088
|
+
file_id: The file ID.
|
|
1089
|
+
permission_id: The permission ID.
|
|
1090
|
+
"""
|
|
1091
|
+
try:
|
|
1092
|
+
result = self.execute(
|
|
1093
|
+
self.service.permissions()
|
|
1094
|
+
.get(
|
|
1095
|
+
fileId=file_id,
|
|
1096
|
+
permissionId=permission_id,
|
|
1097
|
+
fields="id,type,role,emailAddress,displayName,domain,expirationTime,deleted,pendingOwner",
|
|
1098
|
+
supportsAllDrives=True,
|
|
1099
|
+
)
|
|
1100
|
+
)
|
|
1101
|
+
|
|
1102
|
+
output_success(
|
|
1103
|
+
operation="drive.get_permission",
|
|
1104
|
+
file_id=file_id,
|
|
1105
|
+
permission_id=permission_id,
|
|
1106
|
+
type=result.get("type"),
|
|
1107
|
+
role=result.get("role"),
|
|
1108
|
+
email_address=result.get("emailAddress"),
|
|
1109
|
+
display_name=result.get("displayName"),
|
|
1110
|
+
domain=result.get("domain"),
|
|
1111
|
+
expiration_time=result.get("expirationTime"),
|
|
1112
|
+
)
|
|
1113
|
+
return result
|
|
1114
|
+
except HttpError as e:
|
|
1115
|
+
if e.resp.status == 404:
|
|
1116
|
+
output_error(
|
|
1117
|
+
error_code="NOT_FOUND",
|
|
1118
|
+
operation="drive.get_permission",
|
|
1119
|
+
message=f"Permission not found: {permission_id}",
|
|
1120
|
+
)
|
|
1121
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1122
|
+
output_error(
|
|
1123
|
+
error_code="API_ERROR",
|
|
1124
|
+
operation="drive.get_permission",
|
|
1125
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1126
|
+
)
|
|
1127
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1128
|
+
|
|
1129
|
+
def update_permission(
|
|
1130
|
+
self,
|
|
1131
|
+
file_id: str,
|
|
1132
|
+
permission_id: str,
|
|
1133
|
+
role: str,
|
|
1134
|
+
expiration_time: str | None = None,
|
|
1135
|
+
) -> dict[str, Any]:
|
|
1136
|
+
"""Update a permission's role.
|
|
1137
|
+
|
|
1138
|
+
Args:
|
|
1139
|
+
file_id: The file ID.
|
|
1140
|
+
permission_id: The permission ID.
|
|
1141
|
+
role: New role (reader, commenter, writer, organizer, owner).
|
|
1142
|
+
expiration_time: Optional expiration (ISO 8601 format).
|
|
1143
|
+
"""
|
|
1144
|
+
try:
|
|
1145
|
+
body: dict[str, Any] = {"role": role}
|
|
1146
|
+
if expiration_time:
|
|
1147
|
+
body["expirationTime"] = expiration_time
|
|
1148
|
+
|
|
1149
|
+
result = self.execute(
|
|
1150
|
+
self.service.permissions()
|
|
1151
|
+
.update(
|
|
1152
|
+
fileId=file_id,
|
|
1153
|
+
permissionId=permission_id,
|
|
1154
|
+
body=body,
|
|
1155
|
+
fields="id,type,role,emailAddress,expirationTime",
|
|
1156
|
+
supportsAllDrives=True,
|
|
1157
|
+
)
|
|
1158
|
+
)
|
|
1159
|
+
|
|
1160
|
+
output_success(
|
|
1161
|
+
operation="drive.update_permission",
|
|
1162
|
+
file_id=file_id,
|
|
1163
|
+
permission_id=permission_id,
|
|
1164
|
+
new_role=role,
|
|
1165
|
+
email_address=result.get("emailAddress"),
|
|
1166
|
+
)
|
|
1167
|
+
return result
|
|
1168
|
+
except HttpError as e:
|
|
1169
|
+
if e.resp.status == 404:
|
|
1170
|
+
output_error(
|
|
1171
|
+
error_code="NOT_FOUND",
|
|
1172
|
+
operation="drive.update_permission",
|
|
1173
|
+
message=f"Permission not found: {permission_id}",
|
|
1174
|
+
)
|
|
1175
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1176
|
+
output_error(
|
|
1177
|
+
error_code="API_ERROR",
|
|
1178
|
+
operation="drive.update_permission",
|
|
1179
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1180
|
+
)
|
|
1181
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1182
|
+
|
|
1183
|
+
def delete_permission(
|
|
1184
|
+
self,
|
|
1185
|
+
file_id: str,
|
|
1186
|
+
permission_id: str,
|
|
1187
|
+
) -> dict[str, Any]:
|
|
1188
|
+
"""Remove a permission from a file (unshare).
|
|
1189
|
+
|
|
1190
|
+
Args:
|
|
1191
|
+
file_id: The file ID.
|
|
1192
|
+
permission_id: The permission ID to remove.
|
|
1193
|
+
"""
|
|
1194
|
+
try:
|
|
1195
|
+
self.execute(self.service.permissions().delete(
|
|
1196
|
+
fileId=file_id,
|
|
1197
|
+
permissionId=permission_id,
|
|
1198
|
+
supportsAllDrives=True,
|
|
1199
|
+
))
|
|
1200
|
+
|
|
1201
|
+
output_success(
|
|
1202
|
+
operation="drive.delete_permission",
|
|
1203
|
+
file_id=file_id,
|
|
1204
|
+
permission_id=permission_id,
|
|
1205
|
+
deleted=True,
|
|
1206
|
+
)
|
|
1207
|
+
return {"deleted": True, "permission_id": permission_id}
|
|
1208
|
+
except HttpError as e:
|
|
1209
|
+
if e.resp.status == 404:
|
|
1210
|
+
output_error(
|
|
1211
|
+
error_code="NOT_FOUND",
|
|
1212
|
+
operation="drive.delete_permission",
|
|
1213
|
+
message=f"Permission not found: {permission_id}",
|
|
1214
|
+
)
|
|
1215
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1216
|
+
output_error(
|
|
1217
|
+
error_code="API_ERROR",
|
|
1218
|
+
operation="drive.delete_permission",
|
|
1219
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1220
|
+
)
|
|
1221
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1222
|
+
|
|
1223
|
+
def transfer_ownership(
|
|
1224
|
+
self,
|
|
1225
|
+
file_id: str,
|
|
1226
|
+
new_owner_email: str,
|
|
1227
|
+
) -> dict[str, Any]:
|
|
1228
|
+
"""Transfer ownership of a file to another user.
|
|
1229
|
+
|
|
1230
|
+
Args:
|
|
1231
|
+
file_id: The file ID.
|
|
1232
|
+
new_owner_email: Email address of the new owner.
|
|
1233
|
+
"""
|
|
1234
|
+
try:
|
|
1235
|
+
result = self.execute(
|
|
1236
|
+
self.service.permissions()
|
|
1237
|
+
.create(
|
|
1238
|
+
fileId=file_id,
|
|
1239
|
+
body={
|
|
1240
|
+
"type": "user",
|
|
1241
|
+
"role": "owner",
|
|
1242
|
+
"emailAddress": new_owner_email,
|
|
1243
|
+
},
|
|
1244
|
+
transferOwnership=True,
|
|
1245
|
+
fields="id,emailAddress,role",
|
|
1246
|
+
supportsAllDrives=True,
|
|
1247
|
+
)
|
|
1248
|
+
)
|
|
1249
|
+
|
|
1250
|
+
output_success(
|
|
1251
|
+
operation="drive.transfer_ownership",
|
|
1252
|
+
file_id=file_id,
|
|
1253
|
+
new_owner=new_owner_email,
|
|
1254
|
+
permission_id=result.get("id"),
|
|
1255
|
+
)
|
|
1256
|
+
return result
|
|
1257
|
+
except HttpError as e:
|
|
1258
|
+
output_error(
|
|
1259
|
+
error_code="API_ERROR",
|
|
1260
|
+
operation="drive.transfer_ownership",
|
|
1261
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1262
|
+
)
|
|
1263
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1264
|
+
|
|
1265
|
+
# =========================================================================
|
|
1266
|
+
# REPLIES (extended)
|
|
1267
|
+
# =========================================================================
|
|
1268
|
+
|
|
1269
|
+
def list_replies(
|
|
1270
|
+
self,
|
|
1271
|
+
file_id: str,
|
|
1272
|
+
comment_id: str,
|
|
1273
|
+
max_results: int = 20,
|
|
1274
|
+
) -> dict[str, Any]:
|
|
1275
|
+
"""List replies to a comment.
|
|
1276
|
+
|
|
1277
|
+
Args:
|
|
1278
|
+
file_id: The file ID.
|
|
1279
|
+
comment_id: The comment ID.
|
|
1280
|
+
max_results: Maximum replies to return.
|
|
1281
|
+
"""
|
|
1282
|
+
try:
|
|
1283
|
+
result = self.execute(
|
|
1284
|
+
self.service.replies()
|
|
1285
|
+
.list(
|
|
1286
|
+
fileId=file_id,
|
|
1287
|
+
commentId=comment_id,
|
|
1288
|
+
pageSize=max_results,
|
|
1289
|
+
fields="replies(id,content,author,createdTime,modifiedTime)",
|
|
1290
|
+
)
|
|
1291
|
+
)
|
|
1292
|
+
|
|
1293
|
+
replies = []
|
|
1294
|
+
for reply in result.get("replies", []):
|
|
1295
|
+
replies.append({
|
|
1296
|
+
"reply_id": reply.get("id"),
|
|
1297
|
+
"content": reply.get("content"),
|
|
1298
|
+
"author": reply.get("author", {}).get("displayName"),
|
|
1299
|
+
"created_time": reply.get("createdTime"),
|
|
1300
|
+
"modified_time": reply.get("modifiedTime"),
|
|
1301
|
+
})
|
|
1302
|
+
|
|
1303
|
+
output_success(
|
|
1304
|
+
operation="drive.list_replies",
|
|
1305
|
+
file_id=file_id,
|
|
1306
|
+
comment_id=comment_id,
|
|
1307
|
+
reply_count=len(replies),
|
|
1308
|
+
replies=replies,
|
|
1309
|
+
)
|
|
1310
|
+
return {"replies": replies}
|
|
1311
|
+
except HttpError as e:
|
|
1312
|
+
output_error(
|
|
1313
|
+
error_code="API_ERROR",
|
|
1314
|
+
operation="drive.list_replies",
|
|
1315
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1316
|
+
)
|
|
1317
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1318
|
+
|
|
1319
|
+
def get_reply(
|
|
1320
|
+
self,
|
|
1321
|
+
file_id: str,
|
|
1322
|
+
comment_id: str,
|
|
1323
|
+
reply_id: str,
|
|
1324
|
+
) -> dict[str, Any]:
|
|
1325
|
+
"""Get a specific reply.
|
|
1326
|
+
|
|
1327
|
+
Args:
|
|
1328
|
+
file_id: The file ID.
|
|
1329
|
+
comment_id: The comment ID.
|
|
1330
|
+
reply_id: The reply ID.
|
|
1331
|
+
"""
|
|
1332
|
+
try:
|
|
1333
|
+
result = self.execute(
|
|
1334
|
+
self.service.replies()
|
|
1335
|
+
.get(
|
|
1336
|
+
fileId=file_id,
|
|
1337
|
+
commentId=comment_id,
|
|
1338
|
+
replyId=reply_id,
|
|
1339
|
+
fields="id,content,author,createdTime,modifiedTime,action",
|
|
1340
|
+
)
|
|
1341
|
+
)
|
|
1342
|
+
|
|
1343
|
+
output_success(
|
|
1344
|
+
operation="drive.get_reply",
|
|
1345
|
+
file_id=file_id,
|
|
1346
|
+
comment_id=comment_id,
|
|
1347
|
+
reply_id=reply_id,
|
|
1348
|
+
content=result.get("content"),
|
|
1349
|
+
author=result.get("author", {}).get("displayName"),
|
|
1350
|
+
created_time=result.get("createdTime"),
|
|
1351
|
+
)
|
|
1352
|
+
return result
|
|
1353
|
+
except HttpError as e:
|
|
1354
|
+
if e.resp.status == 404:
|
|
1355
|
+
output_error(
|
|
1356
|
+
error_code="NOT_FOUND",
|
|
1357
|
+
operation="drive.get_reply",
|
|
1358
|
+
message=f"Reply not found: {reply_id}",
|
|
1359
|
+
)
|
|
1360
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1361
|
+
output_error(
|
|
1362
|
+
error_code="API_ERROR",
|
|
1363
|
+
operation="drive.get_reply",
|
|
1364
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1365
|
+
)
|
|
1366
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1367
|
+
|
|
1368
|
+
def update_reply(
|
|
1369
|
+
self,
|
|
1370
|
+
file_id: str,
|
|
1371
|
+
comment_id: str,
|
|
1372
|
+
reply_id: str,
|
|
1373
|
+
content: str,
|
|
1374
|
+
) -> dict[str, Any]:
|
|
1375
|
+
"""Update a reply's content.
|
|
1376
|
+
|
|
1377
|
+
Args:
|
|
1378
|
+
file_id: The file ID.
|
|
1379
|
+
comment_id: The comment ID.
|
|
1380
|
+
reply_id: The reply ID.
|
|
1381
|
+
content: New reply content.
|
|
1382
|
+
"""
|
|
1383
|
+
try:
|
|
1384
|
+
result = self.execute(
|
|
1385
|
+
self.service.replies()
|
|
1386
|
+
.update(
|
|
1387
|
+
fileId=file_id,
|
|
1388
|
+
commentId=comment_id,
|
|
1389
|
+
replyId=reply_id,
|
|
1390
|
+
body={"content": content},
|
|
1391
|
+
fields="id,content,modifiedTime",
|
|
1392
|
+
)
|
|
1393
|
+
)
|
|
1394
|
+
|
|
1395
|
+
output_success(
|
|
1396
|
+
operation="drive.update_reply",
|
|
1397
|
+
file_id=file_id,
|
|
1398
|
+
comment_id=comment_id,
|
|
1399
|
+
reply_id=reply_id,
|
|
1400
|
+
content=result.get("content"),
|
|
1401
|
+
)
|
|
1402
|
+
return result
|
|
1403
|
+
except HttpError as e:
|
|
1404
|
+
if e.resp.status == 404:
|
|
1405
|
+
output_error(
|
|
1406
|
+
error_code="NOT_FOUND",
|
|
1407
|
+
operation="drive.update_reply",
|
|
1408
|
+
message=f"Reply not found: {reply_id}",
|
|
1409
|
+
)
|
|
1410
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1411
|
+
output_error(
|
|
1412
|
+
error_code="API_ERROR",
|
|
1413
|
+
operation="drive.update_reply",
|
|
1414
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1415
|
+
)
|
|
1416
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1417
|
+
|
|
1418
|
+
def delete_reply(
|
|
1419
|
+
self,
|
|
1420
|
+
file_id: str,
|
|
1421
|
+
comment_id: str,
|
|
1422
|
+
reply_id: str,
|
|
1423
|
+
) -> dict[str, Any]:
|
|
1424
|
+
"""Delete a reply.
|
|
1425
|
+
|
|
1426
|
+
Args:
|
|
1427
|
+
file_id: The file ID.
|
|
1428
|
+
comment_id: The comment ID.
|
|
1429
|
+
reply_id: The reply ID to delete.
|
|
1430
|
+
"""
|
|
1431
|
+
try:
|
|
1432
|
+
self.execute(
|
|
1433
|
+
self.service.replies()
|
|
1434
|
+
.delete(
|
|
1435
|
+
fileId=file_id,
|
|
1436
|
+
commentId=comment_id,
|
|
1437
|
+
replyId=reply_id,
|
|
1438
|
+
)
|
|
1439
|
+
)
|
|
1440
|
+
|
|
1441
|
+
output_success(
|
|
1442
|
+
operation="drive.delete_reply",
|
|
1443
|
+
file_id=file_id,
|
|
1444
|
+
comment_id=comment_id,
|
|
1445
|
+
reply_id=reply_id,
|
|
1446
|
+
)
|
|
1447
|
+
return {"file_id": file_id, "comment_id": comment_id, "reply_id": reply_id}
|
|
1448
|
+
except HttpError as e:
|
|
1449
|
+
if e.resp.status == 404:
|
|
1450
|
+
output_error(
|
|
1451
|
+
error_code="NOT_FOUND",
|
|
1452
|
+
operation="drive.delete_reply",
|
|
1453
|
+
message=f"Reply not found: {reply_id}",
|
|
1454
|
+
)
|
|
1455
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1456
|
+
output_error(
|
|
1457
|
+
error_code="API_ERROR",
|
|
1458
|
+
operation="drive.delete_reply",
|
|
1459
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1460
|
+
)
|
|
1461
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1462
|
+
|
|
1463
|
+
# =========================================================================
|
|
1464
|
+
# REVISIONS (extended)
|
|
1465
|
+
# =========================================================================
|
|
1466
|
+
|
|
1467
|
+
def update_revision(
|
|
1468
|
+
self,
|
|
1469
|
+
file_id: str,
|
|
1470
|
+
revision_id: str,
|
|
1471
|
+
keep_forever: bool | None = None,
|
|
1472
|
+
published: bool | None = None,
|
|
1473
|
+
publish_auto: bool | None = None,
|
|
1474
|
+
) -> dict[str, Any]:
|
|
1475
|
+
"""Update revision metadata.
|
|
1476
|
+
|
|
1477
|
+
Args:
|
|
1478
|
+
file_id: The file ID.
|
|
1479
|
+
revision_id: The revision ID.
|
|
1480
|
+
keep_forever: Whether to keep this revision forever (prevents auto-deletion).
|
|
1481
|
+
published: Whether this revision is published (for Google Docs/Sheets/Slides).
|
|
1482
|
+
publish_auto: Whether future revisions automatically publish.
|
|
1483
|
+
"""
|
|
1484
|
+
try:
|
|
1485
|
+
body: dict[str, Any] = {}
|
|
1486
|
+
if keep_forever is not None:
|
|
1487
|
+
body["keepForever"] = keep_forever
|
|
1488
|
+
if published is not None:
|
|
1489
|
+
body["published"] = published
|
|
1490
|
+
if publish_auto is not None:
|
|
1491
|
+
body["publishAuto"] = publish_auto
|
|
1492
|
+
|
|
1493
|
+
if not body:
|
|
1494
|
+
output_error(
|
|
1495
|
+
error_code="INVALID_ARGUMENT",
|
|
1496
|
+
operation="drive.update_revision",
|
|
1497
|
+
message="At least one update field required (keep_forever, published, publish_auto)",
|
|
1498
|
+
)
|
|
1499
|
+
raise SystemExit(ExitCode.INVALID_ARGS)
|
|
1500
|
+
|
|
1501
|
+
result = self.execute(
|
|
1502
|
+
self.service.revisions()
|
|
1503
|
+
.update(
|
|
1504
|
+
fileId=file_id,
|
|
1505
|
+
revisionId=revision_id,
|
|
1506
|
+
body=body,
|
|
1507
|
+
fields="id,modifiedTime,keepForever,published,publishAuto",
|
|
1508
|
+
supportsAllDrives=True,
|
|
1509
|
+
)
|
|
1510
|
+
)
|
|
1511
|
+
|
|
1512
|
+
output_success(
|
|
1513
|
+
operation="drive.update_revision",
|
|
1514
|
+
file_id=file_id,
|
|
1515
|
+
revision_id=revision_id,
|
|
1516
|
+
keep_forever=result.get("keepForever"),
|
|
1517
|
+
published=result.get("published"),
|
|
1518
|
+
)
|
|
1519
|
+
return result
|
|
1520
|
+
except HttpError as e:
|
|
1521
|
+
if e.resp.status == 404:
|
|
1522
|
+
output_error(
|
|
1523
|
+
error_code="NOT_FOUND",
|
|
1524
|
+
operation="drive.update_revision",
|
|
1525
|
+
message=f"Revision not found: {revision_id}",
|
|
1526
|
+
)
|
|
1527
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1528
|
+
output_error(
|
|
1529
|
+
error_code="API_ERROR",
|
|
1530
|
+
operation="drive.update_revision",
|
|
1531
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1532
|
+
)
|
|
1533
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1534
|
+
|
|
1535
|
+
# =========================================================================
|
|
1536
|
+
# CHANGES
|
|
1537
|
+
# =========================================================================
|
|
1538
|
+
|
|
1539
|
+
def get_start_page_token(self) -> dict[str, Any]:
|
|
1540
|
+
"""Get the starting page token for listing future changes."""
|
|
1541
|
+
try:
|
|
1542
|
+
result = self.execute(
|
|
1543
|
+
self.service.changes()
|
|
1544
|
+
.getStartPageToken(supportsAllDrives=True)
|
|
1545
|
+
)
|
|
1546
|
+
|
|
1547
|
+
output_success(
|
|
1548
|
+
operation="drive.get_start_page_token",
|
|
1549
|
+
start_page_token=result.get("startPageToken"),
|
|
1550
|
+
)
|
|
1551
|
+
return result
|
|
1552
|
+
except HttpError as e:
|
|
1553
|
+
output_error(
|
|
1554
|
+
error_code="API_ERROR",
|
|
1555
|
+
operation="drive.get_start_page_token",
|
|
1556
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1557
|
+
)
|
|
1558
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1559
|
+
|
|
1560
|
+
def list_changes(
|
|
1561
|
+
self,
|
|
1562
|
+
page_token: str,
|
|
1563
|
+
page_size: int = 100,
|
|
1564
|
+
include_removed: bool = True,
|
|
1565
|
+
) -> dict[str, Any]:
|
|
1566
|
+
"""List changes to files.
|
|
1567
|
+
|
|
1568
|
+
Args:
|
|
1569
|
+
page_token: The token for continuing a previous list request.
|
|
1570
|
+
page_size: Maximum number of changes to return.
|
|
1571
|
+
include_removed: Whether to include deleted files/drives.
|
|
1572
|
+
"""
|
|
1573
|
+
try:
|
|
1574
|
+
result = self.execute(
|
|
1575
|
+
self.service.changes()
|
|
1576
|
+
.list(
|
|
1577
|
+
pageToken=page_token,
|
|
1578
|
+
pageSize=page_size,
|
|
1579
|
+
includeRemoved=include_removed,
|
|
1580
|
+
fields="nextPageToken,newStartPageToken,changes(fileId,changeType,time,removed,file(id,name,mimeType,trashed))",
|
|
1581
|
+
supportsAllDrives=True,
|
|
1582
|
+
includeItemsFromAllDrives=True,
|
|
1583
|
+
)
|
|
1584
|
+
)
|
|
1585
|
+
|
|
1586
|
+
changes = []
|
|
1587
|
+
for change in result.get("changes", []):
|
|
1588
|
+
change_data = {
|
|
1589
|
+
"file_id": change.get("fileId"),
|
|
1590
|
+
"change_type": change.get("changeType"),
|
|
1591
|
+
"time": change.get("time"),
|
|
1592
|
+
"removed": change.get("removed", False),
|
|
1593
|
+
}
|
|
1594
|
+
if change.get("file"):
|
|
1595
|
+
change_data["file"] = {
|
|
1596
|
+
"name": change["file"].get("name"),
|
|
1597
|
+
"mime_type": change["file"].get("mimeType"),
|
|
1598
|
+
"trashed": change["file"].get("trashed", False),
|
|
1599
|
+
}
|
|
1600
|
+
changes.append(change_data)
|
|
1601
|
+
|
|
1602
|
+
output_success(
|
|
1603
|
+
operation="drive.list_changes",
|
|
1604
|
+
change_count=len(changes),
|
|
1605
|
+
next_page_token=result.get("nextPageToken"),
|
|
1606
|
+
new_start_page_token=result.get("newStartPageToken"),
|
|
1607
|
+
changes=changes,
|
|
1608
|
+
)
|
|
1609
|
+
return result
|
|
1610
|
+
except HttpError as e:
|
|
1611
|
+
output_error(
|
|
1612
|
+
error_code="API_ERROR",
|
|
1613
|
+
operation="drive.list_changes",
|
|
1614
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1615
|
+
)
|
|
1616
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1617
|
+
|
|
1618
|
+
# =========================================================================
|
|
1619
|
+
# SHARED DRIVES
|
|
1620
|
+
# =========================================================================
|
|
1621
|
+
|
|
1622
|
+
def list_shared_drives(
|
|
1623
|
+
self,
|
|
1624
|
+
max_results: int = 100,
|
|
1625
|
+
page_token: str | None = None,
|
|
1626
|
+
) -> dict[str, Any]:
|
|
1627
|
+
"""List shared drives the user has access to.
|
|
1628
|
+
|
|
1629
|
+
Args:
|
|
1630
|
+
max_results: Maximum number of shared drives to return.
|
|
1631
|
+
page_token: Token for pagination.
|
|
1632
|
+
"""
|
|
1633
|
+
try:
|
|
1634
|
+
result = self.execute(
|
|
1635
|
+
self.service.drives()
|
|
1636
|
+
.list(
|
|
1637
|
+
pageSize=max_results,
|
|
1638
|
+
pageToken=page_token,
|
|
1639
|
+
fields="nextPageToken,drives(id,name,createdTime,hidden)",
|
|
1640
|
+
)
|
|
1641
|
+
)
|
|
1642
|
+
|
|
1643
|
+
drives = []
|
|
1644
|
+
for drive in result.get("drives", []):
|
|
1645
|
+
drives.append({
|
|
1646
|
+
"drive_id": drive.get("id"),
|
|
1647
|
+
"name": drive.get("name"),
|
|
1648
|
+
"created_time": drive.get("createdTime"),
|
|
1649
|
+
"hidden": drive.get("hidden", False),
|
|
1650
|
+
})
|
|
1651
|
+
|
|
1652
|
+
output_success(
|
|
1653
|
+
operation="drive.list_shared_drives",
|
|
1654
|
+
drive_count=len(drives),
|
|
1655
|
+
next_page_token=result.get("nextPageToken"),
|
|
1656
|
+
drives=drives,
|
|
1657
|
+
)
|
|
1658
|
+
return result
|
|
1659
|
+
except HttpError as e:
|
|
1660
|
+
output_error(
|
|
1661
|
+
error_code="API_ERROR",
|
|
1662
|
+
operation="drive.list_shared_drives",
|
|
1663
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1664
|
+
)
|
|
1665
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1666
|
+
|
|
1667
|
+
def get_shared_drive(self, drive_id: str) -> dict[str, Any]:
|
|
1668
|
+
"""Get metadata for a shared drive.
|
|
1669
|
+
|
|
1670
|
+
Args:
|
|
1671
|
+
drive_id: The shared drive ID.
|
|
1672
|
+
"""
|
|
1673
|
+
try:
|
|
1674
|
+
result = self.execute(
|
|
1675
|
+
self.service.drives()
|
|
1676
|
+
.get(
|
|
1677
|
+
driveId=drive_id,
|
|
1678
|
+
fields="id,name,createdTime,hidden,capabilities,restrictions",
|
|
1679
|
+
)
|
|
1680
|
+
)
|
|
1681
|
+
|
|
1682
|
+
output_success(
|
|
1683
|
+
operation="drive.get_shared_drive",
|
|
1684
|
+
drive_id=drive_id,
|
|
1685
|
+
name=result.get("name"),
|
|
1686
|
+
created_time=result.get("createdTime"),
|
|
1687
|
+
capabilities=result.get("capabilities"),
|
|
1688
|
+
restrictions=result.get("restrictions"),
|
|
1689
|
+
)
|
|
1690
|
+
return result
|
|
1691
|
+
except HttpError as e:
|
|
1692
|
+
if e.resp.status == 404:
|
|
1693
|
+
output_error(
|
|
1694
|
+
error_code="NOT_FOUND",
|
|
1695
|
+
operation="drive.get_shared_drive",
|
|
1696
|
+
message=f"Shared drive not found: {drive_id}",
|
|
1697
|
+
)
|
|
1698
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1699
|
+
output_error(
|
|
1700
|
+
error_code="API_ERROR",
|
|
1701
|
+
operation="drive.get_shared_drive",
|
|
1702
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1703
|
+
)
|
|
1704
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1705
|
+
|
|
1706
|
+
def create_shared_drive(self, name: str) -> dict[str, Any]:
|
|
1707
|
+
"""Create a new shared drive.
|
|
1708
|
+
|
|
1709
|
+
Args:
|
|
1710
|
+
name: Name for the shared drive.
|
|
1711
|
+
"""
|
|
1712
|
+
import uuid
|
|
1713
|
+
|
|
1714
|
+
try:
|
|
1715
|
+
result = self.execute(
|
|
1716
|
+
self.service.drives()
|
|
1717
|
+
.create(
|
|
1718
|
+
requestId=str(uuid.uuid4()),
|
|
1719
|
+
body={"name": name},
|
|
1720
|
+
fields="id,name,createdTime",
|
|
1721
|
+
)
|
|
1722
|
+
)
|
|
1723
|
+
|
|
1724
|
+
output_success(
|
|
1725
|
+
operation="drive.create_shared_drive",
|
|
1726
|
+
drive_id=result.get("id"),
|
|
1727
|
+
name=result.get("name"),
|
|
1728
|
+
created_time=result.get("createdTime"),
|
|
1729
|
+
)
|
|
1730
|
+
return result
|
|
1731
|
+
except HttpError as e:
|
|
1732
|
+
output_error(
|
|
1733
|
+
error_code="API_ERROR",
|
|
1734
|
+
operation="drive.create_shared_drive",
|
|
1735
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1736
|
+
)
|
|
1737
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1738
|
+
|
|
1739
|
+
def delete_shared_drive(self, drive_id: str) -> dict[str, Any]:
|
|
1740
|
+
"""Delete a shared drive (must be empty).
|
|
1741
|
+
|
|
1742
|
+
Args:
|
|
1743
|
+
drive_id: The shared drive ID to delete.
|
|
1744
|
+
"""
|
|
1745
|
+
try:
|
|
1746
|
+
self.execute(self.service.drives().delete(driveId=drive_id))
|
|
1747
|
+
|
|
1748
|
+
output_success(
|
|
1749
|
+
operation="drive.delete_shared_drive",
|
|
1750
|
+
drive_id=drive_id,
|
|
1751
|
+
deleted=True,
|
|
1752
|
+
)
|
|
1753
|
+
return {"drive_id": drive_id, "deleted": True}
|
|
1754
|
+
except HttpError as e:
|
|
1755
|
+
if e.resp.status == 404:
|
|
1756
|
+
output_error(
|
|
1757
|
+
error_code="NOT_FOUND",
|
|
1758
|
+
operation="drive.delete_shared_drive",
|
|
1759
|
+
message=f"Shared drive not found: {drive_id}",
|
|
1760
|
+
)
|
|
1761
|
+
raise SystemExit(ExitCode.NOT_FOUND)
|
|
1762
|
+
output_error(
|
|
1763
|
+
error_code="API_ERROR",
|
|
1764
|
+
operation="drive.delete_shared_drive",
|
|
1765
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1766
|
+
)
|
|
1767
|
+
raise SystemExit(ExitCode.API_ERROR)
|
|
1768
|
+
|
|
1769
|
+
# =========================================================================
|
|
1770
|
+
# FILE IDS
|
|
1771
|
+
# =========================================================================
|
|
1772
|
+
|
|
1773
|
+
def generate_ids(
|
|
1774
|
+
self,
|
|
1775
|
+
count: int = 10,
|
|
1776
|
+
space: str = "drive",
|
|
1777
|
+
) -> dict[str, Any]:
|
|
1778
|
+
"""Generate file IDs for use with create operations.
|
|
1779
|
+
|
|
1780
|
+
Args:
|
|
1781
|
+
count: Number of IDs to generate (max 1000).
|
|
1782
|
+
space: Space for the IDs ('drive' or 'appDataFolder').
|
|
1783
|
+
"""
|
|
1784
|
+
try:
|
|
1785
|
+
result = self.execute(
|
|
1786
|
+
self.service.files()
|
|
1787
|
+
.generateIds(
|
|
1788
|
+
count=count,
|
|
1789
|
+
space=space,
|
|
1790
|
+
fields="ids,space,kind",
|
|
1791
|
+
)
|
|
1792
|
+
)
|
|
1793
|
+
|
|
1794
|
+
output_success(
|
|
1795
|
+
operation="drive.generate_ids",
|
|
1796
|
+
count=len(result.get("ids", [])),
|
|
1797
|
+
space=result.get("space"),
|
|
1798
|
+
ids=result.get("ids"),
|
|
1799
|
+
)
|
|
1800
|
+
return result
|
|
1801
|
+
except HttpError as e:
|
|
1802
|
+
output_error(
|
|
1803
|
+
error_code="API_ERROR",
|
|
1804
|
+
operation="drive.generate_ids",
|
|
1805
|
+
message=f"Google Drive API error: {e.reason}",
|
|
1806
|
+
)
|
|
1807
|
+
raise SystemExit(ExitCode.API_ERROR)
|