py_ext/PythonFileCTNMixin.py
author Laurent Bessard
Wed, 08 May 2013 22:52:55 +0200
changeset 1097 233681f2a00e
parent 1061 02f371f3e063
child 1103 2fc1eef45bda
permissions -rw-r--r--
Fixed Python editor adding variable panel
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     1
import os
654
9f6c091c316c Removing multiple definition of UndoBuffer
laurent
parents: 430
diff changeset
     2
from PLCControler import UndoBuffer
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
     3
from PythonEditor import PythonEditor
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     4
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     5
from xml.dom import minidom
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     6
from xmlclass import *
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     7
import cPickle
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
     8
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
     9
from CodeFileTreeNode import CodeFile
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    10
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
    11
PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) 
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    12
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    13
class PythonFileCTNMixin(CodeFile):
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    14
    
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    15
    EditorType = PythonEditor
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    16
    
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    17
    def __init__(self):
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    18
        CodeFile.__init__(self)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    19
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    20
        filepath = self.PythonFileName()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    21
        
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    22
        python_code = PythonClasses["Python"]()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    23
        if os.path.isfile(filepath):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    24
            xmlfile = open(filepath, 'r')
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    25
            tree = minidom.parse(xmlfile)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    26
            xmlfile.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    27
            
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    28
            for child in tree.childNodes:
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    29
                if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python":
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    30
                    python_code.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"])
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    31
                    self.CodeFile.globals.settext(python_code.gettext())
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    32
                    os.remove(filepath)
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    33
                    self.CreateCodeFileBuffer(False)
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    34
                    self.OnCTNSave()
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    35
    
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    36
    def CodeFileName(self):
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    37
        return os.path.join(self.CTNPath(), "pyfile.xml")
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    38
    
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    39
    def PythonFileName(self):
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
    40
        return os.path.join(self.CTNPath(), "py_ext.xml")
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    41
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    42
    def GetPythonCode(self):
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    43
        current_location = self.GetCurrentLocation()
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    44
        # define a unique name for the generated C file
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    45
        location_str = "_".join(map(str, current_location))
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    46
        
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    47
        text = "## Code generated by Beremiz python mixin confnode\n\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    48
        
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    49
        # Adding includes
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    50
        text += "## User includes\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    51
        text += self.CodeFile.includes.gettext()
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    52
        text += "\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    53
        
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    54
        # Adding variables
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    55
        text += "## User variables reference\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    56
        for variable in self.CodeFile.variables.variable:
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    57
            text += "%s = %s\n" % (variable.getname(),
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    58
                                   str(variable.getinitial()))
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    59
        text += "\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    60
        
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    61
        # Adding user global variables and routines
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    62
        text += "## User internal user variables and routines\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    63
        text += self.CodeFile.globals.gettext()
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    64
        text += "\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    65
        
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    66
        # Adding Beremiz confnode functions
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    67
        text += "## Beremiz confnode functions\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    68
        for func, args, return_code, code_object in [
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    69
            ("__init_", "*args, **kwargs", 
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    70
             "return 0", self.CodeFile.initFunction),
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    71
            ("__cleanup_", "", "", self.CodeFile.cleanUpFunction),
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    72
            ("__retrieve_", "", "", self.CodeFile.retrieveFunction),
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    73
            ("__publish_", "", "", self.CodeFile.publishFunction),]:
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    74
            text += "def %s%s(%s):\n" % (func, location_str, args)
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    75
            lines = code_object.gettext().splitlines()
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    76
            if len(lines) > 0 or return_code != "":
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    77
                for line in code_object.gettext().splitlines():
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    78
                    text += "    " + line
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    79
                text += "    " + return_code + "\n\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    80
            else:
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    81
                text += "    pass\n\n"
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    82
        
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    83
        return text
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    84