|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the handler class for reading an XML tasks file. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import time |
|
12 |
|
13 from E4Gui.E4Application import e4App |
|
14 |
|
15 from Config import tasksFileFormatVersion |
|
16 from XMLHandlerBase import XMLHandlerBase |
|
17 |
|
18 import Utilities |
|
19 |
|
20 class TasksHandler(XMLHandlerBase): |
|
21 """ |
|
22 Class implementing a sax handler to read an XML tasks file. |
|
23 """ |
|
24 def __init__(self, forProject = False, taskViewer=None): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param forProject flag indicating project related mode (boolean) |
|
29 @param taskViewer reference to the task viewer object |
|
30 """ |
|
31 XMLHandlerBase.__init__(self) |
|
32 |
|
33 self.startDocumentSpecific = self.startDocumentTasks |
|
34 |
|
35 self.elements.update({ |
|
36 'Tasks' : (self.startTasks, self.defaultEndElement), |
|
37 'Summary' : (self.defaultStartElement, self.endSummary), |
|
38 'Description' : (self.defaultStartElement, self.endDescription), |
|
39 'Created' : (self.defaultStartElement, self.endCreated), |
|
40 'Dir' : (self.defaultStartElement, self.endDir), |
|
41 'Name' : (self.defaultStartElement, self.endName), |
|
42 'Filename' : (self.startFilename, self.endFilename), |
|
43 'Linenumber' : (self.defaultStartElement, self.endLinenumber), |
|
44 'Task' : (self.startTask, self.endTask), |
|
45 }) |
|
46 |
|
47 self.forProject = forProject |
|
48 if taskViewer: |
|
49 self.taskViewer = taskViewer |
|
50 else: |
|
51 self.taskViewer = e4App().getObject("TaskViewer") |
|
52 |
|
53 def startDocumentTasks(self): |
|
54 """ |
|
55 Handler called, when the document parsing is started. |
|
56 """ |
|
57 self.version = '' |
|
58 self.pathStack = [] |
|
59 |
|
60 ################################################### |
|
61 ## below follow the individual handler functions |
|
62 ################################################### |
|
63 |
|
64 def startTask(self, attrs): |
|
65 """ |
|
66 Handler method for the "Task" start tag. |
|
67 |
|
68 @param attrs list of tag attributes |
|
69 """ |
|
70 self.task = {"summary" : "", |
|
71 "priority" : 1, |
|
72 "completed" : False, |
|
73 "created" : 0, |
|
74 "filename" : "", |
|
75 "linenumber" : 0, |
|
76 "bugfix" : False, |
|
77 "description" : "", |
|
78 } |
|
79 self.task["priority"] = int(attrs.get("priority", "1")) |
|
80 |
|
81 val = attrs.get("completed", "False") |
|
82 if val in ["True", "False"]: |
|
83 val = (val == "True") |
|
84 else: |
|
85 val = bool(int(val)) |
|
86 self.task["completed"] = val |
|
87 |
|
88 val = attrs.get("bugfix", "False") |
|
89 if val in ["True", "False"]: |
|
90 val = (val == "True") |
|
91 else: |
|
92 val = bool(int(val)) |
|
93 self.task["bugfix"] = val |
|
94 |
|
95 def endTask(self): |
|
96 """ |
|
97 Handler method for the "Task" end tag. |
|
98 """ |
|
99 self.taskViewer.addTask(self.task["summary"], priority = self.task["priority"], |
|
100 filename = self.task["filename"], lineno = self.task["linenumber"], |
|
101 completed = self.task["completed"], |
|
102 _time = self.task["created"], isProjectTask = self.forProject, |
|
103 isBugfixTask = self.task["bugfix"], longtext = self.task["description"]) |
|
104 |
|
105 def endSummary(self): |
|
106 """ |
|
107 Handler method for the "Summary" end tag. |
|
108 """ |
|
109 self.task["summary"] = self.unescape(self.utf8_to_code(self.buffer)) |
|
110 |
|
111 def endDescription(self): |
|
112 """ |
|
113 Handler method for the "Description" end tag. |
|
114 """ |
|
115 if self.version < '4.1': |
|
116 self.task["summary"] = self.unescape(self.utf8_to_code(self.buffer)) |
|
117 elif self.version == '4.1': |
|
118 self.task["description"] = self.unescape(self.utf8_to_code(self.buffer)) |
|
119 else: |
|
120 self.buffer = self.unescape(self.utf8_to_code(self.buffer)) |
|
121 self.task["description"] = self.decodedNewLines(self.buffer) |
|
122 |
|
123 def endCreated(self): |
|
124 """ |
|
125 Handler method for the "Created" end tag. |
|
126 """ |
|
127 self.buffer = self.utf8_to_code(self.buffer) |
|
128 self.task["created"] = \ |
|
129 time.mktime(time.strptime(self.buffer, "%Y-%m-%d, %H:%M:%S")) |
|
130 |
|
131 def endDir(self): |
|
132 """ |
|
133 Handler method for the "Dir" end tag. |
|
134 """ |
|
135 self.buffer = self.utf8_to_code(self.buffer) |
|
136 self.pathStack.append(self.buffer) |
|
137 |
|
138 def endName(self): |
|
139 """ |
|
140 Handler method for the "Name" end tag. |
|
141 """ |
|
142 self.buffer = self.utf8_to_code(self.buffer) |
|
143 self.pathStack.append(self.buffer) |
|
144 |
|
145 def endLinenumber(self): |
|
146 """ |
|
147 Handler method for the "Linenumber" end tag. |
|
148 """ |
|
149 try: |
|
150 self.task["linenumber"] = int(self.buffer) |
|
151 except ValueError: |
|
152 pass |
|
153 |
|
154 def startFilename(self, attrs): |
|
155 """ |
|
156 Handler method for the "Filename" start tag. |
|
157 |
|
158 @param attrs list of tag attributes |
|
159 """ |
|
160 self.pathStack = [] |
|
161 self.buffer = "" |
|
162 |
|
163 def endFilename(self): |
|
164 """ |
|
165 Handler method for the "Filename" end tag. |
|
166 """ |
|
167 if self.version >= '4.2': |
|
168 self.task["filename"] = \ |
|
169 unicode(Utilities.toNativeSeparators(self.utf8_to_code(self.buffer))) |
|
170 else: |
|
171 self.task["filename"] = self.__buildPath() |
|
172 |
|
173 def __buildPath(self): |
|
174 """ |
|
175 Private method to assemble a path. |
|
176 |
|
177 @return The ready assembled path. (string) |
|
178 """ |
|
179 path = "" |
|
180 if self.pathStack and not self.pathStack[0]: |
|
181 self.pathStack[0] = os.sep |
|
182 for p in self.pathStack: |
|
183 path = os.path.join(path, p) |
|
184 return path |
|
185 |
|
186 def startTasks(self, attrs): |
|
187 """ |
|
188 Handler method for the "Tasks" start tag. |
|
189 |
|
190 @param attrs list of tag attributes |
|
191 """ |
|
192 self.version = attrs.get('version', tasksFileFormatVersion) |
|
193 |
|
194 def getVersion(self): |
|
195 """ |
|
196 Public method to retrieve the version of the tasks file. |
|
197 |
|
198 @return String containing the version number. |
|
199 """ |
|
200 return self.version |