|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a class for reading an XML project file. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from .Config import projectFileFormatVersion |
|
13 from .XMLStreamReaderBase import XMLStreamReaderBase |
|
14 |
|
15 import Utilities |
|
16 |
|
17 |
|
18 class ProjectReader(XMLStreamReaderBase): |
|
19 """ |
|
20 Class for reading an XML project file. |
|
21 """ |
|
22 supportedVersions = ["4.6", |
|
23 "5.0", "5.1", |
|
24 "6.0", "6.1", "6.2", "6.3", "6.4"] |
|
25 |
|
26 def __init__(self, device, project): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param device reference to the I/O device to read from (QIODevice) |
|
31 @param project Reference to the project object to store the |
|
32 information into. |
|
33 """ |
|
34 XMLStreamReaderBase.__init__(self, device) |
|
35 |
|
36 self.project = project |
|
37 |
|
38 self.version = "" |
|
39 |
|
40 def readXML(self): |
|
41 """ |
|
42 Public method to read and parse the XML document. |
|
43 """ |
|
44 while not self.atEnd(): |
|
45 self.readNext() |
|
46 if self.isStartElement(): |
|
47 if self.name() == "Project": |
|
48 self.version = \ |
|
49 self.attribute("version", projectFileFormatVersion) |
|
50 if self.version not in self.supportedVersions: |
|
51 self.raiseUnsupportedFormatVersion(self.version) |
|
52 elif self.name() == "Language": |
|
53 self.project.pdata["SPELLLANGUAGE"] = \ |
|
54 self.readElementText() |
|
55 elif self.name() == "ProjectWordList": |
|
56 self.project.pdata["SPELLWORDS"] = \ |
|
57 Utilities.toNativeSeparators(self.readElementText()) |
|
58 elif self.name() == "ProjectExcludeList": |
|
59 self.project.pdata["SPELLEXCLUDES"] = \ |
|
60 Utilities.toNativeSeparators(self.readElementText()) |
|
61 elif self.name() == "Hash": |
|
62 self.project.pdata["HASH"] = self.readElementText() |
|
63 elif self.name() == "ProgLanguage": |
|
64 self.project.pdata["MIXEDLANGUAGE"] = \ |
|
65 int(self.attribute("mixed", "0")) |
|
66 self.project.pdata["PROGLANGUAGE"] = self.readElementText() |
|
67 if self.project.pdata["PROGLANGUAGE"] == "Python": |
|
68 # convert Python to the more specific Python2 |
|
69 self.project.pdata["PROGLANGUAGE"] = "Python2" |
|
70 elif self.name() == "ProjectType": |
|
71 self.project.pdata["PROJECTTYPE"] = self.readElementText() |
|
72 elif self.name() == "Description": |
|
73 self.project.pdata["DESCRIPTION"] = self.readElementText() |
|
74 elif self.name() == "Version": |
|
75 self.project.pdata["VERSION"] = self.readElementText() |
|
76 elif self.name() == "Author": |
|
77 self.project.pdata["AUTHOR"] = self.readElementText() |
|
78 elif self.name() == "Email": |
|
79 self.project.pdata["EMAIL"] = self.readElementText() |
|
80 elif self.name() == "TranslationPattern": |
|
81 self.project.pdata["TRANSLATIONPATTERN"] = \ |
|
82 Utilities.toNativeSeparators(self.readElementText()) |
|
83 elif self.name() == "TranslationsBinPath": |
|
84 self.project.pdata["TRANSLATIONSBINPATH"] = \ |
|
85 Utilities.toNativeSeparators(self.readElementText()) |
|
86 elif self.name() == "Eol": |
|
87 self.project.pdata["EOL"] = \ |
|
88 int(self.attribute("index", "0")) |
|
89 elif self.name() == "Sources": |
|
90 self.__readFiles("Sources", "Source", "SOURCES") |
|
91 elif self.name() == "Forms": |
|
92 self.__readFiles("Forms", "Form", "FORMS") |
|
93 elif self.name() == "Translations": |
|
94 self.__readFiles( |
|
95 "Translations", "Translation", "TRANSLATIONS") |
|
96 elif self.name() == "TranslationExceptions": |
|
97 self.__readFiles( |
|
98 "TranslationExceptions", "TranslationException", |
|
99 "TRANSLATIONEXCEPTIONS") |
|
100 elif self.name() == "Resources": |
|
101 self.__readFiles("Resources", "Resource", "RESOURCES") |
|
102 elif self.name() == "Interfaces": |
|
103 self.__readFiles("Interfaces", "Interface", "INTERFACES") |
|
104 elif self.name() == "Protocols": |
|
105 self.__readFiles("Protocols", "Protocol", "PROTOCOLS") |
|
106 elif self.name() == "Others": |
|
107 self.__readFiles("Others", "Other", "OTHERS") |
|
108 elif self.name() == "MainScript": |
|
109 self.project.pdata["MAINSCRIPT"] = \ |
|
110 Utilities.toNativeSeparators(self.readElementText()) |
|
111 elif self.name() == "Vcs": |
|
112 self.__readVcs() |
|
113 elif self.name() == "FiletypeAssociations": |
|
114 self.__readFiletypeAssociations() |
|
115 elif self.name() == "LexerAssociations": |
|
116 self.__readLexerAssociations() |
|
117 elif self.name() == "Make": |
|
118 self.__readBasicDataField( |
|
119 "Make", "MakeParameters", "MAKEPARAMS") |
|
120 elif self.name() == "IdlCompiler": |
|
121 self.__readBasicDataField( |
|
122 "IdlCompiler", "IdlCompilerParameters", "IDLPARAMS") |
|
123 elif self.name() == "UicCompiler": |
|
124 self.__readBasicDataField( |
|
125 "UicCompiler", "UicCompilerParameters", "UICPARAMS") |
|
126 elif self.name() == "RccCompiler": |
|
127 self.__readBasicDataField( |
|
128 "RccCompiler", "RccCompilerParameters", "RCCPARAMS") |
|
129 elif self.name() == "ProjectTypeSpecific": |
|
130 self.__readBasicDataField( |
|
131 "ProjectTypeSpecific", "ProjectTypeSpecificData", |
|
132 "PROJECTTYPESPECIFICDATA") |
|
133 elif self.name() == "Documentation": |
|
134 self.__readBasicDataField( |
|
135 "Documentation", "DocumentationParams", |
|
136 "DOCUMENTATIONPARMS") |
|
137 elif self.name() == "Packagers": |
|
138 self.__readBasicDataField( |
|
139 "Packagers", "PackagersParams", "PACKAGERSPARMS") |
|
140 elif self.name() == "Checkers": |
|
141 self.__readBasicDataField( |
|
142 "Checkers", "CheckersParams", "CHECKERSPARMS") |
|
143 elif self.name() == "OtherTools": |
|
144 self.__readBasicDataField( |
|
145 "OtherTools", "OtherToolsParams", "OTHERTOOLSPARMS") |
|
146 else: |
|
147 self.raiseUnexpectedStartTag(self.name()) |
|
148 |
|
149 self.showErrorMessage() |
|
150 |
|
151 def __readFiles(self, tag, listTag, dataKey): |
|
152 """ |
|
153 Private method to read a list of files. |
|
154 |
|
155 @param tag name of the list tag (string) |
|
156 @param listTag name of the list element tag (string) |
|
157 @param dataKey key of the project data element (string) |
|
158 """ |
|
159 while not self.atEnd(): |
|
160 self.readNext() |
|
161 if self.isEndElement() and self.name() == tag: |
|
162 break |
|
163 |
|
164 if self.isStartElement(): |
|
165 if self.name() == listTag: |
|
166 self.project.pdata[dataKey].append( |
|
167 Utilities.toNativeSeparators(self.readElementText())) |
|
168 else: |
|
169 self.raiseUnexpectedStartTag(self.name()) |
|
170 |
|
171 def __readBasicDataField(self, tag, dataTag, dataKey): |
|
172 """ |
|
173 Private method to read a list of files. |
|
174 |
|
175 @param tag name of the list tag (string) |
|
176 @param dataTag name of the data tag (string) |
|
177 @param dataKey key of the project data element (string) |
|
178 """ |
|
179 while not self.atEnd(): |
|
180 self.readNext() |
|
181 if self.isEndElement() and self.name() == tag: |
|
182 break |
|
183 |
|
184 if self.isStartElement(): |
|
185 if self.name() == dataTag: |
|
186 self.project.pdata[dataKey] = self._readBasics() |
|
187 else: |
|
188 self.raiseUnexpectedStartTag(self.name()) |
|
189 |
|
190 def __readVcs(self): |
|
191 """ |
|
192 Private method to read the VCS info. |
|
193 """ |
|
194 while not self.atEnd(): |
|
195 self.readNext() |
|
196 if self.isEndElement() and self.name() == "Vcs": |
|
197 break |
|
198 |
|
199 if self.isStartElement(): |
|
200 if self.name() == "VcsType": |
|
201 self.project.pdata["VCS"] = self.readElementText() |
|
202 elif self.name() == "VcsOptions": |
|
203 self.project.pdata["VCSOPTIONS"] = self._readBasics() |
|
204 elif self.name() == "VcsOtherData": |
|
205 self.project.pdata["VCSOTHERDATA"] = self._readBasics() |
|
206 else: |
|
207 self.raiseUnexpectedStartTag(self.name()) |
|
208 |
|
209 def __readFiletypeAssociations(self): |
|
210 """ |
|
211 Private method to read the file type associations. |
|
212 """ |
|
213 while not self.atEnd(): |
|
214 self.readNext() |
|
215 if self.isEndElement() and self.name() == "FiletypeAssociations": |
|
216 break |
|
217 |
|
218 if self.isStartElement(): |
|
219 if self.name() == "FiletypeAssociation": |
|
220 pattern = self.attribute("pattern", "") |
|
221 filetype = self.attribute("type", "OTHERS") |
|
222 if pattern: |
|
223 self.project.pdata["FILETYPES"][pattern] = filetype |
|
224 else: |
|
225 self.raiseUnexpectedStartTag(self.name()) |
|
226 |
|
227 def __readLexerAssociations(self): |
|
228 """ |
|
229 Private method to read the lexer associations. |
|
230 """ |
|
231 while not self.atEnd(): |
|
232 self.readNext() |
|
233 if self.isEndElement() and self.name() == "LexerAssociations": |
|
234 break |
|
235 |
|
236 if self.isStartElement(): |
|
237 if self.name() == "LexerAssociation": |
|
238 pattern = self.attribute("pattern", "") |
|
239 lexer = self.attribute("lexer") |
|
240 if pattern: |
|
241 self.project.pdata["LEXERASSOCS"][pattern] = lexer |
|
242 else: |
|
243 self.raiseUnexpectedStartTag(self.name()) |