Return the url and etag (which may be ``None``) stored for `filename`. Raise ``EnvironmentError`` if `filename` or its stored metadata do not exist.
(filename, cache_dir=None)
| 55 | |
| 56 | |
| 57 | def filename_to_url(filename, cache_dir=None): |
| 58 | """ |
| 59 | Return the url and etag (which may be ``None``) stored for `filename`. |
| 60 | Raise ``EnvironmentError`` if `filename` or its stored metadata do not exist. |
| 61 | """ |
| 62 | if cache_dir is None: |
| 63 | cache_dir = PYTORCH_PRETRAINED_BERT_CACHE |
| 64 | if sys.version_info[0] == 3 and isinstance(cache_dir, Path): |
| 65 | cache_dir = str(cache_dir) |
| 66 | |
| 67 | cache_path = os.path.join(cache_dir, filename) |
| 68 | if not os.path.exists(cache_path): |
| 69 | raise EnvironmentError("file {} not found".format(cache_path)) |
| 70 | |
| 71 | meta_path = cache_path + '.json' |
| 72 | if not os.path.exists(meta_path): |
| 73 | raise EnvironmentError("file {} not found".format(meta_path)) |
| 74 | |
| 75 | with open(meta_path, encoding="utf-8") as meta_file: |
| 76 | metadata = json.load(meta_file) |
| 77 | url = metadata['url'] |
| 78 | etag = metadata['etag'] |
| 79 | |
| 80 | return url, etag |
| 81 | |
| 82 | |
| 83 | def cached_path(url_or_filename, cache_dir=None): |