1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2005 - 2010 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 E5Gui.E5Application import e5App |
|
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 = e5App().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.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.buffer) |
|
117 elif self.version == '4.1': |
|
118 self.task["description"] = self.unescape(self.buffer) |
|
119 else: |
|
120 self.buffer = self.unescape(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.task["created"] = \ |
|
128 time.mktime(time.strptime(self.buffer, "%Y-%m-%d, %H:%M:%S")) |
|
129 |
|
130 def endDir(self): |
|
131 """ |
|
132 Handler method for the "Dir" end tag. |
|
133 """ |
|
134 self.pathStack.append(self.buffer) |
|
135 |
|
136 def endName(self): |
|
137 """ |
|
138 Handler method for the "Name" end tag. |
|
139 """ |
|
140 self.pathStack.append(self.buffer) |
|
141 |
|
142 def endLinenumber(self): |
|
143 """ |
|
144 Handler method for the "Linenumber" end tag. |
|
145 """ |
|
146 try: |
|
147 self.task["linenumber"] = int(self.buffer) |
|
148 except ValueError: |
|
149 pass |
|
150 |
|
151 def startFilename(self, attrs): |
|
152 """ |
|
153 Handler method for the "Filename" start tag. |
|
154 |
|
155 @param attrs list of tag attributes |
|
156 """ |
|
157 self.pathStack = [] |
|
158 self.buffer = "" |
|
159 |
|
160 def endFilename(self): |
|
161 """ |
|
162 Handler method for the "Filename" end tag. |
|
163 """ |
|
164 if self.version >= '4.2': |
|
165 self.task["filename"] = \ |
|
166 Utilities.toNativeSeparators(self.buffer) |
|
167 else: |
|
168 self.task["filename"] = self.__buildPath() |
|
169 |
|
170 def __buildPath(self): |
|
171 """ |
|
172 Private method to assemble a path. |
|
173 |
|
174 @return The ready assembled path. (string) |
|
175 """ |
|
176 path = "" |
|
177 if self.pathStack and not self.pathStack[0]: |
|
178 self.pathStack[0] = os.sep |
|
179 for p in self.pathStack: |
|
180 path = os.path.join(path, p) |
|
181 return path |
|
182 |
|
183 def startTasks(self, attrs): |
|
184 """ |
|
185 Handler method for the "Tasks" start tag. |
|
186 |
|
187 @param attrs list of tag attributes |
|
188 """ |
|
189 self.version = attrs.get('version', tasksFileFormatVersion) |
|
190 |
|
191 def getVersion(self): |
|
192 """ |
|
193 Public method to retrieve the version of the tasks file. |
|
194 |
|
195 @return String containing the version number. |
|
196 """ |
|
197 return self.version |
|