manage_cache¶
Description of all functions implemented in the module bnn_for_14C_calibration.manage_cache:
bnn_for_14C_calibration.manage_cache
¶
clear_cache()
¶
Remove the entire local cache directory used by the library.
This function deletes the cache directory and all its contents from the local filesystem. If the cache directory does not exist, a message is printed to indicate that there is no existing cache to remove.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
None
|
|
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Notes
- The cache directory path is set to
/home/user/.bnn_for_14C_calibration. - This operation is irreversible; all cached data will be lost.
- The cache data can be downloaded again by using the function
download_cache_lib_data.
Examples:
>>> clear_cache()
🗑️ removing cache directory at: /home/user/.bnn_for_14C_calibration...
🗑️ cache removed!
Source code in src/bnn_for_14C_calibration/manage_cache.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
download_cache_lib_data(overwrite=False)
¶
Download and initialize the local cache directory for the library.
This function manages the creation (or re-creation) of the local cache directory
used by the package to store pre-downloaded model and data files.
If overwrite is set to True, any existing cache will be removed before re-downloading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overwrite
|
bool
|
Whether to force re-downloading and replace any existing cache directory (default is False). |
False
|
Returns:
| Type | Description |
|---|---|
None
|
|
Notes
- The cache directory
.bnn_for_14C_calibrationis created in the user's home directory. - Requires an active internet connection for the first run.
- Disk space usage remains below 1 GB.
- This function internally calls the functions
clear_cacheanddownload_github_with_drive_map.
Examples:
>>> download_cache_lib_data(overwrite=False)
This may be the first time you need package functions that use
local cache data to work. A local cache directory is going to be
created and will be used after if needed without a new downloading
unless you delete the cache. To download this cache, network connection
must be available; also some disk space is required (less than 1 GB total).
******************** Creating cache directory at: /home/user/.bnn_for_14C_calibration ********************
âś… Cache directory created at: /home/user/.bnn_for_14C_calibration, and filled with all the
contents of the 'models' directory downloaded from GitHub and Google Drive.
>>> download_cache_lib_data(overwrite=True)
overwrite is True : the cache will be cleared before
downloading it again...
🗑️ removing cache directory at : /home/user/.bnn_for_14C_calibration...
🗑️ cache removed!
******************** Creating cache directory at: /home/user/.bnn_for_14C_calibration ********************
âś… Cache directory created at: /home/user/.bnn_for_14C_calibration, and filled with all the
contents of the 'models' directory downloaded from GitHub and Google Drive.
Source code in src/bnn_for_14C_calibration/manage_cache.py
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | |
download_from_google_drive(url_or_id, output_path, sleep_time=0.2)
¶
Download a file or folder from Google Drive.
If the provided URL or ID refers to a folder, this function uses
gdown.download_folder; otherwise, it downloads a single file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url_or_id
|
str
|
The public Google Drive URL or file/folder ID. |
required |
output_path
|
Path
|
The local path where the downloaded file or folder will be saved. |
required |
sleep_time
|
float
|
Time in seconds to wait after the download (default is 0.2 seconds). |
0.2
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the download fails for any reason. |
Notes
- Parent directories will be created automatically if they do not exist.
- Uses the
gdownlibrary for downloading from Google Drive.
Examples:
>>> download_from_google_drive("https://drive.google.com/file/d/ABC123/view", Path("output.zip"))
📄 Downloading Google Drive file id ABC123 → output.zip
🗂️ Download complete!
Source code in src/bnn_for_14C_calibration/manage_cache.py
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 | |
download_from_huggingface(url, output_path, timeout=10, sleep_time=0.2)
¶
Download a file from the Hugging Face Hub.
If the downloaded file is a .zip archive, its contents are automatically
extracted directly into output_path without preserving the top-level
folder contained in the archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The direct Hugging Face Hub URL to the file. |
required |
output_path
|
Path
|
The local path where the file or extracted contents will be saved. |
required |
timeout
|
int
|
Timeout for HTTP requests, in seconds (default is 10). |
10
|
sleep_time
|
float
|
Delay in seconds after the download to prevent throttling (default is 0.2). |
0.2
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the download or extraction process fails. |
Notes
- Parent directories are automatically created if they do not exist.
- Temporary
.tmpfiles are used during download to ensure file integrity. .ziparchives are extracted with flattened top-level directories.
Examples:
>>> download_from_huggingface(
... "https://huggingface.co/username/model/resolve/main/model.zip",
... Path("models/model")
... )
⬇️ Downloading from Hugging Face: https://huggingface.co/username/model/resolve/main/model.zip → models/model
📦 Extracting zip models/model.tmp → models/model (flatten top-level folder)
Source code in src/bnn_for_14C_calibration/manage_cache.py
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 | |
download_github_with_drive_map(api_url, local_dir, token=None, timeout=10, sleep_time=0.2)
¶
Download a GitHub folder and integrate external sources defined in a drive_map.json file.
This function retrieves a folder from a GitHub repository via the GitHub API.
If a drive_map.json file is present, it will use the mapping defined in it
to download certain files or subfolders from Hugging Face or Google Drive instead
of GitHub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_url
|
str
|
GitHub API URL pointing to the folder to download. |
required |
local_dir
|
Path
|
Local directory where the contents will be downloaded. |
required |
token
|
str
|
GitHub personal access token, if authentication is required (default is None). |
None
|
timeout
|
float
|
Timeout in seconds for HTTP requests (default is 10). |
10
|
sleep_time
|
float
|
Delay in seconds between downloads to prevent rate limiting (default is 0.2). |
0.2
|
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a file or folder cannot be downloaded from both Hugging Face and Google Drive. |
ValueError
|
If the GitHub API URL is invalid or the repository owner/repo cannot be extracted. |
Notes
- Creates parent directories automatically if they do not exist.
- Uses Hugging Face and Google Drive as fallback sources when specified in
drive_map.json. - Partially downloaded cache is cleared if an unrecoverable error occurs.
Examples:
>>> download_github_with_drive_map(
... "https://api.github.com/repos/dest-ash/bnn_for_14C_calibration/contents/models",
... Path.home() / ".bnn_for_14C_calibration" / "models"
... )
📄 Found drive_map.json in https://api.github.com/repos/dest-ash/bnn_for_14C_calibration/contents/models, downloading → ~/.bnn_for_14C_calibration/models/drive_map.json
⬇️ Downloading GitHub file https://raw.githubusercontent.com/... → ~/.bnn_for_14C_calibration/models/file.pt
Source code in src/bnn_for_14C_calibration/manage_cache.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | |
extract_drive_file_id(url)
¶
Extract the Google Drive file ID from a public URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The public Google Drive file URL.
|
required |
Returns:
| Type | Description |
|---|---|
str or None
|
The extracted Google Drive file ID, or None if it could not be found. |
Examples:
>>> extract_drive_file_id("https://drive.google.com/file/d/ABC123/view")
'ABC123'
>>> extract_drive_file_id("https://example.com/file.txt")
None
Source code in src/bnn_for_14C_calibration/manage_cache.py
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 | |
is_google_drive_url(url)
¶
Check whether a given URL corresponds to a Google Drive file or folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the URL contains 'drive.google.com', otherwise False. |
Examples:
>>> is_google_drive_url("https://drive.google.com/file/d/12345/view?usp=sharing")
True
>>> is_google_drive_url("https://example.com/file.txt")
False
Source code in src/bnn_for_14C_calibration/manage_cache.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |