File: macros.py

package info (click to toggle)
mgltools-networkeditor 1.5.7-4
  • links: PTS, VCS
  • area: non-free
  • in suites: buster
  • size: 1,312 kB
  • sloc: python: 17,905; sh: 78; makefile: 10
file content (1074 lines) | stat: -rw-r--r-- 42,529 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
#########################################################################
#
# Date: Nov. 2001  Authors: Michel Sanner, Daniel Stoffler
#
#    sanner@scripps.edu
#    stoffler@scripps.edu
#
#       The Scripps Research Institute (TSRI)
#       Molecular Graphics Lab
#       La Jolla, CA 92037, USA
#
# Copyright: Michel Sanner, Daniel Stoffler and TSRI
#
# revision: Guillaume Vareille
#  
#########################################################################
#
# $Header: /opt/cvs/python/packages/share1.5/NetworkEditor/macros.py,v 1.114.2.2 2016/02/11 23:41:06 annao Exp $
#
# $Id: macros.py,v 1.114.2.2 2016/02/11 23:41:06 annao Exp $
#

import re, string
import Tkinter
import weakref
import types
import datetime

from NetworkEditor.net import Network, ImageNode
from NetworkEditor.items import NetworkNode, NetworkItems
from NetworkEditor.ports import InputPort, OutputPort

################################################################
# to avoid multiple list enclosure, input port macro node 
# must always be singleConnection.
# to have multiple connections, 
# the solution is to duplicate the input port in the macronode
# (there is no such need for the output port macronode)
################################################################



      
class MacroNetwork(Network):
    """class to hold all the information about a bunch of nodes and connections
"""
    def __init__(self, macroNode, name='Noname'):
        Network.__init__(self, name)
        self.macroNode = macroNode

        self._originalNodes = []       # list of nodes that belong to a node
                                       # library macro. The list is populated
                                       # upon adding the macro to a network
                                       # and is used for saving macro networks
        self._originalConnections = [] # list of connections that belong to a
                                       # node library macro.


    def setExec(self,status):
        """Loop recursively over nodes in nested macros and set MacroNetwork's
execStatus to 'stop'
"""
        self.execStatus = status
        for n in self.nodes:
            if isinstance(n, MacroNode):
                n.macroNetwork.setExec(status)


    def stop(self):
        """set execStatus to 'stop'.
The execution will stop after completion of the current node.
For a MacroNode, we need to find the master network and invokde its stop method
"""
        master = self.macroNode.network
        while isinstance(master, MacroNetwork):
            master = self.macroNode.network
        master.stop()


class MacroBase:

    def getPortName(self, port):
        return "%s_%d_%s_%d"%(self.name.replace(' ', '_'), id(self), port.name, port._id)

    def deletePort(self, p, resize=True, updateSignature=False):
        # call base class method
        # NOTE: we do this, because NetworkNode also subclasses this method
        #       and does some more stuff (like updating the node source code)
        NetworkItems.deletePort(self, p, resize)


    def updateCode(self, port='ip', action=None, tagModified=True, **kw):
        """we do not want to change the compute functions of the macro nodes
        upon adding/deleting ports"""
        pass


class MacroNode(MacroBase, NetworkNode):

    def __init__(self, name='NoName', sourceCode=None, originalClass=None,
                 constrkw={}, library=None, progbar=0,
                 macroNetworkClass=MacroNetwork, **kw):

        apply( NetworkNode.__init__, (self, name, sourceCode, originalClass,
                                      constrkw, library, progbar), kw)
        self.expanded = 0
        self.isSchedulingNode = True # the outputPort node will schedule all
                             # children of the macro node
        self.hasRun = 0 # set to 1 when the node is executed. This is used to
                        # force the execution of root nodes in macro the first
                        # time the macro network is executed
        self.mouseAction['<Double-Shift-Button-1>'] = self.expand
        self.macroNetwork = macroNetworkClass(self, self.name)

        # add special nodes for input and output
        self.macroNetwork.ipNode = MacroInputNode(self, 'input Ports')
        self.macroNetwork.opNode = MacroOutputNode(self, 'output Ports')

        code = """def doit(self, *args):

    # propagate forceExecution status
    if self.network.forceExecution:
        self.macroNetwork.forceExecution = self.network.forceExecution

    # copy args to output ports of the macroIpNode
    macroIp = self.macroNetwork.ipNode
    kw = {}
    i = 0
    newData = 0
    for p, data in map(None, self.inputPorts, args):
        if self.macroNetwork.forceExecution or self.inputPorts[i].hasNewValidData(): 
            macroIp.newData = 1
            kw[macroIp.outputPorts[p.number+1].name] = data
        i = i + 1

    if not macroIp.newData and not self.macroNetwork.forceExecution:
        return

    # output the data on the IPnode output ports
    apply( macroIp.outputData, (), kw )
    
    # run root nodes inside macroNetwork (if needed)
    # at the first execution of a macro node all root nodes have to run

    if not self.hasRun or self.macroNetwork.forceExecution:
        # list of roots with InputPortNode as last one
        # because InputPortNode schedules its children
        roots = self.macroNetwork.rootNodes[:]
        self.hasRun = 1
    else:
        roots = []
        for n in self.macroNetwork.rootNodes:
            if self.macroNetwork.forceExecution:
                n.forceExecution = 1
                roots.append(n)
            elif n.newData:
                roots.append(n)

    if len(roots):
        #print 'MacroNode doit', roots, self.hasRun, self.macroNetwork.forceExecution
        subRunNodes = self.macroNetwork.getSubRunNodes(roots = roots)
        # output the data on the IPnode output ports
        apply( macroIp.outputData, (), kw )
        self.macroNetwork.runNodes(subRunNodes)
"""
            
        self.setFunction(code)


    def isMacro(self):
        """Returns False if this node is not a MacroNode, returns True if MacroNode"""
        return True


    def schedule_cb(self, event=None):
        #print "MacroNode.schedule_cb"
        self.macroNetwork.forceExecution = 1
        NetworkNode.schedule_cb(self)


    def beforeRemovingFromNetwork(self):
        # if a macro node is deleted, we also have to delete its macro network
        ed = self.getEditor()
        ed.deleteNetwork(self.macroNetwork)


    def beforeAddingToNetwork(self, network):
        # here we do have a valid network
        self.macroNetwork.vEditor = weakref.ref(network.getEditor())
        self.macroNetwork.runOnNewData = network.runOnNewData

        
    def afterAddingToNetwork(self):
        ed = self.getEditor()
        ed.addNetwork(self.macroNetwork)
        self.macroNetwork.addNode(self.macroNetwork.ipNode, 200, 20)
        self.macroNetwork.addNode(self.macroNetwork.opNode, 200, 280)
        if ed.hasGUI:
            self.macroNetwork.buildIcons()
            # currently, we are now inside the macro network but the macro node
            # was not flagged "expanded=1" yet, so to get back in sync, we do:
            self.expand()

    def afterBuildingMacroNetwork(self):
        # is called after the macro next work was created
        pass
    
    def buildIcons(self, canvas, posx, posy):
        NetworkNode.buildIcons(self, canvas, posx, posy)
        myfont = list(self.getFont())
        if not 'bold' in myfont:
            myfont.append('bold')
        self.setFont(tuple(myfont))
        self.autoResizeX()

        try:
            self.menu.index('expand')
        except:
            self.menu.add_separator()
            self.menu.add_command(label='expand', command=self.expand)
            self.menu.add_command(label='shrink', command=self.shrink)
            self.addSaveNodeMenuEntries()
            

    def expand(self, event=None):
        ed = self.getEditor()
        if not self.expanded:
            ed.menuButtons['Networks'].menu.entryconfig(
                "Close...", state=Tkinter.DISABLED)
            ed.networkArea.showpage(self.macroNetwork.name)
            self.expanded = 1
        else:
            ed.networkArea.selectpage(self.macroNetwork.name)

        
    def shrink(self, event=None):
        ed = self.getEditor()
        if not self.expanded:
            return
        ed.menuButtons['Networks'].menu.entryconfig(
            "Close...", state=Tkinter.NORMAL)
        self.expanded = 0
        ed.networkArea.hidepage(self.macroNetwork.name)
        # make my own network the current one
        # Note: because we can have nested macros, we have to find the one
        # macro node that is currently expanded
        net = self.network
        while isinstance(net, MacroNetwork):
            if net.macroNode.expanded:
                ed.setNetwork(net)
                return
            else:
                net = net.macroNode.network
        # else: we have to set the network to what we got here, or we loose
        # the focus
        ed.setNetwork(net)


    def getRootMacro(self):
        """This method is used to find the root macro node for nested macros"""
        result = [self]
        parent = self

        while isinstance(parent.network, MacroNetwork):
            parent = parent.network.macroNode
            result.append(parent)
        return result[::-1]


    def getNodeDefinitionSourceCode(self, networkName, indent="",
                                    ignoreOriginal=False):
        """This method builds the text-string to describe a macro node
in a saved file."""
        
        lines = []
        nodeName = self.nameInSavedFile = self.getUniqueNodeName()
        
        ###############################################################
        # add lines to import node from macros.py, add macro to network
        ###############################################################

        txt = NetworkNode.getNodeDefinitionSourceCode(
            self, networkName, indent)
        lines.extend(txt)
        ## if self.library:
        ##     txt = NetworkNode.getNodeDefinitionSourceCode(
        ##         self, networkName, indent)
        ##     lines.extend(txt)
            
        ## else:
        ##     if not self._original:
        ##         txt1 = 'from NetworkEditor.macros import MacroNode\n'
        ##         txt2 = "%s = MacroNode(name='%s')\n"%(nodeName, self.name)
        ##         txt3 = "%s.addNode(%s, %d, %d)\n"%(
        ##             networkName, nodeName, self.posx, self.posy)

        ##         lines.append(indent+txt1)
        ##         lines.append(indent+txt2)
        ##         lines.append(indent+txt3)
            
        ###############################################################
        # add lines to add all macro nodes first, recursively
        ###############################################################
        
        # We have to add all macro nodes first, and then start in the leaf
        # macros, add the nodes there, and work our way back up
        # (because of connections)
        for node in self.macroNetwork.nodes:
            if isinstance(node, MacroNode):
                txt1 = node.getNodeDefinitionSourceCode(
                    nodeName+".macroNetwork", indent)
                lines.extend(txt1)
        
        ###############################################################
        # check if an original node was deleted
        ###############################################################
        deletedNodes = 0 # count how many nodes have already been deleted
        # NOTE: because we add a line for each node we want to delete,
        # we have to decrement the orignumber by how many nodes we already
        # deleted
        for orignode, orignumber in self.macroNetwork._originalNodes:
            if orignode not in self.macroNetwork.nodes:
                # add handle to macro node
                lines = self.checkIfNodeForSavingIsDefined(
                    lines, networkName, indent)
                # add line to delete node
                txt = "%s.deleteNodes([%s])\n"%(
                    nodeName+".macroNetwork",
                    nodeName+".macroNetwork.nodes["+str(orignumber-deletedNodes)+"]")
                deletedNodes += 1
                lines.append(indent+txt)
 
        ###############################################################
        # check if an original connection was deleted
        ###############################################################
        for origconn, p1, n1, p2, n2 in self.macroNetwork._originalConnections:
            if origconn not in self.macroNetwork.connections:
                # only generate code if the nodes still exist (if not this
                # means, the node has been deleted which will delete the
                # connections so we do not have to add code,
                # and also if ports are not None. If ports were None means
                # that the user deleted the port which we catch below and
                # this also deletes the connection so we need not add code here
            
                invalid = False # this indicates a connection to a deleted
                                # node or port

                # port1 or port2 deleted?
                if type(p1) == types.NoneType or type(p2) == types.NoneType:
                    invalid = True

                # node1 deleted?
                if n1 not in self.macroNetwork.nodes:
                    invalid = True
                # node2 deleted?
                if n2 not in self.macroNetwork.nodes:
                    invalid = True
                    
                # only if both ports still exist do the following
                if not invalid:
                    lines = self.checkIfNodeForSavingIsDefined(
                        lines, networkName, indent)

                    node1 = nodeName+".macroNetwork.nodes[%d]"%(
                        self.macroNetwork.nodeIdToNumber(n1._id),)
                    node2 = nodeName+".macroNetwork.nodes[%d]"%(
                        self.macroNetwork.nodeIdToNumber(n2._id),)
                    txt = "%s.deleteConnection(%s, '%s', %s, '%s')\n"%(
                    nodeName+".macroNetwork", node1, p1.name, node2, p2.name)
                    lines.extend(indent+txt)
            
        ###############################################################
        # add lines to add/modify nodes in a macro network
        ###############################################################
        for node in self.macroNetwork.nodes:
            if not isinstance(node, MacroNode):
                txt2 = node.getNodeDefinitionSourceCode(
                    nodeName+".macroNetwork", indent)
                lines.extend(txt2)

        ###############################################################
        # add lines to create connections in macro networks
        ###############################################################
        macroNetworkName = "%s.macroNetwork"%nodeName
        if len(self.macroNetwork.connections):
            lines.append(
                '\n'+indent+"## saving connections for network "+\
                "%s ##\n"%self.name)
            lines.append(indent+'%s.freeze()\n'%macroNetworkName)
            for conn in self.macroNetwork.connections: 
                lines.extend(conn.getSourceCode(
                    macroNetworkName, False, indent))
            lines.append(indent+'%s.unfreeze()\n'%macroNetworkName)

        ###############################################################
        # add lines to configure dynamically created MacroOutputPorts
        # Note: right now we catch the port name 
        ###############################################################
        txt = self.macroNetwork.ipNode.getDynamicPortsModificationSourceCode(
            macroNetworkName, indent, ignoreOriginal)
        lines.extend(txt)

        ###############################################################
        # add lines to configure dynamically created MacroOutputPorts
        # Note: right now we catch the name and "singleConnection"
        # which might be modified by the user
        ###############################################################
        txt = self.macroNetwork.opNode.getDynamicPortsModificationSourceCode(
            macroNetworkName, indent, ignoreOriginal)
        lines.extend(txt)

        ###############################################################
        # Also, catch singleConnection events on the MacroNode input ports
        # if they were changed compared to the node that is connected to
        # the MacroInput node. We can do this only after we have formed
        # the connections inside the macro network
        ###############################################################
        # find node connected
        txt = []
        for ip in self.inputPorts:
            # add line to allow the renaming of the macronode's input ports
            txt.append(indent+\
                       "%s.inputPorts[%d].configure(name='%s')\n"%(
                       nodeName, ip.number, ip.name) )
            txt.append(indent+\
                       "%s.inputPorts[%d].configure(datatype='%s')\n"%(
                       nodeName, ip.number, ip.datatypeObject['name']) )
        txt.append(indent+"## configure MacroNode input ports\n")
        lines.extend(txt)

        # add line to allow the renaming of the macronode's output ports
        txt = []
        for op in self.outputPorts:
            txt.append(indent+\
                       "%s.outputPorts[%d].configure(name='%s')\n"%(
                       nodeName, op.number, op.name) )
            txt.append(indent+\
                       "%s.outputPorts[%d].configure(datatype='%s')\n"%(
                       nodeName, op.number, op.datatypeObject['name']) )
        txt.append(indent+"## configure MacroNode output ports\n")
        lines.extend(txt)
        
        ###############################################################
        # Shrink the macro node
        ###############################################################
        lines.append(indent+nodeName+".shrink()\n")

        ###############################################################
        # configure macro node: Freeze, etc
        ###############################################################
        ind, txt = self.getNodeSourceCodeForNode(networkName, indent,
                                                 ignoreOriginal)
        lines.extend(txt)
        return lines


    def getNodeSourceCodeForPorts(self, networkName, indent="", full=0):
        """We need to override the baseclass method since we do not want to
        save port modifications to a macro node"""

        lines = []
        return indent, lines


    def getAfterConnectionsSourceCode(self, networkName, indent="",
                                      ignoreOriginal=False):

        """This method allows users to add source code that needs to be
        generated after nodes were connected, which might trigger the
        creation of new ports."""
        
        lines = []
        
        for node in self.macroNetwork.nodes:
            lines.extend(node.getAfterConnectionsSourceCode(
                networkName, indent, ignoreOriginal) )
        
        return lines


    def resetTags(self):
        """This method subclasses NetworkNode.resetTags(). Used to reset
        the attributes _modified and _original in node, ports, widgets, conn"""
        NetworkNode.resetTags(self)
        for node in self.macroNetwork.nodes:
            node.resetTags()
        self.macroNetwork.ipNode._setOriginal(False)
        self.macroNetwork.opNode._setOriginal(False)
        

    def buildOriginalList(self):
        """Summary: used to catch delete node and delete connection events
        in the macro network.
        Puts nodes and connections into the macronetwork._originalNodes
        and macronetwork._originalConnections lists. This is used to find
        out if a node or connection in a macro network of a macro node that
        came from a node library was deleted."""

        macNet = self.macroNetwork
        macNet._originalNodes = []
        macNet._originalConnections = []

        # store instance of node and current number in net.nodes
        for node in macNet.nodes:
            macNet._originalNodes.append( (
                node, self.macroNetwork.nodeIdToNumber(node._id) ) )
            
        # store instance of original connection, port1, node1, port2, node2
        for conn in macNet.connections:
            macNet._originalConnections.append( (
                conn,
                conn.port1, conn.port1.node, conn.port2, conn.port2.node) )



    def getNodeSourceCode(self, className, 
                          networkName='self.masterNetwork', 
                          indent="", dependencies=True):
        """This method is called through the 'save source code' mechanism.

The idea here is to generate source code for a macro network that can
be put into a node library. This is not for saving networks

dependencies: True/False
    False: the saved macro node is fully independent from it's original macro (if any). 
    True : if relevant, the macro node is saved as a subclass of an original 
           macro, only modifications from the original are saved (not implemented yet) 

    in both case, saved macros depend as well of other embeded macros and of nodes from libraries. 
"""
        lines = []
        self.nameInSavedFile = self.getUniqueNodeName()
        
        ## get header descr
        headerBlock = self.getHeaderBlock(className, indent)
        lines.extend(headerBlock)

        ## get __init__ descr, and proper indent
        initBlock = self.getInitBlock(className, indent)
        lines.extend(initBlock)

        ## get library import cache
        ## then write libray import code
        cache = {'files':[]}
        cache = self.network.buildLibraryImportCache(
            cache, self.macroNetwork, selectedOnly=False)

        ## get beforeAddingToNetwork descr
        beforeBlock = self.getBeforeBlock(cache, indent)
        lines.extend(beforeBlock)

        ## get afterAddingToNetwork descr
        afterBlock = self.getAfterBlock(cache, networkName, indent)
        lines.extend(afterBlock)

        
        return lines

    ####################################################
    #### Helper Methods follow to generate save file ###
    ####################################################

    def getHeaderBlock(self, className, indent=""):
        lines = []

        lNow = datetime.datetime.now().strftime("%A %d %B %Y %H:%M:%S") 
        
        lCopyright = """########################################################################
#
#    Vision Macro - Python source code - file generated by vision
#    %s 
#    
#       The Scripps Research Institute (TSRI)
#       Molecular Graphics Lab
#       La Jolla, CA 92037, USA
#
# Copyright: Daniel Stoffler, Michel Sanner and TSRI
#   
# revision: Guillaume Vareille
#  
#########################################################################
#
# $%s$
#
# $%s$
#

"""%(lNow, "Header:", "Id:") # if directly in the txt, CVS fills these fields

        lines.append(lCopyright)

        #lines.append(indent+"from NetworkEditor.macros import MacroNode\n")
        #lines.append(indent+"class "+className+"(MacroNode):\n")

        mod = self.originalClass.__module__
        klass = self.originalClass.__name__
        if klass != 'c':
            lines.append(indent+"from NetworkEditor.macros import MacroNode\n")
        if className == klass:
            txt2 = "class %s(MacroNode):\n"%(className)
            lines.append(indent+txt2)
        else:
            txt1 = "from %s import %s\n"%(mod,klass)
            lines.append(indent+txt1)
            txt2 = "class %s(%s):\n"%(className,klass)
            lines.append(indent+txt2)
                

        if self.originalClass.__doc__ is not None:
            lines.append(indent+'    \"\"\"'+self.originalClass.__doc__)
            lines.append('\"\"\"\n')
        return lines


    def getInitBlock(self, className, indent=""):
        nodeName = self.name
        klass = self.originalClass.__name__
        
        indent = indent + 4*" " # move forward 4
        lines = []
        lines.append("\n")
        lines.append(indent+"def __init__(self, constrkw={}, name='"+\
                     nodeName+"', **kw):\n")
        indent = indent + 4*" " # move forward 4
        lines.append(indent+"kw['name'] = name\n")

