File: tasklist.py

package info (click to toggle)
zim 0.68-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,032 kB
  • sloc: python: 59,144; xml: 731; makefile: 45; sh: 35
file content (358 lines) | stat: -rw-r--r-- 9,052 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# -*- coding: utf-8 -*-

# Copyright 2011-2017 Jaap Karssenberg <jaap.karssenberg@gmail.com>

from __future__ import with_statement

import tests


import zim.plugins
import zim.config
import zim.formats

from zim.plugins import PluginManager
from zim.parsing import parse_date
from zim.plugins.tasklist import *


from zim.tokenparser import TokenBuilder, testTokenStream
from zim.formats import ParseTreeBuilder
from zim.formats.wiki import Parser as WikiParser

WIKI_TEXT = '''\
Try all kind of combos - see if the parser trips

=== TODO: test heading with label ===

=== Not a task ===

TODO:
[ ] A
[ ] B
[ ] C

[ ] D
[ ] E

FIXME: dus
~~FIXME:~~ foo

[ ] Simple
[ ] List

[ ] List with
	[ ] Nested items
	[*] Some are done
	[*] Done but with open child
		[x] Others not
		[ ] FOOOOO
[ ] Bar

[ ] And then there are @tags
[ ] Next: And due dates
[ ] Date [d: 11/12]
[ ] Date [d: 11/12/2012]
	[ ] TODO: BAR !!!
[ ] Date <2012-03-27 >2012-03-01
[ ] Date < wk1213.3
[ ] Date < wk1213.3! with punctuation
[ ] Not a date < wk1213.8
[ ] Not a date < wk1213foooooo

TODO @home:
[ ] Some more tasks !!!
	[ ] Foo !
		* some sub item
		* some other item
	[ ] Bar

TODO: dus
FIXME: jaja - TODO !! @FIXME
~~TODO~~: Ignore this one - it is strike out

* TODO: dus - list item
* FIXME: jaja - TODO !! @FIXME - list item
* ~~TODO~~: Ignore this one - it is strike out - list item

* Bullet list
* With tasks as sub items
	[ ] Sub item bullets
* dus

Some text
	With indenting that looks like list but isn't

1. Numbered list
2. With tasks as sub items
	[ ] Sub item numbered
3. dus

Test task inheritance:

[ ] Main @tag1 @tag2 !
	[*] Sub1
	[ ] Sub2 @tag3 !!!!
		[*] Sub2-1
		[*] Sub2-2 @tag4
		[ ] Sub2-3
	[ ] Sub3

TODO: @someday
[ ] A
[ ] B
	[ ] B-1
[ ] C

TODO @home
[ ] main task
	[x] do this
	[ ] Next: do that
	[ ] Next: do something else

[*] Closed parent task
	[ ] With open child
	[ ] Must be open as well to show up in list

[*] Closed parent task
	[*] With closed children
	[*] Should not

Edge case with wrongly nested list
* Foo
		* bullet 1
		* bullet 2

'''

