E5XML/ProjectHandler.py

changeset 50
a36eecf45b2e
parent 45
9a18f4dbb493
child 151
00c27aeede62
equal deleted inserted replaced
49:f991944e859c 50:a36eecf45b2e
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2003 - 2010 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the handler class for reading an XML project file.
8 """
9
10 import os
11
12 from .Config import projectFileFormatVersion
13 from .XMLHandlerBase import XMLHandlerBase
14
15 import Utilities
16
17 class ProjectHandler(XMLHandlerBase):
18 """
19 Class implementing a sax handler to read an XML project file.
20 """
21 def __init__(self, project):
22 """
23 Constructor
24
25 @param project Reference to the project object to store the
26 information into.
27 """
28 XMLHandlerBase.__init__(self)
29
30 self.startDocumentSpecific = self.startDocumentProject
31
32 self.elements.update({
33 'Project' : (self.startProject, self.defaultEndElement),
34 'Language' : (self.defaultStartElement, self.endLanguage),
35 'ProjectWordList' : (self.defaultStartElement, self.endProjectWordList),
36 'ProjectExcludeList' : (self.defaultStartElement,
37 self.endProjectExcludeList),
38 'ProgLanguage' : (self.startProgLanguage, self.endProgLanguage),
39 'ProjectType' : (self.defaultStartElement, self.endProjectType),
40 'Description' : (self.defaultStartElement, self.endDescription),
41 'Version' : (self.defaultStartElement, self.endVersion),
42 'Author' : (self.defaultStartElement, self.endAuthor),
43 'Email' : (self.defaultStartElement, self.endEmail),
44 'VcsType' : (self.defaultStartElement, self.endVcsType),
45 'VcsOptions' : (self.startVcsOptions, self.endVcsOptions),
46 'VcsOtherData' : (self.startVcsOtherData, self.endVcsOtherData),
47 'Dir' : (self.defaultStartElement, self.endDir),
48 'Name' : (self.defaultStartElement, self.endName),
49 'Source' : (self.startSource, self.endSource),
50 'Form' : (self.startForm, self.endForm),
51 'Translation' : (self.startTranslation, self.endTranslation),
52 'TranslationPattern' : (self.defaultStartElement,
53 self.endTranslationPattern),
54 'TranslationsBinPath' : (self.startTranslationsBinPath,
55 self.endTranslationsBinPath),
56 'TranslationException' : (self.startTranslationException,
57 self.endTranslationException),
58 'Resource' : (self.startResource, self.endResource),
59 'Interface' : (self.startInterface, self.endInterface),
60 'Other' : (self.startOther, self.endOther),
61 'MainScript' : (self.startMainScript, self.endMainScript),
62 'FiletypeAssociation' : (self.startFiletypeAssociation,
63 self.defaultEndElement),
64 'LexerAssociation' : (self.startLexerAssociation,
65 self.defaultEndElement),
66 'ProjectTypeSpecificData' : (self.startProjectTypeSpecificData,
67 self.endProjectTypeSpecificData),
68 'DocumentationParams' : (self.startDocumentationParams,
69 self.endDocumentationParams),
70 'PackagersParams' : (self.startPackagersParams, self.endPackagersParams),
71 'CheckersParams' : (self.startCheckersParams, self.endCheckersParams),
72 'OtherToolsParams' : (self.startOtherToolsParams, self.endOtherToolsParams),
73 # parameters kept for backward compatibility
74 'UIType' : (self.defaultStartElement, self.endUIType),
75 'TranslationPrefix' : (self.startTranslationPrefix,
76 self.endTranslationPrefix),
77 'Eric4DocParams' : (self.startEric4DocParams, self.endEric4DocParams),
78 'Eric4ApiParams' : (self.startEric4ApiParams, self.endEric4ApiParams),
79 'CxfreezeParams' : (self.startCxfreezeParams, self.endCxfreezeParams),
80 'PyLintParams' : (self.startPyLintParams, self.endPyLintParams),
81 })
82
83 self.project = project
84
85 def startDocumentProject(self):
86 """
87 Handler called, when the document parsing is started.
88 """
89 self.version = ''
90 self.pathStack = []
91
92 ###################################################
93 ## below follow the individual handler functions
94 ###################################################
95
96 def endLanguage(self):
97 """
98 Handler method for the "Language" end tag.
99 """
100 self.project.pdata["SPELLLANGUAGE"] = [self.buffer]
101
102 def endProjectWordList(self):
103 """
104 Handler method for the "ProjectWordList" end tag.
105 """
106 path = Utilities.toNativeSeparators(self.buffer)
107 self.project.pdata["SPELLWORDS"] = [path]
108
109 def endProjectExcludeList(self):
110 """
111 Handler method for the "ProjectExcludeList" end tag.
112 """
113 path = Utilities.toNativeSeparators(self.buffer)
114 self.project.pdata["SPELLEXCLUDES"] = [path]
115
116 def startProgLanguage(self, attrs):
117 """
118 Handler method for the "Source" start tag.
119
120 @param attrs list of tag attributes
121 """
122 mixedLanguage = int(attrs.get("mixed", "0"))
123 self.project.pdata["MIXEDLANGUAGE"] = [mixedLanguage]
124 self.buffer = ""
125
126 def endProgLanguage(self):
127 """
128 Handler method for the "ProgLanguage" end tag.
129 """
130 self.project.pdata["PROGLANGUAGE"] = [self.buffer]
131
132 def endProjectType(self):
133 """
134 Handler method for the "ProjectType" end tag.
135 """
136 self.project.pdata["PROJECTTYPE"] = [self.buffer]
137
138 def endDescription(self):
139 """
140 Handler method for the "Description" end tag.
141 """
142 self.buffer = self.unescape(self.buffer)
143 if self.version >= '4.3':
144 self.project.pdata["DESCRIPTION"] = [self.decodedNewLines(self.buffer)]
145 else:
146 self.project.pdata["DESCRIPTION"] = [self.buffer]
147
148 def endVersion(self):
149 """
150 Handler method for the "Version" end tag.
151 """
152 self.buffer = self.unescape(self.buffer)
153 self.project.pdata["VERSION"] = [self.buffer]
154
155 def endAuthor(self):
156 """
157 Handler method for the "Author" end tag.
158 """
159 self.buffer = self.unescape(self.buffer)
160 self.project.pdata["AUTHOR"] = [self.buffer]
161
162 def endEmail(self):
163 """
164 Handler method for the "Email" end tag.
165 """
166 self.buffer = self.unescape(self.buffer)
167 self.project.pdata["EMAIL"] = [self.buffer]
168
169 def endVcsType(self):
170 """
171 Handler method for the "VcsType" end tag.
172 """
173 self.project.pdata["VCS"] = [self.buffer]
174
175 def startVcsOptions(self, attrs):
176 """
177 Handler method for the "VcsOptions" start tag.
178
179 @param attrs list of tag attributes
180 """
181 self.defaultStartElement(attrs)
182 self._prepareBasics()
183
184 def endVcsOptions(self):
185 """
186 Handler method for the "VcsOptions" end tag.
187 """
188 try:
189 self.project.pdata["VCSOPTIONS"] = [self.stack[-1]]
190 except IndexError:
191 self.project.pdata["VCSOPTIONS"] = []
192
193 def startVcsOtherData(self, attrs):
194 """
195 Handler method for the "VcsOtherData" start tag.
196
197 @param attrs list of tag attributes
198 """
199 self.defaultStartElement(attrs)
200 self._prepareBasics()
201
202 def endVcsOtherData(self):
203 """
204 Handler method for the "VcsOtherData" end tag.
205 """
206 try:
207 self.project.pdata["VCSOTHERDATA"] = [self.stack[-1]]
208 except IndexError:
209 self.project.pdata["VCSOTHERDATA"] = []
210
211 def startProjectTypeSpecificData(self, attrs):
212 """
213 Handler method for the "ProjectTypeSpecificData" start tag.
214
215 @param attrs list of tag attributes
216 """
217 self.defaultStartElement(attrs)
218 self._prepareBasics()
219
220 def endProjectTypeSpecificData(self):
221 """
222 Handler method for the "ProjectTypeSpecificData" end tag.
223 """
224 try:
225 self.project.pdata["PROJECTTYPESPECIFICDATA"] = self.stack[-1]
226 except IndexError:
227 self.project.pdata["PROJECTTYPESPECIFICDATA"] = {}
228
229 def startDocumentationParams(self, attrs):
230 """
231 Handler method for the "DocumentationParams" start tag.
232
233 @param attrs list of tag attributes
234 """
235 self.defaultStartElement(attrs)
236 self._prepareBasics()
237
238 def endDocumentationParams(self):
239 """
240 Handler method for the "DocumentationParams" end tag.
241 """
242 try:
243 self.project.pdata["DOCUMENTATIONPARMS"] = self.stack[-1]
244 except IndexError:
245 self.project.pdata["DOCUMENTATIONPARMS"] = {}
246
247 def startPackagersParams(self, attrs):
248 """
249 Handler method for the "PackagersParams" start tag.
250
251 @param attrs list of tag attributes
252 """
253 self.defaultStartElement(attrs)
254 self._prepareBasics()
255
256 def endPackagersParams(self):
257 """
258 Handler method for the "PackagersParams" end tag.
259 """
260 try:
261 self.project.pdata["PACKAGERSPARMS"] = self.stack[-1]
262 except IndexError:
263 self.project.pdata["PACKAGERSPARMS"] = {}
264
265 def startCheckersParams(self, attrs):
266 """
267 Handler method for the "CheckersParams" start tag.
268
269 @param attrs list of tag attributes
270 """
271 self.defaultStartElement(attrs)
272 self._prepareBasics()
273
274 def endCheckersParams(self):
275 """
276 Handler method for the "CheckersParams" end tag.
277 """
278 try:
279 self.project.pdata["CHECKERSPARMS"] = self.stack[-1]
280 except IndexError:
281 self.project.pdata["CHECKERSPARMS"] = {}
282
283 def startOtherToolsParams(self, attrs):
284 """
285 Handler method for the "OtherToolsParams" start tag.
286
287 @param attrs list of tag attributes
288 """
289 self.defaultStartElement(attrs)
290 self._prepareBasics()
291
292 def endOtherToolsParams(self):
293 """
294 Handler method for the "OtherToolsParams" end tag.
295 """
296 try:
297 self.project.pdata["OTHERTOOLSPARMS"] = self.stack[-1]
298 except IndexError:
299 self.project.pdata["OTHERTOOLSPARMS"] = {}
300
301 def endDir(self):
302 """
303 Handler method for the "Dir" end tag.
304 """
305 self.pathStack.append(self.buffer)
306
307 def endName(self):
308 """
309 Handler method for the "Name" end tag.
310 """
311 self.pathStack.append(self.buffer)
312
313 def endTranslationPattern(self):
314 """
315 Handler method for the "TranslationPattern" end tag.
316 """
317 self.project.pdata["TRANSLATIONPATTERN"].append(
318 Utilities.toNativeSeparators(self.buffer))
319
320 def startTranslationsBinPath(self, attrs):
321 """
322 Handler method for the "TranslationsBinPath" start tag.
323
324 @param attrs list of tag attributes
325 """
326 self.pathStack = []
327 self.buffer = ""
328
329 def endTranslationsBinPath(self):
330 """
331 Handler method for the "TranslationsBinPath" end tag.
332 """
333 if self.version >= '4.3':
334 path = Utilities.toNativeSeparators(self.buffer)
335 else:
336 path = self.__buildPath()
337 self.project.pdata["TRANSLATIONSBINPATH"].append(path)
338
339 def startSource(self, attrs):
340 """
341 Handler method for the "Source" start tag.
342
343 @param attrs list of tag attributes
344 """
345 self.pathStack = []
346 self.buffer = ""
347
348 def endSource(self):
349 """
350 Handler method for the "Source" end tag.
351 """
352 if self.version >= '4.3':
353 path = Utilities.toNativeSeparators(self.buffer)
354 else:
355 path = self.__buildPath()
356 self.project.pdata["SOURCES"].append(path)
357
358 def startForm(self, attrs):
359 """
360 Handler method for the "Form" start tag.
361
362 @param attrs list of tag attributes
363 """
364 self.pathStack = []
365 self.buffer = ""
366
367 def endForm(self):
368 """
369 Handler method for the "Form" end tag.
370 """
371 if self.version >= '4.3':
372 path = Utilities.toNativeSeparators(self.buffer)
373 else:
374 path = self.__buildPath()
375 self.project.pdata["FORMS"].append(path)
376
377 def startTranslation(self, attrs):
378 """
379 Handler method for the "Translation" start tag.
380
381 @param attrs list of tag attributes
382 """
383 self.pathStack = []
384 self.buffer = ""
385
386 def endTranslation(self):
387 """
388 Handler method for the "Translation" end tag.
389 """
390 if self.version >= '4.3':
391 path = Utilities.toNativeSeparators(self.buffer)
392 else:
393 path = self.__buildPath()
394 self.project.pdata["TRANSLATIONS"].append(path)
395
396 def startTranslationException(self, attrs):
397 """
398 Handler method for the "TranslationException" start tag.
399
400 @param attrs list of tag attributes
401 """
402 self.pathStack = []
403 self.buffer = ""
404
405 def endTranslationException(self):
406 """
407 Handler method for the "TranslationException" end tag.
408 """
409 if self.version >= '4.3':
410 path = Utilities.toNativeSeparators(self.buffer)
411 else:
412 path = self.__buildPath()
413 self.project.pdata["TRANSLATIONEXCEPTIONS"].append(path)
414
415 def startResource(self, attrs):
416 """
417 Handler method for the "Resource" start tag.
418
419 @param attrs list of tag attributes
420 """
421 self.pathStack = []
422 self.buffer = ""
423
424 def endResource(self):
425 """
426 Handler method for the "Resource" end tag.
427 """
428 if self.version >= '4.3':
429 path = Utilities.toNativeSeparators(self.buffer)
430 else:
431 path = self.__buildPath()
432 self.project.pdata["RESOURCES"].append(path)
433
434 def startInterface(self, attrs):
435 """
436 Handler method for the "Interface" start tag.
437
438 @param attrs list of tag attributes
439 """
440 self.pathStack = []
441 self.buffer = ""
442
443 def endInterface(self):
444 """
445 Handler method for the "Interface" end tag.
446 """
447 if self.version >= '4.3':
448 path = Utilities.toNativeSeparators(self.buffer)
449 else:
450 path = self.__buildPath()
451 self.project.pdata["INTERFACES"].append(path)
452
453 def startOther(self, attrs):
454 """
455 Handler method for the "Other" start tag.
456
457 @param attrs list of tag attributes
458 """
459 self.pathStack = []
460 self.buffer = ""
461
462 def endOther(self):
463 """
464 Handler method for the "Other" end tag.
465 """
466 if self.version >= '4.3':
467 path = Utilities.toNativeSeparators(self.buffer)
468 else:
469 path = self.__buildPath()
470 self.project.pdata["OTHERS"].append(path)
471
472 def startMainScript(self, attrs):
473 """
474 Handler method for the "MainScript" start tag.
475
476 @param attrs list of tag attributes
477 """
478 self.pathStack = []
479 self.buffer = ""
480
481 def endMainScript(self):
482 """
483 Handler method for the "MainScript" end tag.
484 """
485 if self.version >= '4.3':
486 path = Utilities.toNativeSeparators(self.buffer)
487 else:
488 path = self.__buildPath()
489 self.project.pdata["MAINSCRIPT"] = [path]
490
491 def startFiletypeAssociation(self, attrs):
492 """
493 Handler method for the "FiletypeAssociation" start tag.
494
495 @param attrs list of tag attributes
496 """
497 pattern = attrs.get("pattern", "")
498 filetype = attrs.get("type", "OTHERS")
499 if pattern:
500 self.project.pdata["FILETYPES"][pattern] = filetype
501
502 def startLexerAssociation(self, attrs):
503 """
504 Handler method for the "LexerAssociation" start tag.
505
506 @param attrs list of tag attributes
507 """
508 pattern = attrs.get("pattern", "")
509 lexer = attrs.get("lexer", "")
510 if pattern:
511 self.project.pdata["LEXERASSOCS"][pattern] = lexer
512
513 def __buildPath(self):
514 """
515 Private method to assemble a path.
516
517 @return The ready assembled path. (string)
518 """
519 path = ""
520 if self.pathStack and not self.pathStack[0]:
521 self.pathStack[0] = os.sep
522 for p in self.pathStack:
523 path = os.path.join(path, p)
524 return path
525
526 def startProject(self, attrs):
527 """
528 Handler method for the "Project" start tag.
529
530 @param attrs list of tag attributes
531 """
532 self.version = attrs.get('version', projectFileFormatVersion)
533
534 def getVersion(self):
535 """
536 Public method to retrieve the version of the project.
537
538 @return String containing the version number.
539 """
540 return self.version
541
542 ###############################################################
543 ## below are handler methods kept for backward compatibility
544 ###############################################################
545
546 def endUIType(self):
547 """
548 Handler method for the "UIType" end tag.
549 """
550 self.project.pdata["PROJECTTYPE"] = [self.buffer]
551
552 def startTranslationPrefix(self, attrs):
553 """
554 Handler method for the "TranslationPrefix" start tag.
555
556 @param attrs list of tag attributes
557 """
558 self.pathStack = []
559 self.buffer = ""
560
561 def endTranslationPrefix(self):
562 """
563 Handler method for the "TranslationPrefix" end tag.
564 """
565 if self.version >= '4.3':
566 path = Utilities.toNativeSeparators(self.buffer)
567 else:
568 path = self.__buildPath()
569 if not path.endswith("_"):
570 path = "%s_" % path
571 self.project.pdata["TRANSLATIONPATTERN"].append("%s%%language%%.ts" % path)
572
573 def startEric4DocParams(self, attrs):
574 """
575 Handler method for the "Eric4DocParams" start tag.
576
577 @param attrs list of tag attributes
578 """
579 self.defaultStartElement(attrs)
580 self._prepareBasics()
581
582 def endEric4DocParams(self):
583 """
584 Handler method for the "Eric4DocParams" end tag.
585 """
586 try:
587 self.project.pdata["DOCUMENTATIONPARMS"]["ERIC4DOC"] = self.stack[-1]
588 except IndexError:
589 pass
590
591 def startEric4ApiParams(self, attrs):
592 """
593 Handler method for the "Eric4ApiParams" start tag.
594
595 @param attrs list of tag attributes
596 """
597 self.defaultStartElement(attrs)
598 self._prepareBasics()
599
600 def endEric4ApiParams(self):
601 """
602 Handler method for the "Eric4ApiParams" end tag.
603 """
604 try:
605 self.project.pdata["DOCUMENTATIONPARMS"]["ERIC4API"] = self.stack[-1]
606 except IndexError:
607 pass
608
609 def startCxfreezeParams(self, attrs):
610 """
611 Handler method for the "CxfreezeParams" start tag.
612
613 @param attrs list of tag attributes
614 """
615 self.defaultStartElement(attrs)
616 self._prepareBasics()
617
618 def endCxfreezeParams(self):
619 """
620 Handler method for the "CxfreezeParams" end tag.
621 """
622 try:
623 self.project.pdata["PACKAGERSPARMS"]["CXFREEZE"] = self.stack[-1]
624 except IndexError:
625 self.project.pdata["PACKAGERSPARMS"] = {}
626
627 def startPyLintParams(self, attrs):
628 """
629 Handler method for the "PyLintParams" start tag.
630
631 @param attrs list of tag attributes
632 """
633 self.defaultStartElement(attrs)
634 self._prepareBasics()
635
636 def endPyLintParams(self):
637 """
638 Handler method for the "PyLintParams" end tag.
639 """
640 try:
641 self.project.pdata["CHECKERSPARMS"]["PYLINT"] = self.stack[-1]
642 except IndexError:
643 self.project.pdata["CHECKERSPARMS"] = {}

eric ide

mercurial