diff -r f794fbff8f02 -r 28f96aa9c070 py_ext/PythonFileCTNMixin.py --- a/py_ext/PythonFileCTNMixin.py Tue May 14 00:30:35 2013 +0200 +++ b/py_ext/PythonFileCTNMixin.py Tue May 14 18:41:33 2013 +0900 @@ -46,7 +46,19 @@ def PythonFileName(self): return os.path.join(self.CTNPath(), "py_ext.xml") - def GetSectionsCode(self): + PreSectionsTexts = {} + PostSectionsTexts = {} + def GetSection(self,section): + return self.PreSectionsTexts.get(section,"") + "\n" + \ + getattr(self.CodeFile, section).gettext() + "\n" + \ + self.PostSectionsTexts.get(section,"") + + + def CTNGenerate_C(self, buildpath, locations): + current_location = self.GetCurrentLocation() + # define a unique name for the generated C file + location_str = "_".join(map(lambda x:str(x), current_location)) + # Generate Beremiz python runtime variables code config = self.GetCTRoot().GetProjectConfigNames()[0] @@ -58,23 +70,95 @@ variable.getname(), variable.gettype(), str(variable.getinitial())) - - sections_code = { - "variables": variables_str, - "globals": self.CodeFile.globals.gettext().strip() - } - - # Generate Beremiz python runtime functions code + + # Runtime calls (start, stop, init, and cleanup) + rtcalls = "" 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 + rtcalls += "def _runtime_%s_%s():\n" % (location_str, section) + sectiontext = self.GetSection(section).strip() + if sectiontext: + rtcalls += ' ' + \ + sectiontext.strip().replace('\n', '\n ')+"\n" + else: + rtcalls += " pass\n\n" + + text = """\ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +## Code generated by Beremiz python mixin confnode +## + +## Code for PLC global variable access +%s + +## User code in "global" scope +%s +## Beremiz python runtime calls +%s + +"""%( # variables + variables_str, + # globals + self.GetSection("globals"), + # Beremiz python runtime functions + rtcalls) + + runtimefile_path = os.path.join(buildpath, + "runtime_%s.py"%location_str) + runtimefile = open(runtimefile_path, 'w') + runtimefile.write(text.encode('utf-8')) + runtimefile.close() + + text = """\ +/* + * Code generated by Beremiz py_ext confnode + * for safe global variables access + */ +#include "iec_types_all.h" +""" + + # Adding variables + text += "/* User variables reference */\n" + for variable in self.CodeFile.variables.variable: + var_infos = { + "name": variable.getname(), + "global": "%s__%s" % (config.upper(), + variable.getname().upper()), + "type": "__IEC_%s_t" % variable.gettype()} + text += "extern %(type)s %(global)s;\n" % var_infos + text += "%(type)s __buffer_%(name)s;\n" % var_infos + text += "\n" + + # Adding Beremiz confnode functions + text += "/* Beremiz confnode functions */\n" + text += "int __init_%s(int argc,char **argv)\n{\n"%location_str + text += "/*TODO*/\n" + text += " return 0;\n}\n\n" + + text += "void __cleanup_%s(void)\n{\n"%location_str + text += "/*TODO*/\n" + text += "\n}\n\n" + + text += "void __retrieve_%s(void)\n{\n"%location_str + text += "/*TODO*/\n" + text += "\n}\n\n" + + text += "void __publish_%s(void)\n{\n"%location_str + text += "/*TODO*/\n" + text += "\n}\n\n" + + Gen_PyCfile_path = os.path.join(buildpath, "PyCFile_%s.c"%location_str) + pycfile = open(Gen_PyCfile_path,'w') + pycfile.write(text) + pycfile.close() + + matiec_flags = '"-I%s"'%os.path.abspath( + self.GetCTRoot().GetIECLibPath()) + + return ([(Gen_PyCfile_path, matiec_flags)], + "", + False, + ("runtime_%s.py"%location_str, file(runtimefile_path,"rb"))) +