-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinePatchlets.py
202 lines (170 loc) · 7.35 KB
/
combinePatchlets.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
196
197
198
199
200
201
202
#MIT License
#
#Copyright (c) 2019 Rob Schoen
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
import random
import os
print('')
print("Current working directory is: " + os.getcwd())
print('')
#os.chdir(path)
## Get name of new combined patch
fname = input("What is name of the new combined patch? ")
print('')
# Get names and contents of patchlets to combine
print("Input names of patchlets to combine (leave off '.vcv')")
print("Put all patchlets in the current directory or add the full path")
print("Hit [Enter] to finish")
print("")
done = 0
pl = 0
patchlets = []
while done==0:
temp = input("Name of patchlet #" + str(pl+1) + " ")
if temp=="":
done = 1
else:
if os.path.exists(temp+".vcv"):
patchlets.append(temp)
pl = pl + 1
else:
print("Error: file not found")
newPatch = []; newModules = []; newCables = []; modIdMap = []
# Loop through patchlets, adding modules to new module block and cables to new cable block
for k in range(0,len(patchlets)):
print(patchlets[k] + '.vcv')
# Read contents of patchlet
f = open(patchlets[k] + '.vcv')
A = []
for line in f:
A.append(line)
f.close()
# Create the first 3 lines of a new patch ("{","version: #.#.#", "modules: ["})
if k==0:
newPatch.append(A[0])
newPatch.append(A[1])
newPatch.append(A[2])
# Extract the module block (moduleBlock)
openBracket = -99
for i in range(0,len(A)):
if ('"modules":' in A[i]):
moduleBlockStart = i+1
openBracket = 1
else:
if ('[' in A[i]): openBracket = openBracket + 1
if (']' in A[i]): openBracket = openBracket - 1
if openBracket==0:
moduleBlockEnd = i
openBracket=99
moduleBlock = A[moduleBlockStart:moduleBlockEnd]
# Change the row to the patchlet number
# Change the module id to a random number
for i in range(0,len(moduleBlock)):
if (moduleBlock[i][:14] == ' "pos": ['):
moduleBlock[i+2] = ' '+str(k)
if (moduleBlock[i][:11]== ' "id":'):
temp = moduleBlock[i][12:]; temp = temp.split(","); idnum = int(temp[0])
temp = random.randint(1,65536)
moduleBlock[i] = (' "id": ' + str(temp)+',\n')
modIdMap.append((idnum,temp,k))
# Ensure that all the modules end with a comma
if len(moduleBlock)>0:
moduleBlock[-1] = ' },\n'
# Extract the cable block
openBracket = -99
for i in range(moduleBlockEnd+1,len(A)):
if ('"cables":' in A[i]):
cableBlockStart = i+1
openBracket = 1
else:
if ('[' in A[i]): openBracket = openBracket + 1
if (']' in A[i]): openBracket = openBracket - 1
if openBracket==0:
cableBlockEnd = i
openBracket=99
cableBlock = A[cableBlockStart:cableBlockEnd]
# Ensure that all the cables end with a comma
if len(cableBlock)>0:
cableBlock[-1] = ' },\n'
# Add this module block to newModules
for i in range(0,len(moduleBlock)):
newModules.append(moduleBlock[i])
# Add this cable block to newCables
for i in range(0,len(cableBlock)):
newCables.append(cableBlock[i])
# Ensure that there is no comma at the end of the new module and cable blocks
newModules[-1] = ' }\n'
if len(newCables)>0:
newCables[-1] = ' }\n'
# Close module block with bracket and comma
newModules.append(' ],\n')
# Close cable block with bracket (no comma)
newCables.append(' ]\n')
# Add the new module block to the new patch
for i in range(0,len(newModules)):
newPatch.append(newModules[i])
# Add the new cable block to the new patch
newPatch.append(' "cables": [\n')
for i in range(0,len(newCables)):
newPatch.append(newCables[i])
# Close patch with brace
newPatch.append('}\n')
# change the module ids in the cables to match the random module ids
id = []
for i in range(0,len(newPatch)):
if (newPatch[i][:11]== ' "id":'):
temp = newPatch[i][12:]; temp = temp.split(",");
id.append(int(temp[0]))
for i in range(0,len(newPatch)):
if (newPatch[i][:22]== ' "rightModuleId":'):
#print("in " + newPatch[i])
temp = newPatch[i][23:]; temp = temp.split(","); idnum = int(temp[0])
for j in range(0,len(modIdMap)):
if modIdMap[j][0]==idnum:
newPatch[i] = (' "rightModuleId" :'+ str(modIdMap[j][1]) + ',\n')
#print(str(modIdMap[j][0])+" "+str(modIdMap[j][1]))
#print(idnum)
#print("out " + newPatch[i])
if (newPatch[i][:21]== ' "leftModuleId":'):
temp = newPatch[i][22:]; temp = temp.split(","); idnum = int(temp[0])
for j in range(0,len(modIdMap)):
if modIdMap[j][0]==idnum:
newPatch[i] = (' "leftModuleId": '+ str(modIdMap[j][1]) + ',\n')
if (newPatch[i][:23]== ' "outputModuleId":'):
temp = newPatch[i][24:]; temp = temp.split(","); idnum = int(temp[0])
for j in range(0,len(modIdMap)):
if modIdMap[j][0]==idnum:
newPatch[i] = (' "outputModuleId": '+ str(modIdMap[j][1]) + ',\n')
if (newPatch[i][:22]== ' "inputModuleId":'):
temp = newPatch[i][23:]; temp = temp.split(","); idnum = int(temp[0])
for j in range(0,len(modIdMap)):
if modIdMap[j][0]==idnum:
newPatch[i] = (' "inputModuleId": '+ str(modIdMap[j][1]) + ',\n')
' "rightModuleId": 269,'
# Write new patch to file
f = open(fname+'.vcv','w')
for j in range(0,len(newPatch)):
f.write(newPatch[j])
f.close()
print("")
print(fname + '.vcv created')
print("")
print('Merge Completed')
# latest problem: when combining multiple copies of the same patch, the cables and left/right modules are messed up