-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileinformation.py
128 lines (99 loc) · 3.77 KB
/
fileinformation.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'''
This file works with the FileInformation.xml file in mnlgxdlib / mnlgxdprog files.
It's sort of a strange format given that I think all libraries have all 500 patch files,
and all the tune and scale files, or just 1 patch file and that's it. Also all the _info
file seem to be empty. So I'm not really sure how necessary this file is, but here it
is anyway. Not sure why sound librarian needs this file and can't just scan the zip
for what's there...
'''
import os
import re
import xml.etree.ElementTree as ET
PROG_BIN_RE = 'Prog_(\d{3})\.prog_bin'
PROG_INFO_RE = 'Prog_(\d{3})\.prog_info'
TUNS_BIN_RE = 'TunS_(\d{3})\.TunS_bin'
TUNS_INFO_RE = 'TunS_(\d{3})\.TunS_info'
TUNO_BIN_RE = 'TunO_(\d{3})\.TunO_bin'
TUNO_INFO_RE = 'TunO_(\d{3})\.TunO_info'
def look_for(f, files, kind, suffix = None):
if not suffix:
suffix = kind
base = kind + '_' + '(\d{3})\.' + suffix
bin_re = base + '_bin'
info_re = base + '_info'
base_fmt = kind + '_{i:03d}' + suffix
bin_fmt = base_fmt + kind + '_bin'
info_fmt = base_fmt + kind + '_info'
found = False
m = re.match(bin_re, f)
if m:
found = True
id = m.group(1)
info = info_fmt.format(id)
if not info in files:
print 'Warning! Missing: {}'.format(info)
m = re.match(info_re, f)
if m:
id = m.group(1)
binf = bin_fmt.format(id)
if not binf in files:
print 'Warning! Missing: {}'.format(binf)
return found
def mk_file_info_xml_from_dir(dir_path):
patch_files = 0
has_fave_data = False
tune_scale_data = 0
tune_oct_data = 0
files = set(os.listdir(dir_path))
for f in files:
if f == 'FavoriteData.fav_data':
has_fave_data = True
if f == 'FileInformation.xml':
pass
elif look_for(f, files, 'Prog', 'prog'):
patch_files = patch_files + 1
elif look_for(f, files, 'TunS'):
tune_scale_data = tune_scale_data + 1
elif look_for(f, files, 'TunO'):
tune_oct_data = tune_oct_data + 1
else:
print 'Warning! Unexpected file: {}'.format(f)
return mk_file_info_xml(patch_files, has_fave_data, tune_scale_data, tune_oct_data)
def mk_file_info_xml(patch_files=0, has_fave_data=False, tune_scale_data=0, tune_oct_data=0):
root = ET.Element('KorgMSLibrarian_Data')
product = ET.SubElement(root, 'Product')
product.text = 'minilogue xd'
contents = ET.SubElement(root, 'Contents')
contents.set('NumProgramData', str(patch_files))
contents.set('NumPresetInformation', '0')
contents.set('NumTuneScaleData', str(tune_scale_data))
contents.set('NumTuneOctData', str(tune_oct_data))
if has_fave_data:
contents.set('NumFavoriteData', '1')
fave_data = ET.SubElement(contents, 'FavoriteData')
fave_file = ET.SubElement(fave_data, 'File')
fave_file.text = 'FavoriteData.fav_data'
else:
contents.set('NumFavoriteData', '0')
for i in xrange(0, patch_files):
data = ET.SubElement(contents, 'ProgramData')
info = ET.SubElement(data, 'Information')
info.text = 'Prog_{i:03d}.prog_info'.format(i)
binary = ET.SubElement(data, 'ProgramBinary')
binary.text = 'Prog_{i:03d}.prog_bin'.format(i)
for i in xrange(0, tune_scale_data):
data = ET.SubElement(contents, 'TuneScaleData')
info = ET.SubElement(data, 'Information')
info.text = 'TunS_{i:03d}.TunS_info'.format(i)
binary = ET.SubElement(data, 'TuneScaleBinary')
binary.text = 'TunS_{i:03d}.TunS_bin'.format(i)
for i in xrange(0, tune_oct_data):
data = ET.SubElement(contents, 'TuneOctData')
info = ET.SubElement(data, 'Information')
info.text = 'TunO_{i:03d}.TunO_info'.format(i)
binary = ET.SubElement(data, 'TuneScaleBinary')
binary.text = 'TunO_{i:03d}.TunO_bin'.format(i)
str = ET.tostring(root)
# there's probably a better way to do this but...
header = '<?xml version="1.0" encoding="UTF-8"?>\n\n'
str = header + str