]> 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:

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