laurent@366: import os 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: Laurent@1097: from CodeFileTreeNode import CodeFile Laurent@1097: Edouard@721: PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) laurent@366: Laurent@1097: class PythonFileCTNMixin(CodeFile): laurent@366: laurent@657: EditorType = PythonEditor laurent@657: laurent@366: def __init__(self): Laurent@1097: CodeFile.__init__(self) laurent@366: laurent@366: filepath = self.PythonFileName() laurent@366: Laurent@1097: python_code = 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@1097: python_code.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) Laurent@1097: self.CodeFile.globals.settext(python_code.gettext()) Laurent@1097: os.remove(filepath) Laurent@1097: self.CreateCodeFileBuffer(False) Laurent@1097: self.OnCTNSave() Laurent@1097: Laurent@1097: def CodeFileName(self): Laurent@1097: return os.path.join(self.CTNPath(), "pyfile.xml") Laurent@1097: laurent@366: def PythonFileName(self): Edouard@721: return os.path.join(self.CTNPath(), "py_ext.xml") laurent@366: Laurent@1097: def GetPythonCode(self): Laurent@1097: current_location = self.GetCurrentLocation() Laurent@1097: # define a unique name for the generated C file Laurent@1097: location_str = "_".join(map(str, current_location)) Laurent@1097: Laurent@1097: text = "## Code generated by Beremiz python mixin confnode\n\n" Laurent@1097: Laurent@1097: # Adding includes Laurent@1097: text += "## User includes\n" Laurent@1097: text += self.CodeFile.includes.gettext() Laurent@1097: text += "\n" Laurent@1097: Laurent@1097: # Adding variables Laurent@1097: text += "## User variables reference\n" Laurent@1097: for variable in self.CodeFile.variables.variable: Laurent@1097: text += "%s = %s\n" % (variable.getname(), Laurent@1097: str(variable.getinitial())) Laurent@1097: text += "\n" Laurent@1097: Laurent@1097: # Adding user global variables and routines Laurent@1097: text += "## User internal user variables and routines\n" Laurent@1097: text += self.CodeFile.globals.gettext() Laurent@1097: text += "\n" Laurent@1097: Laurent@1097: # Adding Beremiz confnode functions Laurent@1097: text += "## Beremiz confnode functions\n" Laurent@1097: for func, args, return_code, code_object in [ Laurent@1097: ("__init_", "*args, **kwargs", Laurent@1097: "return 0", self.CodeFile.initFunction), Laurent@1097: ("__cleanup_", "", "", self.CodeFile.cleanUpFunction), Laurent@1097: ("__retrieve_", "", "", self.CodeFile.retrieveFunction), Laurent@1097: ("__publish_", "", "", self.CodeFile.publishFunction),]: Laurent@1097: text += "def %s%s(%s):\n" % (func, location_str, args) Laurent@1097: lines = code_object.gettext().splitlines() Laurent@1097: if len(lines) > 0 or return_code != "": Laurent@1097: for line in code_object.gettext().splitlines(): Laurent@1097: text += " " + line Laurent@1097: text += " " + return_code + "\n\n" Laurent@1097: else: Laurent@1097: text += " pass\n\n" Laurent@1097: Laurent@1097: return text laurent@366: