]> git.madduck.net Git - etc/taskwarrior.git/blob - tasklib/filters.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge pull request #49 from lyz-code/improve/update-gitignore
[etc/taskwarrior.git] / tasklib / filters.py
1 import abc
2 import six
3 from .serializing import SerializingObject
4
5
6 class TaskFilter(object):
7     """
8     Abstract base class that defines interface of a TaskFilter.
9     """
10
11     @abc.abstractmethod
12     def add_filter(self, arg):
13         """
14         Processes an non-keyword filter.
15         """
16         pass
17
18     @abc.abstractmethod
19     def add_filter_param(self, key, value):
20         """
21         Processes a keyword filter.
22         """
23         pass
24
25     @abc.abstractmethod
26     def clone(self):
27         """
28         Returns a new deep copy of itself.
29         """
30         pass
31
32
33 class TaskWarriorFilter(TaskFilter, SerializingObject):
34     """
35     A set of parameters to filter the task list with.
36     """
37
38     def __init__(self, backend, filter_params=None):
39         self.filter_params = filter_params or []
40         super(TaskFilter, self).__init__(backend)
41
42     def add_filter(self, filter_str):
43         self.filter_params.append(filter_str)
44
45     def add_filter_param(self, key, value):
46         key = key.replace('__', '.')
47
48         # Replace the value with empty string, since that is the
49         # convention in TW for empty values
50         attribute_key = key.split('.')[0]
51
52         # Since this is user input, we need to normalize before we serialize
53         value = self._normalize(attribute_key, value)
54         value = self._serialize(attribute_key, value)
55
56         # If we are filtering by uuid:, do not use uuid keyword
57         # due to TW-1452 bug
58         if key == 'uuid':
59             self.filter_params.insert(0, value)
60         else:
61             # Surround value with aphostrophes unless it's a empty string
62             value = "'%s'" % value if value else ''
63
64             # We enforce equality match by using 'is' (or 'none') modifier
65             # Without using this syntax, filter fails due to TW-1479
66             # which is, however, fixed in 2.4.5
67             if self.backend.version < self.backend.VERSION_2_4_5:
68                 modifier = '.is' if value else '.none'
69                 key = key + modifier if '.' not in key else key
70
71             self.filter_params.append(six.u("{0}:{1}").format(key, value))
72
73     def get_filter_params(self):
74         return [f for f in self.filter_params if f]
75
76     def clone(self):
77         c = self.__class__(self.backend)
78         c.filter_params = list(self.filter_params)
79         return c