1 import os |
1 import os, re |
2 from PLCControler import UndoBuffer |
2 from lxml import etree |
|
3 |
|
4 from xmlclass import GenerateParserFromXSD |
|
5 |
|
6 from CodeFileTreeNode import CodeFile |
3 from PythonEditor import PythonEditor |
7 from PythonEditor import PythonEditor |
4 |
|
5 from xml.dom import minidom |
|
6 from xmlclass import GenerateClassesFromXSD |
|
7 import cPickle |
|
8 |
|
9 from CodeFileTreeNode import CodeFile |
|
10 |
|
11 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) |
|
12 |
8 |
13 class PythonFileCTNMixin(CodeFile): |
9 class PythonFileCTNMixin(CodeFile): |
14 |
10 |
15 CODEFILE_NAME = "PyFile" |
11 CODEFILE_NAME = "PyFile" |
16 SECTIONS_NAMES = [ |
12 SECTIONS_NAMES = [ |
24 def __init__(self): |
20 def __init__(self): |
25 CodeFile.__init__(self) |
21 CodeFile.__init__(self) |
26 |
22 |
27 filepath = self.PythonFileName() |
23 filepath = self.PythonFileName() |
28 |
24 |
29 python_code = PythonClasses["Python"]() |
|
30 if os.path.isfile(filepath): |
25 if os.path.isfile(filepath): |
|
26 PythonParser = GenerateParserFromXSD( |
|
27 os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) |
|
28 |
31 xmlfile = open(filepath, 'r') |
29 xmlfile = open(filepath, 'r') |
32 tree = minidom.parse(xmlfile) |
30 pythonfile_xml = xmlfile.read() |
33 xmlfile.close() |
31 xmlfile.close() |
34 |
32 |
35 for child in tree.childNodes: |
33 pythonfile_xml = pythonfile_xml.replace( |
36 if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "Python": |
34 'xmlns="http://www.w3.org/2001/XMLSchema"', |
37 python_code.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) |
35 'xmlns:xhtml="http://www.w3.org/1999/xhtml"') |
38 self.CodeFile.globals.settext(python_code.gettext()) |
36 for cre, repl in [ |
39 os.remove(filepath) |
37 (re.compile("(?<!<xhtml:p>)(?:<!\[CDATA\[)"), "<xhtml:p><![CDATA["), |
40 self.CreateCodeFileBuffer(False) |
38 (re.compile("(?:]]>)(?!</xhtml:p>)"), "]]></xhtml:p>")]: |
41 self.OnCTNSave() |
39 pythonfile_xml = cre.sub(repl, pythonfile_xml) |
|
40 python_code = etree.fromstring(pythonfile_xml, PythonParser) |
|
41 |
|
42 self.CodeFile.globals.setanyText(python_code.getanyText()) |
|
43 os.remove(filepath) |
|
44 self.CreateCodeFileBuffer(False) |
|
45 self.OnCTNSave() |
42 |
46 |
43 def CodeFileName(self): |
47 def CodeFileName(self): |
44 return os.path.join(self.CTNPath(), "pyfile.xml") |
48 return os.path.join(self.CTNPath(), "pyfile.xml") |
45 |
49 |
46 def PythonFileName(self): |
50 def PythonFileName(self): |
48 |
52 |
49 PreSectionsTexts = {} |
53 PreSectionsTexts = {} |
50 PostSectionsTexts = {} |
54 PostSectionsTexts = {} |
51 def GetSection(self,section): |
55 def GetSection(self,section): |
52 return self.PreSectionsTexts.get(section,"") + "\n" + \ |
56 return self.PreSectionsTexts.get(section,"") + "\n" + \ |
53 getattr(self.CodeFile, section).gettext() + "\n" + \ |
57 getattr(self.CodeFile, section).getanyText() + "\n" + \ |
54 self.PostSectionsTexts.get(section,"") |
58 self.PostSectionsTexts.get(section,"") |
55 |
59 |
56 |
60 |
57 def CTNGenerate_C(self, buildpath, locations): |
61 def CTNGenerate_C(self, buildpath, locations): |
58 # location string for that CTN |
62 # location string for that CTN |