Commit e3efa5e9 authored by Jacek Sowiński's avatar Jacek Sowiński Committed by Bram Schoenmakers

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

Conflicts:
	topydo/lib/EditCommand.py
parent d05b856d
...@@ -18,7 +18,7 @@ import os ...@@ -18,7 +18,7 @@ import os
from subprocess import call, check_call, CalledProcessError from subprocess import call, check_call, CalledProcessError
import tempfile import tempfile
from topydo.lib.ListCommand import ListCommand from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.MultiCommand import MultiCommand from topydo.lib.MultiCommand import MultiCommand
from topydo.lib.Config import config from topydo.lib.Config import config
from topydo.lib.Todo import Todo from topydo.lib.Todo import Todo
...@@ -30,7 +30,7 @@ from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers ...@@ -30,7 +30,7 @@ from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers
# cannot use super() inside the class itself # cannot use super() inside the class itself
BASE_TODOLIST = lambda tl: super(TodoList, tl) 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): def __init__(self, p_args, p_todolist, p_output, p_error, p_input):
super(EditCommand, self).__init__(p_args, p_todolist, p_output, super(EditCommand, self).__init__(p_args, p_todolist, p_output,
p_error, p_input) p_error, p_input)
......
# 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)
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
import re import re
from topydo.lib.Command import Command from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.Config import config from topydo.lib.Config import config
from topydo.lib import Filter from topydo.lib import Filter
from topydo.lib.PrettyPrinterFilter import ( from topydo.lib.PrettyPrinterFilter import (
...@@ -26,7 +26,7 @@ from topydo.lib.PrettyPrinterFilter import ( ...@@ -26,7 +26,7 @@ from topydo.lib.PrettyPrinterFilter import (
from topydo.lib.Sorter import Sorter from topydo.lib.Sorter import Sorter
from topydo.lib.View import View from topydo.lib.View import View
class ListCommand(Command): class ListCommand(ExpressionCommand):
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,
...@@ -48,42 +48,6 @@ class ListCommand(Command): ...@@ -48,42 +48,6 @@ class ListCommand(Command):
self.args = args 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): def _print(self):
""" Prints the todos. """ """ Prints the todos. """
indent = config().list_indent() indent = config().list_indent()
......
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