lbessard@145: import os
laurent@401: from xml.dom import minidom
laurent@401: import cPickle
laurent@401:
Laurent@1124: from xmlclass import GenerateClassesFromXSDstring, UpdateXMLClassGlobals
laurent@401:
Laurent@1096: from PLCControler import UndoBuffer
laurent@630:
Laurent@1124: CODEFILE_XSD = """
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124: %(includes_section)s
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124: %(sections)s
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124: Formatted text according to parts of XHTML 1.1
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124:
Laurent@1124: """
Laurent@1124:
Laurent@1124: SECTION_TAG_ELEMENT = ""
lbessard@145:
Laurent@1096: class CodeFile:
laurent@656:
Laurent@1124: CODEFILE_NAME = "CodeFile"
Laurent@1124: SECTIONS_NAMES = []
Laurent@1124:
etisserant@31: def __init__(self):
Laurent@1124: sections_str = {"codefile_name": self.CODEFILE_NAME}
Laurent@1124: if "includes" in self.SECTIONS_NAMES:
Laurent@1124: sections_str["includes_section"] = SECTION_TAG_ELEMENT % "includes"
Laurent@1124: else:
Laurent@1124: sections_str["includes_section"] = ""
Laurent@1124: sections_str["sections"] = "\n".join(
Laurent@1124: [SECTION_TAG_ELEMENT % name
Laurent@1124: for name in self.SECTIONS_NAMES if name != "includes"])
Laurent@1124:
Laurent@1124: self.CodeFileClasses = GenerateClassesFromXSDstring(
Laurent@1124: CODEFILE_XSD % sections_str)
Laurent@1124:
Laurent@1096: filepath = self.CodeFileName()
lbessard@145:
Laurent@1124: self.CodeFile = self.CodeFileClasses[self.CODEFILE_NAME]()
lbessard@145: if os.path.isfile(filepath):
lbessard@145: xmlfile = open(filepath, 'r')
lbessard@145: tree = minidom.parse(xmlfile)
lbessard@145: xmlfile.close()
lbessard@145:
lbessard@145: for child in tree.childNodes:
Laurent@1124: if child.nodeType == tree.ELEMENT_NODE and child.nodeName in [self.CODEFILE_NAME]:
Laurent@1096: self.CodeFile.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"])
Laurent@1096: self.CreateCodeFileBuffer(True)
lbessard@145: else:
Laurent@1096: self.CreateCodeFileBuffer(False)
Edouard@718: self.OnCTNSave()
lbessard@145:
lbessard@145: def GetBaseTypes(self):
Edouard@718: return self.GetCTRoot().GetBaseTypes()
lbessard@145:
Laurent@1096: def GetDataTypes(self, basetypes = False):
Laurent@1096: return self.GetCTRoot().GetDataTypes(basetypes=basetypes)
lbessard@145:
lbessard@145: def SetVariables(self, variables):
Laurent@1096: self.CodeFile.variables.setvariable([])
lbessard@145: for var in variables:
Laurent@1124: variable = self.CodeFileClasses["variables_variable"]()
lbessard@145: variable.setname(var["Name"])
lbessard@145: variable.settype(var["Type"])
Laurent@1096: variable.setinitial(var["Initial"])
Laurent@1096: self.CodeFile.variables.appendvariable(variable)
lbessard@145:
lbessard@145: def GetVariables(self):
lbessard@145: datas = []
Laurent@1096: for var in self.CodeFile.variables.getvariable():
Laurent@1124: datas.append({"Name" : var.getname(),
Laurent@1124: "Type" : var.gettype(),
Laurent@1124: "Initial" : var.getinitial()})
lbessard@145: return datas
lbessard@145:
Laurent@1096: def SetTextParts(self, parts):
Laurent@1124: for section in self.SECTIONS_NAMES:
Laurent@1124: section_code = parts.get(section)
Laurent@1124: if section_code is not None:
Laurent@1124: getattr(self.CodeFile, section).settext(section_code)
Laurent@1124:
Laurent@1096: def GetTextParts(self):
Laurent@1124: return dict([(section, getattr(self.CodeFile, section).gettext())
Laurent@1124: for section in self.SECTIONS_NAMES])
Laurent@1124:
Edouard@718: def CTNTestModified(self):
Laurent@1096: return self.ChangesToSave or not self.CodeFileIsSaved()
laurent@630:
Laurent@1061: def OnCTNSave(self, from_project_path=None):
Laurent@1096: filepath = self.CodeFileName()
lbessard@145:
lbessard@145: text = "\n"
Laurent@1124: text += self.CodeFile.generateXMLText(self.CODEFILE_NAME, 0)
lbessard@145:
lbessard@145: xmlfile = open(filepath,"w")
laurent@430: xmlfile.write(text.encode("utf-8"))
lbessard@145: xmlfile.close()
lbessard@145:
Laurent@1096: self.MarkCodeFileAsSaved()
etisserant@31: return True
etisserant@31:
Laurent@1095: def CTNGlobalInstances(self):
Laurent@1095: current_location = self.GetCurrentLocation()
Laurent@1096: return [(variable.getname(),
Laurent@1096: variable.gettype(),
Laurent@1096: variable.getinitial())
Laurent@1096: for variable in self.CodeFile.variables.variable]
laurent@656:
lbessard@145: #-------------------------------------------------------------------------------
lbessard@145: # Current Buffering Management Functions
lbessard@145: #-------------------------------------------------------------------------------
lbessard@145:
Laurent@1124: def cPickle_loads(self, str_obj):
Laurent@1124: UpdateXMLClassGlobals(self.CodeFileClasses)
Laurent@1124: return cPickle.loads(str_obj)
Laurent@1124:
Laurent@1124: def cPickle_dumps(self, obj):
Laurent@1124: UpdateXMLClassGlobals(self.CodeFileClasses)
Laurent@1124: return cPickle.dumps(obj)
Laurent@1124:
lbessard@145: """
Laurent@1096: Return a copy of the codefile model
lbessard@145: """
lbessard@145: def Copy(self, model):
Laurent@1124: return self.cPickle_loads(self.cPickle_dumps(model))
lbessard@145:
Laurent@1096: def CreateCodeFileBuffer(self, saved):
laurent@658: self.Buffering = False
Laurent@1124: self.CodeFileBuffer = UndoBuffer(self.cPickle_dumps(self.CodeFile), saved)
laurent@651:
Laurent@1096: def BufferCodeFile(self):
Laurent@1124: self.CodeFileBuffer.Buffering(self.cPickle_dumps(self.CodeFile))
lbessard@145:
lbessard@145: def StartBuffering(self):
lbessard@145: self.Buffering = True
lbessard@145:
lbessard@145: def EndBuffering(self):
lbessard@145: if self.Buffering:
Laurent@1124: self.CodeFileBuffer.Buffering(self.cPickle_dumps(self.CodeFile))
lbessard@145: self.Buffering = False
lbessard@145:
Laurent@1096: def MarkCodeFileAsSaved(self):
laurent@651: self.EndBuffering()
Laurent@1096: self.CodeFileBuffer.CurrentSaved()
laurent@651:
Laurent@1096: def CodeFileIsSaved(self):
Laurent@1096: return self.CodeFileBuffer.IsCurrentSaved() and not self.Buffering
laurent@658:
lbessard@145: def LoadPrevious(self):
laurent@651: self.EndBuffering()
Laurent@1124: self.CodeFile = self.cPickle_loads(self.CodeFileBuffer.Previous())
lbessard@145:
lbessard@145: def LoadNext(self):
Laurent@1124: self.CodeFile = self.cPickle_loads(self.CodeFileBuffer.Next())
lbessard@145:
lbessard@145: def GetBufferState(self):
Laurent@1096: first = self.CodeFileBuffer.IsFirst() and not self.Buffering
Laurent@1096: last = self.CodeFileBuffer.IsLast()
lbessard@145: return not first, not last
lbessard@145: