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