from PLCControler import UndoBuffer
from PythonEditor import PythonEditor
from xml.dom import minidom
from xmlclass import GenerateClassesFromXSD
from CodeFileTreeNode import CodeFile
PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd"))
class PythonFileCTNMixin(CodeFile):
EditorType = PythonEditor
filepath = self.PythonFileName()
python_code = PythonClasses["Python"]()
if os.path.isfile(filepath):
xmlfile = open(filepath, 'r')
tree = minidom.parse(xmlfile)
for child in tree.childNodes:
if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python":
python_code.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"])
self.CodeFile.globals.settext(python_code.gettext())
self.CreateCodeFileBuffer(False)
return os.path.join(self.CTNPath(), "pyfile.xml")
def PythonFileName(self):
return os.path.join(self.CTNPath(), "py_ext.xml")
def GetSectionsCode(self):
# Generate Beremiz python runtime variables code
config = self.GetCTRoot().GetProjectConfigNames()[0]
for variable in self.CodeFile.variables.variable:
global_name = "%s_%s" % (config.upper(), variable.getname().upper())
variables_str += "# global_var:%s python_var:%s type:%s initial:%s\n" % (
str(variable.getinitial()))
"variables": variables_str,
"globals": self.CodeFile.globals.gettext().strip()
# Generate Beremiz python runtime functions code
for section in self.SECTIONS_NAMES:
code_object = getattr(self.CodeFile, section)
lines = code_object.gettext().strip().splitlines()
section_str += " " + line + "\n"
sections_code[section] = section_str