diff -r c9ace6a881c9 -r 233681f2a00e py_ext/PythonFileCTNMixin.py --- a/py_ext/PythonFileCTNMixin.py Wed May 08 21:37:31 2013 +0200 +++ b/py_ext/PythonFileCTNMixin.py Wed May 08 22:52:55 2013 +0200 @@ -6,17 +6,20 @@ from xmlclass import * import cPickle +from CodeFileTreeNode import CodeFile + PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) -class PythonFileCTNMixin: +class PythonFileCTNMixin(CodeFile): EditorType = PythonEditor def __init__(self): + CodeFile.__init__(self) filepath = self.PythonFileName() - self.PythonCode = PythonClasses["Python"]() + python_code = PythonClasses["Python"]() if os.path.isfile(filepath): xmlfile = open(filepath, 'r') tree = minidom.parse(xmlfile) @@ -24,87 +27,58 @@ for child in tree.childNodes: if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python": - self.PythonCode.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) - self.CreatePythonBuffer(True) - else: - self.CreatePythonBuffer(False) - self.OnCTNSave() - + python_code.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) + self.CodeFile.globals.settext(python_code.gettext()) + os.remove(filepath) + self.CreateCodeFileBuffer(False) + self.OnCTNSave() + + def CodeFileName(self): + return os.path.join(self.CTNPath(), "pyfile.xml") + def PythonFileName(self): return os.path.join(self.CTNPath(), "py_ext.xml") - def GetFilename(self): - if self.PythonBuffer.IsCurrentSaved(): - return "py_ext" - else: - return "~py_ext~" + def GetPythonCode(self): + current_location = self.GetCurrentLocation() + # define a unique name for the generated C file + location_str = "_".join(map(str, current_location)) + + text = "## Code generated by Beremiz python mixin confnode\n\n" + + # Adding includes + text += "## User includes\n" + text += self.CodeFile.includes.gettext() + text += "\n" + + # Adding variables + text += "## User variables reference\n" + for variable in self.CodeFile.variables.variable: + text += "%s = %s\n" % (variable.getname(), + str(variable.getinitial())) + text += "\n" + + # Adding user global variables and routines + text += "## User internal user variables and routines\n" + text += self.CodeFile.globals.gettext() + text += "\n" + + # 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().splitlines() + if len(lines) > 0 or return_code != "": + for line in code_object.gettext().splitlines(): + text += " " + line + text += " " + return_code + "\n\n" + else: + text += " pass\n\n" + + return text - def SetPythonCode(self, text): - self.PythonCode.settext(text) - - def GetPythonCode(self): - return self.PythonCode.gettext() - - def CTNTestModified(self): - return self.ChangesToSave or not self.PythonIsSaved() - - def OnCTNSave(self, from_project_path=None): - filepath = self.PythonFileName() - - text = "\n" - extras = {"xmlns":"http://www.w3.org/2001/XMLSchema", - "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", - "xsi:schemaLocation" : "py_ext_xsd.xsd"} - text += self.PythonCode.generateXMLText("Python", 0, extras) - - xmlfile = open(filepath,"w") - xmlfile.write(text.encode("utf-8")) - xmlfile.close() - - self.MarkPythonAsSaved() - return True - -#------------------------------------------------------------------------------- -# Current Buffering Management Functions -#------------------------------------------------------------------------------- - - """ - Return a copy of the project - """ - def Copy(self, model): - return cPickle.loads(cPickle.dumps(model)) - - def CreatePythonBuffer(self, saved): - self.Buffering = False - self.PythonBuffer = UndoBuffer(cPickle.dumps(self.PythonCode), saved) - - def BufferPython(self): - self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode)) - - def StartBuffering(self): - self.Buffering = True - - def EndBuffering(self): - if self.Buffering: - self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode)) - self.Buffering = False - - def MarkPythonAsSaved(self): - self.EndBuffering() - self.PythonBuffer.CurrentSaved() - - def PythonIsSaved(self): - return self.PythonBuffer.IsCurrentSaved() and not self.Buffering - - def LoadPrevious(self): - self.EndBuffering() - self.PythonCode = cPickle.loads(self.PythonBuffer.Previous()) - - def LoadNext(self): - self.PythonCode = cPickle.loads(self.PythonBuffer.Next()) - - def GetBufferState(self): - first = self.PythonBuffer.IsFirst() and not self.Buffering - last = self.PythonBuffer.IsLast() - return not first, not last -