##         if self.library:
##             lines.append(indent+"apply(%s.__init__, (self,), kw)\n"%klass)
##         else:
        lines.append(indent+"apply( MacroNode.__init__, (self,), kw)\n")

        # add text for widgetDescr dict
        for p in self.inputPorts:
            w = p.widget
            if w is None:
                continue
            v = w.getConstructorOptions()
            v['class'] = w.__class__.__name__
            lines.append(indent+"""self.widgetDescr['%s'] = %s\n"""%(p.name,
                                                                     str(v)) )
        return lines
        

    def getBeforeBlock(self, cache, indent=""):
        lines = []
        klass = self.originalClass.__name__
        indent = indent + 4*" " # move forward 4

        lines.append("\n")
        lines.append(indent+"def beforeAddingToNetwork(self, net):\n")
        indent = indent + 4*" " # move forward 4
##         if self.library:
##             lines.append(indent+"%s.beforeAddingToNetwork(self, net)\n"%klass)
##         else:
        lines.append(indent+"MacroNode.beforeAddingToNetwork(self, net)\n")
        
        li = self.macroNetwork.getLibraryImportCode(
            cache, indent, editor="net.editor",
            networkName='net',
            #importOnly=True, loadHost=True)
            importOnly=False, loadHost=True)
        # MS May 2012 if importOnly=True the macro fails to add the libraries
        # of nodes in the macronetwork which creates problems when the network
        # is saved again because node.library has no modName and varName set
        lines.extend(li)

        return lines


    def getAfterBlock(self, cache, networkName, indent="",
                      ignoreOriginal=False):
        # used to save macro node
        # add text for the 'def afterAddingToNetwork' method
        aftBlock = []
        aftBlock.append('\n')
        indent = indent + 4*" " # move forward 4
        aftBlock.append(indent+"def afterAddingToNetwork(self):\n")
        indent = indent + 4 * " " # move forward 4

        aftBlock.append(indent+"masterNet = self.macroNetwork\n")
        aftBlock.append(indent+"masterNet.editor._loadingNetwork = True\n")

        ## call base class afterAddingToNetwork
##         if self.library:
##             klass = self.originalClass.__name__
##             mod = self.originalClass.__module__
##             aftBlock.append(indent+"from %s import %s\n"%(mod,klass) )
##             aftBlock.append(indent+"%s.afterAddingToNetwork(self)\n"%klass)
##         else:
        aftBlock.append(
            indent+"from NetworkEditor.macros import MacroNode\n")
        aftBlock.append(indent+"MacroNode.afterAddingToNetwork(self)\n")

        ## loading libraries (we need these to set node libraries)
        li = self.macroNetwork.getLibraryImportCode(
            cache, indent, editor="self.editor",networkName='net',
            importOnly=True)
        aftBlock.extend(li)

        #lib = self.library
        #self.library = None
        ## add nodes to macro network, connect
        nodeName = self.getUniqueNodeName()
        aftBlock.append(indent+"## building macro network ##\n")
        aftBlock.append(indent+"%s = self\n"%nodeName)
        ed = self.getEditor()

        data = self.macroNetwork.getNetworkCreationSourceCode(
            "self.macroNetwork", False, indent, ignoreOriginal=True, importOnly=True)

        aftBlock.extend(data)

        # add code for MacroInpuPorts port modifications (such as
        # name
        txt = self.macroNetwork.ipNode.getDynamicPortsModificationSourceCode(
            "self.macroNetwork", indent, ignoreOriginal=True)
        aftBlock.extend(txt)

        # add code for MacroOutpuPorts port modifications (such as
        # singleConnection
        txt = self.macroNetwork.opNode.getDynamicPortsModificationSourceCode(
            "self.macroNetwork", indent, ignoreOriginal=True)
        aftBlock.extend(txt)

        ###############################################################
        # to allow the macro in the lib to have the correct port names
        ###############################################################
        txt = []
        txt.append(indent+"## configure MacroNode input ports\n")
        for ip in self.inputPorts:
            # add line to allow the renaming of the macronode's input ports
            txt.append(indent+\
                       "%s.inputPorts[%d].configure(name='%s')\n"%(
                       nodeName, ip.number, ip.name) )
            txt.append(indent+\
                       "%s.inputPorts[%d].configure(datatype='%s')\n"%(
                       nodeName, ip.number, ip.datatypeObject['name']) )
        txt.append(indent+"## configure MacroNode output ports\n")
        for op in self.outputPorts:
            txt.append(indent+\
                       "%s.outputPorts[%d].configure(name='%s')\n"%(
                       nodeName, op.number, op.name) )
            txt.append(indent+\
                       "%s.outputPorts[%d].configure(datatype='%s')\n"%(
                       nodeName, op.number, op.datatypeObject['name']) )
        aftBlock.extend(txt)

        aftBlock.append(indent+"masterNet.editor._loadingNetwork = False\n")

        #needed for "save source code"
        aftBlock.append("\n")
        aftBlock.append(indent+"%s.shrink()\n"%nodeName)
        aftBlock.append("\n")

        # add line to reset all tags
        aftBlock.append(indent+"## reset modifications ##\n")
        aftBlock.append(indent+"%s.resetTags()\n"%nodeName)
        aftBlock.append(indent+"%s.buildOriginalList()\n"%nodeName)
        aftBlock.append(indent+"%s.afterBuildingMacroNetwork()\n"%nodeName)
        #self.library = lib

        return aftBlock


