Commit fafe392d authored by Jacek Sowiński's avatar Jacek Sowiński

Add todos from file with '-f' flag.

parent 61a35f8f
......@@ -34,14 +34,26 @@ class AddCommand(Command):
p_args, p_todolist, p_out, p_err, p_prompt)
self.text = ' '.join(p_args)
self.todo = None
self.from_file = False
def _preprocess_input_todo(self):
def _process_flags(self):
opts, args = self.getopt('f')
for opt, value in opts:
if opt == '-f':
self.from_file = True
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.
"""
self.text = re.sub(r'^(.+) (\([A-Z]\))(.*)$', r'\2 \1\3', self.text)
todo_text = re.sub(r'^(.+) (\([A-Z]\))(.*)$', r'\2 \1\3', p_todo_text)
return todo_text
def _postprocess_input_todo(self):
"""
......@@ -83,18 +95,35 @@ class AddCommand(Command):
self.todo.set_creation_date(date.today())
def get_todos_from_file(self, p_filename):
f = open(p_filename, 'r')
todos = f.read().decode('utf-8').splitlines()
return todos
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()
self.out(self.printer.print_todo(self.todo))
def execute(self):
""" Adds a todo item to the list. """
if not super(AddCommand, self).execute():
return False
if self.text:
self._preprocess_input_todo()
self.todo = self.todolist.add(self.text)
self._postprocess_input_todo()
self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
self.out(self.printer.print_todo(self.todo))
self._process_flags()
if self.from_file:
new_todos = self.get_todos_from_file(self.args[0])
for todo in new_todos:
self._add_todo(todo)
else:
if self.text:
self._add_todo(self.text)
else:
self.error(self.usage())
......
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