py_ext/PythonFileCTNMixin.py
changeset 1124 b1705000eba1
parent 1120 35d772ec1a76
child 1132 28f96aa9c070
equal deleted inserted replaced
1123:55ed55ef7aea 1124:b1705000eba1
     1 import os
     1 import os
     2 from PLCControler import UndoBuffer
     2 from PLCControler import UndoBuffer
     3 from PythonEditor import PythonEditor
     3 from PythonEditor import PythonEditor
     4 
     4 
     5 from xml.dom import minidom
     5 from xml.dom import minidom
     6 from xmlclass import *
     6 from xmlclass import GenerateClassesFromXSD
     7 import cPickle
     7 import cPickle
     8 
     8 
     9 from CodeFileTreeNode import CodeFile
     9 from CodeFileTreeNode import CodeFile
    10 
    10 
    11 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) 
    11 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) 
    12 
    12 
    13 class PythonFileCTNMixin(CodeFile):
    13 class PythonFileCTNMixin(CodeFile):
    14     
    14     
       
    15     CODEFILE_NAME = "PyFile"
       
    16     SECTIONS_NAMES = [
       
    17         "globals",
       
    18         "init",
       
    19         "cleanup",
       
    20         "start",
       
    21         "stop"]
    15     EditorType = PythonEditor
    22     EditorType = PythonEditor
    16     
    23     
    17     def __init__(self):
    24     def __init__(self):
    18         CodeFile.__init__(self)
    25         CodeFile.__init__(self)
    19         
    26         
    37         return os.path.join(self.CTNPath(), "pyfile.xml")
    44         return os.path.join(self.CTNPath(), "pyfile.xml")
    38     
    45     
    39     def PythonFileName(self):
    46     def PythonFileName(self):
    40         return os.path.join(self.CTNPath(), "py_ext.xml")
    47         return os.path.join(self.CTNPath(), "py_ext.xml")
    41 
    48 
    42     def GetPythonCode(self):
    49     def GetSectionsCode(self):
    43         current_location = self.GetCurrentLocation()
       
    44         # define a unique name for the generated C file
       
    45         location_str = "_".join(map(str, current_location))
       
    46         
    50         
    47         text = "## Code generated by Beremiz python mixin confnode\n\n"
    51         # Generate Beremiz python runtime variables code
    48         
       
    49         # Adding includes
       
    50         text += "## User includes\n"
       
    51         text += self.CodeFile.includes.gettext().strip()
       
    52         text += "\n"
       
    53         
       
    54         # Adding variables
       
    55         text += "## User variables reference\n"
       
    56         config = self.GetCTRoot().GetProjectConfigNames()[0]
    52         config = self.GetCTRoot().GetProjectConfigNames()[0]
       
    53         variables_str = ""
    57         for variable in self.CodeFile.variables.variable:
    54         for variable in self.CodeFile.variables.variable:
    58             global_name = "%s_%s" % (config.upper(), variable.getname().upper())
    55             global_name = "%s_%s" % (config.upper(), variable.getname().upper())
    59             text += "# global_var:%s python_var:%s type:%s initial:%s\n" % (
    56             variables_str += "# global_var:%s python_var:%s type:%s initial:%s\n" % (
    60                 global_name,
    57                 global_name,
    61                 variable.getname(),
    58                 variable.getname(),
    62                 variable.gettype(),
    59                 variable.gettype(),
    63                 str(variable.getinitial()))
    60                 str(variable.getinitial()))
    64         text += "\n"
       
    65         
    61         
    66         # Adding user global variables and routines
    62         sections_code = {
    67         text += "## User internal user variables and routines\n"
    63             "variables": variables_str,
    68         text += self.CodeFile.globals.gettext().strip()
    64             "globals": self.CodeFile.globals.gettext().strip()
    69         text += "\n"
    65         }
    70         
    66         
    71         # Adding Beremiz confnode functions
    67         # Generate Beremiz python runtime functions code
    72         text += "## Beremiz confnode functions\n"
    68         for section in self.SECTIONS_NAMES:
    73         for func, args, return_code, code_object in [
    69             if section != "globals":
    74             ("__init_", "*args, **kwargs", 
    70                 code_object = getattr(self.CodeFile, section)
    75              "return 0", self.CodeFile.initFunction),
    71                 section_str = ""
    76             ("__cleanup_", "", "", self.CodeFile.cleanUpFunction),
    72                 lines = code_object.gettext().strip().splitlines()
    77             ("__retrieve_", "", "", self.CodeFile.retrieveFunction),
    73                 if len(lines) > 0:
    78             ("__publish_", "", "", self.CodeFile.publishFunction),]:
    74                     for line in lines:
    79             text += "def %s%s(%s):\n" % (func, location_str, args)
    75                         section_str += "    " + line + "\n"
    80             lines = code_object.gettext().strip().splitlines()
    76                     section_str += "\n"
    81             if len(lines) > 0 or return_code != "":
    77                 sections_code[section] = section_str
    82                 for line in lines:
    78             
    83                     text += "    " + line + "\n"
    79         return sections_code
    84                 if return_code != "":
       
    85                     text += "    " + return_code + "\n"
       
    86                 text += "\n"
       
    87             else:
       
    88                 text += "    pass\n\n"
       
    89         
       
    90         return text
       
    91 
    80