-
Notifications
You must be signed in to change notification settings - Fork 0
/
melta.py
executable file
·250 lines (211 loc) · 8.61 KB
/
melta.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
#!/usr/bin/python
"A set of functions designed to make it easier to process a next actions list"
from __future__ import division
from __future__ import print_function
import argparse
import sys
import csv
import random
import datetime
import operator
import os
import time
import csv
import json
import re
TIMESTAMP_FORMAT = '%y-%m-%d %H:%M'
config = json.loads(open(os.path.dirname(os.path.abspath(__file__))+'/config.json').read())
NEXTACTIONS_LOC=config["jurgen_location"] + '/nextactions.md'
ALLACTIONS_LOC=config["jurgen_location"] + '/data/all_tasks.csv'
PRI_LOC=config["jurgen_location"] + '/data/priority.txt'
WAITACTIONS_LOC=config["jurgen_location"] + '/data/waitactions.md'
def eprint(*args, **kwargs):
pass
# print(*args, file=sys.stderr, **kwargs)#from https://stackoverflow.com/a/14981125/170243
def last_line_pri():
return open(PRI_LOC).readlines()[-1]
def update_pri_if_different():
try:
parser = setup_argument_list()
args=parser.parse_args(['sort','-o', '-s'])
now=print_actions(filter_actions(args),args).strip()
before=last_line_pri().strip()
now_a=now[30:]
before_a=before[30:]
if now_a == before_a:
pass
# print "idenical, NO updated needed"
else:
with open(PRI_LOC, 'a') as pri_file:
pri_file.write(now+"\n")
except IOError:
eprint("Could NOT update priority log file")
# print "There has been a change. Writing. "
# print "N"+now_a+"X"
# print "T"+before_a+"X"
def get_sorted_actions(sortfunction=lambda item: re.sub("\D","",item['priority'])+item['timestamp']):
"returns a sorted list of nextActions, strips out any line without four fields"
with open(NEXTACTIONS_LOC, 'rU') as actions_file:
reader = csv.reader(actions_file, skipinitialspace=True)
lines = filter(None, reader)
tasklist=[]
for line in lines:
if len(line)>=4:
if "- [" in line[0]:
task={}
task['timestamp']=line[3]
task['action']=line[2]
task['time']=int(line[1])
# task['context']=line[1]
task['priority']=line[0][6:]
task['extra']=line[4:]
task['completed']=line[0][3:4]
tasklist.append(task)
else:
eprint( "The following line did NOT parse and was removed")
eprint(line)
tasklist =sorted(tasklist,key=sortfunction)
return tasklist
def write_to_file(toprint):
with open(NEXTACTIONS_LOC, 'a') as actions_file:
actions_file.write(toprint)
def write_to_archive(toprint):
with open(ALLACTIONS_LOC, 'a') as actions_file:
actions_file.write(toprint )
def setup_argument_list():
parser = argparse.ArgumentParser(
description="manages a todo list")
parser.add_argument("action", help="What to do/display: options are 'add', 'print', 'count', and 'info' ")
parser.add_argument("content",nargs='?', help='The action to do')
parser.add_argument("delay", nargs='?', default=0, help='A delay to add in days')
parser.add_argument("context", nargs='?', default="0", help='The context for the action')
parser.add_argument("priority", nargs='?', default="0", help='From 0 to 7 how important is this action')
parser.add_argument("time", nargs='?', default="0", help='How long in minutes is this likely to take')
# parser.add_argument('-c', nargs="?", help="if context_filter is activated then only actions in the relevant contexts (contexts are generally in 'bgthop0ry') are counted")
parser.add_argument('-m', action='store_true', help="show only marked tasks")
parser.add_argument('-d', nargs="?" , help="Show only tasks that are at least this many days old")
# parser.add_argument( '-n', nargs="?", help="reverse context filter, eliminates certain contexts from the count")
parser.add_argument( '-o', action='store_true', help="Open tasks")
parser.add_argument( '-s', action='store_true', help="use if called by a script or cron")
return parser
def filter_actions(args):
"fetches the actions and runs a filter on them depending on the arguments"
tasks = get_sorted_actions()
if args.action=="time":
tasks = get_sorted_actions(lambda item: re.sub("-","",item['timestamp']))
# if args.c:
# tasks = [i for i in tasks if i['context'] in args.c]
if args.m:
tasks = [i for i in tasks if i['completed'] in ["x","e"]]
if args.o:
tasks = [i for i in tasks if i['completed'] ==" "]
# if args.n:
# tasks = [i for i in tasks if i['context'] not in args.n]
if args.d:
tasks = [i for i in tasks if days_old(i)>=int(args.d)]
return tasks
def powerhour(tasks):
tasks = [i for i in tasks if i['time'] != 0 ]
tasks =sorted(tasks,key=lambda item: item['time'])
total_time_remaining=60
powertasks=[]
for a in tasks:
if a['time']<total_time_remaining:
powertasks.append(a)
total_time_remaining-=a['time']
return powertasks
def print_actions(tasks,args):
count_items= get_action_age_info_with_priority(tasks, lambda x:1, False)
pri_items= get_action_age_info_with_priority(tasks)
if args.s:
result= str(" %d, %s, %d, %d, %d, %d" % pri_items + ", %d, %d, %d, %d" % count_items)
return result
return """State of Next Actions as of {}
Now: {}({})""".format(pri_items[1],pri_items[0],count_items[0])+"""
1 day {}({})""".format(pri_items[4],count_items[2])+"""
3 day {}({})
7 day {}({})
""".format(pri_items[5],count_items[3],pri_items[3],count_items[1])
def print_time(tasks):
running_total=0
for task in tasks:
running_total+=task['time']
hours=running_total //60
minutes=running_total -hours*60
print("The total time for the set is {} minutes ({}:{})".format(running_total,hours,minutes))
nowtime=time.time()+running_total*60
print("Target finish time: {}".format(time.ctime(nowtime)))
def days_old(task):
seconds_in_day=60*60*24
datestring = task['timestamp']
timestamp_on_action = time.strptime( datestring.strip(), TIMESTAMP_FORMAT)
age = time.time() - time.mktime(timestamp_on_action)
age = age+seconds_in_day*7#when we switched from origin to deadline
days=int(age/(seconds_in_day))
return days
def tasks_this_old(tasklist, scorer, days):
count=0
for row in tasklist:
if days_old(row)>=days:
count += scorer(row)
return count
def priority_to_number(input):
output=re.sub("\D","1", input[-1])#we take the last digit and then if it's text it's 1
return int(output)
def get_action_age_info_with_priority(tasklist, scorer=lambda x:7-priority_to_number(x['priority']), time_print=True):
"prints out the number of nextactions of each age in a current nextactions"
now=tasks_this_old(tasklist,scorer,0)
dayold=tasks_this_old(tasklist,scorer,1)
threedayold=tasks_this_old(tasklist,scorer,3)
weekold=tasks_this_old(tasklist,scorer,7)
if time_print==True:
return (now, datetime.date.today(), time.time(), weekold, dayold, threedayold)
return (now, weekold, dayold, threedayold)
def print_random(tasklist):
task=random.choice(tasklist)
print_task(task)
def print_sorted_tasks(tasklist):
for task in tasklist:
print(action_to_string(task))
def action_to_string(task):
return "- [%s] %s, %2s, \"%s\", %s" % (task['completed'], task['priority'].strip(), task['time'], task['action'] , task['timestamp'])+ ''.join(task['extra'])
def write_to_waiting_list(toprint):
with open(WAITACTIONS_LOC, 'a') as actions_file:
actions_file.write(toprint)
def add(args):
today = datetime.datetime.now()
deadline=today+datetime.timedelta(days=7)
date= deadline.strftime(TIMESTAMP_FORMAT)
if args.content:
toprint = "- [ ] %s,%s, \"%s\", %s\n" % (args.priority, args.time, args.content, date)
write_to_archive(str(date)+", "+toprint)
if args.s:
print(toprint)
write_to_waiting_list(toprint)
else:
write_to_file(toprint)
def run_melta():
parser = setup_argument_list()
args=parser.parse_args()
if args.action == "count":
print(print_actions(filter_actions(args),args))
elif args.action == "sort":
print_sorted_tasks(filter_actions(args))
elif args.action == "sorttime":
print_sorted_tasks(filter_actions(args))
elif args.action == "add":
add(args)
elif args.action == "random":
print_random(filter_actions(args))
elif args.action == "next":
args.o=True
print_sorted_tasks([filter_actions(args)[0]])
elif args.action == "time":
print_time(filter_actions(args))
elif args.action == "powerhour":
print_sorted_tasks(powerhour(filter_actions(args)))
else:
print("Error, missing action")
update_pri_if_different()
if __name__ == '__main__':
run_melta()