/
pythonrepr.py
1688 lines (1164 loc) · 42.2 KB
/
pythonrepr.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Function for extending the graph manager."""
### standard library imports
from textwrap import indent, wrap
### local imports
from ..config import APP_REFS
from ..appinfo import NODE_SCRIPT_NAME
from ..classes2d.collections import List2D
from ..rectsman.utils import get_minimum_distance_function
from .utils import yield_subgraphs
from ..widget.defaultholder import DefaultHolder
### constants
NODE_SCRIPT_MODULE_NAME = NODE_SCRIPT_NAME[:-3]
COMMENT_WRAPPING_WIDTH = 0
NODE_CLUSTERING_INFLATION = (300, 300)
MAXIMUM_NODE_TEXT_BLOCK_DISTANCE = 50
DEFAULT_DOCSTRING = '"""Execute script version of Python visual graph."""'
class WaitingParentException(Exception):
pass
### set to store snippet nodes whose source will be included in
### the Python representation of the graph
###
### this won't necessarily store the ids of all snippet nodes in
### the graph
SNIPPET_NODES = set()
### set to store general viewer nodes temporarily
GENVIEWER_NODES = set()
### main function
def python_repr(self):
"""Return python representation of graph."""
### if there are proxy nodes that don't have a parent (orphan)
### nor a widget (empty), cancel the operation altogether with
### an exception
orphan_empty_proxies = [
## item
node
## source
for node in self.nodes
## filtering
if (
## first condition
hasattr(node, 'proxy_socket')
## second condition
and (
(not hasattr(node.proxy_socket, "parent"))
and (not hasattr(node, "widget"))
)
)
]
if orphan_empty_proxies:
ids = [node.id for node in orphan_empty_proxies]
raise RuntimeError(
"There are orphan redirect nodes/empty data nodes in the graph"
" which probably isn't desired, since they accomplish nothing."
f" Here's a list with their ids: {ids}"
)
### clear temporary collections
SNIPPET_NODES.clear()
GENVIEWER_NODES.clear()
### create list of standard library imports
## gather from user-defined and stlib nodes
from_user_defined_and_stlib_nodes = {
### item
node.stlib_import_text
### source
for node in self.nodes
### filtering
if hasattr(node, "stlib_import_text")
}
## gather from encapsulations usage
from_encapsulations_stlib_usage = set(
##
sum(
(
### item
node.stlib_import_texts
### source
for node in self.nodes
### filtering
if hasattr(node, "stlib_import_texts")
if (
not hasattr(node, 'substitution_callable')
or node.data['mode'] == 'callable'
)
),
[],
)
)
## gather from encapsulations annotations
from_encapsulations_stlib_annotations = set(
##
sum(
(
### item
node.stlib_annotation_import_texts
### source
for node in self.nodes
### filtering
if hasattr(node, "stlib_annotation_import_texts")
if (
not hasattr(node, 'substitution_callable')
or node.data['mode'] == 'callable'
)
),
[],
)
)
##
stlib_imports = sorted(
from_user_defined_and_stlib_nodes
.union(from_encapsulations_stlib_usage)
.union(from_encapsulations_stlib_annotations)
)
### create list of third-party imports
###
### also, while iterating over general viewer nodes,
### gather references to them in a special collection
### for future usage
## gather from general viewer nodes (if any)
from_genviewer_nodes = set()
for node in self.nodes:
if 'genviewer_id' not in node.data:
continue
GENVIEWER_NODES.add(node)
if not hasattr(node, 'thirdlib_import_texts'):
continue
from_genviewer_nodes.update(node.thirdlib_import_texts)
## gather from other nodes (if any)
# from user-defined and thirdlib nodes
from_user_defined_and_thirdlib_nodes = {
### item
node.third_party_import_text
### source
for node in self.nodes
### filtering
if hasattr(node, "third_party_import_text")
}
# from encapsulation usage
from_encapsulations_third_usage = set(
##
sum(
(
### item
node.third_party_import_texts
### source
for node in self.nodes
### filtering
if hasattr(node, "third_party_import_texts")
if (
not hasattr(node, 'substitution_callable')
or node.data['mode'] == 'callable'
)
),
[],
)
)
# from encapsulation annotations
from_encapsulations_third_annotations = set(
##
sum(
(
### item
node.third_party_annotation_import_texts
### source
for node in self.nodes
### filtering
if hasattr(node, "third_party_annotation_import_texts")
if (
not hasattr(node, 'substitution_callable')
or node.data['mode'] == 'callable'
)
),
[],
)
)
## create sorted list from their union
third_party_imports = sorted(
from_genviewer_nodes
.union(from_user_defined_and_thirdlib_nodes)
.union(from_encapsulations_third_usage)
.union(from_encapsulations_third_annotations)
)
### store a flag indicating whether there are pygame
### imports
at_least_one_pygame_import = any(
'from pygame' in import_text
for import_text in third_party_imports
)
### create list of import statements for user-defined
### nodes
node_callable_imports = sorted(
## a set: so we end up without duplicates
set(
# item: a "from ... import ..." statement
(
"from "
+ ".".join(
node.data["script_id"]
+ (NODE_SCRIPT_MODULE_NAME,)
)
+ " import "
+ node.main_callable.__name__
)
# source
for node in self.nodes
# filtering
if (
"script_id" in node.data
and not hasattr(node, "substitution_callable")
and not hasattr(node, "stlib_import_text")
and not hasattr(node, "third_party_import_text")
)
)
)
# XXX: the 'substitution_callable' check a few lines above
# probably isn't needed, since only some app-defined nodes are
# supposed to use substitution callables; ponder, review and decide
# what to do about this when convenient; there is at least another
# check like this in this module that need reviewing as well;
### def statement
func_name = "".join(
## item (a character)
char
## source
for char in APP_REFS.source_path.stem
## filtering
if char.isalnum() or char == "_"
)
if func_name[0].isdigit() or not func_name:
func_name = "_" + func_name
def_statement = f"def {func_name}():"
### text blocks
text_blocks = list(self.text_blocks)
block_map = {id(block.rect): block for block in text_blocks}
block_rects = [block.rect for block in text_blocks]
## docstring
docstring = DEFAULT_DOCSTRING
if text_blocks:
result = self.origin_rect.collidelist(block_rects)
if result != -1:
colliding_text_block = block_map[id(block_rects.pop(result))]
text = colliding_text_block.data["text"]
docstring = (
'"""' + text + ('\n"""' if len(text.splitlines()) > 1 else '"""')
)
##
text_blocks.remove(colliding_text_block)
##
docstring = indent(
docstring,
" " * 4,
)
### create text from objects in the graph forming
### its body, that is, calls and snippets from nodes,
### comments from nodes and text blocks
##
graph_function_body = ""
##
if self.nodes:
node_clusters = [
List2D(cluster)
for cluster in self.nodes.get_clusters(NODE_CLUSTERING_INFLATION)
]
set_text_block_refs(
self.nodes,
node_clusters,
text_blocks,
)
## for each subgraph, write each node as a call or reference to
## a callable object with proper indentation
for subgraph in yield_subgraphs(self.nodes):
## filter out redirect nodes and nodes in callable mode
subgraph = [
## item
node
## source
for node in subgraph
## filtering
# nodes which are NOT redirect nodes
if not (
hasattr(node, "proxy_socket")
and hasattr(node.proxy_socket, "parent")
)
# nodes which are NOT in callable mode
if node.data.get('mode', 'expanded_signature') != 'callable'
]
##
graph_function_body += "\n" * 2
##
nodes_to_visit = set(subgraph)
visited_nodes = set()
subgraph_calls_text = ""
while nodes_to_visit:
for node in nodes_to_visit:
try:
node_source = node_to_python_source(
node,
nodes_to_visit,
visited_nodes,
node_clusters,
)
except WaitingParentException:
pass
else:
subgraph_calls_text += node_source
visited_nodes.add(node)
nodes_to_visit -= visited_nodes
graph_function_body += indent(
subgraph_calls_text,
" " * 4,
)
###
if (not self.nodes) and text_blocks:
get_lowest_dist_to_origin = get_minimum_distance_function(self.origin_rect)
min_distance_to_origin = lambda block: get_lowest_dist_to_origin(block.rect)
for text_block in sorted(text_blocks, key=min_distance_to_origin):
##
graph_function_body += "\n" * 2
##
lines = (
wrap(
text_block.data["text"],
width=COMMENT_WRAPPING_WIDTH,
)
if COMMENT_WRAPPING_WIDTH
else text_block.data["text"].splitlines()
)
# add each line with a preceeding prefix
# and succeeding '\n' (we didn't manage
# to do this with solutions using
# str.join/textwrap.indent, but we
# achieved what we wanted)
for line_text in lines:
graph_function_body += "### " + line_text + "\n"
##
graph_function_body = indent(
graph_function_body,
" " * 4,
)
### gather source of snippets used
###
### also clear the collection used to keep track
### of them
for node in self.nodes:
if (
'capsule_id' not in node.data
or hasattr(node, 'substitution_callable')
):
continue
SNIPPET_NODES.add(node)
source_of_snippets = (
gather_snippets_source()
if SNIPPET_NODES
else ''
)
SNIPPET_NODES.clear()
### gather source of general viewer nodes used,
### regardless of the mode, if any;
###
### also clear the collection used to keep track
### of them
source_of_general_viewer_nodes = (
gather_genviewer_source()
if GENVIEWER_NODES
else ''
)
GENVIEWER_NODES.clear()
## now concatenate the python text and return it
return (
"\n"
+ (
(
"### standard library imports"
+ ("\n" * 2)
+ "\n".join(stlib_imports)
+ ("\n" * 3)
)
if stlib_imports
else ""
)
+ (
(
"### third-party imports"
+ ("\n" * 2)
+ "\n".join(third_party_imports)
+ ("\n" * 3)
)
if third_party_imports
else ""
)
+ (
"### local imports (node callables)"
+ ("\n" * 2)
+ "\n".join(node_callable_imports)
+ ("\n" * 3)
if node_callable_imports
else ""
)
+ "\n"
+ "### main function"
+ "\n"
+ "\n"
+ def_statement
+ "\n"
+ docstring
+ graph_function_body
+ "\n\n"
+ (
(
"### encapsulations (utility functions)"
+ "\n\n" + source_of_snippets + "\n\n"
)
if source_of_snippets
else ''
)
+ (
(
"### general viewer node scripts"
+ "\n\n"
+ source_of_general_viewer_nodes + "\n\n"
)
if source_of_general_viewer_nodes
else ''
)
+ (
(
"### since your graph uses pygame imports, in case you intend to execute this"
+ "\n### script *outside* Nodezator, make sure pygame-ce/its relevant modules are"
+ "\n### initialized and the display mode is set (depending on the imports used)"
+ "\n\n"
)
if at_least_one_pygame_import
else ''
)
+ "if __name__ == '__main__':\n"
+ f" {func_name}()\n\n"
)
### other utility functions
def set_text_block_refs(
nodes,
node_clusters,
text_blocks,
):
node_map = {id(node.rect): node for node in nodes}
node_rects = [node.rect for node in nodes]
cluster_map = {id(cluster.rect): cluster for cluster in node_clusters}
cluster_rects = [cluster.rect for cluster in node_clusters]
for text_block in text_blocks:
get_lowest_dist = get_minimum_distance_function(text_block.rect)
closest_obj = node_map[
id(
min(
node_rects,
key=get_lowest_dist,
)
)
]
if get_lowest_dist(closest_obj.rect) > MAXIMUM_NODE_TEXT_BLOCK_DISTANCE:
closest_obj = cluster_map[
id(
min(
cluster_rects,
key=get_lowest_dist,
)
)
]
## ignore redirect nodes and associated text
## blocks; text blocks close to redirect
## nodes are considered visual aids to help
## give context to the redirected data,
## rather than part of the graph function;
if hasattr(closest_obj, "proxy_socket") and hasattr(
closest_obj.proxy_socket, "parent"
):
continue
##
try:
refs = closest_obj.text_block_refs
except AttributeError:
refs = closest_obj.text_block_refs = []
refs.append(text_block)
def node_to_python_source(
node,
nodes_to_visit,
visited_nodes,
node_clusters,
):
for cluster in node_clusters:
if node in cluster:
break
else:
raise RuntimeError("this else statement should never be" " reached")
###
if (
"script_id" in node.data
or "stlib_id" in node.data
or "builtin_id" in node.data
or "genviewer_id" in node.data
or "capsule_id" in node.data
or "thirdlib_id" in node.data
) and not hasattr(node, "substitution_callable"):
node_text_yielding_function = callable_node_to_call_text
elif hasattr(node, "widget"):
node_text_yielding_function = data_node_to_variable_assignment
elif "operation_id" in node.data:
node_text_yielding_function = operator_node_to_call_text
else:
node_text_yielding_function = node_to_code_snippet
return node_text_yielding_function(
node,
cluster,
nodes_to_visit,
visited_nodes,
node_clusters,
)
def callable_node_to_call_text(
node,
cluster,
nodes_to_visit,
visited_nodes,
node_clusters,
):
call_text = node.title_text + "("
insocket_flmap = node.input_socket_live_flmap
widget_flmap = node.widget_live_flmap
keyword_lmap = node.subparam_keyword_entry_live_map
for param_name in node.signature_obj.parameters.keys():
### start by assigning a default value to the
### argument, which is the name of the parameter
### itself
argument = param_name
### check whether parameter name is of variable
### kind, storing which kind, if it is the case
try:
kind = node.var_kind_map[param_name]
### if it isn't of variable kind...
except KeyError:
### define a condition where the argument
### must be ommited, defaulting to False
ommit_argument = False
### we check whether the parameter depends on
### different incomming data
insocket = insocket_flmap[param_name]
### if it does...
if hasattr(insocket, "parent"):
### first of all retrieve references to the parent socket
### and parent node
parent = insocket.parent
parent_node = parent.node
### ensure the parent node is not a redirect node
while True:
if hasattr(parent_node, "proxy_socket") and hasattr(
parent_node.proxy_socket, "parent"
):
parent = parent_node.proxy_socket.parent
parent_node = parent.node
continue
break
### now that we are sure we have a proper parent node,
### check whether it is in callable mode;
###
### it it is...
if parent_node.data.get('mode', 'expanded_signature') == 'callable':
### if we have an operator node, the incoming data is a lambda
### representing the node
if 'operation_id' in parent_node.data:
argument = '(' + parent_node.get_lambda_source() + ')'
### otherwise, it is a reference to the nodes callable
else:
argument = parent_node.title_text
## additionally, if the node is a snippet node,
## store a reference in a special collection
if 'capsule_id' in parent_node.data:
SNIPPET_NODES.add(parent_node)
### otherwise...
else:
if parent_node not in visited_nodes:
raise WaitingParentException
argument = "_" + "_".join(
map(
str,
parent.get_id(),
)
)
### otherwise check whether there's an embedded
### widget with data for the parameter
else:
try:
widget = widget_flmap[param_name]
### if there's not, just pass
except KeyError:
pass
### if it has...
else:
## if the argument is a default holder,
## than it means the argument was
## not edited, it might even not be
## a python literal, so we make it
## so it is ommited by assigning True
## to the corresponding flag
if type(widget) is DefaultHolder:
ommit_argument = True
## otherwise we use repr() of the value
## it holds as the argument
else:
argument = repr(widget.get())
## if upon reaching this spot it is decided
## that the argument must not be ommited...
if not ommit_argument:
## if parameter is of kind keyword-only,
## make it so the argument is passed with
## the parameter name
if int(node.signature_obj.parameters[param_name].kind) == 3:
argument = f"{param_name}={argument}"
## concatenate the argument text to the
## call text
call_text += argument + ", "
### if it is of variable kind...
else:
subparam_socket_map = insocket_flmap[param_name]
unpacked_subparams = node.data["subparam_unpacking_map"][param_name]
call_text += "*(" if kind == "var_pos" else "**{"
for subparam_index in sorted(subparam_socket_map.keys()):
insocket = subparam_socket_map[subparam_index]
### we check whether the subparameter
### depends on other sources of data
if hasattr(insocket, "parent"):
### first of all retrieve references to the parent socket
### and parent node
parent = insocket.parent
parent_node = parent.node
### ensure the parent node is not a redirect node
while True:
if hasattr(parent_node, "proxy_socket") and hasattr(
parent_node.proxy_socket, "parent"
):
parent = parent_node.proxy_socket.parent
parent_node = parent.node
continue
break
### now that we are sure we have a proper parent node,
### check whether it is in callable mode;
###
### it it is...
if parent_node.data.get('mode', 'expanded_signature') == 'callable':
### if we have an operator node, the incoming data is a lambda
### representing the node
if 'operation_id' in parent_node.data:
subargument = '(' + parent_node.get_lambda_source() + ')'
### otherwise, it is a reference to the nodes callable
else:
subargument = parent_node.title_text
## additionally, if the node is a snippet node,
## store a reference in a special collection
if 'capsule_id' in parent_node.data:
SNIPPET_NODES.add(parent_node)
### otherwise...
else:
if parent_node not in visited_nodes:
raise WaitingParentException
subargument = "_" + "_".join(map(str, parent.get_id()))
## a widget
else:
widget = widget_flmap[param_name][subparam_index]
subargument = repr(widget.get())
if kind == "var_pos":
if subparam_index in unpacked_subparams:
subargument = f"*{subargument}"
elif kind == "var_key":
if subparam_index in unpacked_subparams:
subargument = f"**{subargument}"
else:
keyword = keyword_lmap[subparam_index].get()
subargument = f"{repr(keyword)}: {subargument}"
call_text += subargument + ", "
call_text += "), " if kind == "var_pos" else "}, "
###
call_text += ")"
###
comment_prefix = "# " if node.data.get("commented_out", False) else ""
output_sockets = node.output_socket_live_map.values()
if len(output_sockets) > 1:
dict_name = f"_temp_dict_{node.id}"
max_char_no = (
max(len(socket.output_name) for socket in output_sockets)
+ len(str(node.id))
+ 2 # initial '_' plus '_' between node id and
# output_name
)
max_char_no = max(max_char_no, len(dict_name))
call_text = (
comment_prefix
+ dict_name.ljust(max_char_no, " ")
+ " = "
+ call_text