Skip to content

Commit 51355fb

Browse files
authored
fs.get_file: add callback support (#137)
1 parent 378c6e8 commit 51355fb

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

pydrive2/fs/spec.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -364,21 +364,30 @@ def cp_file(self, lpath, rpath, **kwargs):
364364
buffer = io.BytesIO(stream.read())
365365
self.upload_fobj(buffer, rpath)
366366

367-
def get_file(self, lpath, rpath, callback=None, **kwargs):
367+
def get_file(self, lpath, rpath, callback=None, block_size=None, **kwargs):
368368
item_id = self._get_item_id(lpath)
369-
return self.gdrive_get_file(item_id, rpath, callback)
369+
return self.gdrive_get_file(
370+
item_id, rpath, callback=callback, block_size=block_size
371+
)
370372

371373
@_gdrive_retry
372-
def gdrive_get_file(self, item_id, rpath, callback):
374+
def gdrive_get_file(self, item_id, rpath, callback=None, block_size=None):
373375
param = {"id": item_id}
374376
# it does not create a file on the remote
375377
gdrive_file = self.client.CreateFile(param)
376378

377379
extra_args = {}
380+
if block_size:
381+
extra_args["chunksize"] = block_size
382+
378383
if callback:
384+
385+
def cb(value, _):
386+
callback.absolute_update(value)
387+
379388
gdrive_file.FetchMetadata(fields="fileSize")
380389
callback.set_size(int(gdrive_file.get("fileSize")))
381-
extra_args["callback"] = callback.update
390+
extra_args["callback"] = cb
382391

383392
gdrive_file.GetContentFile(rpath, **extra_args)
384393

pydrive2/test/test_fs.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from concurrent import futures
55

66
import pytest
7-
7+
import fsspec
88
from pydrive2.auth import GoogleAuth
99
from pydrive2.fs import GDriveFileSystem
1010
from pydrive2.test.test_util import settings_file_path, setup_credentials
@@ -210,3 +210,21 @@ def test_get_file(fs, tmpdir, remote_dir):
210210
fs.put_file(src_file, remote_dir + "/a.txt")
211211
fs.get_file(remote_dir + "/a.txt", dest_file)
212212
assert dest_file.read() == "data"
213+
214+
215+
def test_get_file_callback(fs, tmpdir, remote_dir):
216+
src_file = tmpdir / "a.txt"
217+
dest_file = tmpdir / "b.txt"
218+
219+
with open(src_file, "wb") as file:
220+
file.write(b"data" * 10)
221+
222+
fs.put_file(src_file, remote_dir + "/a.txt")
223+
callback = fsspec.Callback()
224+
fs.get_file(
225+
remote_dir + "/a.txt", dest_file, callback=callback, block_size=10
226+
)
227+
assert dest_file.read() == "data" * 10
228+
229+
assert callback.size == 40
230+
assert callback.value == 40

0 commit comments

Comments
 (0)