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):
super(AddCommand, self).__init__(
p_args, p_todolist, p_out, p_err, p_prompt)
self.text = ' '.join(p_args)
self.todo = None
self.from_file = None
def _process_flags(self):
......@@ -48,72 +47,73 @@ class AddCommand(Command):
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.
"""
todo_text = re.sub(r'^(.+) (\([A-Z]\))(.*)$', r'\2 \1\3', p_todo_text)
def get_todos_from_file(self):
if self.from_file == '-':
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):
"""
Post-processes a parsed todo when adding it to the list.
return todos
* 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 = self.todo.tag_value(p_tag)
def _add_todo(self, p_todo_text):
def _preprocess_input_todo(p_todo_text):
"""
Preprocesses user input when adding a task.
if value:
dateobj = relative_date_to_date(value)
if dateobj:
self.todo.set_tag(p_tag, dateobj.isoformat())
It detects a priority mid-sentence and puts it at the start.
"""
todo_text = re.sub(r'^(.+) (\([A-Z]\))(.*)$', r'\2 \1\3', p_todo_text)
def add_dependencies(p_tag):
for value in self.todo.tag_values(p_tag):
try:
dep = self.todolist.todo(value)
return todo_text
if p_tag == 'after':
self.todolist.add_dependency(self.todo, dep)
elif p_tag == 'before' or p_tag == 'partof':
self.todolist.add_dependency(dep, self.todo)
except InvalidTodoException:
pass
def _postprocess_input_todo(p_todo):
"""
Post-processes a parsed todo when adding it to the list.
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())
convert_date(config().tag_due())
if value:
dateobj = relative_date_to_date(value)
if dateobj:
p_todo.set_tag(p_tag, dateobj.isoformat())
add_dependencies('partof')
add_dependencies('before')
add_dependencies('after')
def add_dependencies(p_tag):
for value in p_todo.tag_values(p_tag):
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):
if self.from_file == '-':
f = stdin
else:
f = codecs.open(self.from_file, 'r', encoding='utf-8')
p_todo.remove_tag(p_tag, value)
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):
todo_text = self._preprocess_input_todo(p_todo_text)
self.todo = self.todolist.add(todo_text)
self._postprocess_input_todo()
p_todo.set_creation_date(date.today())
todo_text = _preprocess_input_todo(p_todo_text)
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):
""" 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