class TestTaskParser(tests.TestCase):

	def testAllCheckboxes(self):
		from zim.plugins.tasklist.indexer import TaskParser

		from zim.parsing import parse_date
		NO_DATE = '9999'

		def t(desc, open=True, start=0, due=NO_DATE, prio=0, tags=''):
			# Generate a task tuple
			# 0:open, 1:prio, 2:start, 3:due, 4:tags, 5:desc
			if tags:
				tags = set(unicode(tags).split(','))
			else:
				tags = set()
			return [open, prio, start, due, tags, unicode(desc)]

		mydate = '%04i-%02i-%02i' % parse_date('11/12')

		wanted = [
			(t('TODO: test heading with label'), []),
			(t('A'), []),
			(t('B'), []),
			(t('C'), []),
			(t('D'), []),
			(t('E'), []),
			(t('FIXME: dus'), []),
			(t('Simple'), []),
			(t('List'), []),
			(t('List with'), [
				(t('Nested items'), []),
				(t('Some are done', open=False), []),
				(t('Done but with open child', open=True), [
					(t('Others not', open=False), []),
					(t('FOOOOO'), []),
				]),
			]),
			(t('Bar'), []),
			(t('And then there are @tags', tags='tags'), []),
			(t('Next: And due dates'), []),
			(t('Date [d: 11/12]', due=mydate), []),
			(t('Date [d: 11/12/2012]', due='2012-12-11'), [
				(t('TODO: BAR !!!', prio=3, due='2012-12-11'), []),
				# due date is inherited
			]),
			(t('Date <2012-03-27 >2012-03-01', due='2012-03-27', start='2012-03-01'), []),
			(t('Date < wk1213.3', due='2012-03-28'), []),
			(t('Date < wk1213.3! with punctuation', due='2012-03-28', prio=1), []),
			(t('Not a date < wk1213.8'), []),
			(t('Not a date < wk1213foooooo'), []),

			# this list inherits the @home tag - and inherits prio
			(t('Some more tasks !!!', prio=3, tags='home'), [
				(t('Foo !', prio=1, tags='home'), []),
				(t('Bar', prio=3, tags='home'), []),
			]),
			(t('TODO: dus'), []),
			(t('FIXME: jaja - TODO !! @FIXME', prio=2, tags='FIXME'), []),
			(t('TODO: dus - list item'), []),
			(t('FIXME: jaja - TODO !! @FIXME - list item', prio=2, tags='FIXME'), []),
			(t('Sub item bullets'), []),
			(t('Sub item numbered'), []),
			(t('Main @tag1 @tag2 !', prio=1, tags='tag1,tag2'), [
				(t('Sub1', prio=1, open=False, tags='tag1,tag2'), []),
				(t('Sub2 @tag3 !!!!', prio=4, tags='tag1,tag2,tag3'), [
					(t('Sub2-1', prio=4, open=False, tags='tag1,tag2,tag3'), []),
					(t('Sub2-2 @tag4', prio=4, open=False, tags='tag1,tag2,tag3,tag4'), []),
					(t('Sub2-3', prio=4, tags='tag1,tag2,tag3'), []),
				]),
				(t('Sub3', prio=1, tags='tag1,tag2'), []),
			]),
			(t('A', tags='someday'), []),
			(t('B', tags='someday'), [
				(t('B-1', tags='someday'), []),
			]),
			(t('C', tags='someday'), []),
			(t('main task', tags='home'), [
				(t('do this', open=False, tags='home'), []),
				(t('Next: do that', tags='home'), []),
				(t('Next: do something else', tags='home'), []),
			]),
			(t('Closed parent task', open=True), [
				(t('With open child'), []),
				(t('Must be open as well to show up in list'), []),
			]),
			(t('Closed parent task', open=False), [
				(t('With closed children', open=False), []),
				(t('Should not', open=False), []),
			]),
		]


		tree = WikiParser().parse(WIKI_TEXT)
		tb = TokenBuilder()
		tree.visit(tb)
		tokens = tb.tokens
		testTokenStream(tokens)

		parser = TaskParser()
		with tests.LoggingFilter('zim.plugins.tasklist', 'Invalid date format'):
			tasks = parser.parse(tokens)

		#~ import pprint; pprint.pprint(tasks)
		self.assertEqual(tasks, wanted)

	def testLabelledCheckboxes(self):
		from zim.plugins.tasklist.indexer import TaskParser

		from zim.parsing import parse_date
		NO_DATE = '9999'

		def t(desc, open=True, start=0, due=NO_DATE, prio=0, tags=''):
			# Generate a task tuple
			# 0:open, 1:prio, 2:start, 3:due, 4:tags, 5:desc
			if tags:
				tags = set(unicode(tags).split(','))
			else:
				tags = set()
			return [open, prio, start, due, tags, unicode(desc)]

		mydate = '%04i-%02i-%02i' % parse_date('11/12')

		wanted = [
			(t('TODO: test heading with label'), []),
			(t('A'), []),
			(t('B'), []),
			(t('C'), []),
			(t('FIXME: dus'), []),

			# this time does not inherit due-date from non-task:
			(t('TODO: BAR !!!', prio=3), []),

			(t('Some more tasks !!!', prio=3, tags='home'), [
				(t('Foo !', prio=1, tags='home'), []),
				(t('Bar', prio=3, tags='home'), []),
			]),
			(t('TODO: dus'), []),
			(t('FIXME: jaja - TODO !! @FIXME', prio=2, tags='FIXME'), []),
			(t('TODO: dus - list item'), []),
			(t('FIXME: jaja - TODO !! @FIXME - list item', prio=2, tags='FIXME'), []),
			(t('A', tags='someday'), []),
			(t('B', tags='someday'), [
				(t('B-1', tags='someday'), []),
			]),
			(t('C', tags='someday'), []),
			(t('main task', tags='home'), [
				(t('do this', open=False, tags='home'), []),
				(t('Next: do that', tags='home'), []),
				(t('Next: do something else', tags='home'), []),
			]),
		]


		tree = WikiParser().parse(WIKI_TEXT)
		tb = TokenBuilder()
		tree.visit(tb)
		tokens = tb.tokens
		testTokenStream(tokens)

		parser = TaskParser(all_checkboxes=False)
		with tests.LoggingFilter('zim.plugins.tasklist', 'Invalid date format'):
			tasks = parser.parse(tokens)

		#~ import pprint; pprint.pprint(tasks)
		self.assertEqual(tasks, wanted)


