Commit 4380d872 authored by Jacek Sowiński's avatar Jacek Sowiński

AddCommand refactoring

1. Move _preprocess_input_todo adn _postprocess_input_todo inside
_add_todo.
2. Remove self.todo
parent 9adafb91
...@@ -36,7 +36,6 @@ class AddCommand(Command): ...@@ -36,7 +36,6 @@ class AddCommand(Command):
super(AddCommand, self).__init__( super(AddCommand, self).__init__(
p_args, p_todolist, p_out, p_err, p_prompt) p_args, p_todolist, p_out, p_err, p_prompt)
self.text = ' '.join(p_args) self.text = ' '.join(p_args)
self.todo = None
self.from_file = None self.from_file = None
def _process_flags(self): def _process_flags(self):
...@@ -48,72 +47,73 @@ class AddCommand(Command): ...@@ -48,72 +47,73 @@ class AddCommand(Command):
self.args = args self.args = args
def _preprocess_input_todo(self, p_todo_text):
"""
Preprocesses user input when adding a task.
It detects a priority mid-sentence and puts it at the start. def get_todos_from_file(self):
""" if self.from_file == '-':
todo_text = re.sub(r'^(.+) (\([A-Z]\))(.*)$', r'\2 \1\3', p_todo_text) f = stdin
else:
f = codecs.open(self.from_file, 'r', encoding='utf-8')
return todo_text todos = f.read().splitlines()
def _postprocess_input_todo(self): return todos
"""
Post-processes a parsed todo when adding it to the list.
* It converts relative dates to absolute ones. def _add_todo(self, p_todo_text):
* Automatically inserts a creation date if not present. def _preprocess_input_todo(p_todo_text):
* Handles more user-friendly dependencies with before:, partof: and """
after: tags Preprocesses user input when adding a task.
"""
def convert_date(p_tag):
value = self.todo.tag_value(p_tag)
if value: It detects a priority mid-sentence and puts it at the start.
dateobj = relative_date_to_date(value) """
if dateobj: todo_text = re.sub(r'^(.+) (\([A-Z]\))(.*)$', r'\2 \1\3', p_todo_text)
self.todo.set_tag(p_tag, dateobj.isoformat())
def add_dependencies(p_tag): return todo_text
for value in self.todo.tag_values(p_tag):
try:
dep = self.todolist.todo(value)
if p_tag == 'after': def _postprocess_input_todo(p_todo):
self.todolist.add_dependency(self.todo, dep) """
elif p_tag == 'before' or p_tag == 'partof': Post-processes a parsed todo when adding it to the list.
self.todolist.add_dependency(dep, self.todo)
except InvalidTodoException:
pass
self.todo.remove_tag(p_tag, value) * It converts relative dates to absolute ones.
* Automatically inserts a creation date if not present.
* Handles more user-friendly dependencies with before:, partof: and
after: tags
"""
def convert_date(p_tag):
value = p_todo.tag_value(p_tag)
convert_date(config().tag_start()) if value:
convert_date(config().tag_due()) dateobj = relative_date_to_date(value)
if dateobj:
p_todo.set_tag(p_tag, dateobj.isoformat())
add_dependencies('partof') def add_dependencies(p_tag):
add_dependencies('before') for value in p_todo.tag_values(p_tag):
add_dependencies('after') try:
dep = self.todolist.todo(value)
self.todo.set_creation_date(date.today()) if p_tag == 'after':
self.todolist.add_dependency(p_todo, dep)
elif p_tag == 'before' or p_tag == 'partof':
self.todolist.add_dependency(dep, p_todo)
except InvalidTodoException:
pass
def get_todos_from_file(self): p_todo.remove_tag(p_tag, value)
if self.from_file == '-':
f = stdin
else:
f = codecs.open(self.from_file, 'r', encoding='utf-8')
todos = f.read().splitlines() convert_date(config().tag_start())
convert_date(config().tag_due())
return todos add_dependencies('partof')
add_dependencies('before')
add_dependencies('after')
def _add_todo(self, p_todo_text): p_todo.set_creation_date(date.today())
todo_text = self._preprocess_input_todo(p_todo_text)
self.todo = self.todolist.add(todo_text) todo_text = _preprocess_input_todo(p_todo_text)
self._postprocess_input_todo() todo = self.todolist.add(todo_text)
_postprocess_input_todo(todo)
self.out(self.printer.print_todo(self.todo)) self.out(self.printer.print_todo(todo))
def execute(self): def execute(self):
""" Adds a todo item to the list. """ """ Adds a todo item to the list. """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment