acadfetch 0.6.4__tar.gz → 0.7.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. {acadfetch-0.6.4/acadfetch.egg-info → acadfetch-0.7.1}/PKG-INFO +3 -3
  2. {acadfetch-0.6.4 → acadfetch-0.7.1}/README.md +2 -2
  3. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/client.py +20 -0
  4. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/consts.py +1 -1
  5. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/downloader.py +34 -1
  6. acadfetch-0.7.1/acadfetch/pooch.py +127 -0
  7. {acadfetch-0.6.4 → acadfetch-0.7.1/acadfetch.egg-info}/PKG-INFO +3 -3
  8. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch.egg-info/SOURCES.txt +2 -0
  9. {acadfetch-0.6.4 → acadfetch-0.7.1}/pyproject.toml +1 -1
  10. acadfetch-0.7.1/tests/test_pooch.py +149 -0
  11. {acadfetch-0.6.4 → acadfetch-0.7.1}/LICENSE +0 -0
  12. {acadfetch-0.6.4 → acadfetch-0.7.1}/MANIFEST.in +0 -0
  13. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/__init__.py +0 -0
  14. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/__main__.py +0 -0
  15. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/atomic.py +0 -0
  16. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/auth.py +0 -0
  17. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/cli.py +0 -0
  18. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/debug.py +0 -0
  19. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/errors.py +0 -0
  20. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/manifest.py +0 -0
  21. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/naming.py +0 -0
  22. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/progress.py +0 -0
  23. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/py.typed +0 -0
  24. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/ratelimit.py +0 -0
  25. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/remote.py +0 -0
  26. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/sources/__init__.py +0 -0
  27. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/sources/arxiv.py +0 -0
  28. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/sources/base.py +0 -0
  29. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/sources/direct.py +0 -0
  30. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/sources/doi.py +0 -0
  31. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/sources/registry.py +0 -0
  32. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/sources/removed.py +0 -0
  33. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/sources/zenodo.py +0 -0
  34. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/units.py +0 -0
  35. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/urls.py +0 -0
  36. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch/verify.py +0 -0
  37. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch.egg-info/dependency_links.txt +0 -0
  38. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch.egg-info/entry_points.txt +0 -0
  39. {acadfetch-0.6.4 → acadfetch-0.7.1}/acadfetch.egg-info/top_level.txt +0 -0
  40. {acadfetch-0.6.4 → acadfetch-0.7.1}/setup.cfg +0 -0
  41. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/conftest.py +0 -0
  42. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/fixtures/README.md +0 -0
  43. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/fixtures/ca.pem +0 -0
  44. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/fixtures/figshare_403_block.html +0 -0
  45. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/fixtures/figshare_403_forbidden.json +0 -0
  46. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/fixtures/figshare_article.json +0 -0
  47. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/fixtures/figshare_file.pdf +0 -0
  48. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_atomic.py +0 -0
  49. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_auth.py +0 -0
  50. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_checksum.py +0 -0
  51. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_citation.py +0 -0
  52. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_claims.py +0 -0
  53. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_cli.py +0 -0
  54. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_client.py +0 -0
  55. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_control_names.py +0 -0
  56. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_debug.py +0 -0
  57. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_disk.py +0 -0
  58. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_downloader.py +0 -0
  59. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_hash_once.py +0 -0
  60. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_identity.py +0 -0
  61. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_isolation.py +0 -0
  62. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_json.py +0 -0
  63. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_lock.py +0 -0
  64. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_manifest.py +0 -0
  65. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_naming.py +0 -0
  66. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_naming_race.py +0 -0
  67. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_network.py +0 -0
  68. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_packaging.py +0 -0
  69. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_progress.py +0 -0
  70. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_ratelimit.py +0 -0
  71. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_redirect.py +0 -0
  72. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_remote.py +0 -0
  73. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_removed.py +0 -0
  74. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_sources.py +0 -0
  75. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_urls.py +0 -0
  76. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_urls_glob.py +0 -0
  77. {acadfetch-0.6.4 → acadfetch-0.7.1}/tests/test_verify.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: acadfetch
3
- Version: 0.6.4
3
+ Version: 0.7.1
4
4
  Summary: Resumable, rate-limited, checksum-verified bulk downloads from academic data repositories
5
5
  Author: young-ski
6
6
  License-Expression: MIT
@@ -337,8 +337,8 @@ honest way to test this code path.
337
337
  ## Getting involved
338
338
 
339
339
  A [30-second tour](docs/demo.md) shows the whole loop, and
