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.
(url_or_filename, cache_dir=None)
| 85 | |
| 86 | |
| 87 | def cached_path(url_or_filename, cache_dir=None): |
| 88 | """ |
| 89 | Given something that might be a URL (or might be a local path), |
| 90 | determine which. If it's a URL, download the file and cache it, and |
| 91 | return the path to the cached file. If it's already a local path, |
| 92 | make sure the file exists and then return the path. |
| 93 | """ |
| 94 | if cache_dir is None: |
| 95 | cache_dir = PYTORCH_PRETRAINED_BERT_CACHE |
| 96 | if sys.version_info[0] == 3 and isinstance(url_or_filename, Path): |
| 97 | url_or_filename = str(url_or_filename) |
| 98 | if sys.version_info[0] == 3 and isinstance(cache_dir, Path): |
| 99 | cache_dir = str(cache_dir) |
| 100 | |
| 101 | parsed = urlparse(url_or_filename) |
| 102 | |
| 103 | if parsed.scheme in ('http', 'https', 's3'): |
| 104 | # URL, so get it from the cache (downloading if necessary) |
| 105 | return get_from_cache(url_or_filename, cache_dir) |
| 106 | elif os.path.exists(url_or_filename): |
| 107 | # File, and it exists. |
| 108 | return url_or_filename |
| 109 | elif parsed.scheme == '': |
| 110 | # File, but it doesn't exist. |
| 111 | raise EnvironmentError("file {} not found".format(url_or_filename)) |
| 112 | else: |
| 113 | # Something unknown |
| 114 | raise ValueError("unable to parse {} as a URL or as a local path".format(url_or_filename)) |
| 115 | |
| 116 | |
| 117 | def split_s3_path(url): |
no test coverage detected