1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the writer class for writing an XML templates file. |
|
8 """ |
|
9 |
|
10 import time |
|
11 |
|
12 from .XMLStreamWriterBase import XMLStreamWriterBase |
|
13 from .Config import templatesFileFormatVersion |
|
14 |
|
15 |
|
16 class TemplatesWriter(XMLStreamWriterBase): |
|
17 """ |
|
18 Class implementing the writer class for writing an XML templates file. |
|
19 """ |
|
20 def __init__(self, device, templatesViewer): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param device reference to the I/O device to write to (QIODevice) |
|
25 @param templatesViewer reference to the templates viewer object |
|
26 (TemplateViewer) |
|
27 """ |
|
28 XMLStreamWriterBase.__init__(self, device) |
|
29 |
|
30 self.templatesViewer = templatesViewer |
|
31 |
|
32 def writeXML(self): |
|
33 """ |
|
34 Public method to write the XML to the file. |
|
35 """ |
|
36 XMLStreamWriterBase.writeXML(self) |
|
37 |
|
38 self.writeDTD('<!DOCTYPE Templates SYSTEM "Templates-{0}.dtd">'.format( |
|
39 templatesFileFormatVersion)) |
|
40 |
|
41 # add some generation comments |
|
42 self.writeComment(" eric templates file ") |
|
43 self.writeComment( |
|
44 " Saved: {0} ".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) |
|
45 |
|
46 # add the main tag |
|
47 self.writeStartElement("Templates") |
|
48 self.writeAttribute("version", templatesFileFormatVersion) |
|
49 |
|
50 # do the template groups |
|
51 groups = self.templatesViewer.getAllGroups() |
|
52 for group in groups: |
|
53 self.writeStartElement("TemplateGroup") |
|
54 self.writeAttribute("name", group.getName()) |
|
55 self.writeAttribute("language", group.getLanguage()) |
|
56 # do the templates |
|
57 templates = group.getAllEntries() |
|
58 for template in templates: |
|
59 self.writeStartElement("Template") |
|
60 self.writeAttribute("name", template.getName()) |
|
61 self.writeTextElement( |
|
62 "TemplateDescription", template.getDescription()) |
|
63 self.writeTextElement( |
|
64 "TemplateText", template.getTemplateText()) |
|
65 self.writeEndElement() |
|
66 self.writeEndElement() |
|
67 |
|
68 self.writeEndElement() |
|
69 self.writeEndDocument() |
|