E5XML/TasksReader.py

Sat, 11 Sep 2010 19:00:54 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 11 Sep 2010 19:00:54 +0200
changeset 589
a9a4620f6767
parent 587
3078a9781573
child 590
beb60b9b3d8d
permissions
-rw-r--r--

Added code to handle unexpected start tags to the new XML readers.

585
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de>
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a class for reading an XML tasks file.
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import time
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 from E5Gui.E5Application import e5App
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 from .Config import tasksFileFormatVersion
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from .XMLStreamReaderBase import XMLStreamReaderBase
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 import Utilities
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 class TasksReader(XMLStreamReaderBase):
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 Class for reading an XML tasks file.
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 def __init__(self, device, forProject = False, viewer = None):
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 Constructor
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @param device reference to the I/O device to read from (QIODevice)
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 @param forProject flag indicating project related mode (boolean)
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 @param viewer reference to the task viewer (TaskViewer)
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 XMLStreamReaderBase.__init__(self, device)
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 self.viewer = viewer
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 self.forProject = forProject
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 if viewer:
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 self.viewer = viewer
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 else:
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.viewer = e5App().getObject("TaskViewer")
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 self.version = ""
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 def readXML(self):
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 Public method to read and parse the XML document.
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 while not self.atEnd():
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 self.readNext()
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 if self.isStartElement():
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 if self.name() == "Tasks":
587
3078a9781573 Migrated the templates handler to templates reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 585
diff changeset
51 self.version = self.attribute("version", tasksFileFormatVersion)
585
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 elif self.name() == "Task":
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 self.__readTask()
589
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
54 else:
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
55 self.raiseUnexpectedStartTag(self.name())
585
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 self.showErrorMessage()
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 def __readTask(self):
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 Private method to read the task info.
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 """
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 task = {"summary" : "",
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 "priority" : 1,
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 "completed" : False,
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 "created" : 0,
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 "filename" : "",
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 "linenumber" : 0,
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 "bugfix" : False,
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 "description" : "",
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 }
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 task["priority"] = int(self.attribute("priority", "1"))
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 val = self.attribute("completed", "False")
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 if val in ["True", "False"]:
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 val = (val == "True")
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 else:
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 val = bool(int(val))
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 task["completed"] = val
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 val = self.attribute("bugfix", "False")
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 if val in ["True", "False"]:
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 val = (val == "True")
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 else:
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 val = bool(int(val))
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 task["bugfix"] = val
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 while not self.atEnd():
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 self.readNext()
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 if self.isEndElement() and self.name() == "Task":
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 self.viewer.addTask(task["summary"], priority = task["priority"],
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 filename = task["filename"], lineno = task["linenumber"],
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 completed = task["completed"], _time = task["created"],
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 isProjectTask = self.forProject, isBugfixTask = task["bugfix"],
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 longtext = task["description"])
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 break
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 if self.isStartElement():
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 if self.name() == "Summary":
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 task["summary"] = self.readElementText()
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 elif self.name() == "Description":
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 task["description"] = self.readElementText()
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 elif self.name() == "Created":
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 task["created"] = time.mktime(
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 time.strptime(self.readElementText(), "%Y-%m-%d, %H:%M:%S"))
589
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
106 elif self.name() == "Resource":
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
107 continue # handle but ignore this tag
585
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 elif self.name() == "Filename":
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 task["filename"] = \
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 Utilities.toNativeSeparators(self.readElementText())
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 elif self.name() == "Linenumber":
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 try:
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 task["linenumber"] = int(self.readElementText())
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 except ValueError:
f40889943c0a Migrated the tasks handler to tasks reader.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 pass
589
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
116 else:
a9a4620f6767 Added code to handle unexpected start tags to the new XML readers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 587
diff changeset
117 self.raiseUnexpectedStartTag(self.name())

eric ide

mercurial