class TestTaskList(tests.TestCase):

	def testIndexing(self):
		'''Check indexing of tasklist plugin'''
		klass = PluginManager.get_plugin_class('tasklist')
		plugin = klass()

		notebook = tests.new_notebook()
		plugin.extend(notebook)

		# Test indexing based on index signals
		notebook.index.check_and_update()

		view = TasksView.new_from_index(notebook.index)
		for tasks in (
			list(view.list_open_tasks()),
			list(view.list_open_tasks_flatlist()),
		):
			self.assertTrue(len(tasks) > 5)
			for task in tasks:
				path = view.get_path(task)
				self.assertTrue(not path is None)

	def testTaskListTreeView(self):
		klass = PluginManager.get_plugin_class('tasklist')
		plugin = klass()

		notebook = tests.new_notebook()
		plugin.extend(notebook)
		notebook.index.check_and_update()

		from zim.plugins.tasklist.gui import TaskListTreeView
		view = TasksView.new_from_index(notebook.index)
		opener = tests.MockObject()
		treeview = TaskListTreeView(view, opener, task_labels=['TODO', 'FIXME'])

		menu = treeview.get_popup()

		# Check these do not cause errors - how to verify state ?
		tests.gtk_activate_menu_item(menu, _("Expand _All"))
		tests.gtk_activate_menu_item(menu, _("_Collapse All"))

		# Copy tasklist -> csv
		from zim.gui.clipboard import Clipboard
		tests.gtk_activate_menu_item(menu, 'gtk-copy')
		text = Clipboard.get_text()
		lines = text.splitlines()
		self.assertTrue(len(lines) > 10)
		self.assertTrue(len(lines[0].split(',')) > 3)
		self.assertFalse(any('<span' in l for l in lines)) # make sure encoding is removed

		# Test tags
		tags = treeview.get_tags()
		for tag in ('home', 'FIXME', '__no_tags__', 'tags'):
			self.assertIn(tag, tags)
			self.assertGreater(tags[tag], 0)

		# TODO test filtering for tags, labels, string - all case insensitive


	#~ def testDialog(self):
		#~ '''Check tasklist plugin dialog'''
		#
		# TODO