-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·234 lines (208 loc) · 7.71 KB
/
main.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
#!/usr/bin/python3
import argparse
import random
import string
import sys
import bullet
import dates
from tinydb import TinyDB, Query
default_journal_path='bullet_desktapp.json'
def get_bullet(entry, future=False):
if entry['kind'] == 'event':
return 'o'
elif entry['kind'] == 'note':
return '-'
elif entry['state'] == 'done':
return 'x'
elif future and entry['date'] != '':
return '>'
else:
return '·'
def get_position(key):
all_entries = journal.search(entries.key.exists())
by_position = sorted(all_entries, key=lambda k: k['position'])
for i in range(0, len(by_position)):
if by_position[i]['key'] == key:
return by_position[i]['position'], None if i == 0 else by_position[i-1]['position'], None if i == len(by_position)-1 else by_position[i+1]['position']
return None, None, None
def new_key(size=6, chars=string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def new_position():
if len(journal) == 0:
return 100
last_entry = sorted(journal.search(entries.position.exists()), key=lambda k: k['position'])[-1]
return last_entry['position'] + 100
def new_entry(description='', kind='task', state='pending', date=''):
key = new_key()
while journal.search(entries.key == key):
key = new_key()
position = new_position()
return {'description':description, 'kind':kind, 'state':state, 'date':date, 'key': key, 'position': position}
def reposition(command):
if '<' in command:
keys = command.replace(' ', '').split('<')
if len(keys) == 2:
target, previous, post = get_position(keys[-1])
if previous == None:
journal.update({'position': int(target/2)}, entries.key == keys[0])
else:
journal.update({'position': int((target+previous)/2)}, entries.key == keys[0])
elif '>' in command:
keys = command.replace(' ', '').split('>')
if len(keys) == 2:
target, previous, post = get_position(keys[-1])
if post == None:
journal.update({'position': target + 100}, entries.key == keys[0])
else:
journal.update({'position': int((target+post)/2)}, entries.key == keys[0])
else:
print('Repositioning command must include a position symbol, either: <, >')
sys.exit(1)
parser = argparse.ArgumentParser(description='Manage a minimal digital bullet journal')
parser.add_argument(
'-t',
metavar='Task',
dest='task',
nargs='+',
help='add a new task')
parser.add_argument(
'-n',
metavar='Note',
dest='note',
nargs='+',
help='add a new note')
parser.add_argument(
'-e',
dest='event',
nargs='+',
help='add a new event')
parser.add_argument(
'-d',
metavar='YYYY-MM-DD',
dest='date',
nargs=1,
help='set the date of the new/updated entry. Entries without date or an empty value are placed in the future log. Events must include a date.')
parser.add_argument(
'-j',
metavar='Path',
dest='path',
nargs=1,
help='set an custom path to the journal file.')
parser.add_argument(
'-x',
metavar='Key',
dest='done',
nargs='+',
help='mark a task as done')
parser.add_argument(
'-p',
metavar='Reposition',
dest='reposition',
nargs=1,
help='move a task either before or after another')
parser.add_argument(
'-s',
metavar='Key [Date]',
dest='schedule',
nargs='+',
help='With one argument, send entry to future log. With two arguments set date for an entry.')
parser.add_argument(
'-l',
action='store_true',
help="list today's log")
parser.add_argument(
'-f',
action='store_true',
help="list the future log")
parser.add_argument(
'-m',
action='store_true',
help="list the monthly log. This only shows the events.")
parser.add_argument(
'-w',
action='store_true',
help='list the weekly log.')
parser.add_argument(
'-k',
action='store_true',
help='include the key to the entry listed.')
args = parser.parse_args()
journal = TinyDB(args.path[0] if args.path else default_journal_path)
entries = Query()
# TODO: remove this block when not needed anymore
def date_split():
import datetime
from tinydb.operations import delete
for entry in journal.all():
if entry["date"] == "":
journal.update({"date": {"year": "", "month": "", "day": "", "week": ""}}, entries.key == entry["key"])
elif entry["date"] and type(entry["date"]) == str and entry["date"] != "":
year = entry["date"][0:4]
month = entry["date"][5:7]
day = entry["date"][8:10]
week = "%02d" % datetime.datetime.strptime(entry["date"], "%Y-%m-%d").isocalendar()[1]
journal.update({"date": {"year": year, "month": month, "day": day, "week": week}}, entries.key == entry["key"])
date_split()
if args.schedule:
journal.update({'date': args.schedule[1] if len(args.schedule) > 1 else ''}, entries.key == args.schedule[0])
elif args.reposition:
reposition(args.reposition[0])
elif args.done:
for key in args.done:
journal.update({'state': 'done'}, entries.key == key)
entry = journal.search(entries.key == key)
if entry[0]['date'] == '':
journal.update({'date': dates.get_today()}, entries.key == key)
elif args.task:
date = '' if args.f else (args.date[0] if args.date else dates.get_today())
journal.insert(
new_entry(description=' '.join(args.task),
kind='task',
date=date))
elif args.event:
if not args.date:
print("Events must include a date!")
sys.exit(1)
else:
journal.insert(
new_entry(description=' '.join(args.event),
kind='event',
date=args.date[0]))
elif args.note:
journal.insert(
new_entry(description=' '.join(args.note),
kind='note',
date=(args.date if args.date else dates.get_today())))
elif args.l:
overdue = bullet.overdue(journal)
if len(overdue) > 0:
print("Overdue:")
for entry in overdue:
with_key = ' [%s]' % entry['key'] if args.k else ''
print(' %s %s%s' % (get_bullet(entry), entry['description'], with_key))
print(dates.get_today() + ':')
for entry in sorted(journal.search(entries.date == dates.get_today()), key=lambda k: k['position']):
with_key = ' [%s]' % entry['key'] if args.k else ''
print(' %s %s%s' % (get_bullet(entry), entry['description'], with_key))
elif args.w:
print("Week %d (%d): " % (dates.this_week()[1], dates.this_week()[0]))
for entry in bullet.weekly_log(journal, notes=False, done=False):
with_key = ' [%s]' % entry['key'] if args.k else ''
print(' %s %s%s' % (get_bullet(entry), entry['description'], with_key))
elif args.m:
overdue = bullet.overdue(journal, dates.get_begining_of_month())
if len(overdue) > 0:
print("Overdue:")
for entry in overdue:
with_key = ' [%s]' % entry['key'] if args.k else ''
print(' %s %s%s' % (get_bullet(entry), entry['description'], with_key))
print(dates.this_month() + ':')
for entry in bullet.future_log(journal) + bullet.monthly_log(journal, done=False):
with_key = ' [%s]' % entry['key'] if args.k else ''
print(' %s %s%s' % (get_bullet(entry), entry['description'], with_key))
elif args.f:
print('Future log:')
#for entry in sorted(bullet.future_log(journal), key=lambda k: k['position']):
for entry in bullet.future_log(journal):
with_key = ' [%s]' % entry['key'] if args.k else ''
print(' %s %s%s' % (get_bullet(entry, future=True), entry['description'], with_key))