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

0050547c9bd5c8c17b034bcabe9a3c10279d1ce9
[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 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 replace(self):
43         """
44         Performs conversion to the regular Task object, referenced by the
45         stored UUID.
46         """
47
48         replacement = self._tw.tasks.get(uuid=self._uuid)
49         self.__class__ = replacement.__class__
50         self.__dict__ = replacement.__dict__
51
52
53 class LazyUUIDTaskSet(object):
54     """
55     A lazy wrapper around TaskQuerySet object, for tasks referenced by UUID.
56
57     - Supports 'in' operator with LazyUUIDTask or Task objects
58     - If iteration over the objects in the LazyUUIDTaskSet is requested, the
59       LazyUUIDTaskSet will be converted to QuerySet and evaluated
60     """
61
62     def __init__(self, tw, uuids):
63         self._tw = tw
64         self._uuids = set(uuids)
65
66     def __getattr__(self, name):
67         # Getattr is called only if the attribute could not be found using
68         # normal means
69
70         if name.startswith('__'):
71             # If some internal method was being search, do not convert
72             # to TaskQuerySet just because of that
73             raise AttributeError
74         else:
75             self.replace()
76             return self.name
77
78     def __eq__(self, other):
79         return set(t['uuid'] for t in other) == self._uuids
80
81     def __contains__(self, task):
82         return task['uuid'] in self._uuids
83
84     def __len__(self):
85         return len(self._uuids)
86
87     def __iter__(self):
88         self.replace()
89         for task in self:
90             yield task
91
92     def replace(self):
93         """
94         Performs conversion to the regular TaskQuerySet object, referenced by
95         the stored UUIDs.
96         """
97
98         replacement = self._tw.tasks.filter(' '.join(self._uuids))
99         self.__class__ = replacement.__class__
100         self.__dict__ = replacement.__dict__