class MacroImageNode(MacroNode, ImageNode):

    def __init__(self, name='NoName', library=None, iconFileName=None,
                 iconPath='', macroNetworkClass=MacroNetwork, **kw):

        constrkw = kw.pop('constrkw', None)
        MacroNode.__init__(self, name, None, None, constrkw, library, None,
                           macroNetworkClass, **kw)
        
        self.center= [0,0] # coords of node's center in canvas
        self.rotAngle = 0.0 # keep track of rotation angle
        self.selectOptions = {}
        self.deselectOptions = {}
        self.iconPath = iconPath
        self.iconFileName = iconFileName
        
        # create the node renderer
        from NetworkEditor.drawNode import CairoNodeRenderer
        self.renderer = CairoNodeRenderer()

    def buildIcons(self, canvas, posx, posy):
        ImageNode.buildIcons(self, canvas, posx, posy)
        
        try:
            self.menu.index('expand')
        except:
            self.menu.add_separator()
            self.menu.add_command(label='expand', command=self.expand)
            self.menu.add_command(label='shrink', command=self.shrink)
            self.addSaveNodeMenuEntries()

class MacroInputNode(MacroBase, NetworkNode):

    def __init__(self, macroNode=None, name='NoName', sourceCode=None,
                 originalClass=None, constrkw={}, library=None, progbar=0,
                 **kw):
        self.macroNode = macroNode # node representing the network
        apply( NetworkNode.__init__, (self, name, None, None), kw)

        self.isSchedulingNode = True # this node's function will schedule all
          # children of ports with new data

        self.outputPortsDescr.append({'name':'new',
                                      'balloon':'Add new input port'})

        self.readOnly = 1   # this node should never be edited
        #FIXME we should remove edit entry from node's menu
        # same for MacroInputNode

        code = """def doit(self, *args):
    # run all children of macroIpNode of ports that have new data
    # we add scheduled nodes to a dict so that a given node is not triggered
    # twice or more
    portsWithNewData = []
    for ip, op in map(None, self.macroNode.inputPorts, self.outputPorts[1:]):
        if self.network.forceExecution or ip.hasNewValidData():
            portsWithNewData.append(op)
    #print 'IN MacroInputNode, scheduling:', portsWithNewData
    self.scheduleChildren(portsWithNewData)\n"""

        self.setFunction(code)

        if self.macroNode is not None:
            self.mouseAction['<Double-Shift-Button-1>'] = self.macroNode.shrink


    def buildIcons(self, canvas, posx, posy):
        NetworkNode.buildIcons(self, canvas, posx, posy)
        myfont = list(self.getFont())
        if not 'bold' in myfont:
            myfont.append('bold')
        self.setFont(tuple(myfont))
        self.autoResizeX()
        try:
            self.menu.index('shrink')
        except:
            self.menu.add_command(label='shrink',command=self.macroNode.shrink)
        

    def getNodeDefinitionSourceCode(self, networkName, indent="",
                                    ignoreOriginal=False):
        lines = []

        if not self._modified and ignoreOriginal is False:
            return lines

        #lines = self.checkIfNodeForSavingIsDefined(lines, networkName, indent)

        nodeName = self.getUniqueNodeName()

        # always add a handle to the InputPort node
        txt = "%s = %s.ipNode\n"%(nodeName, networkName)
        lines.append(indent+txt)

        dummy, txt1 = self.getNodeSourceCodeForNode(networkName, indent,
                                                    ignoreOriginal)
        lines.extend(txt1)
        # since position is usually handled by addNode, we have to add
        # a line here to set the correct position
        if self.posx != 200 and self.posy != 20:
            txt2 = "%s.move(%d, %d)\n"%(nodeName, self.posx, self.posy)
            lines.append(indent+txt2)

        return lines


    def getDynamicPortsModificationSourceCode(self, networkName, indent="",
                                              ignoreOriginal=False):

        """This method returns source code to configure newly generated output
ports. Currently, we only catch the renaming of the ports.
"""
        
        lines = []
        if self._original is True and ignoreOriginal is False:
            return lines

        nodeName = self.getUniqueNodeName()

        # catch port modification "singleConnection":
        txt = []
        # add line to allow the renaming of the macronode's output ports
        for op in self.outputPorts[1:]:
            txt = self.checkIfNodeForSavingIsDefined(
                    txt, networkName, indent)
            txt.append(indent+\
                       "%s.outputPorts[%d].configure(name='%s')\n"%(
                       nodeName, op.number, op.name) )

        if len(txt):
            lines.append("\n")
            lines.append(indent+"## modifying MacroInputNode dynamic ports\n")
            lines.extend(txt)
        
        return lines


    def checkIfNodeForSavingIsDefined(self, lines, networkName, indent):
        """This method fixes a problem with saving macros that come from a
node library. If only a widget value has changed, we do not have a handle
to the node. Thus, we need to create this additional line to get a handle"""
        
        ed = self.getEditor()
        nodeName = self.getUniqueNodeName()

        if ed._tmpListOfSavedNodes.has_key(nodeName) is False:
            txt = "%s = %s.ipNode\n"%(nodeName, networkName)
            lines.append(indent+txt)
            ed._tmpListOfSavedNodes[nodeName] = self
        return lines
 

