-
Notifications
You must be signed in to change notification settings - Fork 1
/
todo_item.py
executable file
·182 lines (157 loc) · 5.56 KB
/
todo_item.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
'''
Created on Dec 17, 2012
@author: mstave
'''
import re
import datetime
class TodoItem(object): # pylint: disable-msg=R0902
'''
Represents just one td item in a portable format
'''
def __init__(self, raw_text=None):
self.creation_date = None
self.completion_date = None
self.task = None
self.project = None
self._priority = None
self.context = None
self.gui_index = None
self.done = None
# self.todo_txt_line = raw_text
if raw_text is not None:
self.parse_done(raw_text)
self.parse_creation_date(raw_text)
self.parse_priority(raw_text)
self.parse_context(raw_text)
self.parse_task(raw_text)
self.parse_project(raw_text)
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __str__(self):
return self.todo_txt_line
def __len__(self):
return len(self.todo_txt_line)
def get_todo_dot_txt_line(self):
ret_str = ""
if self.done:
ret_str = "x "
if self.completion_date is not None:
ret_str += self.completion_date + " "
elif self.priority is not None:
ret_str = "(" + self.priority + ") "
if self.creation_date is not None:
ret_str += self.creation_date + " "
if self.task is not None:
ret_str += self.task + " "
if self.context is not None:
ret_str += "@" + self.context
ret_str = ret_str.strip()
if ret_str == "":
ret_str = None
return ret_str
# don't really need a property for this one, but making
# it consistent with the others
@property
def todo_txt_line(self):
return self.get_todo_dot_txt_line()
@todo_txt_line.setter
def todo_txt_line(self, line):
self.todo_txt_line = line
@property
def priority(self):
return self._priority
@priority.setter
def priority(self, new_pri):
if new_pri is not None:
new_pri = new_pri.upper()
self._priority = new_pri
@property
def done(self):
return self._done
@done.setter
def done(self, newdone):
self._done = newdone
if newdone:
self.completion_date = self.curr_date_str()
def parse_project(self, raw_text):
project_pattern = re.compile(r'\+(\w+)')
match = project_pattern.search(raw_text)
if match is not None:
self.project = match.group(1)
def parse_context(self, raw_text):
context_pattern = re.compile(r'@(\w+)')
match = context_pattern.search(raw_text)
if match is not None:
self.context = match.group(1)
def parse_done(self, raw_text):
self.completion_date = None
if raw_text is not None:
self.done = raw_text.startswith("x ")
if self.done:
date_pattern = re.compile(r'(\d{4}-\d{2}-\d{2}).*')
donematch = date_pattern.match(raw_text[2:])
if donematch is not None:
self.completion_date = donematch.group(1)
def parse_priority(self, raw_text):
self.priority = None
if raw_text[0] == "(" and raw_text[2] == ")":
self.priority = raw_text[1]
else:
if self.done:
pass
# may want to investigate supporting this
# but the file format seems to forid it
# if raw_text[24] == "(" and raw_text[26] == ")":
# self.priority = raw_text[25]
def parse_creation_date(self, raw_text):
date_pattern = re.compile(r'(\d{4}-\d{2}-\d{2}).*')
temp_line = raw_text
self.creation_date = None
if self.done:
temp_line = temp_line[13:]
if self.priority is None:
self.parse_priority(raw_text)
if self.priority is not None:
temp_line = raw_text[4:]
match = date_pattern.match(temp_line)
if match is not None:
self.creation_date = match.group(1)
def parse_task(self, raw_text):
self.task = raw_text
self.parse_context(raw_text)
self.parse_done(raw_text)
if self.done:
self.task = self.task[2:]
if self.priority is not None:
self.task = self.task[4:]
if self.creation_date is not None:
self.task = self.task[11:]
if self.completion_date is not None:
self.task = self.task[10:]
if self.context is not None:
self.task = self.task.replace("@" + self.context, "")
if self.task == "":
self.task = None
if self.task is not None:
self.task = self.task.strip()
def parse_json(self, json_data):
for field in json_data:
if json_data[field] == "None":
json_data[field] = None
self.task = json_data.get('task', None)
self.priority = json_data.get('_priority', None)
self.creation_date = json_data.get('creation_date', None)
self.completion_date = json_data.get('completion_date', None)
self.context = json_data.get('context', None)
self.done = json_data.get('_done', None)
self.project = json_data.get('project', None)
def create_today(self):
self.creation_date = self.curr_date_str()
@classmethod
def curr_date_str(cls):
'''
return current date in ISO format
:param cls: implicit param
'''
temp_date = datetime.date.today()
return temp_date.isoformat()