Migrated the templates writer.

Fri, 10 Sep 2010 20:03:59 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 10 Sep 2010 20:03:59 +0200
changeset 584
b5d4c116952a
parent 583
08bc660d6a15
child 585
f40889943c0a

Migrated the templates writer.

E5XML/TemplatesWriter.py file | annotate | diff | comparison | revisions
Templates/TemplateViewer.py file | annotate | diff | comparison | revisions
--- a/E5XML/TemplatesWriter.py	Fri Sep 10 12:41:55 2010 +0200
+++ b/E5XML/TemplatesWriter.py	Fri Sep 10 20:03:59 2010 +0200
@@ -9,20 +9,21 @@
 
 import time
 
-from .XMLWriterBase import XMLWriterBase
+from .XMLStreamWriterBase import XMLStreamWriterBase
 from .Config import templatesFileFormatVersion
 
-class TemplatesWriter(XMLWriterBase):
+class TemplatesWriter(XMLStreamWriterBase):
     """
     Class implementing the writer class for writing an XML templates file.
     """
-    def __init__(self, file, templatesViewer):
+    def __init__(self, device, templatesViewer):
         """
         Constructor
         
-        @param file open file (like) object for writing
+        @param device reference to the I/O device to write to (QIODevice)
+        @param templatesViewer reference to the templates viewer object (TemplateViewer)
         """
-        XMLWriterBase.__init__(self, file)
+        XMLStreamWriterBase.__init__(self, device)
         
         self.templatesViewer = templatesViewer
         
@@ -30,33 +31,49 @@
         """
         Public method to write the XML to the file.
         """
-        XMLWriterBase.writeXML(self)
+        XMLStreamWriterBase.writeXML(self)
         
-        self._write('<!DOCTYPE Templates SYSTEM "Templates-{0}.dtd">'.format(
+        self.writeDTD('<!DOCTYPE Templates SYSTEM "Templates-{0}.dtd">'.format(
             templatesFileFormatVersion))
         
         # add some generation comments
-        self._write("<!-- eric5 templates file -->")
-        self._write("<!-- Saved: {0} -->".format(time.strftime('%Y-%m-%d, %H:%M:%S')))
+        self.writeComment(" eric5 templates file ")
+        self.writeComment(" Saved: {0} ".format(time.strftime('%Y-%m-%d, %H:%M:%S')))
+##        self._write("<!-- eric5 templates file -->")
+##        self._write("<!-- Saved: {0} -->".format(time.strftime('%Y-%m-%d, %H:%M:%S')))
         
         # add the main tag
-        self._write('<Templates version="{0}">'.format(templatesFileFormatVersion))
+        self.writeStartElement("Templates")
+        self.writeAttribute("version", templatesFileFormatVersion)
+##        self._write('<Templates version="{0}">'.format(templatesFileFormatVersion))
         
         # do the template groups
         groups = self.templatesViewer.getAllGroups()
         for group in groups:
-            self._write('  <TemplateGroup name="{0}" language="{1}">'.format(
-                        group.getName(), group.getLanguage()))
+            self.writeStartElement("TemplateGroup")
+            self.writeAttribute("name", group.getName())
+            self.writeAttribute("language", group.getLanguage())
+##            self._write('  <TemplateGroup name="{0}" language="{1}">'.format(
+##                        group.getName(), group.getLanguage()))
             # do the templates
             templates = group.getAllEntries()
             for template in templates:
-                self._write('    <Template name="{0}">'.format(
-                    self.escape(template.getName(), True)))
-                self._write('      <TemplateDescription>{0}</TemplateDescription>'.format(
-                    self.escape("{0}".format(template.getDescription()))))
-                self._write('      <TemplateText>{0}</TemplateText>'.format(
-                    self.escape("{0}".format(template.getTemplateText()))))
-                self._write('    </Template>')
-            self._write('  </TemplateGroup>')
+                self.writeStartElement("Template")
+                self.writeAttribute("name", template.getName())
+                self.writeTextElement("TemplateDescription", template.getDescription())
+                self.writeTextElement("TemplateText", template.getTemplateText())
+                self.writeEndElement()
+            self.writeEndElement()
         
-        self._write('</Templates>', newline = False)
+        self.writeEndElement()
+        self.writeEndDocument()
+##                self._write('    <Template name="{0}">'.format(
+##                    self.escape(template.getName(), True)))
+##                self._write('      <TemplateDescription>{0}</TemplateDescription>'.format(
+##                    self.escape("{0}".format(template.getDescription()))))
+##                self._write('      <TemplateText>{0}</TemplateText>'.format(
+##                    self.escape("{0}".format(template.getTemplateText()))))
+##                self._write('    </Template>')
+##            self._write('  </TemplateGroup>')
+##        
+##        self._write('</Templates>', newline = False)
--- a/Templates/TemplateViewer.py	Fri Sep 10 12:41:55 2010 +0200
+++ b/Templates/TemplateViewer.py	Fri Sep 10 20:03:59 2010 +0200
@@ -858,20 +858,20 @@
         
         @param filename name of a templates file to read (string)
         """
-        try:
-            if filename is None:
-                filename = os.path.join(Utilities.getConfigDir(), "eric5templates.e4c")
-            f = open(filename, "w", encoding = "utf-8")
-            
-            TemplatesWriter(f, self).writeXML()
-            
-            f.close()
-        except IOError:
+        if filename is None:
+            filename = os.path.join(Utilities.getConfigDir(), "eric5templates.e4c")
+        f = QFile(filename)
+        ok = f.open(QIODevice.WriteOnly)
+        if not ok:
             E5MessageBox.critical(self,
                 self.trUtf8("Save templates"),
                 self.trUtf8("<p>The templates file <b>{0}</b> could not be written.</p>")
                     .format(filename))
+            return
         
+        TemplatesWriter(f, self).writeXML()
+        f.close()
+    
     def readTemplates(self, filename = None):
         """
         Public method to read in the templates file (.e4c)
@@ -962,4 +962,4 @@
         names = []
         for group in list(self.groups.values()):
             names.extend(group.getEntryNames(start))
-        return sorted(names)
\ No newline at end of file
+        return sorted(names)

eric ide

mercurial