Skip to content

Commit 4a6590c

Browse files
add copy method to drive.py (#188)
* add copy method to drive.py * removed kind * moved the copy method to files.py * add try except around the API call * corrected some single quote in double quote * removed newline at the end of the file * added param to copy method * first try on the test * test + fix bugs on copy method * fixed not modified file with git, so it doesn't show up on the changes * solved some comments - fixed style - fixed docstring - now the function will return the copied file - if new_title is None the new title will be "Copy of [old-title]" * target_folder is now optional * edits on the copyFile tests - now i use the ability to return the copied file directly - adding new test to copy the file in a different folder * clarified code a bit * add comment * changed description of Copy method * runned pre-commit hook and fixed pydrive_retry * changed SetContentString to SetContentFile * fixed docstring
1 parent 0f5eb74 commit 4a6590c

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

pydrive2/files.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,42 @@ def Delete(self, param=None):
518518
"""
519519
self._FilesDelete(param=param)
520520

521+
@LoadAuth
522+
def Copy(self, target_folder=None, new_title=None, param=None):
523+
"""Creates a copy of this file. Folders cannot be copied.
524+
525+
:param target_folder: Folder where the file will be copied.
526+
:type target_folder: GoogleDriveFile, optional
527+
:param new_title: Name of the new file.
528+
:type new_title: str, optional
529+
:param param: addition parameters to pass.
530+
:type param: dict, optional
531+
:raises ApiRequestError
532+
:return: the copied file
533+
:rtype: GoogleDriveFile
534+
"""
535+
536+
if param is None:
537+
param = {}
538+
539+
param["fileId"] = self["id"]
540+
param["supportsAllDrives"] = True
541+
param["body"] = {}
542+
543+
if target_folder:
544+
param["body"]["parents"] = [{"id": target_folder["id"]}]
545+
param["body"]["title"] = new_title
546+
547+
new_file = None
548+
try:
549+
new_file = (
550+
self.auth.service.files().copy(**param).execute(http=self.http)
551+
)
552+
except errors.HttpError as error:
553+
raise ApiRequestError(error)
554+
555+
return GoogleDriveFile(self.auth, new_file)
556+
521557
def InsertPermission(self, new_permission, param=None):
522558
"""Insert a new permission. Re-fetches all permissions after call.
523559

pydrive2/test/test_file.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,75 @@ def test_Parallel_Insert_File_Passed_HTTP(self):
842842
files = self._parallel_uploader(self.FILE_UPLOAD_COUNT, 10)
843843
self._parallel_downloader(files, 10)
844844

845+
# Tests for Copy file.
846+
# ====================
847+
848+
def test_CopyFileSameFolder(self):
849+
drive = GoogleDrive(self.ga)
850+
851+
content = "hello world!"
852+
853+
# create a temp file and set it's content to a known string
854+
file1 = drive.CreateFile()
855+
filename = self.getTempFile("copytestfile", content=content)
856+
file1["title"] = filename
857+
file1.SetContentFile(filename)
858+
pydrive_retry(file1.Upload)
859+
860+
# copy the file
861+
file2 = pydrive_retry(file1.Copy, new_title="copytestfile_copy")
862+
863+
self.assertIsNotNone(file2)
864+
865+
pydrive_retry(file2.FetchContent)
866+
# assert that the content of the copied file is the same as the original (file1)
867+
self.assertEqual(file2.GetContentString(), content)
868+
869+
self.DeleteUploadedFiles(drive, [file1["id"], file2["id"]])
870+
871+
def test_CopyFileDifferentFolder(self):
872+
drive = GoogleDrive(self.ga)
873+
874+
content = "hello world!"
875+
876+
# create a temp file and set it's content to a known string
877+
file1 = drive.CreateFile()
878+
filename = self.getTempFile("copytestfile", content=content)
879+
file1["title"] = filename
880+
file1.SetContentFile(filename)
881+
pydrive_retry(file1.Upload)
882+
883+
# create a temp directory
884+
temp_dir = pydrive_retry(
885+
drive.CreateFile,
886+
{
887+
"title": "temp_dir",
888+
"mimeType": "application/vnd.google-apps.folder",
889+
"parents": [{"id": file1["parents"][0]["id"]}],
890+
},
891+
)
892+
pydrive_retry(temp_dir.Upload)
893+
894+
# copy the file into the new folder
895+
file2 = pydrive_retry(
896+
file1.Copy, target_folder=temp_dir, new_title="copytestfile_copy"
897+
)
898+
899+
self.assertIsNotNone(file2)
900+
901+
pydrive_retry(file2.FetchContent)
902+
# assert that the content of the copied file is the same as the original (file1)
903+
self.assertEqual(file2.GetContentString(), content)
904+
905+
files = pydrive_retry(
906+
drive.ListFile, {"q": f"'{temp_dir['id']}' in parents"}
907+
).GetList()
908+
self.assertIn("copytestfile_copy", [f["title"] for f in files])
909+
910+
self.DeleteUploadedFiles(
911+
drive, [file1["id"], file2["id"], temp_dir["id"]]
912+
)
913+
845914
# Helper functions.
846915
# =================
847916

0 commit comments

Comments
 (0)