Commit dcb5891d authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add sort subcommand to sort a todo.txt file.

parent b3817f3e
......@@ -33,10 +33,13 @@ Subcommands implemented:
* lscon
* lsprj
* pri
* sort [xx]
Subcommands marked as [x] don't exist in the todo.txt CLI, but were added by
todo.txt-tools.
Subcommands marked as [xx] are new in Topydo.
Motivation
----------
......
......@@ -34,6 +34,7 @@ from ListContextCommand import ListContextCommand
from ListProjectCommand import ListProjectCommand
from PrettyPrinter import *
from PriorityCommand import PriorityCommand
from SortCommand import SortCommand
import TodoFile
import TodoList
from Utils import escape_ansi
......@@ -119,6 +120,7 @@ class CLIApplication(object):
'listprojects': ListProjectCommand,
'pri': PriorityCommand,
'rm': DeleteCommand,
'sort': SortCommand,
}
args = arguments()
......
# 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 *
import Config
import Filter
import Sorter
class SortCommand(Command):
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(SortCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
def execute(self):
if not super(SortCommand, self).execute():
return False
try:
expression = self.argument(0)
except InvalidCommandArgument:
expression = Config.SORT_STRING
sorter = Sorter.Sorter(expression) # TODO: validate
sorted_todos = sorter.sort(self.todolist.todos())
self.todolist.erase()
for todo in sorted_todos:
self.todolist.add_todo(todo)
def usage(self):
return """Synopsis: sort [expression]"""
def help(self):
return """Sorts the file according to the expression. If no expression is given,
the expression in the configuration is used.
The following sort properties are supported:
* creation - Sort by creation date
* completed - Sort by completion state
* importance - Sort by importance
* importance-avg - Sort by average importance (based on parent items)
* text - Sort by text
* <tag> - Sort by values of the given tag
Any property can be prefixed with 'asc:' and 'desc:' to specify the sort order.
The default is ascending sort.
"""
# 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 Config
import SortCommand
import TestFacilities
class SortCommandTest(CommandTest.CommandTest):
def setUp(self):
self.todolist = TestFacilities.load_file_to_todolist("data/SorterTest1.txt")
def test_sort1(self):
""" Alphabetically sorted """
command = SortCommand.SortCommand(["text"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(str(self.todolist), "First\n(A) Foo\n2014-06-14 Last")
def test_sort2(self):
command = SortCommand.SortCommand([], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(str(self.todolist), "(A) Foo\n2014-06-14 Last\nFirst")
def test_help(self):
command = SortCommand.SortCommand(["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