cdm_data_loaders.utils.file_transfer.core ¶
File downloader with tenacity for retry support.
DownloadError ¶
NonRetryableDownloadError ¶
ChecksumMismatchError ¶
Bases: NonRetryableDownloadError
Checksum validation failed.
DownloadCore ¶
HTTP file downloader with retries and checksum testing.
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
retry_kwargs
staticmethod
¶
Build common tenacity retry configuration for sync and async downloaders.
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
handle_download_exception
staticmethod
¶
Log a download failure and re-raise or wrap it in DownloadError.
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
validate_args
staticmethod
¶
validate_args(
destination: str | Path,
checksum_fn: str | None = None,
extra_headers: dict[str, Any] | None = None,
) -> tuple[Path, str, dict[str, Any]]
Validate download arguments, apply defaults, and convert them to the appropriate format.
If no checksum function is supplied, it will be set to "sha256".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
destination
|
str | Path
|
location to save file to |
required |
checksum_fn
|
str | None
|
function for calculating file checksum |
None
|
extra_headers
|
dict[str, Any] | None
|
extra headers to add to the httpx request, defaults to None |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Path, str, dict[str, Any]]
|
arguments spruced up and in shape! |
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
validate_response
staticmethod
¶
Check the response from the server and act accordingly.
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
check_should_download
staticmethod
¶
Validate response and log when the resource is unmodified (HTTP 304).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Response
|
HTTP response from the server |
required |
url
|
str
|
download URL |
required |
log
|
Logger
|
logger instance |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
log_download_success
staticmethod
¶
Log a successful download and return destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
download URL |
required |
destination
|
Path
|
saved file path |
required |
log
|
Logger
|
logger instance |
required |
Returns:
| Type | Description |
|---|---|
Path
|
|
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
check_hash
staticmethod
¶
Compare the expected to actual checksum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
download URL |
required |
expected_checksum
|
str
|
expected checksum |
required |
checksum_fn
|
str
|
checksum algorithm |
required |
hash_obj
|
bool
|
hash object for calculating checksum |
required |
Raises:
| Type | Description |
|---|---|
ChecksumMismatchError
|
if the checksum is incorrect |
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
open_hash_writer
staticmethod
¶
open_hash_writer(
url: str,
destination: Path,
expected_checksum: str | None,
checksum_fn: str,
) -> Generator[Callable[[bytes], None]]
Open destination for binary writing, update an optional hasher per chunk, and verify on exit.
:yield: callback accepting a bytes chunk to write and hash
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
download URL |
required |
destination
|
Path
|
file path to write to |
required |
expected_checksum
|
str | None
|
expected checksum (if known) |
required |
checksum_fn
|
str
|
checksum algorithm name |
required |
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
write_and_hash
staticmethod
¶
write_and_hash(
url: str,
destination: Path,
chunks: Iterable[bytes],
expected_checksum: str | None,
checksum_fn: str,
) -> None
Write downloaded data to disk and accrue data for calculating the checksum of the download.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to download from |
required |
destination
|
Path
|
where to save the file to |
required |
chunks
|
Iterable[bytes]
|
iterable chunks of downloaded data |
required |
expected_checksum
|
str | None
|
expected checksum (if known), defaults to None |
required |
checksum_fn
|
str
|
function used for calculating the checksum, defaults to "sha256" |
required |
Raises:
| Type | Description |
|---|---|
ChecksumMismatchError
|
if the checksum of the downloaded data does not match the expected checksum. |
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
write_and_hash_async
async
staticmethod
¶
write_and_hash_async(
url: str,
destination: Path,
chunks: AsyncIterable[bytes],
expected_checksum: str | None,
checksum_fn: str,
) -> None
Write downloaded data to disk and accrue data for calculating the checksum of the download.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to download from |
required |
destination
|
Path
|
where to save the file to |
required |
chunks
|
AsyncIterable[bytes]
|
iterable chunks of downloaded data |
required |
expected_checksum
|
str | None
|
expected checksum (if known), defaults to None |
required |
checksum_fn
|
str
|
function used for calculating the checksum, defaults to "sha256" |
required |
Raises:
| Type | Description |
|---|---|
ChecksumMismatchError
|
if the checksum of the downloaded data does not match the expected checksum. |
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
BaseFileDownloader ¶
Shared base class for synchronous and asynchronous HTTP file downloaders.
Source code in src/cdm_data_loaders/utils/file_transfer/core.py
__init__ ¶
Initialize shared HTTP client and chunk size attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Any
|
configured sync or async httpx client |
required |
chunk_size
|
int
|
download stream chunk size in bytes |
8192
|