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 [ |
|
37 (re.compile("(?<!<xhtml:p>)(?:<!\[CDATA\[)"), "<xhtml:p><![CDATA["), |
|
38 (re.compile("(?:]]>)(?!</xhtml:p>)"), "]]></xhtml:p>")]: |
|
39 pythonfile_xml = cre.sub(repl, pythonfile_xml) |
|
40 |
|
41 try: |
|
42 python_code, error = PythonParser.LoadXMLString(pythonfile_xml) |
|
43 if error is None: |
|
44 self.CodeFile.globals.setanyText(python_code.getanyText()) |
39 os.remove(filepath) |
45 os.remove(filepath) |
40 self.CreateCodeFileBuffer(False) |
46 self.CreateCodeFileBuffer(False) |
41 self.OnCTNSave() |
47 self.OnCTNSave() |
|
48 except Exception, exc: |
|
49 error = unicode(exc) |
|
50 |
|
51 if error is not None: |
|
52 self.GetCTRoot().logger.write_error( |
|
53 _("Couldn't import old %s file.") % CTNName) |
42 |
54 |
43 def CodeFileName(self): |
55 def CodeFileName(self): |
44 return os.path.join(self.CTNPath(), "pyfile.xml") |
56 return os.path.join(self.CTNPath(), "pyfile.xml") |
45 |
57 |
46 def PythonFileName(self): |
58 def PythonFileName(self): |
48 |
60 |
49 PreSectionsTexts = {} |
61 PreSectionsTexts = {} |
50 PostSectionsTexts = {} |
62 PostSectionsTexts = {} |
51 def GetSection(self,section): |
63 def GetSection(self,section): |
52 return self.PreSectionsTexts.get(section,"") + "\n" + \ |
64 return self.PreSectionsTexts.get(section,"") + "\n" + \ |
53 getattr(self.CodeFile, section).gettext() + "\n" + \ |
65 getattr(self.CodeFile, section).getanyText() + "\n" + \ |
54 self.PostSectionsTexts.get(section,"") |
66 self.PostSectionsTexts.get(section,"") |
55 |
67 |
56 |
68 |
57 def CTNGenerate_C(self, buildpath, locations): |
69 def CTNGenerate_C(self, buildpath, locations): |
58 # location string for that CTN |
70 # location string for that CTN |