Skip to content

Commit

Permalink
Add urls to returned results (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
samaloney authored Apr 8, 2024
1 parent 293133c commit b1a09bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
14 changes: 9 additions & 5 deletions parfive/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ def _format_results(self, retvals, main_pb):
elif isinstance(res, Exception):
raise res
else:
results.append(res)
requested_url, filepath = res
results.append(path=filepath, url=requested_url)

return results

Expand Down Expand Up @@ -551,7 +552,8 @@ async def _get_http(
"File %s already exists and overwrite is False; skipping download.",
filepath,
)
return str(filepath)
return url, str(filepath)

if callable(file_pb):
file_pb = file_pb(
position=token.n,
Expand Down Expand Up @@ -618,9 +620,11 @@ async def _get_http(
await asyncio.gather(*tasks)
# join() waits till all the items in the queue have been processed
await downloaded_chunk_queue.join()

for callback in self.config.done_callbacks:
callback(filepath, url, None)
return str(filepath)

return url, str(filepath)

except (Exception, asyncio.CancelledError) as e:
for task in tasks:
Expand Down Expand Up @@ -810,7 +814,7 @@ async def _get_ftp(
"File %s already exists and overwrite is False; skipping download.",
filepath,
)
return str(filepath)
return url, str(filepath)

if callable(file_pb):
total_size = await get_ftp_size(client, parse.path)
Expand Down Expand Up @@ -845,7 +849,7 @@ async def _get_ftp(
for callback in self.config.done_callbacks:
callback(filepath, url, None)

return str(filepath)
return url, str(filepath)

except (Exception, asyncio.CancelledError) as e:
if writer is not None:
Expand Down
20 changes: 17 additions & 3 deletions parfive/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ class Results(UserList):
"""
The results of a download from `parfive.Downloader.download`.
This object contains the filenames of successful downloads as well
as a list of any errors encountered in the `~parfive.Results.errors`
This object contains the filenames of successful downloads as well,
a list of all urls requested in the `~parfive.Results.urls` property
and a list of any errors encountered in the `~parfive.Results.errors`
property.
"""

def __init__(self, *args, errors=None):
def __init__(self, *args, errors=None, urls=None):
super().__init__(*args)
self._errors = errors or list()
self._urls = urls or list()

def _get_nice_resp_repr(self, response):
# This is a modified version of aiohttp.ClientResponse.__repr__
Expand Down Expand Up @@ -63,6 +65,10 @@ def __repr__(self):
out += str(self)
return out

def append(self, *, path, url):
super().append(path)
self._urls.append(url)

def add_error(self, filename, url, exception):
"""
Add an error to the results.
Expand All @@ -82,3 +88,11 @@ def errors(self):
``exception`` is the error raised during download.
"""
return self._errors

@property
def urls(self):
"""
A list of requested urls.
"""
return self._urls
6 changes: 5 additions & 1 deletion parfive/tests/test_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_download(httpserver, tmpdir):
assert dl.queued_downloads == 1

f = dl.download()
f.urls == [httpserver.url]
validate_test_file(f)


Expand Down Expand Up @@ -302,7 +303,10 @@ def test_failed_download():
def test_results():
res = Results()

res.append("hello")
res.append(path="hello", url="aurl")

assert res[0] == "hello"
assert res.urls[0] == "aurl"

res.add_error("wibble", "notaurl", "out of cheese")

Expand Down

0 comments on commit b1a09bb

Please sign in to comment.