laurent@366: import os Edouard@728: from util import opjimg laurent@654: from PLCControler import UndoBuffer laurent@657: from PythonEditor import PythonEditor laurent@366: laurent@366: from xml.dom import minidom laurent@366: from xmlclass import * laurent@366: import cPickle laurent@366: Edouard@721: PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) laurent@366: Edouard@728: class PythonFileCTNMixin: laurent@366: laurent@657: EditorType = PythonEditor laurent@657: laurent@366: def __init__(self): laurent@366: Edouard@717: self.ConfNodeMethods.insert(0, laurent@366: {"bitmap" : opjimg("editPYTHONcode"), laurent@366: "name" : _("Edit Python File"), laurent@366: "tooltip" : _("Edit Python File"), laurent@366: "method" : "_OpenView"}, laurent@366: ) laurent@366: laurent@366: filepath = self.PythonFileName() laurent@366: laurent@366: self.PythonCode = PythonClasses["Python"]() laurent@366: if os.path.isfile(filepath): laurent@366: xmlfile = open(filepath, 'r') laurent@366: tree = minidom.parse(xmlfile) laurent@366: xmlfile.close() laurent@366: laurent@366: for child in tree.childNodes: laurent@366: if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python": laurent@366: self.PythonCode.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) laurent@657: self.CreatePythonBuffer(True) laurent@366: else: laurent@657: self.CreatePythonBuffer(False) Edouard@718: self.OnCTNSave() laurent@366: laurent@366: def PythonFileName(self): Edouard@721: return os.path.join(self.CTNPath(), "py_ext.xml") laurent@366: laurent@366: def GetFilename(self): laurent@366: if self.PythonBuffer.IsCurrentSaved(): Edouard@721: return "py_ext" laurent@366: else: Edouard@721: return "~py_ext~" laurent@366: laurent@366: def SetPythonCode(self, text): laurent@366: self.PythonCode.settext(text) laurent@366: laurent@366: def GetPythonCode(self): laurent@366: return self.PythonCode.gettext() laurent@366: Edouard@718: def CTNTestModified(self): laurent@657: return self.ChangesToSave or not self.PythonIsSaved() laurent@366: Edouard@718: def OnCTNSave(self): laurent@366: filepath = self.PythonFileName() laurent@366: laurent@366: text = "\n" laurent@366: extras = {"xmlns":"http://www.w3.org/2001/XMLSchema", laurent@366: "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", Edouard@721: "xsi:schemaLocation" : "py_ext_xsd.xsd"} laurent@366: text += self.PythonCode.generateXMLText("Python", 0, extras) laurent@366: laurent@366: xmlfile = open(filepath,"w") laurent@430: xmlfile.write(text.encode("utf-8")) laurent@366: xmlfile.close() laurent@366: laurent@657: self.MarkPythonAsSaved() laurent@366: return True laurent@366: laurent@366: #------------------------------------------------------------------------------- laurent@366: # Current Buffering Management Functions laurent@366: #------------------------------------------------------------------------------- laurent@366: laurent@366: """ laurent@366: Return a copy of the project laurent@366: """ laurent@366: def Copy(self, model): laurent@366: return cPickle.loads(cPickle.dumps(model)) laurent@366: laurent@657: def CreatePythonBuffer(self, saved): laurent@657: self.Buffering = False laurent@657: self.PythonBuffer = UndoBuffer(cPickle.dumps(self.PythonCode), saved) laurent@657: laurent@366: def BufferPython(self): laurent@657: self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode)) laurent@366: laurent@366: def StartBuffering(self): laurent@366: self.Buffering = True laurent@366: laurent@366: def EndBuffering(self): laurent@366: if self.Buffering: laurent@657: self.PythonBuffer.Buffering(cPickle.dumps(self.PythonCode)) laurent@366: self.Buffering = False laurent@366: laurent@657: def MarkPythonAsSaved(self): laurent@657: self.EndBuffering() laurent@657: self.PythonBuffer.CurrentSaved() laurent@657: laurent@657: def PythonIsSaved(self): laurent@657: return self.PythonBuffer.IsCurrentSaved() and not self.Buffering laurent@657: laurent@366: def LoadPrevious(self): laurent@657: self.EndBuffering() laurent@657: self.PythonCode = cPickle.loads(self.PythonBuffer.Previous()) laurent@366: laurent@366: def LoadNext(self): laurent@657: self.PythonCode = cPickle.loads(self.PythonBuffer.Next()) laurent@366: laurent@366: def GetBufferState(self): laurent@657: first = self.PythonBuffer.IsFirst() and not self.Buffering laurent@366: last = self.PythonBuffer.IsLast() laurent@366: return not first, not last laurent@366: