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

        """subclass baseclass method to add lines to load the molecule in
        Pmv if it is not loaded yet"""

        lines = []
        if self.network.filename is not None:
            lNetworkDir = os.path.dirname(os.path.abspath(self.network.filename))
            lFileName = relpath(lNetworkDir, os.path.abspath(self.mol.parser.filename))
        else:
            lFileName = self.mol.parser.filename

        # we save the name of the file in a path relatives to the network
        s00 = 'import os\n'
        s01 = 'from mglutil.util.relpath import rel2abs\n'
        s02 = "lRelFileName = '%s'\n"%lFileName
        # we prepare the readding with an absolut path
        s03 = 'lNetworkDir = os.path.dirname(os.path.abspath(%s.filename))\n'%networkName
        s04 = 'lAbsFileName = rel2abs(lRelFileName, lNetworkDir)\n'
        
        s1 = 'mol = %s.editor.vf.loadMoleculeIfNeeded(lAbsFileName)\n'%networkName
        s2 = 'assert mol\n'
        lines.append(indent+s00)
        lines.append(indent+s01)
        lines.append(indent+s02)
        lines.append(indent+s03)
        lines.append(indent+s04)
        lines.append(indent+s1)
        lines.append(indent+s2)
        lines.extend(NetworkNode.getNodeDefinitionSourceCode(
            self, networkName, indent, ignoreOriginal) )
        return lines
示例#2
0
    def getNodeDefinitionSourceCode(self,
                                    networkName,
                                    indent="",
                                    ignoreOriginal=False):
        """subclass baseclass method to add lines to load the molecule in
        Pmv if it is not loaded yet"""

        if self.network.filename is not None:
            lNetworkDir = os.path.dirname(
                os.path.abspath(self.network.filename))
        elif hasattr(self.network, 'macroNode') \
          and self.network.macroNode.network.filename is not None:
            lNetworkDir = os.path.dirname(
                os.path.abspath(self.network.macroNode.network.filename))
        else:
            lNetworkDir = None
            assert False, "my guess is we don't go here"

        lines = []
        molecules = []
        for mol in self.set.top.uniq().data:
            # we save the name of the file in a path relatives to the network
            if lNetworkDir is not None:
                lFileName = relpath(lNetworkDir,
                                    os.path.abspath(mol.parser.filename))
            else:
                lFileName = mol.parser.filename
            molecules.append(lFileName)

        # we prepare the readding with an absolut path
        s0 = 'import os\n'
        s1 = 'from mglutil.util.relpath import rel2abs\n'
        s2 = 'lNetworkDir = os.path.dirname(os.path.abspath(%s.filename))\n' % networkName
        s3 = 'mols = %s\n' % molecules
        s4 = 'for m in mols:\n'
        s5 = '    lAbsFileName = rel2abs(m, lNetworkDir)\n'
        s6 = '    mol = %s.editor.vf.loadMoleculeIfNeeded(lAbsFileName)\n' % networkName
        s7 = '    assert mol\n'
        s8 = 'selString = "%s"\n' % self.selString
        s9 = '%s.editor.vf.loadModule("selectionCommands")\n' % networkName
        s10 = '%s.editor.vf.createSetIfNeeded(selString, "%s")\n' % (
            networkName, self.name)
        s11 = 'from Pmv.selectionCommands import sets__\n'
        lines.append(indent + s0)
        lines.append(indent + s1)
        lines.append(indent + s2)
        lines.append(indent + s3)
        lines.append(indent + s4)
        lines.append(indent + s5)
        lines.append(indent + s6)
        lines.append(indent + s7)
        lines.append(indent + s8)
        lines.append(indent + s9)
        lines.append(indent + s10)
        lines.append(indent + s11)

        lines.extend(
            NetworkNode.getNodeDefinitionSourceCode(self, networkName, indent,
                                                    ignoreOriginal))
        return lines
