-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileTodo.py
More file actions
executable file
·1588 lines (1449 loc) · 55.6 KB
/
Copy pathFileTodo.py
File metadata and controls
executable file
·1588 lines (1449 loc) · 55.6 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##########################################################################
#
# Gaia, task list organiser in with Caldav server sync.
#
# Copyright (C) 2013-2014 Dr Adam S. Candy.
# Dr Adam S. Candy, contact@gaiaproject.org
#
# This file is part of the Gaia project.
#
# Gaia is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Gaia is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gaia. If not, see <http://www.gnu.org/licenses/>.
#
##########################################################################
from Universe import universe, colour
import sys
import os
from datetime import datetime, timedelta
import re
from uuid import uuid4
from Support import error, report
from Support import generate_mono
from Support import repo_add, repo_remove, repo_update
from Parsers import is_relative_date, calculate_delta, prioritystring, is_same_time, timedelta_to_human, do_avoid_weekend, next_weekday, next_increment
def indentation(s, tabsize=2):
sx = s.expandtabs(tabsize)
return (len(sx) - len(sx.lstrip()))/tabsize
#return 0 if sx.isspace() else (len(sx) - len(sx.lstrip()))/tabsize
def parsedate(string, reference=None, alarm=False, allday=False, forward=False):
date = None
if (string is None or len(string) == 0):
if alarm:
if reference is not None:
if allday:
# Warning for day events 1800 - 1000 = 8 hours
date = reference + universe.defaulttime.alldaydiff
else:
# Default warning of an hour
date = reference + universe.defaulttime.diff
else:
string = string.strip()
# Deal with tasks due on a day, not specific time
if len(string) == 6:
allday = True
if alarm:
string = string + universe.defaulttime.alarm.strftime('%H%M')
else:
string = string + universe.defaulttime.due.strftime('%H%M')
try:
if re.match('^\d{6}$', string):
date = datetime.strptime(string, '%y%m%d')
elif re.match('^\d{10}$', string):
try:
date = universe.timezone.localize(datetime.strptime(string, '%y%m%d%H%M'))
#date = datetime.strptime(string, '%y%m%d%H%M')
except Exception, e:
date = None
error('Date parse error [' + string + ']' + ' Exception: ' + str(e))
if universe.debug: raise
pass
elif is_relative_date(string):
d = calculate_delta(string)
if d is not None:
if reference is not None:
if forward:
date = reference + d
else:
date = reference - d
else:
date = universe.timezone.localize(datetime.strptime(string))
#date = datetime.strptime(string)
except Exception, e:
date = None
error('Date parse error [' + string + ']' + ' Exception: ' + str(e))
if universe.debug: raise
pass
return date, allday
def tasklist_read(name, category=None):
if category is None:
filename = universe.dataroot + name
else:
filename = universe.dataroot + category + '/' + name
if not os.path.exists(filename):
return None
f = open(filename, 'r')
level = 0
taskline = ''
notes = ''
lines = (f.read().decode('utf8') + os.linesep).splitlines()
f.close()
#end = len(lines)
#blank = False
#for i in range(len(lines)):
# if len(lines[i]) > 0:
# blank = False
# continue
# if not blank:
# blank = True
# continue
# end = i
# break
# Temp
#end = len(lines)
#root = FileTodos(lines[:end], title=name, parents=[category], filenotes=lines[end+1:])
root = FileTodos(lines, title=name, parents=[category])
root.check_for_modified_children()
if root.is_empty():
report(' ' + colour.grey + 'Removing EMPTY ' + colour.blue + category + colour.grey + '/' + colour.yellowbright + root.name + colour.end + ' ' + colour.grey + '(' + colour.grey + filename + colour.grey + ')' + colour.end)
if not universe.dry:
root.set_modified()
try:
if os.path.exists(filename):
os.remove(filename)
repo_remove(filename)
except:
pass
return root
class FileTodos(object):
def __init__(self, lines=None, filenotes=None, parents=[], parent=None, title=None, flow='parallel', translate=None, number=1, level=None, uid=None, caldav=False, next_action=None):
self.lines = None
self.filenotes = filenotes
if self.filenotes is None:
self.filenotes = []
self.block = []
self.level = -2
# top level modified flag for file updates
self.modified = False
# task level update flag for caldav
self.updated = False
self.sequence = 0
if lines is not None:
self.lines = lines
self.block = [ 0, len(self.lines) ]
if title is not None:
self.level = 0
else:
self.level = indentation(self.lines[0]) + 1
title = self.lines[0].lstrip()
if level is not None:
self.level = level
self.name = None
self.duetext = None
self.alarmtext = None
self.is_checklist = False
self.flowtext = None
self.flow = flow
self.is_header = False
self.is_completed = False
#if caldav:
# self.is_onhold = None
# self.starttext = None
# self.repeat = None
#else:
#self.is_everpresent = False
self.is_onhold = False
self.starttext = None
self.repeat = None
self.expiretext = None
self.wait = ''
self.waitonrepeat = False
self.priority = None
self.is_permanent = False
self.avoidweekends = False
self.current = False
self.error = False
self.sublist = None
self.parents = parents
self.parent = parent
self.number = number
self.uid = uid
self.translate = ''
if translate is not None:
self.translate = translate
self.interpret_task(title)
#if len(self.translate) > 0:
# print self.name, self.translate
self.note = self.find_note()
self.childblocks = self.identify_blocks()
self.children = []
self.due, allday = parsedate(self.duetext)
self.alarm, allday = parsedate(self.alarmtext, reference=self.due, alarm=True, allday=allday)
self.start, allday = parsedate(self.starttext, reference=self.due)
self.expire, allday = parsedate(self.expiretext, reference=self.due, forward=True)
self.active = False
self.titleoptions = ''
self.type = 'file'
self.next_action = next_action
if self.next_action is not None:
self.next_action = next_action.lstrip()
# Need to add next action, in case of checklist, main header is first??
if lines is not None:
if len(self.childblocks) > 0:
filenotesstart = self.childblocks[-1][-1]
else:
filenotesstart = 0
i = filenotesstart
for i in range(filenotesstart, len(lines)):
if len(lines[i]) > 0:
filenotesstart = i
break
if self.level == 0:
#print self.name, filenotesstart
if filenotesstart < len(lines):
if lines[filenotesstart] is not None:
if len(lines[filenotesstart]) > 0:
self.filenotes = lines[filenotesstart:]
if len(self.childblocks) > 0:
self.find_children()
def child_is_task(self, task):
found = False
for child in self.children:
if child.is_same_task(task):
found = True
break
return found
def is_empty(self):
return (not self.has_children() and len(self.filenotes) == 0)
def is_same_task(self, task):
if (len(self.parents) == 0 or len(task.parents) == 0):
return self.name == task.name
else:
return (self.name == task.name and self.parents[0] == task.parents[0])
def is_translate_header(self):
if self.has_children():
if self.is_translate():
if self.parent is None:
return True
else:
if not self.parent.is_translate():
return True
return False
def group(self, masked=True):
if self.is_wait() and masked:
group = 'wait'
elif (self.is_translate() and (not self.is_translate_header()) and masked):
group = self.translate
elif len(self.parents) > 0:
group = self.parents[0]
else:
# Either root of tree, or an un-tied task!
group = 'home'
return group
def allday(self):
return (is_same_time(self.due, universe.defaulttime.due) and is_same_time(self.alarm, universe.defaulttime.alarm) )
def do_repeat(self):
avoid_weekends = (self.group(masked=False) in universe.skipweekendlists or self.avoidweekends)
# Deal with permanent task
if self.is_permanent:
#self.is_onhold = True
detail = ''
if self.waitonrepeat:
self.wait = 'wait'
detail = ' and moved to wait status'
self.set_updated()
report(colour.yellow + 'Permenant task' + detail + colour.end + ' ' + colour.yellowbright + '|'.join(self.parents) + colour.yellow + ':' + colour.end + ' ' + self.name + colour.end)
return
if (self.repeat is None or len(self.repeat) == 0): return
if (self.due is None): return
d = None
if self.waitonrepeat:
self.wait = 'wait'
self.set_updated()
every = False
after = False
random = False
string = self.repeat
if string in ['decennially', 'biennially', 'annually', 'monthly', 'fortnightly', 'weekly', 'daily']:
every = True
if string == 'decennially':
string = '10years'
elif string == 'biennially':
string = '2years'
elif string == 'annually':
string = 'year'
elif string == 'monthly':
string = 'month'
elif string == 'fortnightly':
string = '2weeks'
elif string == 'weekly':
string = 'week'
elif string == 'daily':
string = 'day'
elif re.match('^every\w+$', string):
every = True
string = string[5:]
elif re.match('^after\w+$', string):
after = True
string = string[5:]
elif re.match('^random$', string):
random = True
if every or after or random:
d = calculate_delta(string)
if d is not None:
# Including case of absolute date
new_due = None
new_start = None
new_alarm = None
detail = ''
if every:
# Ensure at least advanced by one d delta
multi = 1
while (self.due + d * multi) < universe.now:
multi += 1
if multi > 1000:
multi = 1
error('Determining multiple every recur time delta for (>1000) ' + self.name)
break
#print 'A', d * multi
#print 'B', self.due
#print 'C', self.due + d * multi
#multi = 0
#d = d * multi
#dmulti = int((universe.now - self.due).total_seconds() // d.total_seconds())
#if dmulti > 0:
# # Event very overdue, such that subsequent repeats missed
# d = (dmulti + 1) * d
# #print "Multi d event", d, dmulti
new_due = self.due + d * multi
if self.start is not None:
if is_relative_date(self.starttext):
new_start = self.start + d * multi
elif (after or random):
if after:
# Use .replace on datetime object instead?
#shift = ((self.due.hour - universe.now.hour) * 60 + (self.due.minute - universe.now.minute)) * 60 + self.due.second - universe.now.second
#new_due = universe.now + d + timedelta(seconds=shift) + timedelta(microseconds=-universe.now.microsecond)
#
new_due = universe.now.replace(second=0, microsecond=0)
shift = (self.due.hour - new_due.hour) * 60 + self.due.minute - new_due.minute
new_due = new_due + d + timedelta(minutes=shift)
#
elif random:
new_due = universe.now.replace(second=0, microsecond=0) + d
new_due = do_avoid_weekend(new_due, avoid_weekends=avoid_weekends)
if (self.starttext is not None and len(self.starttext) > 0):
string = self.starttext
if is_relative_date(string):
d = calculate_delta(string)
if d is not None:
new_start = new_due - d
if self.alarm is not None:
if self.alarmtext is not None:
self.alarm, allday = parsedate(self.alarmtext, reference=new_due, alarm=True, allday=self.allday())
elif self.allday():
# Warning for day events 1800 - 1000 = 8 hours
new_alarm = new_due + universe.defaulttime.alldaydiff
else:
# Default warning of an hour
new_alarm = new_due + universe.defaulttime.diff
if new_due is not None:
detail = detail + ' due: %(old)s -> %(new)s' % {
'old': '[empty]' if self.due is None else self.due.strftime('%y%m%d%H%M%z'),
'new': '[empty]' if new_due is None else new_due.strftime('%y%m%d%H%M%z')
}
self.due = new_due
if new_start is not None:
detail = detail + ' start: %(old)s -> %(new)s' % {
'old': '[empty]' if self.start is None else self.start.strftime('%y%m%d%H%M%z'),
'new': '[empty]' if new_start is None else new_start.strftime('%y%m%d%H%M%z')
}
self.start = new_start
if new_alarm is not None:
detail = detail + ' alarm: %(old)s -> %(new)s' % {
'old': '[empty]' if self.alarm is None else self.alarm.strftime('%y%m%d%H%M%z'),
'new': '[empty]' if new_alarm is None else new_alarm.strftime('%y%m%d%H%M%z')
}
self.alarm = new_alarm
report(colour.yellow + 'Recur task in' + colour.end + ' ' + colour.yellowbright + '|'.join(self.parents) + colour.yellow + ':' + colour.end + ' ' + self.name + colour.grey + detail + colour.end)
else:
error('Determining recur time delta for ' + self.name + ' string[' + string + ']')
return
def add(self, task):
if len(task.parents) == 1:
lists = []
for c in self.children:
if c.name == task.parents[0]:
lists = c.child_names()
break
if (task.sublist is None) or not (task.sublist in lists):
if (task.sublist is not None) and not (task.sublist in lists):
report(colour.red + 'Selected sublist ' + task.sublist + ' not present, adding to the inbox' + colour.end)
task.sublist = 'inbox'
task.parents.append(task.sublist)
task.sublist = None
match = self
for group in task.parents:
found = False
for child in match.children:
if child.name == group:
found = True
match = child
break
if not found:
inbox = FileTodos(title='inbox', parents=match.parents + [match.name], parent=match, translate=self.translate, level=match.level + 1)
match.add_child(inbox)
match = inbox
found = True
match.set_modified(task)
new = FileTodos(lines=task.reformat().splitlines(), parents=match.parents + [match.name], parent=match)
report(colour.green + 'Adding task to ' + colour.greenbright + 'file' + colour.green + ' in ' + '|'.join(new.parents) + colour.green + ':' + colour.end + ' ' + new.name)
match.add_child(new)
def find_task(self, task):
match = None
if self.is_same_task(task):
return self
for child in self.children:
match = child.find_task(task)
if match is not None:
match = match.find_task(task)
break
return match
def find_tasks_by_name(self, task=None, name=None, matches=None, check_is_wait=False):
if matches is None:
matches = []
if task is not None:
name = task.name
if name == self.name:
if (not check_is_wait or (check_is_wait and self.is_wait()) ):
matches.append(self)
for child in self.children:
matches = child.find_tasks_by_name(name=name, matches=matches)
return matches
def find_task_parent(self, task):
#if task.name in self.child_names():
if self.child_is_task(task):
return self
for child in self.children:
parents = child.find_task_parent(task)
if parents is not None:
return parents
return None
def children_all_completed(self):
allcomplete = True
for child in self.children:
if not child.is_completed:
allcomplete = False
return allcomplete
def uncomplete_childen(self):
self.is_completed = False
for child in self.children:
child.uncomplete_childen()
def unwait_childen(self):
# Assumes working just after uncompleted (for waitonrepeat test)
if self.waitonrepeat:
self.wait = 'wait'
else:
self.wait = ''
for child in self.children:
child.unwait_childen()
def is_repeat(self):
if self.repeat is not None:
if len(self.repeat) > 0:
if self.due is not None:
return True
if self.is_permanent:
return True
return False
def recur(self, task, root=None, recursive=False):
if root is None:
root = self
match = None
removed = False
#if task.name in self.child_names():
if self.child_is_task(task):
for child in self.children:
#if child.name == task.name:
if child.is_same_task(task):
match = child
break
# Should complete/remove any children here - otherwise need to wait for next run
match.uncomplete_childen()
match.unwait_childen()
if ((match.repeat is not None and match.due is not None) or match.is_permanent):
match.do_repeat()
#match.update()
else:
root.remove(task)
removed = True
else:
for child in self.children:
match = child.recur(task, root=root, recursive=True)
if match is not None:
break
if not recursive:
if match is not None:
self.make_modified(match)
if removed: return None
return match
def remove(self, task, root=None, repeats=False, recursive=False):
if root is None:
root = self
match = None
if self.child_is_task(task):
# Check if new tasks become active
if self.is_repeat():
repeats = True
new_children = []
for child in self.children:
#if child.name == task.name:
if child.is_same_task(task):
match = child
if repeats:
match.is_completed = True
else:
new_children.append(child)
if not match.is_header:
if repeats:
action = 'Completing'
else:
self.children = new_children
action = 'Removing'
stat = colour.greenbright + 'OK' + colour.end if match is not None else colour.redbright + 'FAIL' + colour.end
report(colour.red + action + ' task from full tree in' + colour.end + ' ' + colour.redbright + 'file' + '|' + '|'.join(match.parents) + colour.red + ':' + colour.end + ' ' + match.name + ' ' + stat)
else:
if self.is_repeat():
repeats = True
for child in self.children:
match = child.remove(task, root=root, repeats=repeats, recursive=True)
if match is not None:
break
# Check if parent requires removal
if match is not None:
# removed: child, parent: self X actually match?
if child.level > 0:
if child.name == match.parents[-1]:
if (child.is_repeat() or repeats):
if child.children_all_completed():
report(colour.red + ' need to complete parent also, ' + colour.redbright + child.name + colour.end)
# Uncomplete all children of child
child.uncomplete_childen()
child.unwait_childen()
if child.is_repeat():
# Apply repeat to child
child.do_repeat()
else:
self.remove(child, repeats=repeats, recursive=True)
match = child
else:
if not child.has_children():
if not child.is_header:
report(colour.red + ' need to remove parent also, ' + colour.redbright + child.name + colour.end)
self.remove(child, recursive=True)
match = child
if not recursive:
if match is not None:
self.make_modified(match)
return match
def clear_titleoptions(self):
self.starttext = None
self.repeat = None
#self.is_onhold = False
def is_equal(self, other, caldav=False):
if (self.due != other.due):
return False
if (self.alarm != other.alarm):
return False
if (self.note != other.note):
return False
if (self.priority != other.priority):
return False
if (self.wait != other.wait):
return False
if (self.next_action != other.next_action):
return False
#print self.name, '|', self.group(), other.group()
# Don't compare translate if either task is waiting
if (not self.is_wait() and not other.is_wait()):
if (self.translate != other.translate):
#print self.name, '|', self.group(), other.group()
return False
if caldav:
return True
# Optional checks:
# Note not possible for caldav
# start, starttext
#if (self.starttext is not None and other.starttext is not None):
if (self.starttext != other.starttext):
return False
# repeat
#if (self.repeat is not None and other.repeat is not None):
if (self.repeat != other.repeat):
return False
# is_onhold
#if (self.is_onhold is not None and other.is_onhold is not None):
if (self.is_onhold != other.is_onhold):
return False
# flow (no access, add later?)
# is_permanent (no access - add later?)
# is_header (no access from Caldav?)
# is_checklist (not used)
return True
def __eq__(self, other):
if isinstance(other, FileTodos):
return self.is_equal(other)
return NotImplemented
def __ne__(self, other):
result = self.__eq__(other)
if result is NotImplemented:
return result
return not result
def __lt__(self, other):
# Check due
if (self.due is None and other.due is not None):
return False
if (self.due is not None and other.due is None):
return True
if ((self.due is not None and other.due is not None) and self.due != other.due):
return self.due < other.due
# Check priorities
if (self.priority is None and other.priority is not None):
return False
if (self.priority is not None and other.priority is None):
return True
if ((self.priority is not None and other.priority is not None) and self.priority != other.priority):
# Note priroties in reverse
return self.priority < other.priority
# Check wait
if (self.is_wait() and not other.is_wait):
return False
if (not self.is_wait() and other.is_wait):
return True
return self.name < other.name
def update(self, task, due=False, note=False, priority=False, wait=False, recursive=False, caldav=False, previous=None, caldavsource=False):
# Also update FileTodo.__eq__
# To stop passing all of the above around...:
if previous is not None:
due = (task.due != previous.due) or (task.alarm != previous.alarm) or due
note = (task.note != previous.note) or note
next_action = (task.next_action != previous.next_action)
#next_action = True
#print '['+previous.next_action+']', '['+task.next_action+']'
priority = (task.priority != previous.priority) or priority
wait = (task.wait != previous.wait) or wait
# new:
#starttext = (task.starttext is not None and previous.starttext is not None) and (task.starttext != previous.starttext)
#repeat = (task.repeat is not None and previous.repeat is not None) and (task.repeat != previous.repeat)
#is_onhold = (task.is_onhold is not None and previous.is_onhold is not None) and (task.is_onhold != previous.is_onhold)
translate = False
if (not task.is_wait() and not previous.is_wait()):
translate = (task.translate != previous.translate)
# Deal with updates on tasks from caldav data (i.e. ensure below are False)
starttext = (task.starttext != previous.starttext) and (not caldavsource)
repeat = (task.repeat != previous.repeat) and (not caldavsource)
is_onhold = (task.is_onhold != previous.is_onhold) and (not caldavsource)
#print 'caldavsource', caldavsource, starttext, repeat, is_onhold, task.name
found = None
#if self.name == task.name:
if self.is_same_task(task):
detail = ''
if priority:
detail = detail + ' priority: %(old)s -> %(new)s' % {
'old': prioritystring(self.priority, shownone=True),
'new': prioritystring(task.priority, shownone=True),
}
self.priority = task.priority
if due:
detail = detail + ' due: %(old)s -> %(new)s, alarm: %(aold)s -> %(anew)s' % {
'old': '[empty]' if self.due is None else self.due.strftime('%y%m%d%H%M%z'),
'new': '[empty]' if task.due is None else task.due.strftime('%y%m%d%H%M%z'),
'aold': '[empty]' if self.alarm is None else self.alarm.strftime('%y%m%d%H%M%z'),
'anew': '[empty]' if task.alarm is None else task.alarm.strftime('%y%m%d%H%M%z'),
}
self.due = task.due
self.alarm = task.alarm
# If due becomes None any start is now no longer relevant so ensure it is also cleared
# Might need to do this for alarm too? bit complicated...
if (self.due is None and self.starttext is not None):
detail = detail + ' start: %(old)s -> [empty] (enforced)' % {
'old': '[empty:'+str(self.starttext)+']' if (self.starttext is None or self.starttext == '') else ' + '.join(self.starttext.splitlines()),
}
self.starttext = None
if wait:
detail = detail + ' wait: %(old)s -> %(new)s' % {
'old': '[empty:'+str(self.wait)+']' if (self.wait is None or self.wait == '') else self.wait,
'new': '[empty:'+str(task.wait)+']' if (task.wait is None or task.wait == '') else task.wait
}
self.wait = task.wait
# asc 131203
# if translate:
# detail = detail + ' translate: %(old)s -> %(new)s' % {
# 'old': '[empty:'+str(self.translate)+']' if (self.translate is None or self.translate == '') else self.translate,
# 'new': '[empty:'+str(task.translate)+']' if (task.translate is None or task.translate == '') else task.translate
# }
# self.translate = task.translate
if note:
detail = detail + ' note: %(old)s -> %(new)s' % {
'old': '[empty:'+str(self.note)+']' if (self.note is None or self.note == '') else ' + '.join(self.note.splitlines()),
'new': '[empty:'+str(task.note)+']' if (task.note is None or task.note == '') else ' + '.join(task.note.splitlines()),
}
self.note = task.note
# new
if is_onhold:
detail = detail + ' hold: %(old)s -> %(new)s' % {
'old': '[empty:'+str(self.is_onhold)+']' if (self.is_onhold is None or self.is_onhold == '') else self.is_onhold,
'new': '[empty:'+str(task.is_onhold)+']' if (task.is_onhold is None or task.is_onhold == '') else task.is_onhold
}
self.is_onhold = task.is_onhold
if starttext:
detail = detail + ' start: %(old)s -> %(new)s' % {
'old': '[empty:'+str(self.starttext)+']' if (self.starttext is None or self.starttext == '') else ' + '.join(self.starttext.splitlines()),
'new': '[empty:'+str(task.starttext)+']' if (task.starttext is None or task.starttext == '') else ' + '.join(task.starttext.splitlines()),
}
self.starttext = task.starttext
if repeat:
detail = detail + ' repeat: %(old)s -> %(new)s' % {
'old': '[empty:'+str(self.repeat)+']' if (self.repeat is None or self.repeat == '') else ' + '.join(self.repeat.splitlines()),
'new': '[empty:'+str(task.repeat)+']' if (task.repeat is None or task.repeat == '') else ' + '.join(task.repeat.splitlines()),
}
self.repeat = task.repeat
if next_action:
detail = detail + ' next action: %(old)s -> %(new)s' % {
'old': '[empty:'+str(self.next_action)+']' if (self.next_action is None or self.next_action == '') else ' + '.join(self.next_action.splitlines()),
'new': '[empty:'+str(task.next_action)+']' if (task.next_action is None or task.next_action == '') else ' + '.join(task.next_action.splitlines()),
}
self.next_action = task.next_action
#self.sequence_increment()
if caldav:
caltype = 'caldav'
elif recursive:
caltype = 'file'
else:
caltype = 'active'
updated = False
if caldav:
# Assumes have previous
if (due or note or priority or wait or translate or next_action):
from CaldavClient import ical_event_update
ical_event_update(self, due=due, note=note, priority=priority, wait=wait, translate=translate, previous=previous, next_action=next_action)
updated = True
else:
updated = True
if updated:
report(colour.yellow + 'Updating task in' + colour.end + ' ' + colour.yellowbright + caltype + '|' + '|'.join(self.parents) + colour.yellow + ':' + colour.end + ' ' + self.name + colour.grey + detail + colour.end)
else:
report(colour.yellow + 'Updating task in' + colour.end + ' ' + colour.yellowbright + caltype + '|' + '|'.join(self.parents) + colour.yellow + ' not required and '+ colour.yellowbright +'skipped' + colour.end + ' ' + self.name + colour.grey + detail + colour.end)
found = self
else:
for child in self.children:
found = child.update(task, due=due, note=note, priority=priority, wait=wait, recursive=True, caldav=caldav, previous=previous, caldavsource=caldavsource)
if found is not None:
break
if ((not recursive) and (not caldav)):
self.make_modified(found)
return found
def make_modified_parents(self, task=None):
if task is None:
task = self
if len(self.parents) > 1:
self.parent.make_modified_parents(task=task)
elif len(self.parents) == 1:
self.make_modified(task=task)
return
def check_for_modified_children(self, root=True):
modified = False
if self.modified:
modified = True
for child in self.children:
modified = modified or child.check_for_modified_children(root=False)
if root and modified:
self.set_modified()
return modified
def set_modified(self, task=None):
if task is not None:
name = task.name
else:
name = '[not provided]'
if len(self.parents) > 0:
parentstr = self.parents[-1]
else:
parentstr = '[parent unknown]'
report(colour.magenta+'Marking modified ' + parentstr + '|' + self.name + ' for task ' + name + colour.end)
self.modified = True
def make_modified(self, task):
def to_mark(current, task):
if len(current.parents) == 0:
return False
return (task.parents[1] == current.name and task.parents[0] == current.parents[0])
if len(task.parents) < 2:
return
if to_mark(self, task):
if not self.modified:
self.set_modified(task)
else:
for child in self.children:
child.make_modified(task)
def child_names(self):
names = []
for child in self.children:
names.append(child.name)
return names
def has_children(self):
if len(self.children) > 0:
return True
return False
def is_sequential(self):
return self.flow == 'sequential'
def set_wait(self, string=None):
if string is None:
string = 'wait'
self.wait = string
for child in self.children:
child.set_wait(string)
def set_updated(self, follow=True):
self.updated = True
if follow:
for child in self.children:
child.set_updated(follow=follow)
def is_translate(self):
if self.translate is not None:
if len(self.translate) > 0:
return True
return False
def is_wait(self):
if self.wait is not None:
if len(self.wait) > 0:
return True
return False
def is_available(self):
if self.is_onhold:
return False
if self.error:
return False
#if self.is_wait():
# return False
if self.start is not None:
if self.start > universe.now:
return False
return True
def is_expired(self):
if self.expire is not None:
if self.expire <= universe.now:
return True
return False
def is_active(self):
# Exclude the root and projects
if self.level <= 0:
return False
if self.is_header:
return False
if not self.is_available():
return False
if self.parent.is_wait():
# Only include highest wait
return False
#if (self.parent.is_translate_header() and self.parent.is_wait()):
# # Note onhold wipes out children anyway - here wait is special case
# return False
#if ( len(self.translate) > 0 and len(self.parent.translate) == 0 ):
if self.is_translate_header():
# Header of aux list
# Not great returning True here
return True
# Clause for grouped / lists
if ((not self.is_checklist) and (self.has_children())):
return False
# Restricted to next actions, when sequential
return True
def find_all_names(self, todos=None):
if todos == None:
todos = []
if not self.error:
if self.level >= 1:
todos.append(self.name)
for child in self.children:
todos = child.find_all_names(todos)
return todos
def find_all_tasks(self, todos=None):
if todos == None:
todos = []
if not self.error:
if self.level >= 1:
todos.append(self)
for child in self.children:
todos = child.find_all_tasks(todos)
return todos
def find_all_task_occurances(self, task, occurances=None):
if occurances == None:
occurances = 0
if self.is_same_task(task):
occurances +=1
#report(' DUPLICATE CALDAV: ' + str(occurances) + ' ' + task.name)
for child in self.children:
occurances = child.find_all_task_occurances(task, occurances)
return occurances
def find_active(self, active=None):
if active == None:
active = []
if self.is_active():
active.append(self)
self.active = True
is_sequential = self.is_sequential()
for child in self.children:
if child.is_completed:
continue
if not child.is_available():
if is_sequential:
break
continue
active = child.find_active(active)
if is_sequential:
break
return active
def is_valid_task(self):
if self.level <= 0:
return False
if self.is_header:
return False
if self.is_onhold:
return False
if self.error:
return False