-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_stf.py
executable file
·147 lines (121 loc) · 3.67 KB
/
rename_stf.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
#!/usr/bin/python
import glob
import re
import os
with open("Stf-contents-list-numerical.csv") as f:
lines = f.readlines()
tunes = {}
tags = {}
for line in lines:
fields = line.split("@")
try:
number = int(fields[0])
tunes[number] = fields[1]
if "Kendrick" in fields[3] or \
"Redman" in fields[3] or \
"Hughes" in fields[3] or \
"John L. Bell" in fields[3] or \
"Utterbach" in fields[3] or \
"Tomlin" in fields[3] or \
"Townend" in fields[3] or \
"Andy Park" in fields[3] or \
"Doerksen" in fields[3] or \
"Brenton Brown" in fields[3]:
tags[number] = "M"
elif "Wesley" in fields[3] or \
"Pratt Green" in fields[3] or \
"Isaac Watts" in fields[3] or \
"John Newton" in fields[3] or \
"Brian Wren" in fields[3]:
tags[number] = "H"
except:
pass
with open("HAP-cross-reference.csv") as f:
lines = f.readlines()
HAP = {}
for line in lines:
fields = line.split("@")
try:
istf = int(fields[1])
ihap = int(fields[2])
HAP[istf] = ihap
except:
pass
rexp = re.compile( "^music/stf/(\d+)([a-z]*) +([^\(]+)([^\)]*)?(.*)\.MID$" )
oldnames = {}
records = []
haprecords = []
numbers = []
maxnumber = 0
for file in glob.glob("music/stf/*.MID"):
match = rexp.search(file)
if match:
number = int( match.group(1).strip() )
alt = match.group(2).strip()
title = match.group(3).strip()
ftitle = title.replace(" ","_")
ftitle = re.sub(r'\W', '', ftitle)
tune = match.group(4).replace("(","").strip()
ftune = tune.replace(" ","_")
ftune = re.sub(r'\W', '', ftune)
extra = match.group(5).strip()
if tune != "" and extra == "":
print("!!! {0} has missing ) after tune".format(file))
break
if extra.replace(")","").strip() != "":
print("!!! {0} has unexpected text after tune".format(file))
break
if tune == "":
if number in tunes:
tune = tunes[number]
else:
print("!!! {0} did not match the regexp".format(file))
break
if ftune != "":
newname = "{0}_{1}_{2}.mid".format( number, ftitle, ftune )
elif alt == "":
newname = "{0}_{1}.mid".format( number, ftitle )
else:
print("!!! {0} did not include a tune".format(file))
break
if newname in oldnames:
print("!!! Duplicate new file name {0}".format(newname))
break
else:
oldnames[newname] = file
if number > maxnumber:
maxnumber = number
if number in tags:
tag = tags[number]
else:
if "thee" in title.lower() or \
"thou" in title.lower() or \
"thine" in title.lower() or \
"thy" in title.lower():
tag = "H"
else:
tag = ""
if number >= 165 and number <= 189:
tag += "A"
elif number >= 190 and number <= 222:
tag += "C"
elif number >= 234 and number <= 241:
tag += "L"
elif number >= 292 and number <= 316:
tag += "E"
records.append("{0}@stf/{1}@{4}@STF@{2}@{3}@KEYBD@STF@@0@0@0@-1\n".format(title,newname,number,tune,tag))
numbers.append(number)
if number in HAP:
haprecords.append("{0}@stf/{1}@@HAP@{2}@{3}@KEYBD@STF@@0@0@0@-1\n".format(title,newname,HAP[number],tune))
for newname in oldnames:
os.rename( oldnames[newname], "music/stf/{0}".format(newname) )
fd = open( "cpmusic.txt", "a" )
nnum = len( numbers)
for number in range(1,maxnumber+1):
for j in range(nnum):
if numbers[j] == number:
fd.write(records[j])
for record in haprecords:
fd.write(record)
fd.close()
print( "New catalogue data appended to 'cpmusic.txt'" )