Commit a7d51550 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Simplify TagCommand by eliminating the subsubcommands.

parent 30c961bf
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 Bram Schoenmakers <me@bramschoenmakers.nl>
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
......@@ -28,9 +28,9 @@ class TagCommandTest(CommandTest.CommandTest):
]
self.todolist = TodoList.TodoList(todos)
def test_add_tag1(self):
command = TagCommand.TagCommand(["add", "1", "due", "2014-10-22"], self.todolist, self.out, self.error)
command = TagCommand.TagCommand(["1", "due", "2014-10-22"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(self.todolist.todo(1).source(), "Foo due:2014-10-22")
......@@ -38,16 +38,8 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
self.assertTrue(self.todolist.is_dirty())
def test_add_tag2(self):
command = TagCommand.TagCommand(["add", "1", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Missing value for tag.\n" + command.usage() + "\n")
self.assertFalse(self.todolist.is_dirty())
def test_add_tag3(self):
command = TagCommand.TagCommand(["add", "2", "due", "2014-10-19"], self.todolist, self.out, self.error)
command = TagCommand.TagCommand(["-f", "2", "due", "2014-10-19"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(self.todolist.todo(2).source(), "Bar due:2014-10-22 due:2014-10-19")
......@@ -55,40 +47,8 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
self.assertTrue(self.todolist.is_dirty())
def test_add_tag4(self):
command = TagCommand.TagCommand(["add", "1", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Missing value for tag.\n" + command.usage() + "\n")
self.assertFalse(self.todolist.is_dirty())
def test_set_tag1(self):
command = TagCommand.TagCommand(["set", "1", "due", "2014-10-22"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, " 1 Foo due:2014-10-22\n")
self.assertEquals(self.errors, "")
def test_set_tag2(self):
command = TagCommand.TagCommand(["set", "1", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Missing value for tag.\n" + command.usage() + "\n")
def test_set_tag3(self):
command = TagCommand.TagCommand(["set", "2", "due", "2014-10-20"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, " 2 Bar due:2014-10-20\n")
self.assertEquals(self.errors, "")
def test_set_tag4(self):
command = TagCommand.TagCommand(["set", "3", "due", "2014-10-20"], self.todolist, self.out, self.error)
command = TagCommand.TagCommand(["3", "due", "2014-10-20"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
......@@ -96,7 +56,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_set_tag5(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "all")
command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "all")
command.execute()
self.assertTrue(self.todolist.is_dirty())
......@@ -104,7 +64,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_set_tag6(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "1")
command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "1")
command.execute()
self.assertFalse(self.todolist.is_dirty())
......@@ -112,7 +72,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_set_tag7(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "2")
command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "2")
command.execute()
self.assertTrue(self.todolist.is_dirty())
......@@ -120,7 +80,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_set_tag8(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "")
command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "")
command.execute()
self.assertFalse(self.todolist.is_dirty())
......@@ -128,7 +88,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_set_tag9(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "99")
command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "99")
command.execute()
self.assertFalse(self.todolist.is_dirty())
......@@ -136,7 +96,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_rm_tag1(self):
command = TagCommand.TagCommand(["rm", "1", "due"], self.todolist, self.out, self.error)
command = TagCommand.TagCommand(["1", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
......@@ -144,7 +104,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_rm_tag2(self):
command = TagCommand.TagCommand(["rm", "2", "due"], self.todolist, self.out, self.error)
command = TagCommand.TagCommand(["2", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
......@@ -152,7 +112,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_rm_tag3(self):
command = TagCommand.TagCommand(["rm", "4", "due"], self.todolist, self.out, self.error, lambda t: "all")
command = TagCommand.TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "all")
command.execute()
self.assertTrue(self.todolist.is_dirty())
......@@ -160,7 +120,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_rm_tag4(self):
command = TagCommand.TagCommand(["rm", "4", "due"], self.todolist, self.out, self.error, lambda t: "1")
command = TagCommand.TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "1")
command.execute()
self.assertTrue(self.todolist.is_dirty())
......@@ -168,7 +128,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_rm_tag6(self):
command = TagCommand.TagCommand(["rm", "4", "due"], self.todolist, self.out, self.error, lambda t: "99")
command = TagCommand.TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "99")
command.execute()
self.assertFalse(self.todolist.is_dirty())
......@@ -176,7 +136,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_rm_tag7(self):
command = TagCommand.TagCommand(["rm", "4", "due"], self.todolist, self.out, self.error, lambda t: "A")
command = TagCommand.TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "A")
command.execute()
self.assertFalse(self.todolist.is_dirty())
......@@ -184,7 +144,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "")
def test_rm_tag8(self):
command = TagCommand.TagCommand(["rm", "5", "due"], self.todolist, self.out, self.error)
command = TagCommand.TagCommand(["5", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
......@@ -192,21 +152,13 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "Invalid todo number.\n")
def test_rm_tag9(self):
command = TagCommand.TagCommand(["rm", "A", "due"], self.todolist, self.out, self.error)
command = TagCommand.TagCommand(["A", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Invalid todo number.\n")
def test_invalid_subsubcommand(self):
command = TagCommand.TagCommand(["foo", "bar"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, command.usage() + "\n")
def test_help(self):
command = TagCommand.TagCommand(["help"], self.todolist, self.out, self.error)
command.execute()
......
......@@ -26,21 +26,35 @@ class TagCommand(Command):
p_prompt=lambda a: None):
super(TagCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.subsubcommand = None
self.force = False
self.todo = None
self.tag = None
self.value = None
self.values = []
def _process_flags(self):
flags, args = self.getopt("f")
for flag, value in flags:
if flag == "-f":
self.force = True
self.args = args
def _process_args(self):
self._process_flags()
try:
self.subsubcommand = self.argument(0)
number = convert_todo_number(self.argument(1))
number = convert_todo_number(self.argument(0))
self.todo = self.todolist.todo(number)
self.tag = self.argument(2)
self.tag = self.argument(1)
self.current_values = self.todo.tag_values(self.tag)
self.value = self.argument(3)
except (InvalidCommandArgument, InvalidTodoNumberException, TodoList.InvalidTodoException):
pass
self.error("Invalid todo number.")
try:
self.value = self.argument(2)
except InvalidCommandArgument:
self.value = ""
def _print(self):
self.out(pretty_print(self.todo, [self.todolist.pp_number()]))
......@@ -65,63 +79,41 @@ class TagCommand(Command):
return answer
def _add(self):
self._set(True)
def _set_helper(self, p_force, p_old_value=""):
def _set_helper(self, p_old_value=""):
old_src = self.todo.source()
self.todo.set_tag(self.tag, self.value, p_force, p_old_value)
self.todo.set_tag(self.tag, self.value, self.force, p_old_value)
if old_src != self.todo.source():
self.todolist.set_dirty()
def _set(self, p_force_add=False):
if self.value == None:
self.error("Missing value for tag.")
self.error(self.usage())
else:
if len(self.current_values) > 1:
answer = self._choose()
if answer == "all":
for value in self.current_values:
self._set_helper(False, value)
elif answer != None and self.value != self.current_values[answer]:
self._set_helper(False, self.current_values[answer])
def _set(self):
if len(self.current_values) > 1:
answer = self._choose()
else: # if not self.todo.has_tag(self.tag, self.value):
self._set_helper(p_force_add)
if answer == "all":
for value in self.current_values:
self._set_helper(value)
elif answer != None and self.value != self.current_values[answer]:
self._set_helper(self.current_values[answer])
self._print()
else:
self._set_helper()
def _rm(self):
self.value = ""
self._set()
self._print()
def execute(self):
if not super(TagCommand, self).execute():
return False
dispatch = {
"add": self._add,
"set": self._set,
"del": self._rm,
"rm": self._rm,
}
if self.subsubcommand in dispatch and self.todo and self.tag:
dispatch[self.subsubcommand]()
elif self.subsubcommand not in dispatch:
self.error(self.usage())
elif not self.todo:
self.error("Invalid todo number.")
self._process_args()
if self.todo and self.tag:
self._set()
def usage(self):
return """Synopsis:
tag (add|set) <NUMBER> <tag> <value>
tag rm <NUMBER> <tag> [value]"""
return """Synopsis: tag <NUMBER> <tag> [<value>]"""
def help(self):
return """* add: Add a tag to the given todo.
* set: Changes a tag of the given todo.
* rm: Removes a tag from the given todo."""
return """Sets the given tag to the given todo number with the given value. If
the value is omitted, the tag is removed from the todo item.
"""
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