Commit 4218f1bb authored by Jacek Sowiński's avatar Jacek Sowiński

Migrate commands operating on multiple todo IDs to MultiCommand

parent 9b1a5fd6
...@@ -16,12 +16,12 @@ ...@@ -16,12 +16,12 @@
import re import re
from topydo.lib.Command import Command from topydo.lib.MultiCommand import MultiCommand
from topydo.lib.PrettyPrinter import PrettyPrinter from topydo.lib.PrettyPrinter import PrettyPrinter
from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers
from topydo.lib.TodoListBase import InvalidTodoException from topydo.lib.TodoListBase import InvalidTodoException
class DCommand(Command): class DCommand(MultiCommand):
""" """
A common class for the 'do' and 'del' operations, because they're quite A common class for the 'do' and 'del' operations, because they're quite
alike. alike.
...@@ -38,14 +38,7 @@ class DCommand(Command): ...@@ -38,14 +38,7 @@ class DCommand(Command):
self.process_flags() self.process_flags()
self.length = len(self.todolist.todos()) # to determine newly activated todos self.length = len(self.todolist.todos()) # to determine newly activated todos
self.get_todos(self.args)
self.todos = []
self.invalid_numbers = []
for number in self.args:
try:
self.todos.append(self.todolist.todo(number))
except InvalidTodoException:
self.invalid_numbers.append(number)
def get_flags(self): def get_flags(self):
""" Default implementation of getting specific flags. """ """ Default implementation of getting specific flags. """
...@@ -140,14 +133,9 @@ class DCommand(Command): ...@@ -140,14 +133,9 @@ class DCommand(Command):
if not super(DCommand, self).execute(): if not super(DCommand, self).execute():
return False return False
if len(self.args) == 0: todo_errors = self.catch_todo_errors()
self.error(self.usage())
elif len(self.invalid_numbers) > 1 or len(self.invalid_numbers) > 0 and len(self.todos) > 0: if not todo_errors:
for number in self.invalid_numbers:
self.error("Invalid todo number given: {}.".format(number))
elif len(self.invalid_numbers) == 1 and len(self.todos) == 0:
self.error("Invalid todo number given.")
else:
old_active = self._active_todos() old_active = self._active_todos()
for todo in self.todos: for todo in self.todos:
...@@ -159,4 +147,6 @@ class DCommand(Command): ...@@ -159,4 +147,6 @@ class DCommand(Command):
current_active = self._active_todos() current_active = self._active_todos()
self._print_unlocked_todos(old_active, current_active) self._print_unlocked_todos(old_active, current_active)
else:
for error in todo_errors:
self.error(error)
...@@ -16,14 +16,15 @@ ...@@ -16,14 +16,15 @@
from datetime import date, timedelta from datetime import date, timedelta
from topydo.lib.Command import Command, InvalidCommandArgument from topydo.lib.MultiCommand import MultiCommand
from topydo.lib.Command import InvalidCommandArgument
from topydo.lib.Config import config from topydo.lib.Config import config
from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers
from topydo.lib.RelativeDate import relative_date_to_date from topydo.lib.RelativeDate import relative_date_to_date
from topydo.lib.TodoListBase import InvalidTodoException from topydo.lib.TodoListBase import InvalidTodoException
from topydo.lib.Utils import date_string_to_date from topydo.lib.Utils import date_string_to_date
class PostponeCommand(Command): class PostponeCommand(MultiCommand):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
p_out=lambda a: None, p_out=lambda a: None,
p_err=lambda a: None, p_err=lambda a: None,
...@@ -32,6 +33,8 @@ class PostponeCommand(Command): ...@@ -32,6 +33,8 @@ class PostponeCommand(Command):
p_args, p_todolist, p_out, p_err, p_prompt) p_args, p_todolist, p_out, p_err, p_prompt)
self.move_start_date = False self.move_start_date = False
self._process_flags()
self.get_todos(self.args[:-1])
def _process_flags(self): def _process_flags(self):
opts, args = self.getopt('s') opts, args = self.getopt('s')
...@@ -56,50 +59,35 @@ class PostponeCommand(Command): ...@@ -56,50 +59,35 @@ class PostponeCommand(Command):
if not super(PostponeCommand, self).execute(): if not super(PostponeCommand, self).execute():
return False return False
self._process_flags() todo_errors = self.catch_todo_errors()
todos = []
invalid_numbers = []
for number in self.args[:-1]: if not todo_errors:
try:
todos.append(self.todolist.todo(number))
except InvalidTodoException:
invalid_numbers.append(number)
if len(invalid_numbers) > 1 or len(invalid_numbers) > 0 and len(todos) > 0:
for number in invalid_numbers:
self.error("Invalid todo number given: {}.".format(number))
elif len(invalid_numbers) == 1 and len(todos) == 0:
self.error("Invalid todo number given.")
else:
try: try:
pattern = self.args[-1] pattern = self.args[-1]
self.printer.add_filter(PrettyPrinterNumbers(self.todolist)) self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
if len(todos) > 0: for todo in self.todos:
for todo in todos: offset = _get_offset(todo)
offset = _get_offset(todo) new_due = relative_date_to_date(pattern, offset)
new_due = relative_date_to_date(pattern, offset)
if new_due:
if self.move_start_date and todo.has_tag(config().tag_start()):
length = todo.length()
new_start = new_due - timedelta(length)
todo.set_tag(config().tag_start(), new_start.isoformat())
todo.set_tag(config().tag_due(), new_due.isoformat()) if new_due:
if self.move_start_date and todo.has_tag(config().tag_start()):
length = todo.length()
new_start = new_due - timedelta(length)
todo.set_tag(config().tag_start(), new_start.isoformat())
self.todolist.set_dirty() todo.set_tag(config().tag_due(), new_due.isoformat())
self.out(self.printer.print_todo(todo))
else:
self.error("Invalid date pattern given.")
break
else:
self.error(self.usage())
self.todolist.set_dirty()
self.out(self.printer.print_todo(todo))
else:
self.error("Invalid date pattern given.")
break
except (InvalidCommandArgument, IndexError): except (InvalidCommandArgument, IndexError):
self.error(self.usage()) self.error(self.usage())
else:
for error in todo_errors:
self.error(error)
def usage(self): def usage(self):
return "Synopsis: postpone [-s] <NUMBER> [<NUMBER2> ...] <PATTERN>" return "Synopsis: postpone [-s] <NUMBER> [<NUMBER2> ...] <PATTERN>"
......
...@@ -14,12 +14,12 @@ ...@@ -14,12 +14,12 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from topydo.lib.Command import Command from topydo.lib.MultiCommand import MultiCommand
from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers
from topydo.lib.TodoListBase import InvalidTodoException from topydo.lib.TodoListBase import InvalidTodoException
from topydo.lib.Utils import is_valid_priority from topydo.lib.Utils import is_valid_priority
class PriorityCommand(Command): class PriorityCommand(MultiCommand):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
p_out=lambda a: None, p_out=lambda a: None,
p_err=lambda a: None, p_err=lambda a: None,
...@@ -27,49 +27,39 @@ class PriorityCommand(Command): ...@@ -27,49 +27,39 @@ class PriorityCommand(Command):
super(PriorityCommand, self).__init__( super(PriorityCommand, self).__init__(
p_args, p_todolist, p_out, p_err, p_prompt) p_args, p_todolist, p_out, p_err, p_prompt)
self.get_todos(self.args[:-1])
def execute(self): def execute(self):
if not super(PriorityCommand, self).execute(): if not super(PriorityCommand, self).execute():
return False return False
priority = None priority = None
todos = [] todo_errors = self.catch_todo_errors()
invalid_numbers = []
for number in self.args[:-1]: if not todo_errors:
try:
todos.append(self.todolist.todo(number))
except InvalidTodoException:
invalid_numbers.append(number)
if len(invalid_numbers) > 1 or len(invalid_numbers) > 0 and len(todos) > 0:
for number in invalid_numbers:
self.error("Invalid todo number given: {}.".format(number))
elif len(invalid_numbers) == 1 and len(todos) == 0:
self.error("Invalid todo number given.")
else:
try: try:
priority = self.args[-1] priority = self.args[-1]
self.printer.add_filter(PrettyPrinterNumbers(self.todolist)) self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
if len(todos) > 0: if is_valid_priority(priority):
if is_valid_priority(priority): for todo in self.todos:
for todo in todos: old_priority = todo.priority()
old_priority = todo.priority() self.todolist.set_priority(todo, priority)
self.todolist.set_priority(todo, priority)
if old_priority and priority and old_priority != priority: if old_priority and priority and old_priority != priority:
self.out("Priority changed from {} to {}".format( self.out("Priority changed from {} to {}".format(
old_priority, priority)) old_priority, priority))
elif not old_priority: elif not old_priority:
self.out("Priority set to {}.".format(priority)) self.out("Priority set to {}.".format(priority))
self.out(self.printer.print_todo(todo)) self.out(self.printer.print_todo(todo))
else:
self.error("Invalid priority given.")
else: else:
self.error(self.usage()) self.error("Invalid priority given.")
except IndexError: except IndexError:
self.error(self.usage()) self.error(self.usage())
else:
for error in todo_errors:
self.error(error)
def usage(self): def usage(self):
return """Synopsis: pri <NUMBER1> [<NUMBER2> ...] <PRIORITY>""" return """Synopsis: pri <NUMBER1> [<NUMBER2> ...] <PRIORITY>"""
......
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