-
Notifications
You must be signed in to change notification settings - Fork 2
/
todo.py
executable file
·124 lines (111 loc) · 2.98 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
#!/usr/bin/env python
#Filename: todo.py
# A script to do things with todo.txt
import os
import sys
import re
# Configuration
todo_path = os.path.expanduser('~/todo.txt')
def LoadTodos():
global todos
todo_file = open(todo_path, 'r')
raw_todos = todo_file.readlines()
todo_file.close()
todos = []
for item in raw_todos:
item = item.strip("\n")
todos.append(item)
def WriteTodos(arguments = ""):
LoadTodos()
todo_file = open(todo_path, 'a')
# Is there a newline aready here for us? If not, add it.
if todos[-1][-1] != "\n":
todo_file.write("\n")
# If we've been given something, add each of them followed by a new line.
if arguments != "":
for item in arguments:
todo_file.write(str(item) + "\n")
todo_file.close()
def ShowResults(results, numbers = 1):
print('')
if len(results) == 0:
print("No results.")
else:
i = 1
for item in results:
if numbers == 1:
print("{0}. {1}".format(i, item))
else:
print(item)
i += 1
print('')
# And on with the show...
# If we've got nothing to do, show what we've got!
if len(sys.argv) == 1:
LoadTodos()
ShowResults(todos)
sys.exit()
# What if we want to look up something?
arg = sys.argv[1:]
results = []
if arg[0] == "=" or arg[0] == "@":
regex = str(arg[0]) + r"\S*"
projects = set()
LoadTodos()
for item in todos:
matches = re.findall(regex, item)
for match in matches:
match = match.strip("]")
projects.add(match)
ShowResults(sorted(projects), 0)
elif arg[0][0] == '@' or arg[0][0] == '=':
#Yes, we are looking for something! Let's find it.
search_terms = "|".join(arg)
regex = re.compile(search_terms)
LoadTodos()
for item in todos:
if re.search(regex, item):
results.append(item)
ShowResults(results)
elif arg[0][0] == '+': # Emulate an "AND" operator.
LoadTodos()
old_results = todos[:] # Start with all the items.
for term in arg[1:]: # Iterate through each of the terms
new_results = []
regex = re.compile(term)
for item in old_results:
if re.search(regex, item):
item = item.strip('\n')
new_results.append(item) # Move matching items into a new list
old_results = new_results # ... which goes on to the next round.
ShowResults(new_results)
elif arg[0] == "done":
search_terms = "|".join(arg[1:])
regex = re.compile(search_terms)
LoadTodos()
for item in todos:
if re.search(regex, item):
results.append(item)
i = 1
numbered_results = {}
for item in results:
numbered_results[i] = item
i += 1
ShowResults(results)
to_del = input("? ")
to_del = to_del.split()
if to_del != "q":
new_todos = []
for item in todos:
keep = True
for number in to_del:
if item == numbered_results[int(number)]:
keep = False
if keep == True:
new_todos.append(item)
todo_file = open(todo_path, 'w')
todo_file.write("\n".join(new_todos))
todo_file.close()
else:
# Looks like we want to add something instead.
WriteTodos(arg)