laurent@366: import os Edouard@728: from POULibrary import POULibrary Edouard@728: from PythonFileCTNMixin import PythonFileCTNMixin laurent@366: Edouard@728: class PythonLibrary(POULibrary): Edouard@728: def GetLibraryPath(self): Edouard@728: return os.path.join(os.path.split(__file__)[0], "pous.xml") laurent@366: Edouard@728: def Generate_C(self, buildpath, varlist, IECCFLAGS): laurent@366: laurent@366: plc_python_filepath = os.path.join(os.path.split(__file__)[0], "plc_python.c") laurent@366: plc_python_file = open(plc_python_filepath, 'r') laurent@366: plc_python_code = plc_python_file.read() laurent@366: plc_python_file.close() laurent@366: python_eval_fb_list = [] Edouard@728: for v in varlist: laurent@366: if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL","PYTHON_POLL"]: laurent@366: python_eval_fb_list.append(v) laurent@366: python_eval_fb_count = max(1, len(python_eval_fb_list)) laurent@366: laurent@366: # prepare python code Edouard@728: plc_python_code = plc_python_code % { "python_eval_fb_count": python_eval_fb_count } laurent@366: Edouard@728: Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c") laurent@366: pythonfile = open(Gen_Pythonfile_path,'w') laurent@366: pythonfile.write(plc_python_code) laurent@366: pythonfile.close() laurent@366: Edouard@728: return (["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), "" Edouard@728: Edouard@728: class PythonFile(PythonFileCTNMixin): laurent@738: laurent@781: def GetIconName(self): laurent@781: return "Pyfile" laurent@738: Edouard@728: def CTNGenerate_C(self, buildpath, locations): Edouard@728: current_location = self.GetCurrentLocation() Edouard@728: # define a unique name for the generated C file Edouard@728: location_str = "_".join(map(lambda x:str(x), current_location)) Edouard@728: Laurent@1124: sections_code = self.GetSectionsCode() Laurent@1124: Laurent@1124: text = "## Code generated by Beremiz python mixin confnode\n\n" Laurent@1124: Laurent@1124: # Adding variables Laurent@1124: text += "## User variables reference\n" Laurent@1124: text += sections_code["variables"] Laurent@1124: text += "\n" Laurent@1124: Laurent@1124: # Adding user global variables and routines Laurent@1124: text += "## User internal user variables and routines\n" Laurent@1124: text += sections_code["globals"] Laurent@1124: text += "\n" Laurent@1124: Laurent@1124: # Adding Beremiz python runtime functions Laurent@1124: text += "## Beremiz python runtime functions\n" Laurent@1124: for section in self.SECTIONS_NAMES: Laurent@1124: if section != "globals": Laurent@1124: code_object = getattr(self.CodeFile, section) Laurent@1124: text += "def _runtime_%s_%s():\n" % (location_str, section) Laurent@1124: section_code = sections_code.get(section) Laurent@1124: if section_code: Laurent@1124: text += section_code Laurent@1124: else: Laurent@1124: text += " pass\n\n" Laurent@1124: laurent@366: runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str) laurent@366: runtimefile = open(runtimefile_path, 'w') Laurent@1124: runtimefile.write(text) laurent@366: runtimefile.close() laurent@366: Edouard@728: return [], "", False, ("runtime_%s.py"%location_str, file(runtimefile_path,"rb")) Edouard@728: