Skip to content

Commit 3172dea

Browse files
Implement mv method in GDriveFileSystem (#217)
* draft of rename method * reverted files.py, add mv methond to spec.py * modified mv method * modified mv method * adding gdrive_retry and GetChanges debug mode GetChanges will be reverted after a bugfix! * reverted apiattr.py * removed fetchMetadata * add todo to remove the cache * fixed > fs.mv('root/tmp/b.pdf', 'root/') * _FilesUpdate > Upload * adding exeption if the user tryes to move a dir with recursive=False * fix style * adding reference to issue recarding cache invalidation * removed recursive flag
1 parent 688459b commit 3172dea

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pydrive2/fs/spec.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,37 @@ def cp_file(self, lpath, rpath, **kwargs):
542542
buffer = io.BytesIO(stream.read())
543543
self.upload_fobj(buffer, rpath)
544544

545+
@_gdrive_retry
546+
def mv(self, path1, path2, maxdepth=None, **kwargs):
547+
548+
if maxdepth is not None:
549+
raise NotImplementedError("Max depth move is not supported")
550+
551+
src_name = posixpath.basename(path1)
552+
553+
src_parent = self._parent(path1)
554+
555+
if self.exists(path2):
556+
dst_name = src_name
557+
dst_parent = path2
558+
else:
559+
dst_name = posixpath.basename(path2)
560+
dst_parent = self._parent(path2)
561+
562+
file1_id = self._get_item_id(path1)
563+
564+
file1 = self.client.CreateFile({"id": file1_id})
565+
566+
if src_name != dst_name:
567+
file1["title"] = dst_name
568+
569+
if src_parent != dst_parent:
570+
file2_parent_id = self._get_item_id(dst_parent)
571+
file1["parents"] = [{"id": file2_parent_id}]
572+
573+
# TODO need to invalidate the cache for the old path, see #232
574+
file1.Upload()
575+
545576
def get_file(self, lpath, rpath, callback=None, block_size=None, **kwargs):
546577
item_id = self._get_item_id(lpath)
547578
return self._gdrive_get_file(

0 commit comments

Comments
 (0)