示例#3
0
    def getNodeDefinitionSourceCode(self, networkName, indent="",
                                    ignoreOriginal=False):

        """subclass baseclass method to add lines to load the molecule in
        Pmv if it is not loaded yet"""

        if self.network.filename is not None:
            lNetworkDir = os.path.dirname(os.path.abspath(self.network.filename))
        elif hasattr(self.network, 'macroNode') \
          and self.network.macroNode.network.filename is not None:
            lNetworkDir = os.path.dirname(os.path.abspath(self.network.macroNode.network.filename))
        else:
            lNetworkDir = None
            assert False, "my guess is we don't go here"

        lines = []
        molecules = []
        for mol in self.set.top.uniq().data:
            # we save the name of the file in a path relatives to the network
            if lNetworkDir is not None:
                lFileName = relpath(lNetworkDir, os.path.abspath(mol.parser.filename))
            else:
                lFileName = mol.parser.filename
            molecules.append(lFileName)

        # we prepare the readding with an absolut path
        s0 = 'import os\n'
        s1 = 'from mglutil.util.relpath import rel2abs\n'
        s2 = 'lNetworkDir = os.path.dirname(os.path.abspath(%s.filename))\n'%networkName
        s3 = 'mols = %s\n'%molecules
        s4 = 'for m in mols:\n'
        s5 = '    lAbsFileName = rel2abs(m, lNetworkDir)\n'
        s6 = '    mol = %s.editor.vf.loadMoleculeIfNeeded(lAbsFileName)\n'%networkName
        s7 = '    assert mol\n'
        s8 = 'selString = "%s"\n'%self.selString
        s9 = '%s.editor.vf.loadModule("selectionCommands")\n'%networkName
        s10 = '%s.editor.vf.createSetIfNeeded(selString, "%s")\n'%(networkName, self.name)
        s11 = 'from Pmv.selectionCommands import sets__\n'
        lines.append(indent+s0)
        lines.append(indent+s1)
        lines.append(indent+s2)
        lines.append(indent+s3)
        lines.append(indent+s4)
        lines.append(indent+s5)
        lines.append(indent+s6)
        lines.append(indent+s7)
        lines.append(indent+s8)
        lines.append(indent+s9)
        lines.append(indent+s10)
        lines.append(indent+s11)

        lines.extend(NetworkNode.getNodeDefinitionSourceCode(
            self, networkName, indent, ignoreOriginal) )
        return lines
示例#4
0
    def getNodeDefinitionSourceCode(self, lib, nodeId, nodeUniqId, networkName,
                                    ):
        
        txt = '#create CGI Form object:\n'
        txt = txt + 'from mglutil.web.HTMLParser import CGIForm\n'
        txt = txt + 'obj='+self.CGIFormObj.getCreationSourceCode()

        self.constrkw['CGIFormObj']='obj' # temporarily add kw to generate
                                          # correct list of arguments
        src = NetworkNode.getNodeDefinitionSourceCode(self, lib, nodeId,
                                                    nodeUniqId, networkName)

        del self.constrkw['CGIFormObj']   # now we remove it

        for line in src[0]:
            txt = txt + line
        
        return [txt], nodeId, nodeUniqId
示例#5
0
    def getNodeDefinitionSourceCode(self,
                                    networkName,
                                    indent="",
                                    ignoreOriginal=False):
        """subclass baseclass method to add lines to load the molecule in
        Pmv if it is not loaded yet"""

        lines = []
        if self.network.filename is not None:
            lNetworkDir = os.path.dirname(
                os.path.abspath(self.network.filename))
            lFileName = relpath(lNetworkDir,
                                os.path.abspath(self.mol.parser.filename))
        elif hasattr(self.network, 'macroNode') \
          and self.network.macroNode.network.filename is not None:
            lNetworkDir = os.path.dirname(
                os.path.abspath(self.network.macroNode.network.filename))
            lFileName = relpath(lNetworkDir,
                                os.path.abspath(self.mol.parser.filename))
        else:
            lFileName = self.mol.parser.filename

        # we save the name of the file in a path relatives to the network
        s00 = 'import os\n'
        s01 = 'from mglutil.util.relpath import rel2abs\n'
        lFileNameUnix = lFileName.replace('\\', '/')
        s02 = "lRelFileName = '%s'\n" % lFileNameUnix
        # we prepare the readding with an absolut path
        s03 = 'lNetworkDir = os.path.dirname(os.path.abspath(%s.filename))\n' % networkName
        s04 = 'lAbsFileName = rel2abs(lRelFileName, lNetworkDir)\n'

        s1 = 'mol = %s.editor.vf.loadMoleculeIfNeeded(lAbsFileName)\n' % networkName
        s2 = 'assert mol\n'
        lines.append(indent + s00)
        lines.append(indent + s01)
        lines.append(indent + s02)
        lines.append(indent + s03)
        lines.append(indent + s04)
        lines.append(indent + s1)
        lines.append(indent + s2)
        lines.extend(
            NetworkNode.getNodeDefinitionSourceCode(self, networkName, indent,
                                                    ignoreOriginal))
        return lines
示例#6
0
    def getNodeDefinitionSourceCode(
        self,
        lib,
        nodeId,
        nodeUniqId,
        networkName,
    ):

        txt = '#create CGI Form object:\n'
        txt = txt + 'from mglutil.web.HTMLParser import CGIForm\n'
        txt = txt + 'obj=' + self.CGIFormObj.getCreationSourceCode()

        self.constrkw['CGIFormObj'] = 'obj'  # temporarily add kw to generate
        # correct list of arguments
        src = NetworkNode.getNodeDefinitionSourceCode(self, lib, nodeId,
                                                      nodeUniqId, networkName)

        del self.constrkw['CGIFormObj']  # now we remove it

        for line in src[0]:
            txt = txt + line

        return [txt], nodeId, nodeUniqId
示例#7
0
    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.getUniqueNodeName()

        ###############################################################
        # add lines to import node from macros.py, add macro to network
        ###############################################################

        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
示例#8
0
    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.getUniqueNodeName()

        ###############################################################
        # add lines to import node from macros.py, add macro to network
        ###############################################################

        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