diff --git a/combinePatchlets.py b/combinePatchlets.py index 9847245..f4f58e7 100644 --- a/combinePatchlets.py +++ b/combinePatchlets.py @@ -23,180 +23,180 @@ import random import os -print('') +print('') print("Current working directory is: " + os.getcwd()) -print('') +print('') #os.chdir(path) ## Get name of new combined patch -fname = input("What is name of the new combined patch? ") -print('') +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("") +print("") done = 0 pl = 0 patchlets = [] -while done==0: +while done==0: temp = input("Name of patchlet #" + str(pl+1) + " ") - if temp=="": + if temp=="": done = 1 - else: + else: if os.path.exists(temp+".vcv"): - patchlets.append(temp) + 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') - +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() - + f.close() + # Create the first 3 lines of a new patch ("{","version: #.#.#", "modules: ["}) - if k==0: - newPatch.append(A[0]) + if k==0: + newPatch.append(A[0]) newPatch.append(A[1]) newPatch.append(A[2]) - - # Extract the module block (moduleBlock) + + # Extract the module block (moduleBlock) openBracket = -99 - for i in range(0,len(A)): + 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 ('[' in A[i]): openBracket = openBracket + 1 + if (']' in A[i]): openBracket = openBracket - 1 if openBracket==0: moduleBlockEnd = i openBracket=99 - moduleBlock = A[moduleBlockStart:moduleBlockEnd] - + 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)): + 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]) + 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') + 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 + moduleBlock[-1] = ' },\n' + + # Extract the cable block openBracket = -99 - for i in range(moduleBlockEnd+1,len(A)): + 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 ('[' in A[i]): openBracket = openBracket + 1 + if (']' in A[i]): openBracket = openBracket - 1 if openBracket==0: cableBlockEnd = i openBracket=99 - cableBlock = A[cableBlockStart:cableBlockEnd] - + 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 + + # 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: +if len(newCables)>0: newCables[-1] = ' }\n' # Close module block with bracket and comma -newModules.append(' ],\n') +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') +newPatch.append(' "cables": [\n') for i in range(0,len(newCables)): newPatch.append(newCables[i]) -# Close patch with brace +# 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)): + +# 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(","); + temp = newPatch[i][12:]; temp = temp.split(","); id.append(int(temp[0])) -for i in range(0,len(newPatch)): - if (newPatch[i][:22]== ' "rightModuleId":'): +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])) + 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]) + #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: + 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]) + 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: + 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]) + 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: + if modIdMap[j][0]==idnum: newPatch[i] = (' "inputModuleId": '+ str(modIdMap[j][1]) + ',\n') - + ' "rightModuleId": 269,' - -# Write new patch to file + +# Write new patch to file f = open(fname+'.vcv','w') for j in range(0,len(newPatch)): f.write(newPatch[j]) -f.close() -print("") +f.close() +print("") print(fname + '.vcv created') -print("") +print("") print('Merge Completed') - + # latest problem: when combining multiple copies of the same patch, the cables and left/right modules are messed up diff --git a/extractPatchlets.py b/extractPatchlets.py index 4ea9ec6..1a1220d 100644 --- a/extractPatchlets.py +++ b/extractPatchlets.py @@ -23,10 +23,10 @@ import re import os -print('') +print('') print("Current working directory is: " + os.getcwd()) -print('') -print('') +print('') +print('') print('Which .vcv file would you like to create patchlets from?') fname = input("? ") print("") @@ -43,186 +43,164 @@ temp = re.findall(r'\"(.+?)\"',A[1]) rackVersion = temp[1]; -# Extract the module block (moduleBlock) +# Extract the module block (moduleBlock) openBracket = -99 -for i in range(0,len(A)): +for i in range(0,len(A)): if (A[i]== ' "modules": [\n'): moduleBlockStart = i+1 openBracket = 1 else: - if ('[' in A[i]): openBracket = openBracket + 1 - if (']' in A[i]): openBracket = openBracket - 1 + 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] - -# Extract the cable block (cableBlock) +moduleBlock = A[moduleBlockStart:moduleBlockEnd] + +# Extract the cable block (cableBlock) openBracket = -99 -for i in range(moduleBlockEnd+1,len(A)): +for i in range(moduleBlockEnd+1,len(A)): if (A[i]== ' "cables": [\n'): cableBlockStart = i+1 openBracket = 1 else: - if ('[' in A[i]): openBracket = openBracket + 1 - if (']' in A[i]): openBracket = openBracket - 1 + 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] +cableBlock = A[cableBlockStart:cableBlockEnd] # Create list of all the modules modules = []; numModules = 0; openbracket = 0 -for i in range(0,len(moduleBlock)): +for i in range(0,len(moduleBlock)): if ('{' in moduleBlock[i]): if openbracket==0: cc = [] - openbracket = openbracket + 1 - if openbracket>0: - cc.append(moduleBlock[i]) - if ('}' in moduleBlock[i]): openbracket = openbracket - 1 - if openbracket == 0: + openbracket = openbracket + 1 + if openbracket>0: + cc.append(moduleBlock[i]) + if ('}' in moduleBlock[i]): openbracket = openbracket - 1 + if openbracket == 0: numModules = numModules + 1 modules.append(cc) - + # Create list of all the cables cables = []; opencable = 0; numCables = 0 -for i in range(0,len(cableBlock)): - if (cableBlock[i]== ' {\n'): - opencable = 1 +for i in range(0,len(cableBlock)): + if (cableBlock[i]== ' {\n'): + opencable = 1 cc = [] - if opencable == 1: - cc.append(cableBlock[i]) - if (' }' in cableBlock[i]): - opencable = 0 + if opencable == 1: + cc.append(cableBlock[i]) + if (' }' in cableBlock[i]): + opencable = 0 numCables = numCables + 1 cables.append(cc) # Create list of all module IDs, module rows, module columns -col = []; row = []; rowrow = []; colrow = []; id = [] -for i in range(0,numModules): +col = []; row = []; rowrow = []; colrow = []; id = [] +for i in range(0,numModules): A = modules[i] - for j in range(0,len(A)): - if (A[j][:11] == ' "id":'): + for j in range(0,len(A)): + if (A[j][:11] == ' "id":'): temp = re.findall(r'\d',A[j]) temp = int("".join(list(map(str,temp)))) - id.append(temp) + id.append(temp) if A[j][:14] == ' "pos": [': temp = re.findall(r'\d',A[j+1]) temp1 = int("".join(list(map(str,temp)))) temp = re.findall(r'\d',A[j+2]) temp2 = int("".join(list(map(str,temp)))) - col.append(temp1) + col.append(temp1) row.append(temp2) - colrow.append(j+1) - rowrow.append(j+2) - + colrow.append(j+1) + rowrow.append(j+2) + # Create list of all cable inputs and outputs -outMod = []; inMod = []; outId = []; inId = [] -for i in range(0,numCables): +outMod = []; inMod = []; outId = []; inId = [] +for i in range(0,numCables): A = cables[i] - for j in range(0,len(A)): - if (('outputModuleId') in A[j]): + for j in range(0,len(A)): + if (('outputModuleId') in A[j]): temp = re.findall(r'\d',A[j]) temp = int("".join(list(map(str,temp)))) outMod.append(temp) - if (('inputModuleId') in A[j]): + if (('inputModuleId') in A[j]): temp = re.findall(r'\d',A[j]) temp = int("".join(list(map(str,temp)))) inMod.append(temp) - if (('outputId') in A[j]): + if (('outputId') in A[j]): temp = re.findall(r'\d',A[j]) temp = int("".join(list(map(str,temp)))) - outId.append(temp) - if (('inputId') in A[j]): + outId.append(temp) + if (('inputId') in A[j]): temp = re.findall(r'\d',A[j]) temp = int("".join(list(map(str,temp)))) inId.append(temp) -# Create patchlets +# Create patchlets for k in range(0,max(row)+1): - + # Create the first 3 lines of a new patchlet ("{","version: #.#.#", "modules: ["}) - new_file = original[0:3] - + new_file = original[0:3] + # Find leftmost x-coordinate in the row mincol = 9999 for u in range(0,len(modules)): if (row[u]==k): if col[u]0: - new_cables[-1][-1] = ' }\n' - - # Add module block to new patchlet + new_cables.append(cables[u]) + + if len(new_cables)>0: + new_cables[-1][-1] = ' }\n' + + # Add module block to new patchlet for u in range(0,len(new_modules)): for j in range(0,len(new_modules[u])): - new_file.append(new_modules[u][j]) + new_file.append(new_modules[u][j]) # Close module block with bracket and comma - new_file.append(' ],\n') - + new_file.append(' ],\n') + # Open cable block - new_file.append('"cables": [\n') - for u in range(0,len(new_cables)): - for j in range(0,len(new_cables[u])): - new_file.append(new_cables[u][j]) - + new_file.append('"cables": [\n') + for u in range(0,len(new_cables)): + for j in range(0,len(new_cables[u])): + new_file.append(new_cables[u][j]) + # Close cable block with bracket (no comma) new_file.append(' ]\n') - + # Close patch with brace new_file.append('}\n') - + # Save each new patchlet plet_name = fname + "_" + str(k+1) + '.vcv' f = open(plet_name,'w') for j in range(0,len(new_file)): f.write(new_file[j]) - f.close() - print('Row ' + str(k+1) + " -> " + plet_name) - -print('Extraction Completed') - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + f.close() + print('Row ' + str(k+1) + " -> " + plet_name) + +print('Extraction Completed') diff --git a/vcvreader.py b/vcvreader.py new file mode 100644 index 0000000..9c0a4e0 --- /dev/null +++ b/vcvreader.py @@ -0,0 +1,49 @@ +import re +import os +import json + +def get_file_name(): + print('') + print("Current working directory is: " + os.getcwd()) + print('') + print('') + print('Which .vcv file would you like to create patchlets from?') + fname = input("? ") + print("") + return fname + + +def get_patchlets(modules): + mlist = [] + for current_module in modules: + mlist.append(current_module['pos'][1]) + h=max(mlist) + parray=[] + for x in range(0,h+1): + parray.append([]) + for current_module in modules: + parray[current_module['pos'][1]].append(current_module['id']) + return (parray) + + +def crawl_file(c_module,all_modules,keys): + for module in all_modules: + if c_module == module['id']: + r_dict={} + for c_key in keys: + r_dict[str(c_key)]=module[str(c_key)] + + return (r_dict) + + +def print_patchlets(): +# with open(get_file_name() + '.vcv') as json_file: + with open('generative.vcv') as json_file: + data = json.load(json_file) +# patchlet_array=get_number_of_patchlets(data['modules']) + for current_patchlet in get_patchlets(data['modules']): + print ("new patchlet") + for current_module in current_patchlet: + print (crawl_file(current_module,data['modules'],["plugin","model"])) + +print_patchlets()