py_ext/PythonFileCTNMixin.py
author Laurent Bessard
Mon, 13 May 2013 14:31:23 +0200
changeset 1124 b1705000eba1
parent 1120 35d772ec1a76
child 1132 28f96aa9c070
permissions -rw-r--r--
Fixed support for defining python runtime code using sections like in c_ext
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
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
     6
from xmlclass import GenerateClassesFromXSD
366
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
    
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    15
    CODEFILE_NAME = "PyFile"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    16
    SECTIONS_NAMES = [
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    17
        "globals",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    18
        "init",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    19
        "cleanup",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    20
        "start",
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    21
        "stop"]
657
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    22
    EditorType = PythonEditor
340c0b9caeca Adding support for integrating Python code Editor into Beremiz frame.
laurent
parents: 654
diff changeset
    23
    
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    24
    def __init__(self):
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    25
        CodeFile.__init__(self)
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    26
        
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    27
        filepath = self.PythonFileName()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    28
        
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    29
        python_code = PythonClasses["Python"]()
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    30
        if os.path.isfile(filepath):
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    31
            xmlfile = open(filepath, 'r')
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    32
            tree = minidom.parse(xmlfile)
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    33
            xmlfile.close()
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    34
            
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    35
            for child in tree.childNodes:
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    36
                if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python":
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    37
                    python_code.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"])
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    38
                    self.CodeFile.globals.settext(python_code.gettext())
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    39
                    os.remove(filepath)
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    40
                    self.CreateCodeFileBuffer(False)
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    41
                    self.OnCTNSave()
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    42
    
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    43
    def CodeFileName(self):
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    44
        return os.path.join(self.CTNPath(), "pyfile.xml")
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    45
    
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    46
    def PythonFileName(self):
721
ecf4d203c4d4 refactoring
Edouard Tisserant
parents: 718
diff changeset
    47
        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
    48
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    49
    def GetSectionsCode(self):
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    50
        
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    51
        # Generate Beremiz python runtime variables code
1103
2fc1eef45bda Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents: 1097
diff changeset
    52
        config = self.GetCTRoot().GetProjectConfigNames()[0]
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    53
        variables_str = ""
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    54
        for variable in self.CodeFile.variables.variable:
1103
2fc1eef45bda Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents: 1097
diff changeset
    55
            global_name = "%s_%s" % (config.upper(), variable.getname().upper())
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    56
            variables_str += "# global_var:%s python_var:%s type:%s initial:%s\n" % (
1103
2fc1eef45bda Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents: 1097
diff changeset
    57
                global_name,
2fc1eef45bda Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents: 1097
diff changeset
    58
                variable.getname(),
2fc1eef45bda Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents: 1097
diff changeset
    59
                variable.gettype(),
2fc1eef45bda Fixed python global variable declaration example in generated runtime python file
Laurent Bessard
parents: 1097
diff changeset
    60
                str(variable.getinitial()))
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    61
        
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    62
        sections_code = {
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    63
            "variables": variables_str,
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    64
            "globals": self.CodeFile.globals.gettext().strip()
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    65
        }
1097
233681f2a00e Fixed Python editor adding variable panel
Laurent Bessard
parents: 1061
diff changeset
    66
        
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    67
        # Generate Beremiz python runtime functions code
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    68
        for section in self.SECTIONS_NAMES:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    69
            if section != "globals":
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    70
                code_object = getattr(self.CodeFile, section)
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    71
                section_str = ""
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    72
                lines = code_object.gettext().strip().splitlines()
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    73
                if len(lines) > 0:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    74
                    for line in lines:
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    75
                        section_str += "    " + line + "\n"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    76
                    section_str += "\n"
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    77
                sections_code[section] = section_str
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    78
            
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1120
diff changeset
    79
        return sections_code
366
cd90e4c10261 Move python evaluator to create a python plugin containing any related python module
laurent
parents:
diff changeset
    80