-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask-org.py
154 lines (123 loc) · 3.96 KB
/
task-org.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
import argparse
from pathlib import Path
from datetime import datetime, timedelta
from modules import log
from modules import utils
from modules import config
from todo import DailyTodo, WeeklyTodo, ArchiveTodo
# To add @recurring tag completion, add this line to the
# \Packages\PlainTasks\PlainTasks.sublime-completions file:
# { "trigger": "r\t@recurring", "contents": "@recurring"}
config = config.get_config('config')
log_path = Path(config['global']['log_path'])
todo_path = Path(config['task-org']['todo_path'])
backup_path = Path(config['task-org']['backup_path'])
archive_path = Path(config['task-org']['archive_path'])
script_name = utils.get_script_name(__file__)
logger = log.get_logger(script_name, log_path=log_path)
today = datetime.today().date()
tomorrow = today + timedelta(days=1)
TODO_FEXT = '.todo'
TODAY_FNAME = 'today'
TOMORROW_FNAME = 'tomorrow'
CALENDAR_FNAME = 'calendar'
THIS_WEEK_FNAME = 'this-week'
NEXT_WEEK_FNAME = 'next-week'
ARCHIVE_FNAME = 'archive'
def main(args):
if args['test'] or args['show']:
log.remove_file_handler(logger)
today_todo = DailyTodo(today)
today_fpath = get_fpath(TODAY_FNAME)
today_todo.load(today_fpath)
weekly_todo = WeeklyTodo(today)
weekly_fpath = get_fpath(THIS_WEEK_FNAME)
weekly_todo.load(weekly_fpath)
calendar_todo = ArchiveTodo()
calendar_fpath = get_fpath(CALENDAR_FNAME)
calendar_todo.load(calendar_fpath)
archive_todo = ArchiveTodo()
archive_fpath = get_fpath(ARCHIVE_FNAME, folder=archive_path)
archive_todo.load(archive_fpath)
if args['update']:
update(today_todo, weekly_todo, calendar_todo, archive_todo)
write(today_todo, today_fpath, args['test'])
write(weekly_todo, weekly_fpath, args['test'])
write(calendar_todo, calendar_fpath, args['test'])
write(archive_todo, archive_fpath, args['test'])
if args['show'] == 'today':
show(today_todo)
elif args['show'] == 'week':
show(weekly_todo)
elif args['show'] == 'calendar':
show(calendar_todo)
def update(today_todo, weekly_todo, calendar_todo, archive_todo):
# Transfer current tasks to today's todo
logger.debug('updating calendar todo')
for todo in calendar_todo.todos:
if todo.sdate <= today:
for task in todo.tasks:
task.priority = 'today'
today_todo.append(task)
calendar_todo.todos.remove(todo)
# Update daily tasks
logger.debug('updating daily todo')
today_todo.update(archive_todo)
# Update weekly tasks
logger.debug('updating weekly todo')
weekly_todo.update(archive_todo)
def write(todo, fpath, test=False):
logger.info(f"writing file '{fpath}'")
if not test:
try:
todo.write(fpath)
except Exception as e:
logger.exception(e)
else:
logger.debug(str(todo))
def show(todo):
logger.info(str(todo))
def backup_or_delete(fpath, test, action='backup'):
if action == 'backup':
logger.info(f"moving '{fpath.name}' to backup folder '{backup_path}'")
if test: return
fpath.replace(backup_path / fpath.name)
elif action == 'delete':
logger.info(f"deleting '{fpath}'")
if test: return
fpath.unlink()
else:
raise ValueError("action must be either 'backup' or 'delete'")
def get_fpath(fname, folder=todo_path):
return folder / f'{fname}{TODO_FEXT}'
def get_parser():
parser = argparse.ArgumentParser(
description='ToDo file manager',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-u',
'--update',
action='store_true',
help='Update tasks to current date and archive completed tasks'
)
parser.add_argument(
'-s',
'--show',
choices=['today', 'week', 'calendar'],
help='Show daily, weekly or calendar tasks'
)
parser.add_argument(
'-t',
'--test',
action='store_true',
help='Test run. No files will be modified'
)
return parser
if __name__ == "__main__":
parser = get_parser()
args = vars(parser.parse_args())
try:
main(args)
except Exception as e:
logger.exception(e)