|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2010 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 os |
|
11 import time |
|
12 |
|
13 from .XMLWriterBase import XMLWriterBase |
|
14 from .Config import templatesFileFormatVersion |
|
15 |
|
16 class TemplatesWriter(XMLWriterBase): |
|
17 """ |
|
18 Class implementing the writer class for writing an XML templates file. |
|
19 """ |
|
20 def __init__(self, file, templatesViewer): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param file open file (like) object for writing |
|
25 """ |
|
26 XMLWriterBase.__init__(self, file) |
|
27 |
|
28 self.templatesViewer = templatesViewer |
|
29 |
|
30 def writeXML(self): |
|
31 """ |
|
32 Public method to write the XML to the file. |
|
33 """ |
|
34 XMLWriterBase.writeXML(self) |
|
35 |
|
36 self._write('<!DOCTYPE Templates SYSTEM "Templates-%s.dtd">' % \ |
|
37 templatesFileFormatVersion) |
|
38 |
|
39 # add some generation comments |
|
40 self._write("<!-- eric5 templates file -->") |
|
41 self._write("<!-- Saved: %s -->" % time.strftime('%Y-%m-%d, %H:%M:%S')) |
|
42 |
|
43 # add the main tag |
|
44 self._write('<Templates version="%s">' % templatesFileFormatVersion) |
|
45 |
|
46 # do the template groups |
|
47 groups = self.templatesViewer.getAllGroups() |
|
48 for group in groups: |
|
49 self._write(' <TemplateGroup name="%s" language="%s">' % \ |
|
50 (group.getName(), group.getLanguage())) |
|
51 # do the templates |
|
52 templates = group.getAllEntries() |
|
53 for template in templates: |
|
54 self._write(' <Template name="%s">' % \ |
|
55 self.escape(template.getName(), True)) |
|
56 self._write(' <TemplateDescription>%s</TemplateDescription>' % \ |
|
57 self.escape("%s" % template.getDescription())) |
|
58 self._write(' <TemplateText>%s</TemplateText>' % \ |
|
59 self.escape("%s" % template.getTemplateText())) |
|
60 self._write(' </Template>') |
|
61 self._write(' </TemplateGroup>') |
|
62 |
|
63 self._write('</Templates>', newline = False) |