py_ext/PythonFileCTNMixin.py
branch1.1 Korean release
changeset 1384 02fe382c4511
parent 1330 96b242e4c59d
child 1436 e15ca67197b9
child 1473 23045c351012
--- a/py_ext/PythonFileCTNMixin.py	Wed Jul 31 10:45:07 2013 +0900
+++ b/py_ext/PythonFileCTNMixin.py	Mon Nov 18 12:12:31 2013 +0900
@@ -1,15 +1,11 @@
-import os
-from PLCControler import UndoBuffer
+import os, re
+from lxml import etree
+
+from xmlclass import GenerateParserFromXSD
+
+from CodeFileTreeNode import CodeFile
 from PythonEditor import PythonEditor
 
-from xml.dom import minidom
-from xmlclass import GenerateClassesFromXSD
-import cPickle
-
-from CodeFileTreeNode import CodeFile
-
-PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) 
-
 class PythonFileCTNMixin(CodeFile):
     
     CODEFILE_NAME = "PyFile"
@@ -26,19 +22,35 @@
         
         filepath = self.PythonFileName()
         
-        python_code = PythonClasses["Python"]()
         if os.path.isfile(filepath):
+            PythonParser = GenerateParserFromXSD(
+                os.path.join(os.path.dirname(__file__), "py_ext_xsd.xsd")) 
+            
             xmlfile = open(filepath, 'r')
-            tree = minidom.parse(xmlfile)
+            pythonfile_xml = xmlfile.read()
             xmlfile.close()
             
-            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())
+            pythonfile_xml = pythonfile_xml.replace(
+                'xmlns="http://www.w3.org/2001/XMLSchema"', 
+                'xmlns:xhtml="http://www.w3.org/1999/xhtml"')
+            for cre, repl in [
+                (re.compile("(?<!<xhtml:p>)(?:<!\[CDATA\[)"), "<xhtml:p><![CDATA["),
+                (re.compile("(?:]]>)(?!</xhtml:p>)"), "]]></xhtml:p>")]:
+                pythonfile_xml = cre.sub(repl, pythonfile_xml)
+            
+            try:
+                python_code, error = PythonParser.LoadXMLString(pythonfile_xml)
+                if error is None:    
+                    self.CodeFile.globals.setanyText(python_code.getanyText())
                     os.remove(filepath)
                     self.CreateCodeFileBuffer(False)
                     self.OnCTNSave()
+            except Exception, exc:
+                error = unicode(exc)
+            
+            if error is not None:
+                self.GetCTRoot().logger.write_error(
+                    _("Couldn't import old %s file.") % CTNName)
     
     def CodeFileName(self):
         return os.path.join(self.CTNPath(), "pyfile.xml")
@@ -50,7 +62,7 @@
     PostSectionsTexts = {}
     def GetSection(self,section):
         return self.PreSectionsTexts.get(section,"") + "\n" + \
-               getattr(self.CodeFile, section).gettext() + "\n" + \
+               getattr(self.CodeFile, section).getanyText() + "\n" + \
                self.PostSectionsTexts.get(section,"")