Given something that might be a URL (or might be a local path), determine which. If it's a URL, download the file and cache it, and return the path to the cached file. If it's already a local path, make sure the file exists and then return the path. Args: cache_dir: spec
(
url_or_filename,
cache_dir=None,
force_download=False,
proxies=None,
resume_download=False,
user_agent: Union[Dict, str, None] = None,
extract_compressed_file=False,
force_extract=False,
local_files_only=False,
)
| 523 | |
| 524 | |
| 525 | def cached_path( |
| 526 | url_or_filename, |
| 527 | cache_dir=None, |
| 528 | force_download=False, |
| 529 | proxies=None, |
| 530 | resume_download=False, |
| 531 | user_agent: Union[Dict, str, None] = None, |
| 532 | extract_compressed_file=False, |
| 533 | force_extract=False, |
| 534 | local_files_only=False, |
| 535 | ) -> Optional[str]: |
| 536 | """ |
| 537 | Given something that might be a URL (or might be a local path), |
| 538 | determine which. If it's a URL, download the file and cache it, and |
| 539 | return the path to the cached file. If it's already a local path, |
| 540 | make sure the file exists and then return the path. |
| 541 | Args: |
| 542 | cache_dir: specify a cache directory to save the file to (overwrite the default cache dir). |
| 543 | force_download: if True, re-dowload the file even if it's already cached in the cache dir. |
| 544 | resume_download: if True, resume the download if incompletly recieved file is found. |
| 545 | user_agent: Optional string or dict that will be appended to the user-agent on remote requests. |
| 546 | extract_compressed_file: if True and the path point to a zip or tar file, extract the compressed |
| 547 | file in a folder along the archive. |
| 548 | force_extract: if True when extract_compressed_file is True and the archive was already extracted, |
| 549 | re-extract the archive and overide the folder where it was extracted. |
| 550 | |
| 551 | Return: |
| 552 | None in case of non-recoverable file (non-existent or inaccessible url + no cache on disk). |
| 553 | Local path (string) otherwise |
| 554 | """ |
| 555 | if cache_dir is None: |
| 556 | cache_dir = TRANSFORMERS_CACHE |
| 557 | if isinstance(url_or_filename, Path): |
| 558 | url_or_filename = str(url_or_filename) |
| 559 | if isinstance(cache_dir, Path): |
| 560 | cache_dir = str(cache_dir) |
| 561 | |
| 562 | if is_remote_url(url_or_filename): |
| 563 | # URL, so get it from the cache (downloading if necessary) |
| 564 | output_path = get_from_cache( |
| 565 | url_or_filename, |
| 566 | cache_dir=cache_dir, |
| 567 | force_download=force_download, |
| 568 | proxies=proxies, |
| 569 | resume_download=resume_download, |
| 570 | user_agent=user_agent, |
| 571 | local_files_only=local_files_only, |
| 572 | ) |
| 573 | elif os.path.exists(url_or_filename): |
| 574 | # File, and it exists. |
| 575 | output_path = url_or_filename |
| 576 | elif urlparse(url_or_filename).scheme == "": |
| 577 | # File, but it doesn't exist. |
| 578 | raise EnvironmentError("file {} not found".format(url_or_filename)) |
| 579 | else: |
| 580 | # Something unknown |
| 581 | raise ValueError("unable to parse {} as a URL or as a local path".format(url_or_filename)) |
| 582 |
no test coverage detected