py_ext/PythonFileCTNMixin.py
changeset 1124 b1705000eba1
parent 1120 35d772ec1a76
child 1132 28f96aa9c070
--- a/py_ext/PythonFileCTNMixin.py	Sun May 12 23:32:30 2013 +0200
+++ b/py_ext/PythonFileCTNMixin.py	Mon May 13 14:31:23 2013 +0200
@@ -3,7 +3,7 @@
 from PythonEditor import PythonEditor
 
 from xml.dom import minidom
-from xmlclass import *
+from xmlclass import GenerateClassesFromXSD
 import cPickle
 
 from CodeFileTreeNode import CodeFile
@@ -12,6 +12,13 @@
 
 class PythonFileCTNMixin(CodeFile):
     
+    CODEFILE_NAME = "PyFile"
+    SECTIONS_NAMES = [
+        "globals",
+        "init",
+        "cleanup",
+        "start",
+        "stop"]
     EditorType = PythonEditor
     
     def __init__(self):
@@ -39,53 +46,35 @@
     def PythonFileName(self):
         return os.path.join(self.CTNPath(), "py_ext.xml")
 
-    def GetPythonCode(self):
-        current_location = self.GetCurrentLocation()
-        # define a unique name for the generated C file
-        location_str = "_".join(map(str, current_location))
+    def GetSectionsCode(self):
         
-        text = "## Code generated by Beremiz python mixin confnode\n\n"
-        
-        # Adding includes
-        text += "## User includes\n"
-        text += self.CodeFile.includes.gettext().strip()
-        text += "\n"
-        
-        # Adding variables
-        text += "## User variables reference\n"
+        # Generate Beremiz python runtime variables code
         config = self.GetCTRoot().GetProjectConfigNames()[0]
+        variables_str = ""
         for variable in self.CodeFile.variables.variable:
             global_name = "%s_%s" % (config.upper(), variable.getname().upper())
-            text += "# global_var:%s python_var:%s type:%s initial:%s\n" % (
+            variables_str += "# global_var:%s python_var:%s type:%s initial:%s\n" % (
                 global_name,
                 variable.getname(),
                 variable.gettype(),
                 str(variable.getinitial()))
-        text += "\n"
         
-        # Adding user global variables and routines
-        text += "## User internal user variables and routines\n"
-        text += self.CodeFile.globals.gettext().strip()
-        text += "\n"
+        sections_code = {
+            "variables": variables_str,
+            "globals": self.CodeFile.globals.gettext().strip()
+        }
         
-        # Adding Beremiz confnode functions
-        text += "## Beremiz confnode functions\n"
-        for func, args, return_code, code_object in [
-            ("__init_", "*args, **kwargs", 
-             "return 0", self.CodeFile.initFunction),
-            ("__cleanup_", "", "", self.CodeFile.cleanUpFunction),
-            ("__retrieve_", "", "", self.CodeFile.retrieveFunction),
-            ("__publish_", "", "", self.CodeFile.publishFunction),]:
-            text += "def %s%s(%s):\n" % (func, location_str, args)
-            lines = code_object.gettext().strip().splitlines()
-            if len(lines) > 0 or return_code != "":
-                for line in lines:
-                    text += "    " + line + "\n"
-                if return_code != "":
-                    text += "    " + return_code + "\n"
-                text += "\n"
-            else:
-                text += "    pass\n\n"
-        
-        return text
+        # Generate Beremiz python runtime functions code
+        for section in self.SECTIONS_NAMES:
+            if section != "globals":
+                code_object = getattr(self.CodeFile, section)
+                section_str = ""
+                lines = code_object.gettext().strip().splitlines()
+                if len(lines) > 0:
+                    for line in lines:
+                        section_str += "    " + line + "\n"
+                    section_str += "\n"
+                sections_code[section] = section_str
+            
+        return sections_code