340
- [docs/integrations.md](docs/integrations.md) has Make, CI, aria2 and Snakemake
341
- recipes.
340
+ [docs/integrations.md](docs/integrations.md) has Make, CI, aria2, a Snakemake
341
+ storage plugin and a pooch downloader.
342
342
 
343
343
  A nightly job downloads three fixed public records and verifies them, so a
344
344
  rotted adapter opens an issue before a user reports one. It never blocks a
@@ -306,8 +306,8 @@ honest way to test this code path.
306
306
  ## Getting involved
307
307
 
308
308
  A [30-second tour](docs/demo.md) shows the whole loop, and
309
- [docs/integrations.md](docs/integrations.md) has Make, CI, aria2 and Snakemake
310
- recipes.
309
+ [docs/integrations.md](docs/integrations.md) has Make, CI, aria2, a Snakemake
310
+ storage plugin and a pooch downloader.
311
311
 
312
312
  A nightly job downloads three fixed public records and verifies them, so a
313
313
  rotted adapter opens an issue before a user reports one. It never blocks a
@@ -325,6 +325,26 @@ class Client:
325
325
  return {}
326
326
  raise
327
327
 
328
+ def available(self, url: str) -> bool:
329
+ """Whether the URL answers at all — the question a cache asks before fetching.
330
+
331
+ A HEAD is cheap, but ``head()`` folds 403/405/501 into "no headers", which
332
+ is indistinguishable from a 200 that sent none. So an empty answer is
333
+ settled with a one-byte range request instead of being read as success:
334
+ guessing wrong here makes a cache believe a file exists when it does not,
335
+ or refetch one that is fine.
336
+ """
337
+ try:
338
+ if self.head(url):
339
+ return True
340
+ except HttpError:
341
+ return False
342
+ try:
343
+ with self.open(url, {"Range": "bytes=0-0"}, attempts=1):
344
+ return True
345
+ except HttpError:
346
+ return False
347
+
328
348
 
329
349
  def _snippet(exc: urllib.error.HTTPError) -> str:
330
350
  try:
@@ -1,6 +1,6 @@
1
1
  """Package-wide constants and version."""
2
2
 
3
- __version__ = "0.6.4"
3
+ __version__ = "0.7.1"
4
4
 
5
5
  USER_AGENT = f"acadfetch/{__version__} (+https://github.com/young-ski/acadfetch)"
6
6
 
@@ -32,7 +32,7 @@ from typing import Any, cast
32
32
  from . import atomic, debug, naming, verify
33
33
  from .client import Client
34
34
  from .consts import CHUNK
35
- from .errors import HttpError, LocalError, PartialError, TargetError
35
+ from .errors import AcFError, HttpError, LocalError, PartialError, TargetError
36
36
  from .progress import Progress
37
37
  from .sources.base import File, Record, Result
38
38
  from .units import human
@@ -1006,3 +1006,36 @@ def check_total(record: Record, limit: int) -> None:
1006
1006
  from .errors import SourceError
1007
1007
 
1008
1008
  raise SourceError(f"record is {human(total)}, over --max-size ({human(limit)})")
