forked from sigmavirus24/Todo.txt-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.py
executable file
·1129 lines (940 loc) · 36.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
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# TODO.TXT-CLI-python
# Copyright (C) 2011-2012 Sigmavirus24
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
# TLDR: This is licensed under the GPLv3. See LICENSE for more details.
import os
import re
import sys
from optparse import OptionParser
from datetime import datetime, date
VERSION = "development"
REVISION = "$Id$"
try:
import readline
except ImportError:
# This isn't crucial to the execution of the script.
# But it is a nice feature to have. Sucks to be an OSX user.
pass
try:
# Python 3 moved the built-in intern() to sys.intern()
intern = sys.intern
except AttributeError:
pass
try:
input = raw_input
except NameError:
# Python 3 renamed raw_input to input
pass
try:
from string import uppercase
except ImportError:
# Python 3 again
from string import ascii_uppercase as uppercase
if os.name == "nt":
try:
from colorama import init
init()
except Exception:
pass
# colorama provides ANSI -> win32 color support
# If they don't have it, no worries.
PRIORITIES = uppercase[:24]
# concat() is necessary long before the grouping of function declarations
concat = lambda str_list, sep='': sep.join([str(i) for i in str_list])
_path = lambda p: os.path.abspath(os.path.expanduser(p))
_pathc = lambda plist: _path(concat(plist))
TERM_COLORS = {
"black": "\033[0;30m", "red": "\033[0;31m",
"green": "\033[0;32m", "brown": "\033[0;33m",
"blue": "\033[0;34m", "purple": "\033[0;35m",
"cyan": "\033[0;36m", "light grey": "\033[0;37m",
"dark grey": "\033[1;30m", "light red": "\033[1;31m",
"light green": "\033[1;32m", "yellow": "\033[1;33m",
"light blue": "\033[1;34m", "light purple": "\033[1;35m",
"light cyan": "\033[1;36m", "white": "\033[1;37m",
"default": "\033[0m", "reverse": "\033[7m",
"bold": "\033[1m",
}
TODO_DIR = _path("~/.todo")
CONFIG = {
"TODO_DIR": TODO_DIR,
"TODOTXT_DEFAULT_ACTION": "list",
"TODOTXT_CFG_FILE": _pathc([TODO_DIR, "/config"]),
"TODO_FILE": _pathc([TODO_DIR, "/todo.txt"]),
"DONE_FILE": _pathc([TODO_DIR, "/done.txt"]),
"TMP_FILE": "",
"REPORT_FILE": "",
"USE_GIT": False,
"PLAIN": False,
"NO_PRI": False,
"PRE_DATE": False,
"INVERT": False,
"HIDE_PROJ": False,
"HIDE_CONT": False,
"HIDE_DATE": False,
"LEGACY": False,
"ACTIONS": None,
}
for p in PRIORITIES:
CONFIG["PRI_{0}".format(p)] = "default"
del(p, TODO_DIR)
### Helper Functions
def todo_padding(include_done=False):
lines = [line for line in iter_todos(include_done)]
i = len(lines)
pad = 1
while i >= 10:
pad += 1
i /= 10
return pad
def iter_todos(include_done=False):
"""Opens the file in read-only mode; returns an iterator for the todos."""
files = [CONFIG["TODO_FILE"]]
if not os.path.isfile(files[0]):
return
if include_done and os.path.isfile(CONFIG["DONE_FILE"]):
files.append(CONFIG["DONE_FILE"])
for f in files:
with open(f) as fd:
for line in fd:
yield line
def separate_line(number):
"""Takes an integer and returns a string and a list. The string is
the item at that position in the list. The list is the rest of the todos.
If the todo.txt file is empty, separate = lines = None. If the number is
invalid separate = None, lines != None."""
lines = [line for line in iter_todos()]
separate = None
if lines and number - 1 < len(lines) and 0 <= number - 1:
separate = lines.pop(number - 1)
return separate, lines
def rewrite_file(fd, lines):
"""Simple wrapper for three lines used all too frequently. Sets the access
position to the beginning of the file, truncates the file's length to 0 and
then writes all the lines to the file."""
fd.seek(0, 0)
fd.truncate(0)
fd.writelines(lines)
def rewrite_and_post(line_no, old_line, new_line, lines):
"""Wrapper for frequently used semantics for "post-production"."""
with open(CONFIG["TODO_FILE"], "w") as fd:
rewrite_file(fd, lines)
post_success(line_no, old_line, new_line)
def usage(*args):
"""Set the usage string printed out in ./todo.py help."""
def usage_decorator(func):
"""Function that actually sets the usage string."""
func.__usage__ = concat(args, '\n').expandtabs(3)
return func
return usage_decorator
def _git_err(g):
"""Print any errors that result from GitPython and exit."""
if g.stderr:
print(g.stderr)
else:
print(g)
sys.exit(g.status)
@usage('\tpull', '\t\tPulls from your remote git repository.\n')
def _git_pull():
"""Equivalent to running git pull on the command line."""
try:
print(CONFIG["GIT"].pull())
except git.exc.GitCommandError as g:
_git_err(g)
@usage('\tpush', '\t\tPushes to your remote git repository.\n')
def _git_push():
"""Push commits made locally to the remote."""
try:
s = CONFIG["GIT"].push()
except git.exc.GitCommandError as g:
_git_err(g)
if s:
print(s)
else:
print("TODO: 'git push' executed.")
@usage('\tstatus',
'\t\t"git status" of the repository containing your todo files.',
'\t\tRequires git version 1.7.4 or newer.\n')
def _git_status():
"""Print the status of the local repository if the version of git is 1.7
or later."""
if CONFIG["GIT"].version_info >= (1, 7, 3):
print(CONFIG["GIT"].status())
else:
print("status only works for git version 1.7.4 or higher.")
@usage('\tlog', '\t\tShows the last five commits in your repository.\n')
def _git_log():
"""Print the two latest commits in the local repository's log."""
print(CONFIG["GIT"].log("-5", "--oneline"))
def _git_commit(files, message):
"""Make a commit to the git repository.
files -- should be an iterable like ['file_a', 'file_b'] or ['-a']"""
if len(message) > 49:
message = concat([message[:45], "...\n\n", message])
try:
CONFIG["GIT"].commit(files, "-m", message)
except git.exc.GitCommandError as g:
_git_err(g)
committed = CONFIG["TODO_DIR"] if "-a" in files else concat(files, ", ")
print(concat(["TODO: ", committed, " archived."]))
def prompt(*args, **kwargs):
"""Sanitize input collected with raw_input().
Prevents someone from entering 'y\' to attempt to break the program.
args -- can be any collection of strings that require formatting.
kwargs -- will collect the tokens and values."""
args = list(args) # [a for a in args]
args.append(' ')
prompt_str = concat(args).format(**kwargs)
raw = input(prompt_str)
return re.sub(r"\\", "", raw)
def print_x_of_y(x, y):
t_str = "--\nTODO: {0} of {1} tasks shown"
if len(x) > len(y): # EXTREMELY hack-ish
print(t_str.format(len(y), len(y))) # There can't logically be
# more lines of items to do than there actually are.
else:
print(t_str.format(len(x), len(y)))
def test_separated(removed, lines, line_no):
if not (removed or lines):
print("{0}: No such todo.".format(line_no))
return True
return False
### End Helper Functions
### Configuration Functions
def _iter_actual_lines_(config_file):
"""Return only the actual lines of the config file. This skips commented or
blank lines."""
skip_re = re.compile('^\s*(#|$)')
with open(config_file, 'r') as f:
for line in f:
if not skip_re.match(line):
yield line
def get_config(config_name="", dir_name=""):
"""Read the config file"""
if dir_name:
dir_name = _path(dir_name)
CONFIG["TODO_DIR"] = dir_name
CONFIG["TODOTXT_CFG_FILE"] = _pathc([dir_name, "/config"])
CONFIG["TODO_FILE"] = _pathc([dir_name, "/todo.txt"])
CONFIG["DONE_FILE"] = _pathc([dir_name, "/done.txt"])
if config_name:
CONFIG["TODOTXT_CFG_FILE"] = _path(config_name)
os.environ["TODO_DIR"] = CONFIG["TODO_DIR"]
if CONFIG["TODOTXT_CFG_FILE"]:
config_file = CONFIG["TODOTXT_CFG_FILE"]
config_file = _path(config_file)
perms = os.F_OK | os.R_OK | os.W_OK
if not (os.access(CONFIG["TODO_DIR"], perms | os.X_OK) and \
os.access(config_file, perms)) and \
not config_name:
default_config()
else:
strip_re = re.compile('\w+\s([A-Za-z_$="./01]+).*')
pri_re = re.compile('(PRI_[A-X]|DEFAULT)')
for line in _iter_actual_lines_(config_file):
# Extract VAR=VAL and then split VAR and VAL
var = strip_re.sub('\g<1>', line.strip()).split('=')
var[1] = var[1].strip('"')
if var[1] in ("True", "1"):
CONFIG[var[0]] ^= True
elif var[1] in ("False", "0"):
CONFIG[var[0]] ^= False
elif pri_re.match(var[0]):
CONFIG[var[0]] = var[1].strip('$').lower().replace('_', ' ')
else:
CONFIG[var[0]] = os.path.expandvars(var[1])
# make expandvars work for our vars too
os.environ[var[0]] = str(CONFIG[var[0]])
if CONFIG["USE_GIT"]:
if not __import_git__():
return
CONFIG["GIT"] = git.Git(CONFIG["TODO_DIR"])
tracked_files = set(CONFIG["GIT"].ls_files().split())
i = CONFIG["TODOTXT_CFG_FILE"].rfind('/') + 1
if CONFIG["TODOTXT_CFG_FILE"][i:] not in tracked_files:
CONFIG["GIT"].add([CONFIG["TODOTXT_CFG_FILE"][i:]])
def __import_git__():
git_functions()
global git
try:
import git
except ImportError:
if sys.version_info < (3, 0):
print("You must download and install GitPython from: \
http://pypi.python.org/pypi/GitPython")
else:
print("GitPython is not available for Python3 last I checked.")
CONFIG["USE_GIT"] = False
return False
return True
def git_functions():
global repo_config
def repo_config():
"""Help the user configure their git repository."""
from os import getenv
g = CONFIG["GIT"]
# local configuration
try:
user_name = g.config("--global", "--get", "user.name")
except:
user_name = getuser()
try:
user_email = g.config("--global", "--get", "user.email")
except:
user_email = concat([user_name, "@", getenv("HOSTNAME")])
print("First configure your local repository options.")
ret = prompt("git config user.name ", user_name, "?")
if ret:
user_name = ret
ret = prompt("git config user.email ", user_email, "?")
if ret:
user_email = ret
g.config("user.name", user_name)
g.config("user.email", user_email)
# remote configuration
ret = prompt("Would you like to add a remote?")
yes_re = re.compile("y(?:es)?", re.I)
if yes_re.match(ret):
remote_host = None
remote_path = None
remote_user = None
remote_branch = None
def __while_prompt__(prompt_str, error_string):
ret = None
while not ret:
ret = prompt(prompt_str)
if not ret:
print(error_string)
return ret
remote_host = __while_prompt__("Remote hostname:",
"Please enter the remote's hostname.")
remote_path = __while_prompt__("Remote path:",
"Please enter the path to the remote's repository.")
remote_path = __while_prompt__("Remote user:",
"Please enter the user on the remote machine.")
remote_branch = __while_prompt__("Remote branch:",
"Please enter the branch to push to on the remote machine.")
prompt("Press enter when you have initialized a bare\n",
" repository on the remote or are ready to proceed.")
local_branch = git.Repo(CONFIG["TODO_DIR"]).heads[0].name
if not local_branch:
local_branch = "master"
g.remote("add", "origin", concat([remote_user, "@", remote_host,
":", remote_path]))
g.config(concat(["branch.", local_branch, ".remote"]), "origin")
g.config(concat(["branch.", local_branch, ".merge"]),
concat(["refs/heads/", remote_branch]))
def default_config():
"""Set up the default configuration file."""
def touch(filename):
"""Create files if they aren't already there."""
open(filename, "w").close()
if not os.path.exists(CONFIG["TODO_DIR"]):
os.makedirs(CONFIG["TODO_DIR"])
# touch/create files needed for the operation of the script
for item in ['TODO_FILE', 'DONE_FILE', 'REPORT_FILE']:
if CONFIG[item]:
touch(CONFIG[item])
cfg = open(concat([CONFIG["TODO_DIR"], "/config"]), 'w')
# set the defaults for the colors
CONFIG["PRI_A"] = "yellow"
CONFIG["PRI_B"] = "green"
CONFIG["PRI_C"] = "light blue"
TO_CONFIG = {True: 1, False: 0}
for key in TERM_COLORS.keys():
bkey = concat(["$", key.replace(' ', '_').upper()])
TO_CONFIG[key] = bkey
val = prompt("Would you like to use git to manage\n ",
CONFIG["TODO_DIR"], "? [y/N]")
yes_re = re.compile('y(?:es)?', re.I)
if yes_re.match(val):
CONFIG["USE_GIT"] = True
for k, v in CONFIG.items():
if k != "GIT":
if v in TO_CONFIG.keys():
cfg.write("export {0}={1}\n".format(k, TO_CONFIG[v]))
else:
cfg.write("export {0}=\"{1}\"\n".format(k, str(v)))
if CONFIG["USE_GIT"]:
if not __import_git__():
sys.exit(0)
CONFIG["GIT"] = git.Git(CONFIG["TODO_DIR"])
try:
repo = git.Repo(CONFIG["TODO_DIR"])
except git.exc.InvalidGitRepositoryError:
val = prompt("Would you like to create a new git repository in\n ",
CONFIG["TODO_DIR"], "? [y/N]")
if yes_re.match(val):
print(CONFIG["GIT"].init())
val = prompt("Would you like {prog} to help\n you",
" configure your new git repository? [y/n]",
prog=CONFIG["TODO_PY"])
if yes_re.match(val):
repo_config()
files = [CONFIG["TODOTXT_CFG_FILE"], CONFIG["TODO_FILE"]]
for setting in ["TMP_FILE", "DONE_FILE", "REPORT_FILE"]:
if CONFIG[setting]:
files.append(CONFIG[setting])
CONFIG["GIT"].add(files)
CONFIG["GIT"].commit("-m", concat(['"', CONFIG["TODO_PY"],
" initial commit.\""]))
else:
val = prompt("Would you like {prog} to clone\n a",
" remote repository for you? [y/N]",
prog=CONFIG["TODO_PY"])
if yes_re.match(val):
from shutil import rmtree
rmtree(CONFIG["TODO_DIR"])
val = prompt("Please enter user@remote:/path/to/repo.")
git.Repo.clone_from(val, CONFIG["TODO_DIR"])
cfg.close()
print(concat(["Default configuration completed. Please ",
"re-run\n ", CONFIG["TODO_PY"], " with '-h' and 'help' separately."]))
sys.exit(0)
### End Config Functions
### New todo Functions
@usage('\tadd | a "Item to do +project @context #{yyyy-mm-dd}"',
concat(["\t\tAdds 'Item to do +project @context #{yyyy-mm-dd}'",
"to your todo.txt"], ' '), "\t\tfile.",
"\t\t+project, @context, #{yyyy-mm-dd} are optional\n")
def add_todo(args):
"""Add a new item to the list of things todo."""
if str(args) == args:
line = args
elif len(args) >= 1:
line = concat(args, " ")
else:
line = prompt("Add:")
prepend = CONFIG["PRE_DATE"]
l = len([1 for l in iter_todos()]) + 1
pri_re = re.compile('(\([A-X]\))')
if pri_re.match(line) and prepend:
line = pri_re.sub(concat(["\g<1>",
datetime.now().strftime(" %Y-%m-%d ")]), line)
elif prepend:
line = concat([datetime.now().strftime("%Y-%m-%d "), line])
with open(CONFIG["TODO_FILE"], "a") as fd:
fd.write(concat([line, "\n"]))
s = "TODO: '{0}' added on line {1}.".format(line, l)
print(s)
if CONFIG["USE_GIT"]:
_git_commit([CONFIG["TODO_FILE"]], s)
@usage('\taddm "First item to do +project @context #{yyyy-mm-dd}',
'\t\tSecond item to do +project @context #{yyyy-mm-dd}',
'\t\t...', '\t\tLast item to do +project @context #{yyyy-mm-dd}',
'\t\tAdds each line as a separate item to your todo.txt file.\n')
def addm_todo(args):
"""Add new items to the list of things todo."""
if str(args) == args:
lines = args
else:
lines = concat(args, " ")
lines = lines.split("\n")
list(map(add_todo, lines)) # Necessary for python 3000
### End new todo functions
### Start do/del functions
@usage('\tdo NUMBER',
'\t\tMarks item with corresponding number as done and moves it to',
'\t\tyour done.txt file.\n')
def do_todo(line):
"""Mark an item on a specified line as done."""
if not line.isdigit():
print("Usage: {0} do item#".format(CONFIG["TODO_PY"]))
else:
removed, lines = separate_line(int(line))
if test_separated(removed, lines, line):
return
fd = open(CONFIG["TODO_FILE"], "w")
rewrite_file(fd, lines)
fd.close()
today = datetime.now().strftime("%Y-%m-%d")
removed = concat(["x", today,
re.sub("\([A-X]\)\s?", "", removed)], " ")
files = [CONFIG["TODO_FILE"]]
if CONFIG["DONE_FILE"]:
with open(CONFIG["DONE_FILE"], "a") as fd:
fd.write(removed)
files.append(CONFIG["DONE_FILE"])
print(removed[:-1])
print("TODO: Item {0} marked as done.".format(line))
if CONFIG["USE_GIT"]:
_git_commit(files, removed)
@usage('\tdel | rm NUMBER', '\t\tDeletes the item on line NUMBER in todo.txt',
'')
def delete_todo(line):
"""Delete an item without marking it as done."""
if not line.isdigit():
print("Usage: {0} (del|rm) item#".format(CONFIG["TODO_PY"]))
else:
removed, lines = separate_line(int(line))
if test_separated(removed, lines, line):
return
with open(CONFIG["TODO_FILE"], "w") as fd:
rewrite_file(fd, lines)
removed = "'{0}' deleted.".format(removed[:-1])
print(removed)
print("TODO: Item {0} deleted.".format(line))
if CONFIG["USE_GIT"]:
_git_commit([CONFIG["TODO_FILE"]], removed)
### End do/del Functions
### Post-production todo functions
def post_error(command, arg1, arg2):
"""If one of the post-production todo functions isn't given the proper
arguments, the function calls this to notify the user of what they need to
supply."""
if arg2:
print(concat(["'", CONFIG["TODO_PY"], " ", command, "' requires a(n) ",
arg1, " then a ", arg2, "."]))
else:
print(concat(["'", CONFIG["TODO_PY"], " ", command, "' requires a(n) ",
arg1, "."]))
def post_success(item_no, old_line, new_line):
"""After changing a line, pring a standard line and commit the change."""
old_line = old_line.rstrip()
new_line = new_line.rstrip()
print_str = "TODO: Item {0} changed from '{1}' to '{2}'.".format(
item_no, old_line, new_line)
print(print_str)
if CONFIG["USE_GIT"]:
_git_commit([CONFIG["TODO_FILE"]], print_str)
@usage('\tappend | app NUMBER "text to append"',
'\t\tAppend "text to append" to item NUMBER.\n')
def append_todo(args):
"""Append text to the item specified."""
if args[0].isdigit():
line_no = int(args.pop(0))
old_line, lines = separate_line(line_no)
if test_separated(old_line, lines, line_no):
return
new_line = concat([concat([old_line[:-1],
concat(args, " ")], " "), "\n"])
lines.insert(line_no - 1, new_line)
rewrite_and_post(line_no, old_line, new_line, lines)
else:
post_error('append', 'NUMBER', 'string')
@usage('\tpri | p NUMBER [A-X]',
'\t\tAdd priority specified (A, B, C, etc.) to item NUMBER.\n')
def prioritize_todo(args):
"""Add or modify the priority of the specified item."""
args = [arg.upper() for arg in args]
if args[1:] and args[0].isdigit()\
and len(args[1]) == 1 and args[1] in PRIORITIES:
line_no = int(args.pop(0))
old_line, lines = separate_line(line_no)
if test_separated(old_line, lines, line_no):
return
new_pri = concat(["(", args[0], ") "])
r = re.match("(\([A-X]\)\s).*", old_line)
if r:
new_line = re.sub(re.escape(r.groups()[0]), new_pri, old_line)
else:
new_line = concat([new_pri, old_line])
lines.insert(line_no - 1, new_line)
rewrite_and_post(line_no, old_line, new_line, lines)
else:
post_error('pri', 'NUMBER', 'capital letter in [A-X]')
@usage('\tdepri | dp NUMBER',
'\t\tRemove the priority of the item on line NUMBER.\n')
def de_prioritize_todo(number):
"""Remove priority markings from the beginning of the line if they're
there. Don't complain otherwise."""
if number.isdigit():
number = int(number)
old_line, lines = separate_line(number)
if test_separated(old_line, lines, number):
return
new_line = re.sub("(\([A-X]\)\s)", "", old_line)
lines.insert(number - 1, new_line)
rewrite_and_post(number, old_line, new_line, lines)
else:
post_err('depri', 'NUMBER', None)
@usage('\tprepend | pre NUMBER "text to prepend"',
'\t\tAdd "text to prepend" to the beginning of the item.\n')
def prepend_todo(args):
"""Take in the line number and prepend the rest of the arguments to the
item specified by the line number."""
if args[0].isdigit():
line_no = int(args.pop(0))
prepend_str = concat(args, " ") + " "
old_line, lines = separate_line(line_no)
if test_separated(old_line, lines, line_no):
return
pri_re = re.compile('^(\([A-X]\)\s)')
if pri_re.match(old_line):
new_line = pri_re.sub(concat(["\g<1>", prepend_str]), old_line)
else:
new_line = concat([prepend_str, old_line])
lines.insert(line_no - 1, new_line)
rewrite_and_post(line_no, old_line, new_line, lines)
else:
post_error('prepend', 'NUMBER', 'string')
### End Post-production todo functions
### HELP
@usage('\thelp | h',
'\t\tDisplay this message and exit.\n')
def cmd_help():
print(concat(["Use", CONFIG["TODO_PY"], "-h for option help\n"], " "))
print(concat(["Usage:", CONFIG["TODO_PY"], "command [arg(s)]"], " "))
d = {}
for (key, val) in commands.items():
d[val[1]] = (key, val[1])
# By using the function, only one command name will be added
cmds = sorted(d.values()) # Only get the tuples
for (_, f) in cmds:
print(f.__usage__)
sys.exit(0)
### HELP
### List Printing Functions
def format_lines(color_only=False, include_done=False):
"""Take in a list of lines to do, return them formatted with the
TERM_COLORS and organized based upon priority."""
plain = CONFIG["PLAIN"]
no_priority = CONFIG["NO_PRI"]
default = CONFIG.get("DEFAULT", "default")
default = TERM_COLORS[default] if not plain else ""
invert = TERM_COLORS["reverse"] if CONFIG["INVERT"] else ""
pri_re = re.compile('^\(([A-W])\)\s')
category = ""
pad = todo_padding(include_done)
colors = set(TERM_COLORS.keys()) # Supposedly sets are faster for look-ups
formatted = []
if not color_only:
formatted = dict(zip(PRIORITIES, [[] for i in PRIORITIES]))
for (i, line) in enumerate(iter_todos(include_done)):
category = "X"
color = default
r = pri_re.match(line)
if r:
category = r.groups()[0]
color_name = CONFIG["PRI_{0}".format(category)]
if not plain and color_name in colors:
color = TERM_COLORS[color_name]
if no_priority:
line = pri_re.sub("", line)
i = str(i + 1).zfill(pad)
l = concat([color, invert, i, " ", line[:-1], default, "\n"])
if color_only:
formatted.append(l)
else:
formatted[category].append(l)
return formatted
def _legacy_sort(items):
"""Sort items alphabetically, i.e.
# (pri_a) Abc
# (pri_a) Bcd
# (pri_b) Abc
# (pri_c) Bcd
etc., etc., etc."""
line_re = re.compile('^.*\d+\s(\([A-X]\)\s)?')
# The .* in the regexp is needed for the \033[* codes
items = sorted([(line_re.sub("", i), i) for i in items])
items = [line for (k, line) in items]
return items
def _list_(by, regexp):
"""Master list_*() function."""
nonetype = concat(["no", by])
todo = {nonetype: []}
by_list = []
sorted = []
if by in ["date", "project", "context"]:
lines = format_lines(color_only=True)
regexp = re.compile(regexp)
for line in lines:
match = regexp.findall(line)
if match:
line = concat(["\t", line])
for i in match:
if by == "date":
i = date(int(i[0]), int(i[1]), int(i[2]))
if i not in by_list:
by_list.append(i)
todo[i] = [line]
else:
todo[i].append(line)
else:
todo[nonetype].append(line)
elif by == "pri":
lines = format_lines()
todo.update(lines)
by_list = list(PRIORITIES)
by_list.sort()
regstr = '(\+\w+\s?)' if CONFIG["HIDE_PROJ"] else ''
hide_proj_re = re.compile(regstr)
regstr = '(@\w+\s?)' if CONFIG["HIDE_CONT"] else ''
hide_cont_re = re.compile(regstr)
regstr = '(#\{\d+-\d+-\d+\}\s?)' if CONFIG["HIDE_DATE"] else ''
hide_date_re = re.compile(regstr)
for b in by_list:
todo[b] = [hide_proj_re.sub("", l) for l in todo[b]]
todo[b] = [hide_cont_re.sub("", l) for l in todo[b]]
todo[b] = [hide_date_re.sub("", l) for l in todo[b]]
if CONFIG["LEGACY"]:
todo[b] = _legacy_sort(todo[b])
if by != "pri":
sorted.append(concat([b, ":\n"]))
sorted.extend(todo[b])
sorted.extend(todo[nonetype])
return (lines, sorted)
def _list_by_(*args):
"""
Print lines matching items in args
Called when the user does:
todo.py ls search-term1 search-term2 ...
"""
esc = re.escape # keep line length down
relist = [re.compile(concat(["\s?(", esc(a), ")\s?"]), re.I) for a in args]
del(esc) # don't need it anymore
alines = format_lines() # Retrieves all lines.
lines = []
for p in PRIORITIES:
lines.extend(alines[p])
alines = lines[:]
matched_lines = []
for regexp in relist:
matched_lines = [line for line in lines if regexp.search(line)]
lines = matched_lines[:]
if lines:
print(concat(lines)[:-1])
print_x_of_y(lines, alines)
@usage('\tlist | ls',
'\t\tLists all items in your todo.txt file sorted by priority.\n')
def list_todo(args=None, plain=False, no_priority=False):
"""Print the list of todo items in order of priority and position in the
todo.txt file."""
if not args:
lines, sorted = _list_("pri", "")
print(concat(sorted)[:-1])
print_x_of_y(sorted, sorted)
else:
_list_by_(*args)
@usage('\tlistall | lsa',
'\t\tLists all items in your todo.txt file sorted by priority followed',
'\t\tby the items in your done.txt file.\n')
def list_all():
"""Print the list of todo items in order of priority and then print the
done.txt file."""
formatted = format_lines(include_done=True)
lines = []
for p in PRIORITIES:
lines.extend(formatted[p])
if lines:
print(concat(lines)[:-1])
print_x_of_y(lines, lines)
@usage('\tlistdate | lsd',
'\t\tLists all items in your todo.txt file sorted by date.\n')
def list_date():
"""List todo items by date #{yyyy-mm-dd}."""
lines, sorted = _list_("date", "#\{(\d{4})-(\d{1,2})-(\d{1,2})\}")
print(concat(sorted)[:-1])
print_x_of_y(sorted, lines)
@usage('\tlistproj | lsp',
'\t\tLists all items in your todo.txt file sorted by project title.\n')
def list_project():
"""Organizes items by project +prj they belong to."""
lines, sorted = _list_("project", "\+(\w+)")
print(concat(sorted)[:-1])
print_x_of_y(sorted, lines)
@usage('\tlistcon | lsc',
'\t\tLists all items in your todo.txt file sorted by context.\n')
def list_context():
"""Organizes items by context @context associated with them."""
lines, sorted = _list_("context", "@(\w+)")
print(concat(sorted)[:-1])
print_x_of_y(sorted, lines)
### End LP Functions
### Callback functions for options
def version(option, opt, value, parser):
print("""TODO.TXT Command Line Interface v{version}-{id}
First release:
Original conception by: Gina Trapani (http://ginatrapani.org)
Original version project: https://github.com/ginatrapani/todo.txt-cli/
Contributors to original: https://github.com/ginatrapani/todo.txt-cli/network
Python version: https://github.com/sigmavirus24/Todo.txt-python/
Contributors to python version: \
https://github.com/sigmavirus24/Todo.txt-python/network
License: GPLv3
Code repository: \
https://github.com/sigmavirus24/Todo.txt-python/tree/master
Running on {python} {pyversion}""".format(version=VERSION, id=REVISION,
python=sys.subversion[0], pyversion=concat(sys.version_info[:3],
'.')))
sys.exit(0)
def toggle_opt(option, opt_str, val, parser):
"""
Check opt_str to see if it's one of ['-+', '-@', '-#', '-p', '-P', '-t',
'--plain-mode', '--no-priority', '--prepend-date', '-i',
'--invert-colors'] and toggle that option in CONFIG.
"""
toggle_dict = {"-+": "HIDE_PROJ", "-@": "HIDE_CONT", "-#": "HIDE_DATE",
"-p": "PLAIN", "-P": "NO_PRI", "-t": "PRE_DATE",
"--plain-mode": "PLAIN", "--no-priority": "NO_PRI",
"--prepend-date": "PRE_DATE", "-i": "INVERT",
"--invert-colors": "INVERT", "-l": "LEGACY",
"--legacy": "LEGACY",
}
if opt_str in toggle_dict.keys():
k = toggle_dict[opt_str]
CONFIG[k] ^= True
### End callback functions
### Add-on functionality
def load_actions():
if CONFIG.get("TODO_ACTIONS_DIR"):
action_dir = CONFIG["TODO_ACTIONS_DIR"]
else:
action_dir = _pathc([CONFIG["TODO_DIR"], "/actions"])
actions = CONFIG["ACTIONS"].split(",")
if not (os.path.exists(action_dir) and any(actions)):
return
sys.path.insert(0, action_dir)
for action in actions:
try:
tmp = __import__(action)
if hasattr(tmp, "commands"):
commands.update(tmp.commands)
else:
print("Error loading {0}: No commands found.".format(action))
except ImportError:
print("No module named {0} available.".format(action))
except ValueError:
# For some reason there is a '' in the list `actions`
pass
### End Add-on functionality
### Main components
def opt_setup():
opts = OptionParser("Usage: %prog [options] action [arg(s)]")
opts.add_option("-c", "--config", dest="config", default="",
type="string",
nargs=1,
help=concat(["Supply your own configuration file,",
"must be an absolute path"])
)
opts.add_option("-d", "--dir", dest="todo_dir", default="",