diff -r f99d60d6b59b -r 2602857055c5 eric6/E5XML/TemplatesWriter.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/eric6/E5XML/TemplatesWriter.py Sun Apr 14 15:09:21 2019 +0200 @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2005 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing the writer class for writing an XML templates file. +""" + +from __future__ import unicode_literals + +import time + +from .XMLStreamWriterBase import XMLStreamWriterBase +from .Config import templatesFileFormatVersion + + +class TemplatesWriter(XMLStreamWriterBase): + """ + Class implementing the writer class for writing an XML templates file. + """ + def __init__(self, device, templatesViewer): + """ + Constructor + + @param device reference to the I/O device to write to (QIODevice) + @param templatesViewer reference to the templates viewer object + (TemplateViewer) + """ + XMLStreamWriterBase.__init__(self, device) + + self.templatesViewer = templatesViewer + + def writeXML(self): + """ + Public method to write the XML to the file. + """ + XMLStreamWriterBase.writeXML(self) + + self.writeDTD('<!DOCTYPE Templates SYSTEM "Templates-{0}.dtd">'.format( + templatesFileFormatVersion)) + + # add some generation comments + self.writeComment(" eric6 templates file ") + self.writeComment( + " Saved: {0} ".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) + + # add the main tag + self.writeStartElement("Templates") + self.writeAttribute("version", templatesFileFormatVersion) + + # do the template groups + groups = self.templatesViewer.getAllGroups() + for group in groups: + self.writeStartElement("TemplateGroup") + self.writeAttribute("name", group.getName()) + self.writeAttribute("language", group.getLanguage()) + # do the templates + templates = group.getAllEntries() + for template in templates: + self.writeStartElement("Template") + self.writeAttribute("name", template.getName()) + self.writeTextElement( + "TemplateDescription", template.getDescription()) + self.writeTextElement( + "TemplateText", template.getTemplateText()) + self.writeEndElement() + self.writeEndElement() + + self.writeEndElement() + self.writeEndDocument()