]> git.madduck.net Git - etc/taskwarrior.git/blob - tasklib/lazy.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:

LazyUUIDTaskSet: Add reverse operator implementations
[etc/taskwarrior.git] / tasklib / lazy.py
1 """
2 Provides lazy implementations for Task and TaskQuerySet.
3 """
4
5
6 class LazyUUIDTask(object):
7     """
8     A lazy wrapper around Task object, referenced by UUID.
9
10     - Supports comparison with LazyUUIDTask or Task objects (equality by UUIDs)
11     - If any attribute other than 'uuid' requested, a lookup in the
12       backend will be performed and this object will be replaced by a proper
13       Task object.
14     """
15
16     def __init__(self, tw, uuid):
17         self._tw = tw
18         self._uuid = uuid
19
20     def __getitem__(self, key):
21         # LazyUUIDTask does not provide anything else other than 'uuid'
22         if key is 'uuid':
23             return self._uuid
24         else:
25             self.replace()
26             return self[key]
27
28     def __getattr__(self, name):
29         # Getattr is called only if the attribute could not be found using
30         # normal means
31         self.replace()
32         return getattr(self, name)
33
34     def __eq__(self, other):
35         if other['uuid']:
36             # For saved Tasks, just define equality by equality of uuids
37             return self['uuid'] == other['uuid']
38
39     def __hash__(self):
40         return self['uuid'].__hash__()
41
42     def __repr__(self):
43         return "LazyUUIDTask: {0}".format(self._uuid)
44
45     @property
46     def saved(self):
47         """
48         Implementation of the 'saved' property. Always returns True.
49         """
50         return True
51
52     def replace(self):
53         """
54         Performs conversion to the regular Task object, referenced by the
55         stored UUID.
56         """
57
58         replacement = self._tw.tasks.get(uuid=self._uuid)
59         self.__class__ = replacement.__class__
60         self.__dict__ = replacement.__dict__
61
62
63 class LazyUUIDTaskSet(object):
64     """
65     A lazy wrapper around TaskQuerySet object, for tasks referenced by UUID.
66
67     - Supports 'in' operator with LazyUUIDTask or Task objects
68     - If iteration over the objects in the LazyUUIDTaskSet is requested, the
69       LazyUUIDTaskSet will be converted to QuerySet and evaluated
70     """
71
72     def __init__(self, tw, uuids):
73         self._tw = tw
74         self._uuids = set(uuids)
75
76     def __getattr__(self, name):
77         # Getattr is called only if the attribute could not be found using
78         # normal means
79
80         if name.startswith('__'):
81             # If some internal method was being search, do not convert
82             # to TaskQuerySet just because of that
83             raise AttributeError
84         else:
85             self.replace()
86             return getattr(self, name)
87
88     def __repr__(self):
89         return "LazyUUIDTaskSet([{0}])".format(', '.join(self._uuids))
90
91     def __eq__(self, other):
92         return set(t['uuid'] for t in other) == self._uuids
93
94     def __ne__(self, other):
95         return not (self == other)
96
97     def __contains__(self, task):
98         return task['uuid'] in self._uuids
99
100     def __len__(self):
101         return len(self._uuids)
102
103     def __iter__(self):
104         for uuid in self._uuids:
105             yield LazyUUIDTask(self._tw, uuid)
106
107     def __sub__(self, other):
108         return self.difference(other)
109
110     def __isub__(self, other):
111         return self.difference_update(other)
112
113     def __rsub__(self, other):
114         return LazyUUIDTaskSet(self._tw,
115             set(t['uuid'] for t in other) - self._uuids)
116
117     def __or__(self, other):
118         return self.union(other)
119
120     def __ior__(self, other):
121         return self.update(other)
122
123     def __ror__(self, other):
124         return self.union(other)
125
126     def __xor__(self, other):
127         return self.symmetric_difference(other)
128
129     def __ixor__(self, other):
130         return self.symmetric_difference_update(other)
131
132     def __rxor__(self, other):
133         return self.symmetric_difference(other)
134
135     def __le__(self, other):
136         return self.issubset(other)
137
138     def __ge__(self, other):
139         return self.issuperset(other)
140
141     def issubset(self, other):
142         return all([task in other for task in self])
143
144     def issuperset(self, other):
145         return all([task in self for task in other])
146
147     def union(self, other):
148         return LazyUUIDTaskSet(self._tw,
149             self._uuids | set(t['uuid'] for t in other))
150
151     def intersection(self, other):
152         return LazyUUIDTaskSet(self._tw,
153             self._uuids & set(t['uuid'] for t in other))
154
155     def difference(self, other):
156         return LazyUUIDTaskSet(self._tw,
157             self._uuids - set(t['uuid'] for t in other))
158
159     def symmetric_difference(self, other):
160         return LazyUUIDTaskSet(self._tw,
161             self._uuids ^ set(t['uuid'] for t in other))
162
163     def update(self, other):
164         self._uuids |= set(t['uuid'] for t in other)
165         return self
166
167     def intersection_update(self, other):
168         self._uuids &= set(t['uuid'] for t in other)
169         return self
170
171     def difference_update(self, other):
172         self._uuids -= set(t['uuid'] for t in other)
173         return self
174
175     def symmetric_difference_update(self, other):
176         self._uuids ^= set(t['uuid'] for t in other)
177         return self
178
179     def add(self, task):
180         self._uuids.add(task['uuid'])
181
182     def remove(self, task):
183         self._uuids.remove(task['uuid'])
184
185     def pop(self):
186         return self._uuids.pop()
187
188     def clear(self):
189         self._uuids.clear()
190
191     def replace(self):
192         """
193         Performs conversion to the regular TaskQuerySet object, referenced by
194         the stored UUIDs.
195         """
196
197         replacement = self._tw.tasks.filter(' '.join(self._uuids))
198         self.__class__ = replacement.__class__
199         self.__dict__ = replacement.__dict__