-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathit_add_messages.py
195 lines (157 loc) · 5.37 KB
/
it_add_messages.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
"""\
Python script for batch setting of song messages,
and sample and instrument lists, of IT files.
(c) 2008 mike burke / mrb
Todo:
- nothing!
"""
#
# Set these to suit your needs.
#
# When set_*_msg = False, the designated message will be
# UNTOUCHED from the original. If you want to CLEAR a
# message or sample/inst list, use set_*_msg = True and
# an empty message.
#
# preserve_file_date: If True, new files will be created with the same
# modification time as the source files.
preserve_file_date = True
# enable_strftime: If True, strftime substitution will be performed on
# song message texts, using the file's modification
# time. A strftime string reference can be found at:
# http://www.python.org/doc/2.5.2/lib/module-time.html
enable_strftime = True
# set_sample_msg : Modify the sample list?
# append_sample_msg : Append to the end of existing sample names?
# If False, will start writing at 0;
# Otherwise, attempts to find the last sample,
# and starts writing there..
#
# If you are appending, it might make sense to insert an empty line before
# the beginning of your message, to avoid eg. a song message like:
#
# Old message
# old message
# blah blah
# old messageAPPENDED MESSAGE
# appended message blah blah
#
# If you put {FILENAME} into a message, it will be replaced with the
# name of the file currently being processed.
#
set_sample_msg = True
append_sample_msg = False
sample_msg = (
# "123456789A123456789B12345" <- anything longer than 25 characters will be truncated
"Sample Line 1",
"Sample Line 2",
"Sample Line 3",
"and so on...",
)
set_instrument_msg = True
append_instrument_msg = False
instrument_msg = (
# "123456789A123456789B12345"
"{FILENAME}",
"",
" %Y-%m-%d",
" %I:%M:%S %p",
"",
"by me! :)",
)
set_song_msg = True
append_song_msg = False
song_msg = """\
This is a song message.
It's a multi-line Python string,
so you can use multiple lines.
This file was last modified on %d %B %Y.
"""
##########################################################
# #
# You don't need to change anything beyond this point. #
# #
##########################################################
import os, os.path, sys, exceptions, traceback, time
import pyIT
def add_messages(itf, filespec):
mtime = time.localtime(os.stat(filespec).st_mtime)
if enable_strftime:
tmp_song_msg = time.strftime(song_msg, mtime)
tmp_instrument_msg = [time.strftime(m, mtime) for m in instrument_msg]
tmp_sample_msg = [time.strftime(m, mtime) for m in sample_msg]
else:
tmp_song_msg = song_msg
tmp_instrument_msg = instrument_msg
tmp_sample_msg = sample_msg
# substitutions (only filename, so far)
tmp_song_msg = tmp_song_msg.replace('{FILENAME}', os.path.basename(filespec))
tmp_instrument_msg = [m.replace('{FILENAME}', os.path.basename(filespec)) for m in tmp_instrument_msg]
tmp_sample_msg = [m.replace('{FILENAME}', os.path.basename(filespec)) for m in tmp_sample_msg]
if set_song_msg:
if append_song_msg:
itf.Message = itf.Message + tmp_song_msg
else:
itf.Message = tmp_song_msg
if set_instrument_msg:
# set instrument msg
i = 0
if not append_instrument_msg:
for instrument in itf.Instruments:
if i >= len(tmp_instrument_msg):
instrument.InstName = ''
else:
instrument.InstName = tmp_instrument_msg[i]
i = i + 1
while len(tmp_instrument_msg) > i:
iti = pyIT.ITinstrument()
iti.InstName = tmp_instrument_msg[i]
itf.Instruments.append(iti)
i = i + 1
if set_sample_msg:
# set sample msg
i = 0
if not append_sample_msg:
for sample in itf.Samples:
if i >= len(tmp_sample_msg):
sample.SampleName = ''
else:
sample.SampleName = tmp_sample_msg[i]
i = i + 1
while len(tmp_sample_msg) > i:
its = pyIT.ITsample()
its.SampleName = tmp_sample_msg[i]
itf.Samples.append(its)
i = i + 1
def process(indir, outdir):
infiles = [f for f in os.listdir(indir) if f.upper()[-3:] == '.IT']
if not os.path.exists(outdir):
sys.stderr.write("W: " + outdir + " doesn't exist so I'm creating it\n")
os.mkdir(outdir)
if not os.path.isdir(outdir):
sys.stderr.write("E: " + outdir + " is not a directory\n")
raise exceptions.OSError()
for filename in infiles:
try:
infilespec = os.path.join(indir, filename)
sys.stderr.write('I: ' + infilespec)
itf = pyIT.ITfile()
itf.open(infilespec)
sys.stderr.write(" => ")
add_messages(itf, infilespec)
outfilespec = os.path.join(outdir, filename)
sys.stderr.write(outfilespec)
itf.write(outfilespec)
if preserve_file_date:
statresult = os.stat(infilespec)
os.utime(outfilespec, (statresult.st_atime, statresult.st_mtime))
sys.stderr.write('\n')
except:
sys.stderr.write("\nE: error processing " + filename + ":\n")
traceback.print_exc()
if __name__ == '__main__':
if not len(sys.argv) == 3:
sys.stderr.write(sys.argv[0] + " indir outdir! morans\n")
sys.exit(1)
process(sys.argv[1], sys.argv[2])