2018-06-24 17:34:09 +01:00
|
|
|
from re import match
|
|
|
|
from datetime import datetime as d
|
|
|
|
|
|
|
|
|
|
|
|
class Task:
|
|
|
|
def __init__(self, text):
|
|
|
|
# defaults
|
|
|
|
self.completion_date = None
|
|
|
|
self.creation_date = None
|
2018-06-27 23:18:05 +01:00
|
|
|
# for sorting, lower than all alphabet letters
|
|
|
|
self.priority = "{"
|
2018-06-24 17:34:09 +01:00
|
|
|
self.done = False
|
|
|
|
self.projects = []
|
|
|
|
self.contexts = []
|
|
|
|
self.specials = []
|
|
|
|
|
|
|
|
arguments = text.split(' ')
|
|
|
|
counter = 0
|
|
|
|
if arguments[counter] == 'x':
|
|
|
|
self.done = True
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
# try to get priority
|
2018-08-09 19:56:52 +01:00
|
|
|
priorities = match("\\([a-zA-Z]\\)", arguments[counter])
|
2018-06-24 17:34:09 +01:00
|
|
|
if priorities is not None:
|
|
|
|
self.priority = arguments[counter].split('(')[1].split(')')[0]
|
2018-08-09 20:02:44 +01:00
|
|
|
self.priority = self.priority.upper()
|
2018-06-24 17:34:09 +01:00
|
|
|
counter += 1
|
|
|
|
|
|
|
|
# try to get completion date if done
|
|
|
|
|
|
|
|
if self.done:
|
|
|
|
try:
|
|
|
|
self.completion_date = d.strptime(arguments[counter],
|
|
|
|
'%Y-%m-%d')
|
|
|
|
counter += 1
|
|
|
|
except ValueError as e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# try to get creation date
|
|
|
|
try:
|
|
|
|
self.creation_date = d.strptime(arguments[counter], '%Y-%m-%d')
|
|
|
|
counter += 1
|
|
|
|
except ValueError as e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# you cannot have a completion date w/o a creation date
|
|
|
|
if self.creation_date is None and self.completion_date is not None:
|
|
|
|
self.creation_date = self.completion_date
|
|
|
|
self.completion_date = None
|
|
|
|
|
|
|
|
# auto mark tasks with completion date as done
|
|
|
|
if self.completion_date is not None:
|
|
|
|
self.done = True
|
|
|
|
|
2018-07-11 20:28:05 +01:00
|
|
|
self.body = " ".join(arguments[counter:])
|
2018-06-24 17:34:09 +01:00
|
|
|
|
2018-07-11 20:28:05 +01:00
|
|
|
# the rest of the arguments may have projects, contexts, specials
|
2018-06-24 17:34:09 +01:00
|
|
|
for i in arguments[counter:]:
|
|
|
|
if len(i) < 1:
|
|
|
|
continue
|
|
|
|
if i[0] == '+':
|
|
|
|
self.projects.append(i.split('+')[1])
|
|
|
|
elif i[0] == '-':
|
|
|
|
self.contexts.append(i.split('-')[1])
|
|
|
|
elif ':' in i:
|
2018-07-10 19:04:31 +01:00
|
|
|
arguments = i.split(':')
|
|
|
|
key = arguments[0]
|
|
|
|
value = ":".join(arguments[1:])
|
2018-06-24 17:34:09 +01:00
|
|
|
special = {key: value}
|
|
|
|
self.specials.append(special)
|
|
|
|
|
|
|
|
def do(self):
|
|
|
|
if self.done:
|
|
|
|
return
|
|
|
|
self.done = True
|
|
|
|
|
|
|
|
def undo(self):
|
|
|
|
if not self.done:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.done = False
|
|
|
|
|
|
|
|
def __str__(self):
|
2018-07-11 20:28:05 +01:00
|
|
|
text = ""
|
|
|
|
|
|
|
|
if self.done:
|
|
|
|
text += "x "
|
|
|
|
|
|
|
|
if self.priority != '{':
|
|
|
|
text += '(' + self.priority + ') '
|
|
|
|
|
|
|
|
if self.completion_date:
|
|
|
|
text += str(self.completion_date).split(' ')[0] + ' '
|
|
|
|
|
|
|
|
if self.creation_date:
|
|
|
|
text += str(self.creation_date).split(' ')[0] + ' '
|
|
|
|
|
|
|
|
text += self.body
|
|
|
|
|
|
|
|
return text
|