eric6/E5XML/TemplatesReader.py

Wed, 30 Dec 2020 11:00:44 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 30 Dec 2020 11:00:44 +0100
branch
maintenance
changeset 7924
8a96736d465e
parent 7780
41420f82c0ac
permissions
-rw-r--r--

Updated copyright for 2021.

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
7924
8a96736d465e Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7780
diff changeset
3 # Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
587
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 .Config import templatesFileFormatVersion
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 from .XMLStreamReaderBase import XMLStreamReaderBase
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
13
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 class TemplatesReader(XMLStreamReaderBase):
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 Class for reading an XML tasks file.
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 """
590
beb60b9b3d8d Added code to handle unsupported versions to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 589
diff changeset
18 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
19
6425
2f37e52a6c70 TemplatesReader: changed code to always require a reference to a templates viewer instance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
20 def __init__(self, device, viewer):
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 Constructor
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 @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
25 @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
26 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 XMLStreamReaderBase.__init__(self, device)
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
6425
2f37e52a6c70 TemplatesReader: changed code to always require a reference to a templates viewer instance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
29 self.__viewer = viewer
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 self.version = ""
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 self.groupName = "DEFAULT"
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 def readXML(self):
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 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
37 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 while not self.atEnd():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.readNext()
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 if self.isStartElement():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 if self.name() == "Templates":
2992
dbdf27746da5 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
42 self.version = self.attribute(
dbdf27746da5 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
43 "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
44 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
45 self.raiseUnsupportedFormatVersion(self.version)
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 elif self.name() == "TemplateGroup":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 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
48 else:
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
49 self.raiseUnexpectedStartTag(self.name())
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 self.showErrorMessage()
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 def __readTemplateGroup(self):
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 Private method to read a template group.
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 self.groupName = self.attribute('name', "DEFAULT")
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 language = self.attribute('language', "All")
6425
2f37e52a6c70 TemplatesReader: changed code to always require a reference to a templates viewer instance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
59 self.__viewer.addGroup(self.groupName, language)
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 while not self.atEnd():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 self.readNext()
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 if self.isEndElement() and self.name() == "TemplateGroup":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 break
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 if self.isStartElement():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 if self.name() == "Template":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 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
69 else:
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
70 self.raiseUnexpectedStartTag(self.name())
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 def __readTemplate(self):
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 Private method to read the template definition.
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 """
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 templateName = self.attribute('name', '')
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 templateDescription = ""
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 templateText = ""
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 while not self.atEnd():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 self.readNext()
7253
50dbe65a1334 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
82 if (
50dbe65a1334 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
83 self.isEndElement() and
50dbe65a1334 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
84 self.name() == "Template" and
50dbe65a1334 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
85 templateName
50dbe65a1334 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
86 ):
6425
2f37e52a6c70 TemplatesReader: changed code to always require a reference to a templates viewer instance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
87 self.__viewer.addEntry(self.groupName, templateName,
2f37e52a6c70 TemplatesReader: changed code to always require a reference to a templates viewer instance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
88 templateDescription, templateText,
2f37e52a6c70 TemplatesReader: changed code to always require a reference to a templates viewer instance.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
89 quiet=True)
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 break
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 if self.isStartElement():
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 if self.name() == "TemplateDescription":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 templateDescription = self.readElementText()
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 elif self.name() == "TemplateText":
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 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
97 else:
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
98 self.raiseUnexpectedStartTag(self.name())

eric ide

mercurial