-
Notifications
You must be signed in to change notification settings - Fork 3
/
vlt_zh.py
101 lines (77 loc) · 3.22 KB
/
vlt_zh.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""A small code to change all fonts in vlc theme for Chinese
arg: dir font
dir: The director where the vlt file is in or the vlt file path
font: The font file path"""
import os
import shutil
import sys
import tarfile
import tempfile
import xml.etree.ElementTree as ET
import zipfile
def totgz(file_in, file_out):
"Compress a directory into a tgz file"
with tarfile.open(file_out, "w:gz") as tar:
for file in os.listdir(file_in):
tar.add(f"{file_in}/{file}", arcname=file)
def untgz(file_in, file_out):
"Unzip a directory from a tgz file"
with tarfile.open(file_in, "r:gz") as tar:
def is_within_directory(directory, target):
abs_directory = os.path.abspath(directory)
abs_target = os.path.abspath(target)
prefix = os.path.commonprefix([abs_directory, abs_target])
return prefix == abs_directory
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
for member in tar.getmembers():
member_path = os.path.join(path, member.name)
if not is_within_directory(path, member_path):
raise Exception("Attempted Path Traversal in Tar File")
tar.extractall(path, members, numeric_owner=numeric_owner)
safe_extract(tar, file_out)
def tozip(file_in, file_out):
"Compress a directory into a zip file"
shutil.make_archive(file_out, 'zip', file_in)
shutil.move(f"{file_out}.zip", file_out)
def unzip(file_in, file_out):
"Unzip a directory from a zip file"
with zipfile.ZipFile(file_in, "r") as zipf:
zipf.extractall(file_out)
def change_theme(temp_dir, font):
"Move the font file and change the font field"
for root, _, files in os.walk(temp_dir):
for file in files:
if file == "theme.xml":
shutil.copy(font, f"{root}/{os.path.basename(font)}")
theme_file = f"{root}/{file}"
break
theme_tree = ET.parse(theme_file)
for font_ele in theme_tree.iter(tag='Font'):
font_ele.set('file', f"{os.path.basename(font)}")
theme_tree.write(theme_file)
def main(argv):
"Read all vlt file in the directory and creat new file"
vlt_path = argv[1]
font = argv[2]
if os.path.isfile(vlt_path):
vlt_dir = [(f"{vlt_path}", f"{vlt_path}_zh.vlt")]
else:
vlt_dir = [(f"{vlt_path}/{vlt_file}",
f"{vlt_path}/{vlt_file[:-4]}_zh.vlt")
for vlt_file in os.listdir(vlt_path)
if vlt_file.endswith(".vlt")
if os.path.isfile(f"{vlt_path}/{vlt_file}")]
for vlt_file, zh_vlt_file in vlt_dir:
with tempfile.TemporaryDirectory() as temp_dir:
if tarfile.is_tarfile(vlt_file):
untgz(vlt_file, temp_dir)
change_theme(temp_dir, font)
totgz(temp_dir, zh_vlt_file)
elif zipfile.is_zipfile(vlt_file):
unzip(vlt_file, temp_dir)
change_theme(temp_dir, font)
tozip(temp_dir, zh_vlt_file)
else:
print("Unsupport format")
if __name__ == "__main__":
main(sys.argv)