1009
+
1010
+
1011
+ def fetch_one(
1012
+ client: Client,
1013
+ url: str,
1014
+ target: Path,
1015
+ *,
1016
+ retries: int = TRANSFER_ATTEMPTS,
1017
+ fsync: bool = True,
1018
+ ) -> None:
1019
+ """Download ``url`` to exactly ``target``, raising instead of returning a row.
1020
+
1021
+ The CLI reports a row per file because one bad file must not hide the rest; a
1022
+ library caller, by contrast, wants an exception and no partial result. The
1023
+ pooch downloader and the Snakemake plugin both ask this question, so they ask
1024
+ it here rather than each rebuilding the row-to-exception translation.
1025
+
1026
+ The name on disk is the caller's, not the repository's: if ``naming`` had to
1027
+ rewrite it, the produced file is moved back to what was asked for.
1028
+ """
1029
+ target = Path(target)
1030
+ target.parent.mkdir(parents=True, exist_ok=True)
1031
+ result = fetch_file(
1032
+ client,
1033
+ File(url=url, name=target.name),
1034
+ Options(out=target.parent, concurrency=1, retries=retries, fsync=fsync),
1035
+ set(),
1036
+ )
1037
+ if result.state == "failed":
1038
+ raise AcFError(result.error or f"could not fetch {url}")
1039
+ produced = Path(result.path or target)
1040
+ if produced != target:
1041
+ atomic.replace(produced, target)
@@ -0,0 +1,127 @@
1
+ """A pooch downloader that borrows acadfetch's HTTP client.
2
+
3
+ ``pooch`` already caches, verifies and moves a file into place. What it does not
4
+ do is survive a link that stalls at 80%: its own downloader issues a flat GET,
5
+ and a drop means the whole transfer again. This adds the transport only — every
6
+ request goes through :class:`acadfetch.client.Client`, so it honours
7
+ ``Retry-After``, retries a 5xx or a WAF block page, keeps a ``.part`` and resumes
8
+ it with ``Range``/``If-Range`` inside the attempt, and drops a credential header
9
+ when a redirect leaves the origin.
10
+
11
+ .. code-block:: python
12
+
13
+ import pooch
14
+ from acadfetch.pooch import downloader
15
+
16
+ path = pooch.retrieve(url, known_hash="md5:…", downloader=downloader)
17
+
18
+ ``pooch`` still does the hashing and the move, so this cannot make verification
19
+ weaker — only the arrival more likely. It is a plain callable rather than a
20
+ subclass because a callable is the whole contract ``pooch`` asks for.
21
+
22
+ Two limits worth knowing, both from how ``pooch`` drives a downloader:
23
+
24
+ * it hands each attempt a **fresh temporary file**, so a partial is resumed
25
+ within one call and not across calls — ``pooch`` owns the retry loop between
26
+ them;
27
+ * ``pooch``'s ``progressbar=True`` is implemented inside its own downloader and
28
+ is not passed here, so a transfer through this one is silent. The CLI's
29
+ progress line is a separate thing.
30
+ """
31
+
32
+ from __future__ import annotations
33
+
34
+ from collections.abc import Mapping
35
+ from pathlib import Path
36
+ from typing import Any
37
+
38
+ from .client import Client
39
+ from .consts import DEFAULT_RATE, DEFAULT_RETRIES, DEFAULT_TIMEOUT
40
+ from .downloader import CHUNK, fetch_one
41
+ from .errors import TargetError
42
+
43
+ __all__ = ["Downloader", "downloader"]
44
+
45
+
46
+ class Downloader:
47
+ """The callable ``pooch`` wants, configured like the CLI.
48
+
49
+ Every argument maps to a flag of the same name, so a pipeline that already
50
+ tuned the CLI does not have to learn a second vocabulary.
51
+ """
52
+
53
+ def __init__(
54
+ self,
55
+ *,
56
+ rate: float = DEFAULT_RATE,
57
+ retries: int = DEFAULT_RETRIES,
58
+ timeout: float = DEFAULT_TIMEOUT,
59
+ headers: Mapping[str, str] | None = None,
60
+ block_retries: int = 2,
61
+ allow_http: bool = False,
62
+ fsync: bool = True,
63
+ ) -> None:
64
+ self.rate = rate
65
+ self.retries = retries
66
+ self.timeout = timeout
67
+ self.headers = dict(headers or {})
68
+ self.block_retries = block_retries
69
+ self.allow_http = allow_http
70
+ self.fsync = fsync
71
+
72
+ def _client(self) -> Client:
73
+ return Client(
74
+ rate=self.rate,
75
+ retries=self.retries,
76
+ timeout=self.timeout,
77
+ headers=self.headers,
78
+ block_retries=self.block_retries,
79
+ allow_http=self.allow_http,
80
+ )
81
+
82
+ # -- the two things pooch asks for -------------------------------------
83
+ def __call__(
84
+ self,
85
+ url: str,
86
+ output_file: Any = None,
87
+ pooch: Any = None,
88
+ check_only: bool = False,
89
+ ) -> bool | None:
90
+ if check_only:
91
+ return self.available(url)
92
+ if output_file is None:
93
+ raise TargetError("pooch asked for a download but passed no output file")
94
+ if hasattr(output_file, "write"):
95
+ # A caller-supplied handle: pooch owns the file, so there is nothing
96
+ # to rename and nothing worth resuming onto.
97
+ self._stream(url, output_file)
98
+ return None
99
+
100
+ target = Path(output_file)
101
+ fetch_one(
102
+ self._client(),
103
+ url,
104
+ target,
105
+ retries=self.retries,
106
+ fsync=self.fsync,
107
+ )
108
+ return None
109
+
110
+ def available(self, url: str) -> bool:
111
+ """Whether the URL answers at all; see :meth:`acadfetch.client.Client.available`."""
112
+ return self._client().available(url)
113
+
114
+ def _stream(self, url: str, handle: Any) -> None:
115
+ client = self._client()
116
+ with client.open(url, attempts=self.retries + 1) as resp:
117
+ reader = getattr(resp, "read1", None) or resp.read
118
+ while True:
119
+ chunk = reader(CHUNK)
120
+ if not chunk:
121
+ break
122
+ handle.write(chunk)
123
+ handle.flush()
124
+
125
+
126
+ #: The zero-configuration instance: ``pooch.retrieve(..., downloader=downloader)``.
127
+ downloader = Downloader()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: acadfetch
3
- Version: 0.6.4
3
+ Version: 0.7.1
4
4
  Summary: Resumable, rate-limited, checksum-verified bulk downloads from academic data repositories
