/
dfs.py
1581 lines (1157 loc) · 43.1 KB
/
dfs.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
"""
serialiazable model of graph, part of editor bussiness logic, definitions for presentation layer
"""
from sys import version_info
if version_info.major == 3 :
from functools import reduce
from io import StringIO
from queue import Queue, Empty as QueueEmpty
else :
from Queue import Queue, Empty as QueueEmpty
from StringIO import StringIO
from Queue import Queue, Empty as QueueEmpty
from threading import Thread, Lock
import time
import sys
import os
from sys import version_info
from pprint import pprint
from itertools import dropwhile, islice, count
#from collections import namedtuple
import traceback
import core
import build
import ccodegen
import implement
import serializer
from utils import here
import mathutils
# ------------------------------------------------------------------------------------------------------------
class EditorInterface(object) :
"""
draft interface for generic editor to allow support of different types of resources in workbench
"""
def get_capabilities(self) :
return {}
def get_editor_class(self) :
return "bloced", "LibraryMetadataEditor" #for example
def get_changed(self) : pass
def undo(self) : pass
def redo(self) : pass
def get_selection(self) : pass
def clear_selection(self) : pass
def cut(self) : pass
def copy(self) : pass
def paste(self) : pass
def delete(self) : pass
class GraphModelListener(object) :
"""
interface for GraphModel-events consuming object
"""
def block_added(self, sheet, block) : pass
def block_removed(self, sheet, block) : pass
def block_changed(self, sheet, block, event=None) : pass
def connection_added(self, sheet, sb, st, tb, tt, deserializing=False) : pass
def connection_removed(self, sheet, sb, st, tb, tt) : pass
def connection_changed(self, sheet, sb, st, tb, tt) : pass #TODO monitoring etc.
def meta_changed(self, sheet, key, key_present, old_value, new_value) : pass
# ------------------------------------------------------------------------------------------------------------
#TODO replace usages of Tkinter stuff elsewhere
C = "C"
N = "n"
S = "s"
W = "w"
E = "e"
#XXX these consts must go!
TERM_SIZE = 11
#for macroes and foreign functions
MIN_BLOCK_WIDTH = 64
MIN_BLOCK_HEIGHT = 48
# ------------------------------------------------------------------------------------------------------------
#def get_term_poly(tx, ty, tsz, side, direction, txt_width) :
##TODO try to rotate around center of block
# txt_height = tsz
# ang = { N : 90, S : 270, W : 0, E : 180, C : 0, }
# txt_width += (0 if direction == core.INPUT_TERM else tsz)
# shift = { N : (0, 0), S : (0, txt_width+1), W : (0, 0), E : (txt_width+1, 0), C : (0, 0), }
# sx, sy = shift[side]
# glyph = ( (tx-sx, ty-1-sy),
# (tx+1+txt_width-sx, ty-1-sy),
# (tx+txt_width-sx+1+(tsz/2 if direction == core.INPUT_TERM else -tsz/2), ty-sy+tsz/2),
# (tx+1+txt_width-sx, ty+tsz-sy+1),
# (tx-sx, ty+tsz-sy+1) )
# l, t, w, h = mathutils.bounding_rect(glyph)
# orgx = l + 0.5 * w
# orgy = t + 0.5 * h
# a = (ang[side]) % 360
# sin_angle, cos_angle = mathutils.rotate4_trig_tab[a]
# def r(xx, yy) :
# return (orgx + ((xx - orgx) * cos_angle - (yy - orgy) * sin_angle),
# orgy + ((xx - orgx) * sin_angle + (yy - orgy) * cos_angle))
# return tuple(r(*p) for p in glyph)
def translate(sx, sy, p) :
return tuple((x+sx, y+sy) for x, y, in p)
def get_glyph(w, h, direction) :
tip = ((h/2) if direction == core.INPUT_TERM else (-h/2))
w += (0 if direction == core.INPUT_TERM else (h/2))
return ( (0, 0), (w, 0), (w+tip, h/2), (w, h), (0, h) )
#XXX try caching decorator on this?
def get_term_poly(tx, ty, txt_height, side, direction, txt_width) :
ang = {
N : 90,
S : 270,
W : 0,
E : 180,
C : 0,
}
# txt_width += (0 if direction == core.INPUT_TERM else txt_height)
txt_width += txt_height / 2
shift = {
N : (0, 0),
S : (0, txt_width+1),
W : (0, 0),
E : (txt_width+txt_height/2+1, 0),
# E : (txt_width+10, 0),
C : (0, 0),
}
sx, sy = shift[side]
# glyph = ( (tx-sx, ty-1-sy),
# (tx+1+txt_width-sx, ty-1-sy),
# (tx+txt_width-sx+1+(txt_height/2 if direction == core.INPUT_TERM else -txt_height/2), ty-sy+txt_height/2),
# (tx+1+txt_width-sx, ty+txt_height-sy+1),
# (tx-sx, ty+txt_height-sy+1) )
g = get_glyph(txt_width, txt_height+1, direction)
# print(here(), g)
glyph = translate(tx-sx, ty-sy, g)
# print(here(), glyph)
# print here(), txt_width, txt_height
l, t, w, h = mathutils.bounding_rect(glyph)
orgx = l + 0.5 * w
orgy = t + 0.5 * h
a = (ang[side]) % 360
sin_angle, cos_angle = mathutils.rotate4_trig_tab[a]
def r(xx, yy) :
return (orgx + ((xx - orgx) * cos_angle - (yy - orgy) * sin_angle),
orgy + ((xx - orgx) * sin_angle + (yy - orgy) * cos_angle))
g2 = tuple(r(*p) for p in glyph)
return g2
# ------------------------------------------------------------------------------------------------------------
class BlockModelData(object) :
prototype = property(lambda self: self.__prototype)
def __lt__(self, other):
return id(other) < id(self)
terms = property(lambda self: self.__terms)#XXX return copy instead of my instance?
def __init__(self, prototype, model) :
self.__prototype = prototype
self.__terms = prototype.terms
class BlockModel(BlockModelData) :
def to_string(self) :
return "%s(%s)" % (self.prototype.type_name, str(self.value))
def get_term_side(self, t) :
flipv, fliph, rot = self.orientation
ors = {
W: 2 if fliph else 0,
N: 3 if flipv else 1,
E: 0 if fliph else 2,
S: 1 if flipv else 3
}
if t.default_side != C :
old_or = ors[t.default_side]
side = [ W, N, E, S ][ (ors[t.default_side] + rot // 90) % 4 ]
return side
return t.default_side
def get_term_pos(self, t) :
x, y, _ = self.orientation
return (1 - t.default_pos) if x or y else t.default_pos
def get_term_and_lbl_pos_NEW(self, t, t_nr, term_width, term_height, center=True) :
same_side_terms = tuple(sorted((term for term, _ in self.get_terms_flat() if term.direction == t.direction), key=lambda term: term.default_pos))
w, h = self.width, self.height
side = self.get_term_side(t)
if side in (N, S) :
a = w
elif side in (W, E) :
a = h
elif side == C :
return (w/2, h/2), (0, 0)
else :
raise Exception()
term_index = self.get_term_index(t, t_nr) if t.variadic else 1
side_index = same_side_terms.index(t)
(side_index + term_index) * term_height + (len(same_side_terms) / 2) * term_height
def get_term_and_lbl_pos(self, t, t_nr, text_width, txt_height, center=True) :
self.get_term_and_lbl_pos_NEW(t, t_nr, text_width, txt_height, center=center)
# dw, dh = self.__prototype.default_size
t_size = txt_height#XXX may depend on orientation
shift = t_size/2 if center else 0
#XXX XXX XXX
# index = t_nr
index = self.get_term_index(t, t_nr) if t.variadic else 1
# print here(), t.name, t_nr, self.get_term_index(t, t_nr) if t.variadic else 666
c = (((index - 0) * t_size)) if t.variadic else 0
#XXX XXX XXX
p = self.get_term_pos(t)
tw = text_width
side = self.get_term_side(t)
# if t.variadic :
# w, h = self.width, self.height
# else :
# w, h = self.default_size
w, h = self.default_size
if side == N :
pos = ((w*p-shift+c, 0), (0, 0))
elif side == S :
pos = ((w*p-shift+c, h-1), (0, 0))
elif side == W :
pos = ((0, h*p-shift+c), (0, 0))
elif side == E :
pos = ((w-1, h*p-shift+c), (-1-tw, 0))
elif side == C :
pos = ((w/2, h/2), (0, 0))
else :
raise Exception()
#XXX XXX (x, y), (txtx, txty) =
(x, y), (txtx, txty) = pos#sides[t.get_side(self)](t.get_pos(self), text_width)
# txtx, txty = 0, 0
# print(here(), x, y, self.width, self.height)
# return (int(x), int(y)), (x+txtx, int(y-(0.2*txt_height)+txty))
return (int(x), int(y)), (int(x+txtx), int(y+txty))
def get_term_location(self, t, t_nr, text_width, text_height) :
(x, y), _ = self.get_term_and_lbl_pos(t, t_nr, text_width, text_height, center=False)
return (x+self.left, y+self.top)
def get_label_pos(self, txt_width, txt_height) :
side = [W, N, E, S][self.orientation[2]//90]
if side == W :
pos = (0, 0)
elif side == N :
pos = (self.width-txt_height, 0)
elif side == E :
pos = (self.width-txt_width, self.height-txt_height)
elif side == S :
pos = (0, self.height-txt_height)
else :
raise Exception()
return pos
class edit(object) :
def __init__(self, prop_name=None) :
self.prop_name = prop_name
def __call__(self, f) :
def decorated(*v, **w) :
# if self.prop_name is None :
# old_meta = v[0].get_meta()
# else :
# old_meta = { self.prop_name : v[0].get_meta()[self.prop_name] }
old_meta = { self.prop_name : v[0].get_meta()[self.prop_name] }
y = f(*v, **w)
# if self.prop_name is None :
# new_meta = v[0].get_meta()
# else :
# new_meta = { self.prop_name : v[0].get_meta()[self.prop_name] }
new_meta = { self.prop_name : v[0].get_meta()[self.prop_name] }
v[0]._BlockModel__raise_block_changed({"p":self.prop_name}, old_meta, new_meta)
return y
return decorated
def __raise_block_changed(self, e, old_meta, new_meta) :
if self.__model == "itentionally left blank" :
return None
self.__model._GraphModel__on_block_changed(self, e, old_meta, new_meta)
# @edit()
def set_meta(self, meta) :
# print here(3), meta
old_meta = self.get_meta()
for k, v in meta.items() :
self.__getattribute__("_BlockModel__set_"+k)(v)
assert(not(k is None))
self.__raise_block_changed({"p":k}, {k:old_meta[k]}, {k:v})
@edit("value")
def __set_value(self, value) :
if isinstance(value, tuple) :
self.__value = value
else :
self.__value = (value,)
# def __get_value(self) :
# return self.__value
int_left = property(lambda self: self.__left)
int_top = property(lambda self: self.__top)
value = property(lambda self: self.__value, __set_value)
# def get_lbl_pos(self) :
# flipv, fliph, rot = self.orientation
# ors = {
# W: 2 if fliph else 0,
# N: 3 if flipv else 1,
# E: 0 if fliph else 2,
# S: 1 if flipv else 3
# }
# if self.__side != C :
# old_or = ors[self.__side]
# side = [ W, N, E, S ][ (ors[self.__side] + rot / 90) % 4 ]
# return side
# return self.__side
@edit("caption")
def __set_caption(self, v) :
self.__set_caption = v
caption = property(lambda self: self.__caption, __set_caption)
def get_instance_id(self) :
return self.__instance_id
@edit("instance_id")
def __set_instance_id(self, value) :
# print here(), value
self.__instance_id = value
def set_instance_id(self, value) :
self.__set_instance_id(value)
def get_meta(self) :
w, h = self.prototype.default_size
meta = {
"caption" : self.caption,
"left" : self.left,
"top" : self.top,
"width" : w,
"height" : h,
# "width" : self.width,
# "height" : self.height,
"value" : self.value,
"orientation" : self.orientation,
"term_meta" : self.__term_meta,
"instance_id" : self.get_instance_id(),
}
# if (core.compare_proto_to_type(self.prototype, core.MacroProto) or
# core.compare_proto_to_type(self.prototype, core.FunctionProto)) :
if not core.is_builtin_block(self.prototype) :
meta["cached_prototype"] = self.prototype.get_block_proto_data()
# print here()
# print here(), meta
return meta
def __set_term_meta(self, meta) :
self.__term_meta = meta
# print "__set_term_meta:",
@edit("term_meta")
def stme(self, term, n):
self.__term_meta[term.name]["multiplicity"] = n
def set_term_multiplicity(self, term, n, no_events=False) :
if not term.variadic :
raise Exception("set_term_multiplicity: terminal is not variadic")
if no_events :
self.__term_meta[term.name]["multiplicity"] = n
else :
self.stme(term, n)
def get_term_multiplicity(self, term) :
return self.__term_meta[term.name]["multiplicity"] if term.variadic else None
def __get_orientation(self) :
return self.__orientation
@edit("orientation")
def __set_orientation(self, v) :
self.__orientation = v
orientation = property(__get_orientation, __set_orientation)
default_size = property(lambda self: (self.__get_default_width(), self.__get_default_height()))
def __get_default_height(self) :
return self.__width if self.orientation[2] % 180 else self.__height
def __get_default_width(self) :
return self.__height if self.orientation[2] % 180 else self.__width
def __get_width(self) :
return self.__get_prop_height() if self.orientation[2] % 180 else self.__get_prop_width()
@edit("width")
def __set_width(self, v) :
if self.orientation[2] % 180 :
self.__height = v
else :
self.__width = v
def __get_prop_height(self, term_size=13) :
l = self.__height
trms = [ t for t in self.terms if t.default_side in (W, E) ]
varterms = sum(self.get_term_multiplicity(t)-1 if t.variadic else 0 for t in trms)
return l + varterms * term_size
def __get_prop_width(self, term_size=13) :
l = self.__width
trms = [ t for t in self.terms if t.default_side in (N, S) ]
varterms = sum(self.get_term_multiplicity(t)-1 if t.variadic else 0 for t in trms)
return l + varterms * term_size
def __get_height(self) :
return self.__get_prop_width() if self.orientation[2] % 180 else self.__get_prop_height()
@edit("height")
def __set_height(self, v) :
if self.orientation[2] % 180 :
self.__width = v
else :
self.__height = v
def __get_left(self) :
return self.__left + ((self.__width - self.__height) / 2 if self.orientation[2] % 180 else 0)
@edit("left")
def __set_left(self, v) :
self.__left = v - ((self.__width - self.__height) / 2 if self.orientation[2] % 180 else 0)
def __get_top(self) :
return self.__top + ((self.__height - self.__width) / 2 if self.orientation[2] % 180 else 0)
@edit("top")
def __set_top(self, v) :
self.__top = v - ((self.__height - self.__width) / 2 if self.orientation[2] % 180 else 0)
def __get_center(self) :
return self.left+(self.width/2), self.top+(self.height/2)
def __set_center(self, v) :
self.left = v[0] - (self.width/2)
self.top = v[1] - (self.height/2)
width = property(__get_width, __set_width)
height = property(__get_height, __set_height)
left = property(__get_left, __set_left)
top = property(__get_top, __set_top)
center = property(__get_center, __set_center)
#XXX XXX XXX
def get_term_index(self, t, t_nr) :
index = self.__term_meta[t.name][t_nr, "index"]
# print "get_term_index:", self, t.name, t_nr, index
return index
def get_indexed_terms(self, t) :
#self.__term_meta[t.name][t_nr, "index"]
return [ (k[0], v) for k, v in self.__term_meta[t.name].items()
if type(k) == tuple and type(k[0]) == int and k[1] == "index" ]
#XXX XXX XXX
def set_term_index(self, t, t_nr, index) : # index would be usually term_multiplicity-1
self.__term_meta[t.name][t_nr, "index"] = index
# print "set_term_index:", self.__term_meta
#XXX XXX XXX
def pop_term_meta(self, t, t_nr) :
if (t_nr, "index") in self.__term_meta[t.name] :
self.__term_meta[t.name].pop((t_nr, "index")) #and also the rest, if ever some rest will be
# self.__term_meta.pop((t.name, t_nr, "index")) #and also the rest, if ever some rest will be
# def __get__connections(self) :
## print self.__graph.connections
# return None#self.__graph.connections.iteritems()
# connections = property(__get__connections)
def get_terms_flat(self) :
for t in self.terms :
if t.variadic :
# iterms = sorted(self.get_indexed_terms(t), key=lambda x: x[1])#sort by index
for nr, index in sorted(self.get_indexed_terms(t), key=lambda x: x[1]) :
yield t, nr
# m = self.get_term_multiplicity(t)
# if m > 1 :
# for nr in range(m) :
# yield t, nr
# else :
# yield t, 0
else :
yield t, None
def stringified_value(self, value) :
assert(value is None or isinstance(value, tuple))
v = value if value else ("None", )
return ",".join(str(s) for s in v)
def get_presentation_text(self) :
cls = core.get_proto_name(self.prototype)
if cls in self.__lbl_fmt :
newtxt = self.__lbl_fmt[cls].format(self.stringified_value(self.value))
else :
newtxt = self.caption
return newtxt
def __init_label_fmt_table(self) :
#TODO globalize
self.__lbl_fmt = {
core.get_proto_name(core.ConstProto()) : "{0}",
core.get_proto_name(core.DelayProto()) : "Delay({0})",
core.get_proto_name(core.TapProto()) : "Tap({0})",
core.get_proto_name(core.TapEndProto()) : "TapEnd({0})",
core.get_proto_name(core.InputProto()) : "Input({0})",
core.get_proto_name(core.OutputProto()) : "Output({0})",
core.get_proto_name(core.JointProto()) : "",
core.get_proto_name(core.PipeProto()) : "Pipe({0})",
core.get_proto_name(core.PipeEndProto()) : "PipeEnd({0})",
core.get_proto_name(core.TextAreaProto()) : "{0}",
# "ConstInputProto":"ConstInput({0})",
}
presentation_text = property(get_presentation_text)
def get_term_presentation_text(self, t, nr) :
term_label = t.name
if core.compare_proto_to_type(self.prototype, core.ConstProto) :
term_label = ""#str(self.model.value)
if t.variadic : #XXX nr != None
term_label += str(self.get_term_index(t, nr))
return term_label
# def __my_init(self, model, caption, left, top, width, height, terms, values) :
# self.__orientation = (0, 0, 0)
# self.__caption, self.__left, self.__top, self.__width, self.__height, self.__terms = (
# caption, left, top, width, height, terms)
# self.__model = model
# self.__can_move = True
# self.__prototype = None
# self.__value = tuple(dv for name, dv in values) if values else None
# self.__term_meta = { t.name: { "multiplicity" : 1, (0, "index") : 0 } for t in terms if t.variadic }
def __init__(self, prototype, model, instance_id=None, left = 0, top = 0) :
"""
when there is no parent use for model argument value
'itentionally left blank' instead of GraphModel instance
"""
super(BlockModel, self).__init__(prototype, model)
# self.__my_init(model, prototype.type_name, left, top,
# prototype.default_size[0], prototype.default_size[1],
# prototype.terms, prototype.values)
self.__instance_id = instance_id
self.__orientation = (0, 0, 0)
self.__caption = prototype.type_name
self.__left = left
self.__top = top
self.__width, self.__height = prototype.default_size
self.__model = model
self.__can_move = True
self.__value = tuple(dv for name, dv in prototype.values) if prototype.values else None
self.__term_meta = { t.name: { "multiplicity" : 1, (0, "index") : 0 } for t in prototype.terms if t.variadic }
self.__init_label_fmt_table()
def __repr__(self) :
return "%s(%s)" % (self.prototype.type_name, str(self.value))
# return hex(id(self)) + " " + 'blck"' + self.__caption + '"'# + str(id(self))
# ------------------------------------------------------------------------------------------------------------
class GraphModel(object) :
def remove_listener(self, listener) :
self.__listeners.remove(listener)
def add_listener(self, listener) :
self.__listeners.append(listener)
def get_meta(self) :
return self.__meta
def set_meta(self, key, value, remove_key=False) :
key_present, old_value = False, None
if key in self.__meta :
key_present, old_value = True, self.__meta[key]
self.__meta[key] = value
self.__history_frame_append("sheet_meta_changed", (key, old_value, key_present))
self.__on_meta_changed(key, key_present, old_value, value)
# def set_block_meta(self, block, meta) :
# for k, v in meta.iteritems() :
# self.__getattribute__("_BlockModel__set_"+k)(v)
def add_block(self, block) :
if block.get_instance_id() is None :
new_id = max(self.__block_ids.keys()) + 1 if self.__block_ids else 0
assert(not(new_id in self.__block_ids))
block.set_instance_id(new_id)
self.__block_ids[block.get_instance_id()] = block
self.blocks.append(block)
# block.graph = self #XXX ?
self.__history_frame_append("block_added", (block, ))
self.__on_block_added(block)
def remove_block(self, block) :
self.blocks.remove(block)
instance = self.__block_ids.pop(block.get_instance_id())
assert(instance == block)
succs = [ c for c in self.connections.iteritems() if c[0][0] == block ]
for s, dests in succs :
for d in dests :
self.remove_connection(*(s+d))
preds = [ c for c in self.connections.iteritems()
if reduce(lambda a, dest: a or dest[0] == block, c[1], False) ]
for s, dests in preds :
for d in [ dest for dest in dests if dest[0] == block ] :
self.remove_connection(*(s+d))
self.__history_frame_append("block_removed", (block, ))
self.__on_block_removed(block)
def can_connect(self, sb, st, tb, tt) :
# print "can_connect:", sb, st, tb, tt
#TODO if variadic check max count
st, sn = st if isinstance(st, tuple) else (st, 0)
tt, tn = tt if isinstance(tt, tuple) else (tt, 0)
return (st.direction != tt.direction and
st.direction in (core.INPUT_TERM, core.OUTPUT_TERM) and
tt.direction in (core.INPUT_TERM, core.OUTPUT_TERM))
def set_connection_meta(self, b0, t0, b1, t1, meta) :
old_meta = self.__connections_meta[(b0, t0, b1, t1)]
# print(here(3), "old:", old_meta)
if (b0, t0, b1, t1) in self.__connections_meta :
self.__connections_meta[(b0, t0, b1, t1)] = meta
else :
self.__connections_meta[(b0, t0, b1, t1)].update(meta)
# print(here(3), "new:", meta)
self.__history_frame_append("connection_meta", (b0, t0, b1, t1, old_meta))
self.__on_connection_changed(b0, t0, b1, t1)
def get_connection_meta(self, b0, t0, b1, t1) :
# print(here(3), "new:", self.__connections_meta)
return dict(self.__connections_meta[(b0, t0, b1, t1)])
def __variadic_term_test(self, t, t_tst) :
return isinstance(t_tst, tuple) and t_tst[0] == t
def __add_variadic_term(self, block, term, deserializing=False) :
assert(term.variadic)
iterms = sorted(block.get_indexed_terms(term), key=lambda x: x[1])#sort by index
if not deserializing :
new_nr = 0
for nr, _ in sorted(iterms, key=lambda x: x[0]) :
if nr != new_nr :
break
new_nr += 1
m = block.get_term_multiplicity(term)#XXX base this on get_indexed_terms?
assert(len(iterms) == m)
# print "__add_variadic_term: new_nr=", new_nr, "m=", m, "indexed terms:", iterms
block.set_term_index(term, new_nr, m)
block.set_term_multiplicity(term, m+1, no_events=False)
term_number = iterms[-1][0]
# print "__add_variadic_term: m(2)=", block.get_term_multiplicity(term)
# print "__add_variadic_term(3): ", iterms[-1][0], block.get_indexed_terms(term)
return None, (term, term_number)
def add_connection(self, b0, t0, b1, t1, meta, deserializing=False) :
# print here(), b0, t0, b1, t1
if not deserializing :
if not self.can_connect(b0, t0, b1, t1) :#TODO add multiplicity
raise Exception("can't connect")
# print here(), b0, t0, b1, t1
b0, t0, b1, t1 = (b0, t0, b1, t1) if t0.direction == core.OUTPUT_TERM else (b1, t1, b0, t0)
if not isinstance(t0, tuple) and t0.variadic :
m0, t0 = self.__add_variadic_term(b0, t0, deserializing=deserializing)
if not isinstance(t1, tuple) and t1.variadic :
m1, t1 = self.__add_variadic_term(b1, t1, deserializing=deserializing)
if (b0, t0) in self.__connections :
self.__connections[(b0, t0)].append((b1, t1))
else :
self.__connections[(b0, t0)] = [(b1, t1)]
self.__connections_meta[(b0, t0, b1, t1)] = meta
self.__history_frame_append("connection_added", ((b0, t0, b1, t1), meta))
self.__on_connection_added(b0, t0, b1, t1, deserializing=deserializing)
def __remove_variadic_term(self, block, term, number) :
assert(term.variadic)
iterms = sorted(block.get_indexed_terms(term), key=lambda x: x[1])#sort by index
m = block.get_term_multiplicity(term)#XXX base this on get_indexed_terms?
assert(len(iterms) == m)
block.pop_term_meta(term, number)
# print "__remove_variadic_term:", number, m, iterms
new_index = 0
for nr, index in iterms :
if nr != number :
# print "__remove_variadic_term(2):", term, nr, new_index
block.set_term_index(term, nr, new_index)
new_index += 1
# print "__remove_variadic_term(3):", block.get_indexed_terms(term)
block.set_term_multiplicity(term, m-1, no_events=True)
def remove_connection(self, b0, t0, b1, t1) :
t0t, n0 = t0 if isinstance(t0, tuple) else (t0, 0)
t1t, n1 = t1 if isinstance(t1, tuple) else (t1, 0)
i = self.__connections[(b0, t0)]
i.remove((b1, t1))
if not i :
self.__connections.pop((b0, t0))
meta = None
if (b0, t0, b1, t1) in self.__connections_meta :
meta = self.__connections_meta.pop((b0, t0, b1, t1))
if t0t.variadic :
self.__remove_variadic_term(b0, t0t, n0)
if t1t.variadic :
self.__remove_variadic_term(b1, t1t, n1)
self.__history_frame_append("connection_removed", ((b0, t0, b1, t1), meta))
self.__on_connection_removed(b0, t0, b1, t1)
# ---------------------------------------------------------------------------------
def __on_block_added(self, block) :
for listener in self.__listeners :
listener.block_added(self, block)
def __on_block_removed(self, block) :
for listener in self.__listeners :
listener.block_removed(self, block)
def __on_block_changed(self, block, event, old_meta, new_meta) :
assert(not(old_meta is None))
self.__history_frame_append("block_meta", (block, old_meta))
for listener in self.__listeners :
listener.block_changed(self, block, event)
def __on_connection_added(self, sb, st, tb, tt, deserializing=False) :
for listener in self.__listeners :
listener.connection_added(self, sb, st, tb, tt, deserializing)
def __on_connection_removed(self, sb, st, tb, tt) :
for listener in self.__listeners :
listener.connection_removed(self, sb, st, tb, tt)
def __on_connection_changed(self, sb, st, tb, tt) :
# raise Exception("not implemented")
for listener in self.__listeners :
listener.connection_changed(self, sb, st, tb, tt)
# print(here(), (sb, st, tb, tt))
def __on_meta_changed(self, key, key_present, old_value, new_value) :
for listener in self.__listeners :
listener.meta_changed(self, key, key_present, old_value, new_value)
# ---------------------------------------------------------------------------------
def undo(self) :
if self.__undo_stack :
assert(self.__history_frame_depth == 0)
# print(here(), len(self.__undo_stack), len(self.__redo_stack))
frame = self.__undo_stack.pop()
undo_stack = self.__undo_stack
self.__undo_stack = self.__redo_stack
self.begin_edit()
self.__revert_frame(frame)
self.end_edit()
self.__undo_stack = undo_stack
assert(self.__history_frame_depth == 0)
# print(here(), len(self.__undo_stack), len(self.__redo_stack))
def redo(self) :
if self.__redo_stack :
assert(self.__history_frame_depth == 0)
frame = self.__redo_stack.pop()
self.begin_edit()
self.__revert_frame(frame)
self.end_edit()
assert(self.__history_frame_depth == 0)
# print(here(), len(self.__undo_stack), len(self.__redo_stack))
inverse_actions = {
"connection_added" : lambda self, data: self.remove_connection(*data[0]),
"connection_removed" : lambda self, data: self.add_connection(*data[0], meta=data[1]),
"connection_meta" : lambda self, data: self.set_connection_meta(*data),
"block_removed" : lambda self, data: self.add_block(data[0]),
"block_added" : lambda self, data: self.remove_block(data[0]),
"block_meta" : lambda self, data: data[0].set_meta(data[1]),
"sheet_meta_changed" : lambda self, data: self.set_meta(data[0], data[1], remove_key=not data[2]),
}
def __revert_frame(self, frame) :
self.__redoing = True
for act, data in frame :
GraphModel.inverse_actions[act](self, data)
self.__redoing = False
#XXX do i really want to have logic of history logging in _this_ class?!
def __history_frame_append(self, action, data) :
assert(action in GraphModel.inverse_actions)
if not self.__enable_logging :
return None
if not self.__redoing :
del self.__redo_stack[:]
assert(not(data is None))
self.__assert_editing()
if self.__history_frame == None:#XXX should not happen because of __assert_editing
raise Exception("no opened frame!")
else :
self.__history_frame.insert(0, (action, data))
def begin_edit(self) :
if not self.__enable_logging :
return None
self.__editing = True
if self.__history_frame_depth == 0 :
self.__history_frame = []
self.__history_frame_depth += 1
def end_edit(self) :
self.__history_frame_depth -= 1
if self.__history_frame_depth < 0 :
raise Exception("undo framing underflow!")
if self.__history_frame_depth == 0:
if self.__history_frame :
# print(here(), len(self.__history_frame), len(self.__undo_stack), len(self.__redo_stack))
self.__undo_stack.append(self.__history_frame)
# print(here(), len(self.__undo_stack), len(self.__redo_stack))
self.__history_frame = None
self.__editing = False
def __assert_editing(self) :
if self.__enable_logging and not self.__editing :
raise Exception("not editing")
def __set_enable_logging(self, v) :
self.__enable_logging = v
enable_logging = property(lambda self: self.__enable_logging, __set_enable_logging)
# ---------------------------------------------------------------------------------
def enum(self, deserializing=False) :
for block in self.__blocks :
self.__on_block_added(block)