E5XML/TemplatesReader.py

Sat, 11 Sep 2010 19:26:32 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 11 Sep 2010 19:26:32 +0200
changeset 590
beb60b9b3d8d
parent 589
a9a4620f6767
child 791
9ec2ac20e54e
permissions
-rw-r--r--

Added code to handle unsupported versions to the new XML readers.

587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de>
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a class for reading an XML templates file.
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from E5Gui.E5Application import e5App
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 from .Config import templatesFileFormatVersion
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from .XMLStreamReaderBase import XMLStreamReaderBase
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 class TemplatesReader(XMLStreamReaderBase):
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 Class for reading an XML tasks file.
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 """
590
beb60b9b3d8d Added code to handle unsupported versions to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 589
diff changeset
19 supportedVersions = ["4.0"]
beb60b9b3d8d Added code to handle unsupported versions to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 589
diff changeset
20
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 def __init__(self, device, viewer = None):
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 Constructor
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 @param device reference to the I/O device to read from (QIODevice)
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 @param viewer reference to the template viewer object (TemplateViewer)
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 XMLStreamReaderBase.__init__(self, device)
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 if viewer:
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 self.viewer = viewer
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 else:
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 self.viewer = e5App().getObject("TemplateViewer")
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 self.version = ""
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 self.groupName = "DEFAULT"
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 def readXML(self):
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 Public method to read and parse the XML document.
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 while not self.atEnd():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 self.readNext()
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 if self.isStartElement():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 if self.name() == "Templates":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 self.version = self.attribute("version", templatesFileFormatVersion)
590
beb60b9b3d8d Added code to handle unsupported versions to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 589
diff changeset
47 if self.version not in self.supportedVersions:
beb60b9b3d8d Added code to handle unsupported versions to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 589
diff changeset
48 self.raiseUnsupportedFormatVersion(self.version)
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 elif self.name() == "TemplateGroup":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 self.__readTemplateGroup()
589
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
51 else:
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
52 self.raiseUnexpectedStartTag(self.name())
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.showErrorMessage()
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 def __readTemplateGroup(self):
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 Private method to read a template group.
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.groupName = self.attribute('name', "DEFAULT")
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 language = self.attribute('language', "All")
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 self.viewer.addGroup(self.groupName, language)
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 while not self.atEnd():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 self.readNext()
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 if self.isEndElement() and self.name() == "TemplateGroup":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 break
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 if self.isStartElement():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 if self.name() == "Template":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 self.__readTemplate()
589
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
72 else:
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
73 self.raiseUnexpectedStartTag(self.name())
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 def __readTemplate(self):
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 Private method to read the template definition.
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 templateName = self.attribute('name', '')
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 templateDescription = ""
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 templateText = ""
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 while not self.atEnd():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 self.readNext()
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 if self.isEndElement() and self.name() == "Template" and templateName:
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 self.viewer.addEntry(self.groupName, templateName,
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 templateDescription, templateText,
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 quiet = True)
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 break
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 if self.isStartElement():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 if self.name() == "TemplateDescription":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 templateDescription = self.readElementText()
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 elif self.name() == "TemplateText":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 templateText = self.readElementText()
589
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
96 else:
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
97 self.raiseUnexpectedStartTag(self.name())

eric ide

mercurial