5
5
  Author: young-ski
6
6
  License-Expression: MIT
@@ -337,8 +337,8 @@ honest way to test this code path.
337
337
  ## Getting involved
338
338
 
339
339
  A [30-second tour](docs/demo.md) shows the whole loop, and
340
- [docs/integrations.md](docs/integrations.md) has Make, CI, aria2 and Snakemake
341
- recipes.
340
+ [docs/integrations.md](docs/integrations.md) has Make, CI, aria2, a Snakemake
341
+ storage plugin and a pooch downloader.
342
342
 
343
343
  A nightly job downloads three fixed public records and verifies them, so a
344
344
  rotted adapter opens an issue before a user reports one. It never blocks a
@@ -14,6 +14,7 @@ acadfetch/downloader.py
14
14
  acadfetch/errors.py
15
15
  acadfetch/manifest.py
16
16
  acadfetch/naming.py
17
+ acadfetch/pooch.py
17
18
  acadfetch/progress.py
18
19
  acadfetch/py.typed
19
20
  acadfetch/ratelimit.py
@@ -56,6 +57,7 @@ tests/test_naming.py
56
57
  tests/test_naming_race.py
57
58
  tests/test_network.py
58
59
  tests/test_packaging.py
60
+ tests/test_pooch.py
59
61
  tests/test_progress.py
60
62
  tests/test_ratelimit.py
61
63
  tests/test_redirect.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "acadfetch"
7
- version = "0.6.4"
7
+ version = "0.7.1"
8
8
  description = "Resumable, rate-limited, checksum-verified bulk downloads from academic data repositories"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -0,0 +1,149 @@
