File: lazy.py

package info (click to toggle)
python-tasklib 2.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 240 kB
  • sloc: python: 2,257; makefile: 147
file content (240 lines) | stat: -rw-r--r-- 6,470 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""
Provides lazy implementations for Task and TaskQuerySet.
"""


class LazyUUIDTask(object):
    """
    A lazy wrapper around Task object, referenced by UUID.

    - Supports comparison with LazyUUIDTask or Task objects (equality by UUIDs)
    - If any attribute other than 'uuid' requested, a lookup in the
      backend will be performed and this object will be replaced by a proper
      Task object.
    """

    def __init__(self, tw, uuid):
        self._tw = tw
        self._uuid = uuid

    def __getitem__(self, key):
        # LazyUUIDTask does not provide anything else other than 'uuid'
        if key == 'uuid':
            return self._uuid
        else:
            self.replace()
            return self[key]

    def __getattr__(self, name):
        # Getattr is called only if the attribute could not be found using
        # normal means
        self.replace()
        return getattr(self, name)

    def __eq__(self, other):
        if other and other['uuid']:
            # For saved Tasks, just define equality by equality of uuids
            return self['uuid'] == other['uuid']

    def __ne__(self, other):
        return not self.__eq__(other)

    def __hash__(self):
        return self['uuid'].__hash__()

    def __repr__(self):
        return 'LazyUUIDTask: {0}'.format(self._uuid)

    def __copy__(self):
        return LazyUUIDTask(self._tw, self._uuid)

    def __deepcopy__(self, memo):
        return LazyUUIDTask(self._tw, self._uuid)

    @property
    def saved(self):
        """
        Implementation of the 'saved' property. Always returns True.
        """
        return True

    @property
    def _modified_fields(self):
        return set()

    @property
    def modified(self):
        return False

    def replace(self):
        """
        Performs conversion to the regular Task object, referenced by the
        stored UUID.
        """
        replacement = self._tw.get_task(self._uuid)
        self.__class__ = replacement.__class__
        self.__dict__ = replacement.__dict__


class LazyUUIDTaskSet(object):
    """
    A lazy wrapper around TaskQuerySet object, for tasks referenced by UUID.

    - Supports 'in' operator with LazyUUIDTask or Task objects
    - If iteration over the objects in the LazyUUIDTaskSet is requested, the
      LazyUUIDTaskSet will be converted to QuerySet and evaluated
    """

    def __init__(self, tw, uuids):
        self._tw = tw
        self._uuids = set(uuids)

    def __getattr__(self, name):
        # Getattr is called only if the attribute could not be found using
        # normal means

        if name.startswith('__'):
            # If some internal method was being search, do not convert
            # to TaskQuerySet just because of that
            raise AttributeError
        else:
            self.replace()
            return getattr(self, name)

    def __repr__(self):
        return 'LazyUUIDTaskSet([{0}])'.format(', '.join(self._uuids))

    def __eq__(self, other):
        return (set(t['uuid'] for t in other) if other else set()) == self._uuids

    def __ne__(self, other):
        return not (self == other)

    def __contains__(self, task):
        return task['uuid'] in self._uuids

    def __len__(self):
        return len(self._uuids)

    def __iter__(self):
        for uuid in self._uuids:
            yield LazyUUIDTask(self._tw, uuid)

    def __sub__(self, other):
        return self.difference(other)

    def __isub__(self, other):
        return self.difference_update(other)

    def __rsub__(self, other):
        return LazyUUIDTaskSet(
            self._tw,
            set(t['uuid'] for t in other) - self._uuids,
        )

    def __or__(self, other):
        return self.union(other)

    def __ior__(self, other):
        return self.update(other)

    def __ror__(self, other):
        return self.union(other)

    def __xor__(self, other):
        return self.symmetric_difference(other)

    def __ixor__(self, other):
        return self.symmetric_difference_update(other)

    def __rxor__(self, other):
        return self.symmetric_difference(other)

    def __and__(self, other):
        return self.intersection(other)

    def __iand__(self, other):
        return self.intersection_update(other)

    def __rand__(self, other):
        return self.intersection(other)

    def __le__(self, other):
        return self.issubset(other)

    def __ge__(self, other):
        return self.issuperset(other)

    def __lt__(self, other):
        return self._uuids < set(t['uuid'] for t in other)

    def __gt__(self, other):
        return self._uuids > set(t['uuid'] for t in other)

    def issubset(self, other):
        return self._uuids <= set(t['uuid'] for t in other)

    def issuperset(self, other):
        return self._uuids >= set(t['uuid'] for t in other)

    def union(self, other):
        return LazyUUIDTaskSet(
            self._tw,
            self._uuids | set(t['uuid'] for t in other),
        )

    def intersection(self, other):
        return LazyUUIDTaskSet(
            self._tw,
            self._uuids & set(t['uuid'] for t in other),
        )

    def difference(self, other):
        return LazyUUIDTaskSet(
            self._tw,
            self._uuids - set(t['uuid'] for t in other),
        )

    def symmetric_difference(self, other):
        return LazyUUIDTaskSet(
            self._tw,
            self._uuids ^ set(t['uuid'] for t in other),
        )

    def update(self, other):
        self._uuids |= set(t['uuid'] for t in other)
        return self

    def intersection_update(self, other):
        self._uuids &= set(t['uuid'] for t in other)
        return self

    def difference_update(self, other):
        self._uuids -= set(t['uuid'] for t in other)
        return self

    def symmetric_difference_update(self, other):
        self._uuids ^= set(t['uuid'] for t in other)
        return self

    def add(self, task):
        self._uuids.add(task['uuid'])

    def remove(self, task):
        self._uuids.remove(task['uuid'])

    def pop(self):
        return self._uuids.pop()

    def clear(self):
        self._uuids.clear()

    def replace(self):
        """
        Performs conversion to the regular TaskQuerySet object, referenced by
        the stored UUIDs.
        """

        replacement = self._tw.tasks.filter(' '.join(self._uuids))
        self.__class__ = replacement.__class__
        self.__dict__ = replacement.__dict__