-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.py
50 lines (40 loc) · 1.02 KB
/
image.py
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
import tarfile
import zipfile
from pathlib import Path
from functools import lru_cache
import config
@lru_cache()
def get_zip_fd(p: Path) -> zipfile.ZipFile:
z = zipfile.ZipFile(p, 'r')
return z
def get_zip_file(p: Path, file) -> bytes:
p = Path(p)
if not p.is_absolute():
p = config.root / p
z = get_zip_fd(p)
filedata = None
with z.open(file) as f:
filedata = f.read()
return filedata
@lru_cache()
def get_tar_fd(p: Path) -> tarfile.TarFile:
z = tarfile.open(p, 'r')
return z
def get_tar_file(p: Path, file) -> bytes:
p = Path(p)
if not p.is_absolute():
p = config.root / p
tf = get_tar_fd(p)
filedata = None
file_info = tf.getmember(file)
with tf.extractfile(file_info) as f:
filedata = f.read()
return filedata
def get_dir_file(p: Path, file) -> bytes:
p = Path(p)
if not p.is_absolute():
p = config.root / p
filedata = None
with open(p / Path(file), 'rb') as f:
filedata = f.read()
return filedata