Commit 4986593b authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add depri subcommand to remove priorities.

Also guard the set_priority method in TodoList a bit more, to
prevent the dirty bit to be set when an equal priority is passed
on.
parent a2e13324
# 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/>.
from Command import *
from PrettyPrinter import pretty_print
from TodoList import InvalidTodoException
from Utils import *
class DepriCommand(Command):
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(DepriCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
def execute(self):
if not super(DepriCommand, self).execute():
return False
number = None
todo = None
try:
number = convert_todo_number(self.argument(0))
todo = self.todolist.todo(number)
if todo.priority() != None:
self.todolist.set_priority(todo, None)
self.out("Priority removed.")
self.out(pretty_print(todo))
except InvalidCommandArgument:
self.error(self.usage())
except (InvalidTodoNumberException, InvalidTodoException):
if not todo:
self.error( "Invalid todo number given.")
else:
self.error(self.usage())
def usage(self):
return """Synopsis: depri <NUMBER>"""
def help(self):
return """Removes the priority of the given todo item."""
......@@ -24,6 +24,7 @@ from AddCommand import AddCommand
from AppendCommand import AppendCommand
from ArchiveCommand import ArchiveCommand
from DepCommand import DepCommand
from DepriCommand import DepriCommand
import Config
from DoCommand import DoCommand
from ListCommand import ListCommand
......@@ -102,6 +103,7 @@ class CLIApplication(object):
'app': AppendCommand,
'append': AppendCommand,
'dep': DepCommand,
'depri': DepriCommand,
'do': DoCommand,
'ls': ListCommand,
'lscon': ListContextCommand,
......
......@@ -299,8 +299,9 @@ class TodoList(object):
self.dirty = True
def set_priority(self, p_todo, p_priority):
p_todo.set_priority(p_priority)
self.dirty = True
if p_todo.priority() != p_priority:
p_todo.set_priority(p_priority)
self.dirty = True
def number(self, p_todo):
try:
......
# 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/>.
import CommandTest
import DepriCommand
import TodoList
class DepriCommandTest(CommandTest.CommandTest):
def setUp(self):
todos = [
"(A) Foo",
"Bar",
]
self.todolist = TodoList.TodoList(todos)
def test_set_prio1(self):
command = DepriCommand.DepriCommand(["1"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.todolist.todo(1).priority(), None)
self.assertEquals(self.output, "Priority removed.\nFoo\n")
self.assertEquals(self.errors, "")
def test_set_prio2(self):
command = DepriCommand.DepriCommand(["2"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.todolist.todo(2).priority(), None)
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "")
def test_invalid1(self):
command = DepriCommand.DepriCommand(["99"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertFalse(self.output)
self.assertEquals(self.errors, "Invalid todo number given.\n")
def test_empty(self):
command = DepriCommand.DepriCommand([], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertFalse(self.output)
self.assertEquals(self.errors, command.usage() + "\n")
def test_help(self):
command = DepriCommand.DepriCommand(["help"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(self.output, "")
self.assertEquals(self.errors, command.usage() + "\n\n" + command.help() + "\n")
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