class MacroOutputNode(MacroBase, NetworkNode):

    def __init__(self, macroNode=None, name='NoName', sourceCode=None,
                 originalClass=None, constrkw={}, library=None, progbar=0,
                 **kw):
        self.macroNode = macroNode # node representing the network
        apply( NetworkNode.__init__, (self, name, None, None), kw)

        self.inputPortsDescr.append({'name':'new', 'required':False,
                                     'balloon':'Add new output port'})

        self.readOnly = 1   # this node should never be edited
        #FIXME we should remove edit entry from node's menu
        # same for MacroInputNode
        
        code = """def doit(self, *args):
    # output data on macrOpNode
    forceExecution = self.network.forceExecution
    for op, ip in map(None, self.macroNode.outputPorts, self.inputPorts[1:]):
        # valid, new or not we put whatever data we have on the output ports of the macroNode
        if len(ip.connections)==1:
            op.outputData(ip.getData())
        else:
            op.outputData(self.flatten(ip.getData()))

        # reset network force execution flag after the macro network ran
        self.network.forceExecution = 0
        # force the parent network to execute. This is needed for instance when
        # there are 2 levels of macros with an iterate at the lowest level
        self.macroNode.network.forceExecution = forceExecution
    
    # if we are in the MacroNetwork: schedule the children of the MacroNode
    # to run
    ed = self.getEditor()
    if 1: #self.network==ed.currentNetwork or forceExecution: 
        portsWithNewData = []
        for op, ip in map(None, self.macroNode.outputPorts,
            self.inputPorts[1:]):
            if ip.hasNewValidData():
                portsWithNewData.append(op)
        #print 'ABC', forceExecution, portsWithNewData
        #print 'MacroOutputNode scheduling:', portsWithNewData
        self.macroNode.scheduleChildren(portsWithNewData)\n"""

        self.setFunction(code)

        if self.macroNode is not None:
            self.mouseAction['<Double-Shift-Button-1>'] = self.macroNode.shrink


    def flatten(self, data):
        """When input port has multiple parents we want to concatenate the
data provided by all parents as if it came from a single parent since the
output port of the macro will provide all this data on a single port.
"""
        if isinstance(data, list):
            flatdata = []
            for d in data:
                if isinstance(d, list):
                    flatdata.extend(d)
                else:
                    return data
            return flatdata
        else:
            return data
    
                
    def buildIcons(self, canvas, posx, posy):
        NetworkNode.buildIcons(self, canvas, posx, posy)
        myfont = list(self.getFont())
        if not 'bold' in myfont:
            myfont.append('bold')
        self.setFont(tuple(myfont))
        self.autoResizeX()
        try:
            self.menu.index('shrink')
        except:
            self.menu.add_command(label='shrink',command=self.macroNode.shrink)


    def getNodeDefinitionSourceCode(self, networkName, indent="",
                                    ignoreOriginal=False):
        lines = []
        if not self._modified and ignoreOriginal is False:
            return lines
                
        nodeName = self.getUniqueNodeName()

        #lines = self.checkIfNodeForSavingIsDefined(lines, networkName, indent)
        # always define a handle to OutputPorts Node
        txt = "%s = %s.opNode\n"%(nodeName, networkName)
        lines.append(indent+txt)
        
        # save node modifications
        dummy, txt1 = self.getNodeSourceCodeForNode(networkName, indent,
                                                    ignoreOriginal)
        lines.extend(txt1)
        # since position is usually handled by addNode, we have to add
        # a line here to set the correct position
        if self.posx != 200 and self.posy != 280:
            txt2 = "%s.move(%d, %d)\n"%(nodeName, self.posx, self.posy)
            lines.append(indent+txt2)

        return lines


    def getDynamicPortsModificationSourceCode(self, networkName, indent="",
                                              ignoreOriginal=False):

        """This method returns source code to configure newly generated input
        ports. Currently, we only catch the event 'singleConnection'."""
        
        lines = []
        if self._original is True and ignoreOriginal is False:
            return lines

        nodeName = self.getUniqueNodeName()

        # catch port modification "singleConnection":
        txt = []
        for i in range(1, len(self.inputPorts)):
            p = self.inputPorts[i]
            txt = self.checkIfNodeForSavingIsDefined(
                    txt, networkName, indent)
            status = p.singleConnection
            if p.singleConnection == 'auto':
                status = "'auto'"
            else:
                status = p.singleConnection      
            txt.append(indent+\
                       "%s.inputPorts[%d].configure(singleConnection=%s)\n"%(nodeName,i,status))

        # add line to allow the renaming of the macronode's output ports
        for ip in self.inputPorts[1:]:
            txt.append(indent+\
                       "%s.inputPorts[%d].configure(name='%s')\n"%(
                       nodeName, ip.number, ip.name) )

        if len(txt):
            lines.append("\n")
            lines.append(indent+"## modifying MacroOutputNode dynamic ports\n")
            lines.extend(txt)
        
        return lines


    def checkIfNodeForSavingIsDefined(self, lines, networkName, indent):
        """This method fixes a problem with saving macros that come from a
node library. If only a widget value has changed, we do not have a handle
to the node. Thus, we need to create this additional line to get a handle"""
        
        ed = self.getEditor()
        nodeName = self.getUniqueNodeName()
        if ed._tmpListOfSavedNodes.has_key(nodeName) is False:
            txt = "%s = %s.opNode\n"%(nodeName, networkName)
            lines.append(indent+txt)
            ed._tmpListOfSavedNodes[nodeName] = self
        return lines