-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpatchbook.py
executable file
·575 lines (490 loc) · 18 KB
/
patchbook.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#!/usr/bin/env python3
"""
PATCHBOOK MARKUP LANGUAGE & PARSER
CREATED BY SPEKTRO AUDIO
http://spektroaudio.com/
"""
import sys
import re
import os
import argparse
import json
# Parser INFO
parserVersion = "b3"
# Reset main dictionary
mainDict = {
"info": {"patchbook_version": parserVersion},
"modules": {},
"comments": []
}
# Available connection types
connectionTypes = {
"->": "audio",
">>": "cv",
"p>": "pitch",
"g>": "gate",
"t>": "trigger",
"c>": "clock"
}
# Reset global variables
lastModuleProcessed = ""
lastVoiceProcessed = ""
# Parse script arguments
parser = argparse.ArgumentParser()
parser.add_argument("-file", type=str, default="",
help="Name of the text file that will be parsed (including extension)")
parser.add_argument("-debug", type=int, default=0,
help="Enable Debugging Mode")
parser.add_argument("-dir", type=str, default="LR",
help="Graph direction: LR (left-to-right) or DN (top-to-bottom)")
parser.add_argument("-modules", action="store_const", const="modules", dest="command",
help="Print all modules")
parser.add_argument("-print", action="store_const", const="print", dest="command",
help="Print data structure")
parser.add_argument("-export", action="store_const", const="export", dest="command",
help="Print JSON")
parser.add_argument("-connections", action="store_const", const="connections", dest="command",
help="Print connections")
parser.add_argument("-graph", action="store_const", const="graph", dest="command",
help="Print dot code for graph")
args = parser.parse_args()
filename = args.file
debugMode = args.debug
direction = args.dir
if args.command:
one_shot_command = args.command
quiet = True
else:
one_shot_command = None
quiet = False
connectionID = 0
# Set up debugMode
if args.debug == 1:
debugMode = True
else:
debugMode = False
def initial_print():
global quiet
if not quiet:
print()
print("██████████████████████████████")
print(" PATCHBOOK PARSER ")
print(" Created by Spektro Audio ")
print("██████████████████████████████")
print()
print("Version " + parserVersion)
print()
def get_script_path():
# Get path to python script
return os.path.dirname(os.path.realpath(sys.argv[0]))
def getFilePath(filename):
try:
# Append script path to the filename
base_dir = get_script_path()
filepath = os.path.join(base_dir, filename)
if debugMode:
print("File path: " + filepath)
return filepath
except IndexError:
pass
def parseFile(filename):
# This function reads the txt file and process each line.
global quiet
lines = []
try:
if not quiet: print("Loading file: " + filename)
with open(filename, "r") as file:
for l in file:
lines.append(l)
regexLine(l)
except TypeError:
print("ERROR. Please add text file path after the script.")
except FileNotFoundError:
print("ERROR. File not found.")
if not quiet:
print("File successfully processed.")
print()
def regexLine(line):
global lastModuleProcessed
global lastVoiceProcessed
if debugMode:
print()
if debugMode:
print("Processing: " + line)
# CHECK FOR COMMENTS
if debugMode:
print("Checking input for comments...")
re_filter = re.compile(r"^\/\/\s?(.+)$") # Regex for "// Comments"
re_results = re_filter.search(line.strip())
try:
comment = re_results.group().replace("//", "").strip()
if debugMode:
print("New comment found: " + comment)
addComment(comment)
return
except AttributeError:
pass
# CHECK FOR VOICES
if debugMode:
print("Cheking input for voices...")
re_filter = re.compile(r"^(.+)\:$") # Regex for "VOICE 1:"
re_results = re_filter.search(line)
try:
# For some reason the Regex filter was still detecting parameter declarations as voices,
# so I'm also running the results through an if statement.
results = re_results.group().replace(":", "")
if "*" not in results and "-" not in results and "|" not in results:
if debugMode:
print("New voice found: " + results.upper())
lastVoiceProcessed = results.upper()
return
except AttributeError:
pass
# CHECK FOR CONNECTIONS
if debugMode:
print("Cheking input for connections...")
re_filter = re.compile(
r"\-\s(.+)[(](.+)[)]\s(\>\>|\-\>|[a-z]\>)\s(.+)[(](.+)[)]\s(\[.+\])?$")
re_results = re_filter.search(line)
try:
results = re_results.groups()
voice = lastVoiceProcessed
if len(results) == 6:
if debugMode:
print("New connection found, parsing info...")
# args = parseArguments(results[5])
# results = results[:5]
addConnection(results, voice)
return
except AttributeError:
pass
# CHECK PARAMETERS
if debugMode:
print("Checking for parameters...")
# If single-line parameter declaration:
re_filter = re.compile(r"^\*\s(.+)\:\s?(.+)?$")
re_results = re_filter.search(line.strip())
try:
# Get module name
results = re_results.groups()
module = results[0].strip().lower()
if debugMode:
print("New module found: " + module)
if results[1] != None:
# If parameters are also declared
parameters = results[1].split(" | ")
for p in parameters:
p = p.split(" = ")
addParameter(module, p[0].strip().lower(), p[1].strip())
return
elif results[1] == None:
if debugMode:
print("No parameters found. Storing module as global variable...")
lastModuleProcessed = module
return
except AttributeError:
pass
# If multi-line parameter declaration:
if "|" in line and "=" in line and "*" not in line:
module = lastModuleProcessed.lower()
if debugMode:
print("Using global variable: " + module)
parameter = line.split(" = ")[0].replace("|", "").strip().lower()
value = line.split(" = ")[1].strip()
addParameter(module, parameter, value)
return
def parseArguments(args):
# This method takes an arguments string like "[color = blue]" and converts it to a dictionary
args_string = args.replace("[", "").replace("]", "")
args_array = args_string.split(",")
args_dict = {}
if debugMode:
print("Parsing arguments: " + args)
for item in args_array:
item = item.split("=")
name = item[0].strip()
value = item[1].strip()
args_dict[name] = value
if debugMode:
print(name + " = " + value)
if debugMode:
print("All arguments processes.")
return args_dict
def addConnection(list, voice="none"):
global mainDict
global connectionTypes
global connectionID
connectionID += 1
if debugMode:
print("Adding new connection...")
print("-----")
output_module = list[0].lower().strip()
output_port = list[1].lower().strip()
if debugMode:
print("Output module: " + output_module)
print("Output port: " + output_port)
try:
connection_type = connectionTypes[list[2].lower()]
if debugMode:
print("Matched connection type: " + connection_type)
except KeyError:
print("Invalid connection: " + list[2])
connection_type = "cv"
input_module = list[3].lower().strip()
input_port = list[4].lower().strip()
if list[5] is not None:
arguments = parseArguments(list[5])
else:
arguments = {}
if debugMode:
print("Input module: " + input_module)
print("Input port: " + output_port)
checkModuleExistance(output_module, output_port, "out")
checkModuleExistance(input_module, input_port, "in")
if debugMode:
print("Appending output and input connections to mainDict...")
output_dict = {
"input_module": input_module,
"input_port": input_port,
"connection_type": connection_type,
"voice": voice,
"id": connectionID}
input_dict = {
"output_module": output_module,
"output_port": output_port,
"connection_type": connection_type,
"voice": voice,
"id": connectionID}
for key in arguments:
output_dict[key] = arguments[key]
input_dict[key] = arguments[key]
mainDict["modules"][output_module]["connections"]["out"][output_port].append(
output_dict)
mainDict["modules"][input_module]["connections"]["in"][input_port] = input_dict
if debugMode:
print("-----")
def checkModuleExistance(module, port="port", direction=""):
global mainDict
if debugMode:
print("Checking if module already existing in main dictionary: " + module)
# Check if module exists in main dictionary
if module not in mainDict["modules"]:
mainDict["modules"][module] = {
"parameters": {},
"connections": {"out": {}, "in": {}}
}
# If it exists, check if the port exists
if direction == "in":
if port not in mainDict["modules"][module]["connections"]["in"]:
mainDict["modules"][module]["connections"]["in"][port] = []
if direction == "out":
if port not in mainDict["modules"][module]["connections"]["out"]:
mainDict["modules"][module]["connections"]["out"][port] = []
def addParameter(module, name, value):
checkModuleExistance(module)
# Add parameter to mainDict
if debugMode:
print("Adding parameter: " + module + " - " + name + " - " + value)
mainDict["modules"][module]["parameters"][name] = value
def addComment(value):
mainDict["comments"].append(value)
def askCommand(command=None):
global one_shot_command
if one_shot_command:
command = one_shot_command
if not command:
command = input("> ").lower().strip()
if command == "module":
detailModule()
elif command == "modules":
detailModule(all=True)
elif command == "print":
printDict()
elif command == "export":
exportJSON()
elif command == "connections":
printConnections()
elif command == "graph":
graphviz()
else:
print("Invalid command, please try again.")
if one_shot_command:
return
askCommand()
def _print_module(module):
global mainDict, quiet
print("-------")
print("Showing information for module: " + module.upper())
print()
print("Inputs:")
for c in mainDict["modules"][module]["connections"]["in"]:
keyvalue = mainDict["modules"][module]["connections"]["in"][c]
print(keyvalue["output_module"].title() + " (" + keyvalue["output_port"].title(
) + ") > " + c.title() + " - " + keyvalue["connection_type"].title())
print()
print("Outputs:")
for x in mainDict["modules"][module]["connections"]["out"]:
port = mainDict["modules"][module]["connections"]["out"][x]
for c in port:
keyvalue = c
print(x.title() + " > " + keyvalue["input_module"].title() + " (" + keyvalue["input_port"].title(
) + ") " + " - " + keyvalue["connection_type"].title() + " - " + keyvalue["voice"])
print()
print("Parameters:")
for p in mainDict["modules"][module]["parameters"]:
value = mainDict["modules"][module]["parameters"][p]
print(p.title() + " = " + value)
print()
if not quiet: print("-------")
def detailModule(all=False):
global mainDict
if not all:
module = input("Enter module name: ").lower()
if module in mainDict["modules"]:
_print_module(module)
else:
for module in mainDict["modules"]:
_print_module(module)
def printConnections():
print()
print("Printing all connections by type...")
print()
for ctype in connectionTypes:
ctype_name = connectionTypes[ctype]
print("Connection type: " + ctype_name)
# For each module
for module in mainDict["modules"]:
# Get all outgoing connections:
connections = mainDict["modules"][module]["connections"]["out"]
for c in connections:
connection = connections[c]
for subc in connection:
# print(connection)
if subc["connection_type"] == ctype_name:
print(module.title(
) + " > " + subc["input_module"].title() + " (" + subc["input_port"].title() + ") ")
print()
def exportJSON():
# Exports mainDict as json file
# name = filename.split(".")[0]
# filepath = getFilePath(name + '.json')
# print("Exporting dictionary as file: " + filepath)
# with open(filepath, 'w') as fp:
# json.dump(mainDict, fp)
print(json.dumps(mainDict))
def graphviz():
global quiet, direction
linetypes = {
"audio": {"style": "bold"},
"cv": {"color": "gray"},
"gate": {"color": "red", "style": "dashed"},
"trigger": {"color": "orange", "style": "dashed"},
"pitch": {"color": "blue"},
"clock": {"color": "purple", "style": "dashed"}
}
if direction == "DN":
rank_dir_token = "rankdir = BT;\n"
from_token = ":s -> "
to_token = ":n "
else:
rank_dir_token = "rankdir = LR;\n"
from_token = ":e -> "
to_token = ":w "
if not quiet:
print("Generating signal flow code for GraphViz.")
print("Copy the code between the line break and paste it into https://dreampuf.github.io/GraphvizOnline/ to download a SVG / PNG chart.")
conn = []
total_string = ""
if not quiet: print("-------------------------")
print("digraph G{\n" + rank_dir_token + "splines = polyline;\nordering=out;")
total_string += "digraph G{\n" + rank_dir_token + "splines = polyline;\nordering=out;\n"
for module in sorted(mainDict["modules"]):
# Get all outgoing connections:
outputs = mainDict["modules"][module]["connections"]["out"]
module_outputs = ""
out_count = 0
for out in sorted(outputs):
out_count += 1
out_formatted = "_" + re.sub('[^A-Za-z0-9]+', '', out)
module_outputs += "<" + out_formatted + "> " + out.upper()
if out_count < len(outputs.keys()):
module_outputs += " | "
connections = outputs[out]
for c in connections:
line_style_array = []
graphviz_parameters = [
"color", "weight", "style", "arrowtail", "dir"]
for param in graphviz_parameters:
if param in c:
line_style_array.append(param + "=" + c[param])
elif param in linetypes[c["connection_type"]]:
line_style_array.append(
param + "=" + linetypes[c["connection_type"]][param])
if len(line_style_array) > 0:
line_style = "[" + ', '.join(line_style_array) + "]"
else:
line_style = ""
in_formatted = "_" + \
re.sub('[^A-Za-z0-9]+', '', c["input_port"])
connection_line = module.replace(" ", "") + ":" + out_formatted + from_token + \
c["input_module"].replace(
" ", "") + ":" + in_formatted + to_token + line_style
conn.append([c["input_port"], connection_line])
# Get all incoming connections:
inputs = mainDict["modules"][module]["connections"]["in"]
module_inputs = ""
in_count = 0
for inp in sorted(inputs):
inp_formatted = "_" + re.sub('[^A-Za-z0-9]+', '', inp)
in_count += 1
module_inputs += "<" + inp_formatted + "> " + inp.upper()
if in_count < len(inputs.keys()):
module_inputs += " | "
# Get all parameters:
params = mainDict["modules"][module]["parameters"]
module_params = ""
param_count = 0
for par in sorted(params):
param_count += 1
module_params += par.title() + " = " + params[par]
if param_count < len(params.keys()):
module_params += r'\n'
# If module contains parameters
if module_params != "":
# Add them below module name
middle = "{{" + module.upper() + "}|{" + module_params + "}}"
else:
# Otherwise just display module name
middle = module.upper()
final_box = module.replace(
" ", "") + "[label=\"{ {" + module_inputs + "}|" + middle + "| {" + module_outputs + "}}\" shape=Mrecord]"
print(final_box)
total_string += final_box + "; "
# Print Connections
for c in sorted(conn):
print(c[1])
total_string += c[1] + "; "
if len(mainDict["comments"]) != 0:
format_comments = ""
comments_count = 0
for comment in mainDict["comments"]:
comments_count += 1
format_comments += "{" + comment + "}"
if comments_count < len(mainDict["comments"]):
format_comments += "|"
format_comments = "comments[label=<{{{<b>PATCH COMMENTS</b>}|" + format_comments + "}}> shape=Mrecord]"
print(format_comments)
print("}")
total_string += "}"
if not quiet:
print("-------------------------")
print()
return total_string
def printDict():
global mainDict
for key in mainDict["modules"]:
print(key.title() + ": " + str(mainDict["modules"][key]))
if __name__ == "__main__":
initial_print()
parseFile(filename)
askCommand()