E4XML/ProjectHandler.py

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

eric ide

mercurial