-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcache.py
More file actions
62 lines (41 loc) · 1.48 KB
/
Copy pathcache.py
File metadata and controls
62 lines (41 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Dataset cache utility functions"""
import zipfile
from pathlib import Path
from functools import partial
from requests import get as _get_file
get_file = partial(_get_file, timeout=30)
from data_profiling.utils.paths import get_data_path
def cache_file(file_name: str, url: str) -> Path:
"""Check if file_name already is in the data path, otherwise download it from url.
Args:
file_name: the file name
url: the URL of the dataset
Returns:
The relative path to the dataset
"""
data_path = get_data_path()
data_path.mkdir(exist_ok=True)
file_path = data_path / file_name
# If not exists, download and create file
if not file_path.exists():
response = get_file(url, allow_redirects=True)
response.raise_for_status()
file_path.write_bytes(response.content)
return file_path
def cache_zipped_file(file_name: str, url: str) -> Path:
"""Check if file_name already is in the data path, otherwise download it from url.
Args:
file_name: the file name
url: the URL of the dataset
Returns:
The relative path to the dataset
"""
data_path = get_data_path()
file_path = data_path / file_name
# If not exists, download and create file
if not file_path.exists():
tmp_path = cache_file("tmp.zip", url)
with zipfile.ZipFile(tmp_path, "r") as zip_file:
zip_file.extract(file_path.name, data_path)
tmp_path.unlink()
return file_path