-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresx2files.py
56 lines (45 loc) · 1.58 KB
/
resx2files.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
import os, sys, getopt, base64
import xml.etree.ElementTree as ET
def resx2files(resx, outdir):
tree = ET.parse(resx)
root = tree.getroot()
os.makedirs(outdir, exist_ok=True)
for data in root.iter('data'):
name = data.get('name')
type = data.get('type')
value = data.findtext('value')
filename = os.path.join(outdir, name)
if isinstance(type, str) and type.startswith('System.Byte[]'):
bin = base64.decodebytes(bytes(value, 'ASCII'))
with open(filename, 'bw') as f:
f.write(bin)
else:
with open(filename, 'w') as f:
f.write(value)
def usage():
print(f'usage: python3 {sys.argv[0]} [--help] [-o OUTDIR] FILE\n')
print(f'optional arguments:')
print(f' -h, --help show this help message and exit')
print(f' -o OUTDIR use OUTDIR as output directory, default: the input filename without extension')
print(f' FILE The input filename')
print()
print(f'example:')
print(f' python3 {sys.argv[0]} Device.resx')
try:
optlist, args = getopt.getopt(sys.argv[1:], 'ho:', ['help'])
except getopt.error as err:
print(err)
usage()
sys.exit(2)
if len(optlist) == 0 and len(args) == 0 or len(args) > 1:
usage()
sys.exit(0)
file = args[0]
outdir = os.path.splitext(os.path.basename(file))[0]
for current_argument, current_value in optlist:
if current_argument in ('-h', '--help'):
usage()
sys.exit(0)
elif current_argument in ('-o'):
outdir = current_value
resx2files(file, outdir)