1
+ """The pooch downloader, driven through pooch's own machinery.
2
+
3
+ These run the real ``pooch.retrieve`` and ``Pooch.is_available`` against the local
4
+ test server, so the contract under test is pooch's, not a paraphrase of it. The
5
+ point is that acadfetch only supplies transport: pooch must still do the hashing
6
+ and the move, and must still be the one to fail on a bad hash.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import hashlib
12
+ import io
13
+ from pathlib import Path
14
+
15
+ import pooch
16
+ import pytest
17
+
18
+ from acadfetch import naming
19
+ from acadfetch.client import Client
20
+ from acadfetch.pooch import Downloader, downloader
21
+
22
+ PAYLOAD = b"pooch payload" * 500
23
+ MD5 = hashlib.md5(PAYLOAD).hexdigest()
24
+
25
+
26
+ def _serve(server, path="/a.bin", payload=PAYLOAD):
27
+ server.cfg["files"][path] = payload
28
+ return server.base + path
29
+
30
+
31
+ def test_pooch_gets_the_bytes_and_verifies_them(server, tmp_path):
32
+ url = _serve(server)
33
+
34
+ got = pooch.retrieve(url, known_hash=f"md5:{MD5}", downloader=downloader, path=str(tmp_path))
35
+
36
+ assert Path(got).read_bytes() == PAYLOAD
37
+ assert server.cfg["hits"]["/a.bin"] == 1
38
+ # pooch names the cached file "<hash>-a.bin" and moves it into place; we must
39
+ # not have left a .part or anything else behind.
40
+ kept = list(tmp_path.iterdir())
41
+ assert len(kept) == 1 and kept[0].name.endswith("-a.bin"), kept
42
+
43
+
44
+ def test_a_bad_hash_is_still_pooch_that_refuses_it(server, tmp_path):
45
+ """The downloader must not be able to talk pooch out of a mismatch."""
46
+ url = _serve(server)
47
+
48
+ with pytest.raises(ValueError) as caught:
49
+ pooch.retrieve(url, known_hash="md5:" + "0" * 32, downloader=downloader, path=str(tmp_path))
50
+
51
+ assert "hash" in str(caught.value).lower()
52
+ assert list(tmp_path.iterdir()) == [], "a rejected download must not be kept"
53
+
54
+
55
+ def test_a_truncated_response_is_resumed_within_the_attempt(server, tmp_path):
56
+ """One cut connection must cost a chunk, not the whole file."""
57
+ url = _serve(server)
58
+ server.cfg["truncate_at"] = 100
59
+ server.cfg["truncate_times"] = 1
60
+
61
+ got = pooch.retrieve(
62
+ url, known_hash=f"md5:{MD5}", downloader=Downloader(rate=100), path=str(tmp_path)
63
+ )
64
+
65
+ assert Path(got).read_bytes() == PAYLOAD
66
+ assert server.cfg["ranges"][1] is not None, "the retry should have asked for a range"
67
+
68
+
69
+ def test_a_missing_file_is_reported_to_the_caller(server, tmp_path):
70
+ url = server.base + "/gone.bin"
71
+
72
+ with pytest.raises(Exception) as caught:
73
+ pooch.retrieve(url, known_hash=None, downloader=downloader, path=str(tmp_path))
74
+
75
+ assert "404" in str(caught.value)
76
+
77
+
78
+ def test_availability_answers_without_downloading(server, tmp_path):
79
+ _serve(server)
80
+ pool = pooch.Pooch(path=str(tmp_path), base_url=server.base + "/")
81
+ pool.registry["a.bin"] = None
82
+
83
+ assert pool.is_available("a.bin", downloader=downloader) is True
84
+ # A HEAD is expected; a GET of the body is not.
85
+ assert server.cfg["ranges"] == [], "availability must not fetch the body"
86
+
87
+
88
+ def test_a_missing_file_is_not_available(server, tmp_path):
89
+ pool = pooch.Pooch(path=str(tmp_path), base_url=server.base + "/")
90
+ pool.registry["gone.bin"] = None
91
+
92
+ assert pool.is_available("gone.bin", downloader=downloader) is False
93
+
94
+
95
+ def test_a_header_survives_to_the_origin(server, tmp_path):
96
+ """A restricted record is reached by handing the token to the downloader."""
97
+ url = _serve(server)
98
+ configured = Downloader(headers={"Authorization": "Bearer secret"})
99
+
100
+ pooch.retrieve(url, known_hash=f"md5:{MD5}", downloader=configured, path=str(tmp_path))
101
+
102
+ assert server.cfg["auth_seen"][0] == "Bearer secret"
103
+
104
+
105
+ def test_a_caller_supplied_handle_is_written_through(server, tmp_path):
106
+ """pooch's docs allow a file-like object; pooch owns it, so we do not rename."""
107
+ url = _serve(server)
108
+ buffer = io.BytesIO()
109
+
110
+ assert downloader(url, buffer) is None
111
+ assert buffer.getvalue() == PAYLOAD
112
+
113
+
114
+ def test_no_output_file_is_a_clear_error(server):
115
+ with pytest.raises(Exception) as caught:
116
+ downloader(_serve(server))
117
+
118
+ assert "no output file" in str(caught.value)
119
+
120
+
121
+ def test_a_name_the_sanitiser_changes_still_lands_where_the_caller_asked(server, tmp_path):
122
+ """`naming` is allowed to rewrite a name; pooch's requested path wins."""
123
+ url = _serve(server)
124
+ target = tmp_path / "data .bin"
125
+ assert target.name != naming.sanitize(target.name), "this test needs a rewritable name"
126
+
127
+ downloader(url, target)
128
+
129
+ assert target.read_bytes() == PAYLOAD
130
+ assert not (tmp_path / naming.sanitize(target.name)).exists()
131
+
132
+
133
+ def test_a_head_that_fails_is_not_available(server):
134
+ assert downloader.available(server.base + "/gone.bin") is False
135
+
136
+
137
+ def test_an_empty_head_is_not_read_as_available(server, monkeypatch):
138
+ """A 403/405/501 loses its headers; that must not count as success."""
139
+ monkeypatch.setattr(Client, "head", lambda self, url: {})
140
+
141
+ assert downloader.available(server.base + "/gone.bin") is False
142
+
143
+
144
+ def test_an_empty_head_still_finds_a_file_that_is_there(server, monkeypatch):
145
+ """The fallback exists so a 405 on HEAD does not make a real file look absent."""
146
+ url = _serve(server)
147
+ monkeypatch.setattr(Client, "head", lambda self, url: {})
148
+
149
+ assert downloader.available(url) is True
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes