Commit 6ab221a5 authored by Jacek Sowiński's avatar Jacek Sowiński

Add new ExpressionCommand class

Currently it contains only `_view` and `_filters` methods, which were
previously defined in ListCommand. Now both EditCommand and ListCommand
will inherit from it to solve problems inside EditCommand.

This fixes #28
parent 125dd8f1
......@@ -20,7 +20,7 @@ import tempfile
from six import text_type, u
from topydo.commands.ListCommand import ListCommand
from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.MultiCommand import MultiCommand
from topydo.lib.Config import config
from topydo.lib.Todo import Todo
......@@ -32,7 +32,7 @@ from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers
# cannot use super() inside the class itself
BASE_TODOLIST = lambda tl: super(TodoList, tl)
class EditCommand(MultiCommand, ListCommand):
class EditCommand(MultiCommand, ExpressionCommand):
def __init__(self, p_args, p_todolist, p_output, p_error, p_input):
super(EditCommand, self).__init__(p_args, p_todolist, p_output,
p_error, p_input)
......
......@@ -16,7 +16,7 @@
import re
from topydo.lib.Command import Command
from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.Config import config
from topydo.lib import Filter
from topydo.lib.PrettyPrinterFilter import (
......@@ -26,7 +26,7 @@ from topydo.lib.PrettyPrinterFilter import (
from topydo.lib.Sorter import Sorter
from topydo.lib.View import View
class ListCommand(Command):
class ListCommand(ExpressionCommand):
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
......@@ -48,42 +48,6 @@ class ListCommand(Command):
self.args = args
def _filters(self):
filters = []
def arg_filters():
result = []
for arg in self.args:
if re.match(Filter.ORDINAL_TAG_MATCH, arg):
argfilter = Filter.OrdinalTagFilter(arg)
elif len(arg) > 1 and arg[0] == '-':
# when a word starts with -, exclude it
argfilter = Filter.GrepFilter(arg[1:])
argfilter = Filter.NegationFilter(argfilter)
else:
argfilter = Filter.GrepFilter(arg)
result.append(argfilter)
return result
if not self.show_all:
filters.append(Filter.DependencyFilter(self.todolist))
filters.append(Filter.RelevanceFilter())
filters += arg_filters()
if not self.show_all:
filters.append(Filter.LimitFilter(config().list_limit()))
return filters
def _view(self):
sorter = Sorter(self.sort_expression)
filters = self._filters()
return View(sorter, filters, self.todolist, self.printer)
def _print(self):
""" Prints the todos. """
indent = config().list_indent()
......
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 - 2015 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 re
from topydo.lib.Command import Command
from topydo.lib.Config import config
from topydo.lib import Filter
from topydo.lib.Sorter import Sorter
from topydo.lib.View import View
class ExpressionCommand(Command):
"""
A common class for commands operating on todos selected by expressions.
"""
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(ExpressionCommand, self).__init__(
p_args, p_todolist, p_out, p_err, p_prompt)
self.sort_expression = config().sort_string()
self.show_all = False
def _filters(self):
filters = []
def arg_filters():
result = []
for arg in self.args:
if re.match(Filter.ORDINAL_TAG_MATCH, arg):
argfilter = Filter.OrdinalTagFilter(arg)
elif len(arg) > 1 and arg[0] == '-':
# when a word starts with -, exclude it
argfilter = Filter.GrepFilter(arg[1:])
argfilter = Filter.NegationFilter(argfilter)
else:
argfilter = Filter.GrepFilter(arg)
result.append(argfilter)
return result
if not self.show_all:
filters.append(Filter.DependencyFilter(self.todolist))
filters.append(Filter.RelevanceFilter())
filters += arg_filters()
if not self.show_all:
filters.append(Filter.LimitFilter(config().list_limit()))
return filters
def _view(self):
sorter = Sorter(self.sort_expression)
filters = self._filters()
return View(sorter, filters, self.todolist, self.printer)
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