-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.py
executable file
·989 lines (783 loc) · 26.5 KB
/
todo.py
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
#!/usr/bin/env python
'''
Simple CLI TODO program with time tracking
by Stamat <[email protected]>
'''
import sys, os, re, time, string
from datetime import datetime, date, timedelta
import threading, atexit
import csv, ConfigParser
texttable_available = True
try:
import texttable
except ImportError:
try:
import lib.texttable as texttable
except ImportError:
texttable_available = False
args = sys.argv
args.pop(0)
args = ' '.join(args)
version = '1.0.5'
#TODO: Preferences for argumentless display of tasks, default query
#TODO: add UID for tasks for server synchronisation, should be creation timestamp in combination with autoincrement ID, to prevent multidevice sync confusion
#synchronisation should happen by last modified has priority
#completed tasks has a special additional file for synchronisation newly finished tasks after last synchronisation, server appends to completed.csv on serverside. THINK ABOUT THIS, IT WILL BE A HUGE FILE.
#TODO: droplets gui for this TODO
fieldnames = ['task', 'created', 'important', 'due', 'time_spent', 'tasklist', 'tags', 'last_modified']
filename = 'todo.csv'
timefile = 'time.csv'
tmp_filename = 'tmp_todo.csv'
filename_completed = 'todo_completed.csv'
tmp_filename_completed = 'tmp_todo_completed.csv'
pat_cmds = re.compile(r"(\-\-?[a-zA-Z0-9\-]+\s?[^\-]*)")
pat_sepcmd = re.compile(r"(\-\-?)([a-zA-Z0-9\-]+)\s?([^\-]*)")
pat_tl = re.compile(r"^@([^@\s\-]+)")
pat_tg = re.compile(r"^\+([^\+\s\-]+)")
_TIME = 0 #global time var for storing time tracking delta
user_path = os.path.expanduser('~')
config_path = os.path.join(user_path, '.todo')
config_cfg = os.path.join(config_path, 'config.cfg')
# Recursive ask to set the directory untill the the pathe xists
def _bother(default):
npath = raw_input('Enter directory to store data files (default='+default+'):').strip()
if npath == "":
print 'As you command sir, we\'ll be using the default path'
return default
try:
assert os.path.exists(npath) and os.path.isdir(npath)
except AssertionError:
print 'Error: Sorry, the directory "'+npath+'" doesn\'t exits! Please try again.'
return _bother(default)
return npath
# Reads an INI file and returns a ConfigParser object that can be iterated
def _readconf(file_path):
conf = ConfigParser.RawConfigParser()
if not os.path.exists(file_path) and not os.path.isfile(file_path):
return conf
try:
f = open(file_path)
conf.readfp(f)
f.close
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
raise
return conf
# Sets a value of a INI ConfigParser object by a given section and key
def _setconf(conf, section, key, value):
if not conf.has_section(section):
conf.add_section(section)
conf.set(section, key, value)
return conf
# Writes INI ConfigParser object to a file
def _writeconf(file_path, conf):
try:
f = open(file_path, 'wb')
conf.write(f)
f.close
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
raise
return True
def _UTCTimestamp():
return int( time.mktime(time.gmtime()) )
def _UTC2LocalTimestamp(utc_timestamp):
return utc_timestamp + (int(time.time()) - _UTCTimestamp())
conf = _readconf(config_cfg)
# FIRST INIT
if not os.path.exists(config_path):
os.mkdir(config_path)
if not os.path.exists(config_cfg):
uname = os.path.split(user_path) #XXX: Will this always work?
uname = uname[-1]
print '''
TODO v{ver}
the simple CLI task manager with time tracking
----------------------------------------------
Oh hai, {name}!
It looks like it\'s your first time using this application!?
If you wish you can enter a directory where you would like to save the CSV todo data files. Saving them to Dropbox folder can be a good idea to backup them and access them across the devices.
'''.format(ver=version, name=uname.capitalize())
npath = _bother(config_path)
_setconf(conf, 'general', 'dir', npath)
_setconf(conf, 'general', 'name', uname.capitalize())
_writeconf(config_cfg, conf)
print '''
Thanks, you're a real pal!
,d88b.d88b,
88888888888
`Y8888888Y'
`Y888Y'
`Y'
'''
destination_dir = conf.get('general', 'dir')
filename = os.path.join(destination_dir, filename)
tmp_filename = os.path.join(destination_dir, tmp_filename)
filename_completed = os.path.join(destination_dir, filename_completed)
tmp_filename_completed = os.path.join(destination_dir, tmp_filename_completed)
# create the todo file if it doesn't exists
if not os.path.exists(filename):
csv_out = open(filename, 'w')
writer = csv.DictWriter(csv_out, fieldnames=fieldnames)
writer.writeheader()
csv_out.close
# updated print, used when outputing spent time on a task
def _uprint(new):
CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'
print(CURSOR_UP_ONE + ERASE_LINE + str(new))
# gets the current task number, if the value past is string "last" then it is the length of the CSV rows
def _parsenum(num, mod=None):
num = num.split(',')
last = 1
try:
reader
except NameError:
csv_in = open(filename)
reader = csv.DictReader(csv_in)
reader = list(reader)
last = len(reader);
csv_in.close
for i in range(0, len(num)):
if num[i] == 'last':
num[i] = last
num[i] = int(num[i])
if mod:
num[i] += mod
return num
# Sets a value to a CSV file
def _set(num, field, value, value_array = True):
csv_in = open(filename)
csv_out = open(tmp_filename, 'w')
reader = csv.DictReader(csv_in)
reader = list(reader)
writer = csv.DictWriter(csv_out, fieldnames=fieldnames)
writer.writeheader()
if not isinstance(num, list):
num = [int(num)-1]
value = [value]
for i in range(0, len(num)):
try:
if field:
reader[num[i]][field] = value[i] if value_array else value
else:
reader[num[i]] = value[i] if value_array else value
reader[num[i]]['last_modified'] = _UTCTimestamp()
except IndexError:
print 'Error: Nonexistent entry', str(num[i]+1)
for row in reader:
writer.writerow(row)
csv_in.close
csv_out.close
os.rename(tmp_filename, filename)
# Gets a value from a csv file
def _get(num, field=None):
csv_in = open(filename)
reader = csv.DictReader(csv_in)
reader = list(reader)
one_result = False
if not isinstance(num, list):
num = [int(num)-1]
one_result = True
result = []
for i in range(0, len(num)):
try:
if field:
result.append(reader[num[i]][field])
else:
result.append(reader[num[i]])
except IndexError:
print 'Error: Nonexistent entry', str(num[i]+1)
csv_in.close
if one_result:
return result[0]
return result
# transforms CSV list value into a py list
def _csvlist(string):
pat_inside = re.compile(r"^\[\'(.*)\'\]$")
r = pat_inside.search(string)
if not r:
return None
return re.split(r"\',\s?\'", r.group(1))
# executes a command from a dictionary of commands
def _execute(command, args=None):
if command in fn:
fn[command](value)
else:
print 'Error: Unknown command',command
#TODO savetime if tracking a task time and you exit a terminal
def _savetime():
global _TIME
if _TIME is not 0:
_TIME = time.time() - _TIME
_set(num, 'time_spent', _TIME)
#atexit.register(_savetime)
def _deltatime(string):
if type(string) is str and string.strip() == '':
return '0:00:00'
time = timedelta(0, float(string))
return re.sub(r"\.[0-9]+", '', str(time))
def _csvnum(val, fn):
val = val.strip()
if val and val != '':
try:
return fn(val)
except Exception as ex:
pass
return 0
def _csvint(val, fn):
return _csvnum(val, int)
def _csvint(val, fn):
return _csvnum(val, float)
# display all tags and number of tasks, number of important tasks, number of due soon tasks
def display_tags(args=None):
csv_in = open(filename)
reader = csv.DictReader(csv_in)
res = {}
for row in reader:
tags = _csvlist(row['tags'])
time_spent = row['time_spent'];
if tags:
for t in tags:
if t in res:
r = res[t]
r['count'] += 1
if _isImportant(row['important']):
r['important'] += 1
if _isDue(row['due']):
r['due'] += 1
r['time'] += _csvint(row['time_spent'])
else:
due = 1 if _isDue(row['due']) else 0
important = 1 if _isImportant(row['important']) else 0
res[t] = {
'count': 1,
'important': important,
'due': due,
'time': _csvint(row['time_spent'])
}
if texttable_available:
table = texttable.Texttable()
table.header(['tag', 'tasks', 'important', 'due soon', 'time'])
table.set_chars([' ',' ',' ','-'])
table.set_deco(table.HEADER | table.VLINES)
for r in res:
name = r;
r = res[r];
table.add_row([name, r['count'], r['important'], r['due'], _deltatime(r['time'])])
print
print table.draw()
print
else:
print
for r in res:
name = r;
r = res[r];
print '+'+name+' \t\t[ count: '+str(r['count'])+', important: '+str(r['important'])+', due: '+str(r['due'])+' ]';
print
# display all tasklists and number of tasks, number of important tasks, number of due soon tasks
def display_tasklists(args=None):
csv_in = open(filename)
reader = csv.DictReader(csv_in)
res = {}
for row in reader:
if row['tasklist'] in res:
r = res[row['tasklist']]
r['count'] += 1
if _isImportant(row['important']):
r['important'] += 1
if _isDue(row['due']):
r['due'] += 1
if row['time_spent'] and row['time_spent'] != '':
r['time'] += _csvint(row['time_spent'])
else:
due = 1 if _isDue(row['due']) else 0
important = 1 if _isImportant(row['important']) else 0
res[row['tasklist']] = {
'count': 1,
'important': important,
'due': due,
'time': _csvint(row['time_spent'])
}
if texttable_available:
table = texttable.Texttable()
table.header(['list', 'tasks', 'important', 'due soon', 'time'])
table.set_chars([' ',' ',' ','-'])
table.set_deco(table.HEADER | table.VLINES)
for r in res:
name = r;
r = res[r];
table.add_row([name, r['count'], r['important'], r['due'], _deltatime(r['time'])])
#print '@'+name+' ('+str(r['count'])+') important: '+str(r['important'])+', due: '+str(r['due']);
print
print table.draw()
print
else:
print
for r in res:
name = r;
r = res[r];
print '@'+name+' \t\t[ count: '+str(r['count'])+', important: '+str(r['important'])+', due: '+str(r['due'])+' ]';
print
def _isImportant(string):
if not string or string == '0':
return False
return True
def _isDue(string):
if not string or string == '0' or string == 'later':
return False
#if string or string == '1' or string == 'soon':
return True
#TODO: compare due time with current time and by configuration treshold decide if it is soon or later
def _print(num, row, details=False):
if not details:
tl = ' @'+row['tasklist'] if row['tasklist'] and row['tasklist'].strip() != '' else '';
print str(num) + ' ' +row['task']+tl
else:
tags = _csvlist(row['tags'])
if tags:
tags = ', '.join(tags)
else:
tags = ''
time = _deltatime(row['time_spent'])
details.add_row([num, row['task'], 'o' if int(row['important']) else '', 'o' if int(row['due']) else '', row['tasklist'], tags, time]);
def parseQuery(s):
pat_tl = re.compile(r"@([^@\s\-\+]+)\s?")
pat_tg = re.compile(r"\+([^@\s\-\+]+)\s?")
pat_i = re.compile(r"unimportant|important")
pat_d = re.compile(r"later|soon")
res = {}
tasklists = pat_tl.findall(s)
if tasklists:
res['tasklists'] = tasklists
s = re.sub(pat_tl, '', s)
tags = pat_tg.findall(s)
if tags:
res['tags'] = tags
s = re.sub(pat_tg, '', s)
i = pat_i.findall(s)
if i:
if i[0] == 'important':
res['important'] = True
else:
res['important'] = False
d = pat_d.findall(s)
if d:
if d[0] == 'soon':
res['due'] = True
else:
res['due'] = False
return res
def query(q, reader):
res = []
count = 1
for row in reader:
tags = _csvlist(row['tags'])
tasklist = row['tasklist']
def check(arr1, arr2):
if arr1 and arr2:
for a1 in arr1:
for a2 in arr2:
if a1 == a2:
return True
return False
tags_flag = True
if 'tags' in q:
tags_flag = check(tags, q['tags'])
tasklists_flag = True
if 'tasklists' in q:
tasklists_flag = check([tasklist], q['tasklists'])
i_flag = True
if 'important' in q:
i_flag = _isImportant(row['important']) == q['important']
d_flag = True
if 'due' in q:
d_flag = _isDue(row['due']) == q['due']
row['count'] = count
if tags_flag and tasklists_flag and i_flag and d_flag:
res.append(row)
count += 1
return res
#TODO: Complex queries, query tag, important and/or due inside a tasklist or a tag
def display(args=None, details=False):
csv_in = open(filename)
reader = csv.DictReader(csv_in)
if not texttable_available:
details = False
if details:
details = texttable.Texttable()
details.header(['id', 'task', '*', '!', 'task list', 'tags', 'time'])
regular = True
if args:
q = parseQuery(args)
if q:
regular = False
rows = query(q, reader)
if not details:
print
for row in rows:
_print(row['count'], row, details)
if not details:
print
if regular:
count = 1
if not details:
print
for row in reader:
_print(count, row, details)
count += 1
if not details:
print
if details:
details.set_cols_width([3, 30, 1, 1, 10, 8, 8])
details.set_deco(details.HEADER | details.VLINES)
details.set_chars([' ',' ',' ','-'])
print
print details.draw()
print
csv_in.close
def display_detailed(args=None):
display(args, True);
# deletes a task
def delete(num):
csv_in = open(filename)
csv_out = open(tmp_filename, 'w')
reader = csv.DictReader(csv_in)
reader = list(reader)
num = _parsenum(num)
writer = csv.DictWriter(csv_out, fieldnames=fieldnames)
writer.writeheader()
count = 1
for row in reader:
flag = True
for i in range(0, len(num)):
if count is num[i]:
flag = False
break
if flag:
writer.writerow(row)
count += 1
csv_in.close
csv_out.close
os.rename(tmp_filename, filename)
# on completion a task is moved to the other file todo_complete.csv where it's stored for later mining
def complete(arg):
pass
#task time tracking
def track(num):
#TODO: log of times per day / statistics
global _TIME, _SPENT_TIME
_TIME = _get(num, 'time_spent')
cdif = time.time();
if _TIME is '':
_TIME = time.time()
else:
_TIME = time.time() - int(_TIME)
def timed_output(st, delay):
while True:
_uprint(_deltatime(time.time() - st))
time.sleep(delay)
delay = 1
t = threading.Thread(target = timed_output, args = (_TIME, delay))
print
t.daemon = True
t.start()
try:
while True:
time.sleep(delay)
except KeyboardInterrupt:
_TIME = time.time() - _TIME
_set(num, 'time_spent', int(_TIME))
logtime(timefile, int(time.time() - cdif), num)
_TIME = 0
def _filepath(filename):
return os.path.join(destination_dir, filename)
def _tmppath(filename):
return os.path.join(destination_dir, 'tmp_'+filename)
def logtime(filename, sec, taskid):
path = _filepath(filename)
taskid = int(taskid)
task = _get(taskid)
fieldnames = ['time', 'start_timestamp', 'task', 'tasklist', 'day', 'month', 'year', 'end_timestamp', 'task_creation_timestamp', 'tags']
ctime = _UTCTimestamp()
log = {}
log['time'] = sec
log['end_timestamp'] = ctime
log['start_timestamp'] = ctime - sec
log['task'] = task['task']
log['tasklist'] = task['tasklist']
log['tags'] = task['tags']
log['task_creation_timestamp'] = task['created']
d = date.fromtimestamp(_UTC2LocalTimestamp(log['start_timestamp']))
log['day'] = d.day
log['month'] = d.month
log['year'] = d.year
if not os.path.exists(path):
csv_out = open(path, 'w')
writer = csv.DictWriter(csv_out, fieldnames=fieldnames)
writer.writeheader()
writer.writerow(log)
csv_out.close
else:
tmp = _tmppath(filename)
csv_in = open(path)
csv_out = open(tmp, 'w')
reader = csv.DictReader(csv_in)
writer = csv.DictWriter(csv_out, fieldnames=fieldnames)
writer.writeheader()
count = 1
for row in reader:
writer.writerow(row)
count += 1
writer.writerow(log)
csv_in.close
csv_out.close
os.rename(tmp, path)
def addtime(): #add hours to hours spent
pass
def settime(): #replaces time spent with user given value
pass
#set due of the task, for now only true and false
def due(num):
#TODO: today, tomorrow, two weeks, two days, someday
num = _parsenum(num, -1)
res = _get(num, 'due')
for i in range(0, len(num)):
if res[i] is '':
res[i] = 0
else:
res[i] = int(res[i])
if res[i] is 0:
res[i] +=1
print 'Task {0} set to due soon'.format(num[i]+1)
else:
res[i] -=1
print 'Task {0} set to due later'.format(num[i]+1)
_set(num, 'due', res)
#task importance toggle
def important(num):
num = _parsenum(num, -1)
res = _get(num, 'important')
for i in range(0, len(num)):
if res[i] is '':
res[i] = 0
else:
res[i] = int(res[i])
if res[i] is 0:
res[i] +=1
print 'Task {0} set to important'.format(num[i]+1)
else:
res[i] -=1
print 'Task {0} set to unimportant'.format(num[i]+1)
_set(num, 'important', res)
#asigns a task to a task list / project
def tasklist(args):
pts = args.split(' ', 2)
if len(pts) < 1:
_err('tasklist option requires 2 parameters, first the task id/ids, second tasklist name', 'invalid argument')
return
if len(pts) > 1:
pts[1] = re.sub(r"@",'',pts[1])
else:
pts.insert(1, '')
num = _parsenum(pts[0], -1)
_set(num, 'tasklist', pts[1], False)
#assigns tags to the task, add tags to a task list, remove the tags, etc..
def tag(args):
pts = args.split(' ', 1)
if len(pts) < 2:
_err('tag option requires 2 parameters, first the task id, second tag names separated by space', 'invalid argument')
return
pts[1] = re.sub(r"\+",'',pts[1])
ntags = pts[1].split(' ')
num = _parsenum(pts[0], -1)
tags = _get(num, 'tags')
result = []
nntags = set(ntags)
for i in range(0, len(num)):
if tags[i]:
tg = _csvlist(tags[i])
if tg:
otags = set(tg)
result.append(tg + list(nntags-otags))
else:
result.append(ntags)
else:
result.append(ntags)
_set(num, 'tags', result)
# removes given tags separated by space beginning with +
def rmtag(args):
pts = args.split(' ', 1)
if len(pts) < 2:
_err('rmtag option requires 2 parameters, first the task id, second tag names separated by space','invalid argument')
return
pts[1] = re.sub(r"\+",'',pts[1])
ntags = pts[1].split(' ')
num = _parsenum(pts[0], -1)
tags = _get(num, 'tags')
result = []
nntags = set(ntags)
for i in range(0, len(num)):
if tags[i]:
tg = _csvlist(tags[i])
if tg:
otags = set(tg)
result.append(list(otags-nntags))
else:
result.append('')
else:
result.append('')
_set(num, 'tags', result)
# imports a CSV
def imprt():
#maybe parses your code for TODO: comments displays line number in task text
pass
def _err(string, code=None):
code = ' ['+code+']' if code else ''
print 'Error{1}: {0}'.format(string, code)
# edits task by a given id, asks user to dubmit the new title
def edit(args):
if not args:
_err('You need to pass an ID of a task you would like to edit', 'invalid argument')
pass
pat_edit = re.compile(r"^([1-9]{1}[0-9]*|last)\s(.*)")
mo = pat_edit.match(args)
if mo:
num = _parsenum(mo.group(1), -1)
task = mo.group(2).strip()
if task:
_set(num, 'task', task, False)
else:
_err('Task text cannot be empty','invalid argument')
else:
_err('Pass an integer ID of the task you wish to edit and task text separated by space', 'invalid argument')
def show(args):
#shows a single task with all the details
pass
# adds a new task
def new(args):
new_task = {'created': _UTCTimestamp(),
'important': 0,
'due': 0}
#check task for @tasklist #tag #tag
pat_all_tl = re.compile(r"^@[^@\s\-]+\s?|\s@[^@\s\-]+\s?")
pat_all_tg = re.compile(r"\s?\+[^\+\s\-]+\s?")
def tlrepl(mo):
if mo.group(0).startswith(' ') and mo.group(0).endswith(' '): return ' '
else: return ''
tl = pat_all_tl.findall(args)
if tl:
args = re.sub(pat_all_tl, tlrepl, args)
new_task['tasklist'] = re.sub(r"[\s@]", '', tl[0])
tg = pat_all_tg.findall(args)
if tg:
args = re.sub(pat_all_tg, tlrepl, args)
for i in range(0,len(tg)):
tg[i] = re.sub(r"[\s\+]", '', tg[i])
new_task['tags'] = tg
new_task['task'] = args.strip()
if not os.path.exists(filename):
csv_out = open(filename, 'w')
writer = csv.DictWriter(csv_out, fieldnames=fieldnames)
writer.writeheader()
writer.writerow(new_task)
print 'Added task 1'
csv_out.close
else:
csv_in = open(filename)
csv_out = open(tmp_filename, 'w')
reader = csv.DictReader(csv_in)
writer = csv.DictWriter(csv_out, fieldnames=fieldnames)
writer.writeheader()
count = 1
for row in reader:
writer.writerow(row)
count += 1
print 'Added task ' + str(count)
writer.writerow(new_task)
csv_in.close
csv_out.close
os.rename(tmp_filename, filename)
# prints a help text
def help(args=None):
print '''
TODO - CLI task manager with time tracking
<http://todotron.com>
Usage: todo ...TITLE...[@TASKLIST][+TAG]
Add a new task, it can have one task list and multiple tags
todo
Lists all the tasks
todo [OPTION] ...PARAMS...
Performs an option
-l, --list [TASKLIST [TAG]] lists all tasks, or all task within a list or with a tag
-a, --details [TASKLIST [TAG]] lists all tasks with details
-r, --remove ID[,ID] removes a task by a given task ID
-c, --complete ID[,ID] completes a task by a given ID
-t, --track ID time track single task, exit keyboard interupt
-e, --edit ID edit the text of a single task
-i, --important ID[,ID] toggles the important state
-d, --due ID[,ID] toggles the due state (soon / later)
-T, --tasklist ID[,ID] [TASKLIST] adds tasks to a tasklist
--tag ID[,ID] TAG[ TAG] adds tags to tasks
--rmtag ID[,ID] TAG[ TAG] removes existing tags from tasks
--tasklists lists all task lists
--tags lists all tags
-h, --help displays this help
-v, --version displays version
'''
#TODO: uninstall
#TODO: update
def vers(args=None):
print 'version: ' + version
# Connects commands with real functions
fn = {
'r': delete,
'remove': delete,
's': show,
'show': show,
'v': vers,
'version': vers,
'e': edit,
'edit': edit,
'd': due,
'due': due,
'c': complete,
'complete': complete,
'l': display,
'list': display,
't': track,
'track': track,
'i': important,
'important': important,
'T': tasklist,
'tasklist': tasklist,
'tag': tag,
'rmtag': rmtag,
'h': help,
'help': help,
'tasklists': display_tasklists,
'tags': display_tags,
'a': display_detailed,
'details': display_detailed
}
# parse commands passed as arguments
m = pat_cmds.findall(args)
if m:
#execute commands
for cmd in m:
c = pat_sepcmd.search(cmd.strip())
if c:
dashes = c.group(1)
command = c.group(2)
value = c.group(3)
if len(dashes) is 1:
for cm in command:
_execute(cm, value)
else:
_execute(command, value)
else:
#add new todo
if args.strip() == '':
display()
else:
new(args)