mirror of
https://github.com/alvierahman90/pydo.git
synced 2025-01-12 02:04:25 +00:00
Initial commit
This commit is contained in:
commit
97966d2565
102
pydo/__init__.py
Normal file
102
pydo/__init__.py
Normal file
@ -0,0 +1,102 @@
|
||||
|
||||
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
|
||||
# for sorting, lower than all alphabet letters
|
||||
self.priority = "{"
|
||||
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
|
||||
priorities = match("\\([a-zA-Z]\\)", arguments[counter])
|
||||
if priorities is not None:
|
||||
self.priority = arguments[counter].split('(')[1].split(')')[0]
|
||||
self.priority = self.priority.upper()
|
||||
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
|
||||
|
||||
self.body = " ".join(arguments[counter:])
|
||||
|
||||
# the rest of the arguments may have projects, contexts, specials
|
||||
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:
|
||||
arguments = i.split(':')
|
||||
key = arguments[0]
|
||||
value = ":".join(arguments[1:])
|
||||
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):
|
||||
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
|
148
pydo_tests.py
Executable file
148
pydo_tests.py
Executable file
@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import unittest
|
||||
import datetime
|
||||
from pydo import Task as T
|
||||
|
||||
|
||||
class TaskTestCase(unittest.TestCase):
|
||||
def test_basic(self):
|
||||
text = "task"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, False)
|
||||
self.assertEqual(task.priority, '{')
|
||||
self.assertEqual(task.completion_date, None)
|
||||
self.assertEqual(task.creation_date, None)
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [])
|
||||
|
||||
def test_basic_done(self):
|
||||
text = "x basic test task"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, True)
|
||||
self.assertEqual(task.priority, '{')
|
||||
self.assertEqual(task.completion_date, None)
|
||||
self.assertEqual(task.creation_date, None)
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [])
|
||||
|
||||
def test_prioritized(self):
|
||||
text = "(A) prioritized test task"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, False)
|
||||
self.assertEqual(task.priority, 'A')
|
||||
self.assertEqual(task.completion_date, None)
|
||||
self.assertEqual(task.creation_date, None)
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [])
|
||||
|
||||
def test_prioritized_ignore_incorrect(self):
|
||||
text = "(AA) prioritized test task"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, False)
|
||||
self.assertEqual(task.priority, '{')
|
||||
self.assertEqual(task.completion_date, None)
|
||||
self.assertEqual(task.creation_date, None)
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [])
|
||||
|
||||
def test_prioritized_done(self):
|
||||
text = "x (A) prioritized test task"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, True)
|
||||
self.assertEqual(task.priority, 'A')
|
||||
self.assertEqual(task.completion_date, None)
|
||||
self.assertEqual(task.creation_date, None)
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [])
|
||||
|
||||
def test_with_creation_date(self):
|
||||
text = "2018-06-24 test task"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, False)
|
||||
self.assertEqual(task.priority, '{')
|
||||
self.assertEqual(task.completion_date, None)
|
||||
self.assertEqual(task.creation_date,
|
||||
datetime.datetime(2018, 6, 24, 0, 0))
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [])
|
||||
|
||||
def test_with_creation_and_completion_date(self):
|
||||
text = "x 2018-06-24 2018-05-24 test task"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, True)
|
||||
self.assertEqual(task.priority, '{')
|
||||
self.assertEqual(task.completion_date,
|
||||
datetime.datetime(2018, 6, 24, 0, 0))
|
||||
self.assertEqual(task.creation_date,
|
||||
datetime.datetime(2018, 5, 24, 0, 0))
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [])
|
||||
|
||||
def test_with_creation_and_completion_and_priority_date(self):
|
||||
text = "x (B) 2018-06-24 2018-05-24 test task"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, True)
|
||||
self.assertEqual(task.priority, 'B')
|
||||
self.assertEqual(task.completion_date,
|
||||
datetime.datetime(2018, 6, 24, 0, 0))
|
||||
self.assertEqual(task.creation_date,
|
||||
datetime.datetime(2018, 5, 24, 0, 0))
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [])
|
||||
|
||||
def test_special(self):
|
||||
text = "special task special:value"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, False)
|
||||
self.assertEqual(task.priority, '{')
|
||||
self.assertEqual(task.completion_date, None)
|
||||
self.assertEqual(task.creation_date, None)
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [{"special": "value"}])
|
||||
|
||||
def test_specials_with_colons(self):
|
||||
text = "give muffin her pen back due:2028-07-10T14:28:15Z+0100"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text)
|
||||
self.assertEqual(task.done, False)
|
||||
self.assertEqual(task.priority, '{')
|
||||
self.assertEqual(task.completion_date, None)
|
||||
self.assertEqual(task.creation_date, None)
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [{"due": "2028-07-10T14:28:15Z+0100"}])
|
||||
|
||||
def test_standardized_priority_case(self):
|
||||
text = "(a) standard prioritization test"
|
||||
text_standardized_priority = "(A) standard prioritization test"
|
||||
task = T(text)
|
||||
self.assertEqual(str(task), text_standardized_priority)
|
||||
self.assertEqual(task.done, False)
|
||||
self.assertEqual(task.priority, 'A')
|
||||
self.assertEqual(task.completion_date, None)
|
||||
self.assertEqual(task.creation_date, None)
|
||||
self.assertEqual(task.projects, [])
|
||||
self.assertEqual(task.contexts, [])
|
||||
self.assertEqual(task.specials, [])
|
||||
|
||||
|
||||
unittest.main()
|
3
readme.md
Normal file
3
readme.md
Normal file
@ -0,0 +1,3 @@
|
||||
# pydo
|
||||
|
||||
A module for parsing todo.txt tasks in python
|
Loading…
Reference in New Issue
Block a user