Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions fsspec/implementations/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,20 @@ async def _get_file(

callback.set_size(size)
self._raise_not_found_for_status(r, rpath)
if not isfilelike(lpath):
lpath = open(lpath, "wb")
chunk = True
while chunk:
chunk = await r.content.read(chunk_size)
lpath.write(chunk)
callback.relative_update(len(chunk))
if isfilelike(lpath):
outfile = lpath
else:
outfile = open(lpath, "wb")

try:
chunk = True
while chunk:
chunk = await r.content.read(chunk_size)
outfile.write(chunk)
callback.relative_update(len(chunk))
finally:
if not isfilelike(lpath):
outfile.close()

async def _put_file(
self,
Expand Down
31 changes: 16 additions & 15 deletions fsspec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,25 +848,26 @@ def get_file(
"""Copy single remote file to local"""
if isfilelike(lpath):
outfile = lpath
else:
if self.isdir(rpath):
os.makedirs(lpath, exist_ok=True)
return None
elif self.isdir(rpath):
os.makedirs(lpath, exist_ok=True)
return None
Comment on lines 849 to +853
Copy link
Member Author

@efiop efiop Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't seem mutually exclusive, but I've preserved logic that was here before. Not sure what outfile argument is even used for, but it is strange that makedirs-behaviour (which is also odd by itself) doesn't affect it.

EDIT: nevermind, indeed they are mutually exclusive.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also calling isdir on every file is very wasteful. Ideally, we should get rid of this at some point in the future, as it seems to be a misplaced piece of logic from fs.get rather than something that is intentional here. From user's perspective I would expect IsADirectoryError here.


with self.open(rpath, "rb", **kwargs) as f1:
if outfile is None:
outfile = open(lpath, "wb")

with self.open(rpath, "rb", **kwargs) as f1:
callback.set_size(getattr(f1, "size", None))
data = True
while data:
data = f1.read(self.blocksize)
segment_len = outfile.write(data)
if segment_len is None:
segment_len = len(data)
callback.relative_update(segment_len)
if not isfilelike(lpath):
outfile.close()
try:
callback.set_size(getattr(f1, "size", None))
data = True
while data:
data = f1.read(self.blocksize)
segment_len = outfile.write(data)
if segment_len is None:
segment_len = len(data)
callback.relative_update(segment_len)
finally:
if not isfilelike(lpath):
outfile.close()

def get(self, rpath, lpath, recursive=False, callback=_DEFAULT_CALLBACK, **kwargs):
"""Copy file(s) to local.
Expand Down