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

backends: Move TaskWarrior class into the backends file
[etc/taskwarrior.git] / tasklib / task.py
1 from __future__ import print_function
2 import copy
3 import datetime
4 import json
5 import logging
6 import os
7 import pytz
8 import six
9 import sys
10 import tzlocal
11
12 from backends import TaskWarrior
13
14 DATE_FORMAT = '%Y%m%dT%H%M%SZ'
15 DATE_FORMAT_CALC = '%Y-%m-%dT%H:%M:%S'
16 REPR_OUTPUT_SIZE = 10
17 PENDING = 'pending'
18 COMPLETED = 'completed'
19
20 logger = logging.getLogger(__name__)
21 local_zone = tzlocal.get_localzone()
22
23
24 class TaskWarriorException(Exception):
25     pass
26
27
28 class ReadOnlyDictView(object):
29     """
30     Provides simplified read-only view upon dict object.
31     """
32
33     def __init__(self, viewed_dict):
34         self.viewed_dict = viewed_dict
35
36     def __getitem__(self, key):
37         return copy.deepcopy(self.viewed_dict.__getitem__(key))
38
39     def __contains__(self, k):
40         return self.viewed_dict.__contains__(k)
41
42     def __iter__(self):
43         for value in self.viewed_dict:
44             yield copy.deepcopy(value)
45
46     def __len__(self):
47         return len(self.viewed_dict)
48
49     def get(self, key, default=None):
50         return copy.deepcopy(self.viewed_dict.get(key, default))
51
52     def items(self):
53         return [copy.deepcopy(v) for v in self.viewed_dict.items()]
54
55     def values(self):
56         return [copy.deepcopy(v) for v in self.viewed_dict.values()]
57
58
59 class SerializingObject(object):
60     """
61     Common ancestor for TaskResource & TaskFilter, since they both
62     need to serialize arguments.
63
64     Serializing method should hold the following contract:
65       - any empty value (meaning removal of the attribute)
66         is deserialized into a empty string
67       - None denotes a empty value for any attribute
68
69     Deserializing method should hold the following contract:
70       - None denotes a empty value for any attribute (however,
71         this is here as a safeguard, TaskWarrior currently does
72         not export empty-valued attributes) if the attribute
73         is not iterable (e.g. list or set), in which case
74         a empty iterable should be used.
75
76     Normalizing methods should hold the following contract:
77       - They are used to validate and normalize the user input.
78         Any attribute value that comes from the user (during Task
79         initialization, assignign values to Task attributes, or
80         filtering by user-provided values of attributes) is first
81         validated and normalized using the normalize_{key} method.
82       - If validation or normalization fails, normalizer is expected
83         to raise ValueError.
84     """
85
86     def __init__(self, warrior):
87         self.warrior = warrior
88
89     def _deserialize(self, key, value):
90         hydrate_func = getattr(self, 'deserialize_{0}'.format(key),
91                                lambda x: x if x != '' else None)
92         return hydrate_func(value)
93
94     def _serialize(self, key, value):
95         dehydrate_func = getattr(self, 'serialize_{0}'.format(key),
96                                  lambda x: x if x is not None else '')
97         return dehydrate_func(value)
98
99     def _normalize(self, key, value):
100         """
101         Use normalize_<key> methods to normalize user input. Any user
102         input will be normalized at the moment it is used as filter,
103         or entered as a value of Task attribute.
104         """
105
106         # None value should not be converted by normalizer
107         if value is None:
108             return None
109
110         normalize_func = getattr(self, 'normalize_{0}'.format(key),
111                                  lambda x: x)
112
113         return normalize_func(value)
114
115     def timestamp_serializer(self, date):
116         if not date:
117             return ''
118
119         # Any serialized timestamp should be localized, we need to
120         # convert to UTC before converting to string (DATE_FORMAT uses UTC)
121         date = date.astimezone(pytz.utc)
122
123         return date.strftime(DATE_FORMAT)
124
125     def timestamp_deserializer(self, date_str):
126         if not date_str:
127             return None
128
129         # Return timestamp localized in the local zone
130         naive_timestamp = datetime.datetime.strptime(date_str, DATE_FORMAT)
131         localized_timestamp = pytz.utc.localize(naive_timestamp)
132         return localized_timestamp.astimezone(local_zone)
133
134     def serialize_entry(self, value):
135         return self.timestamp_serializer(value)
136
137     def deserialize_entry(self, value):
138         return self.timestamp_deserializer(value)
139
140     def normalize_entry(self, value):
141         return self.datetime_normalizer(value)
142
143     def serialize_modified(self, value):
144         return self.timestamp_serializer(value)
145
146     def deserialize_modified(self, value):
147         return self.timestamp_deserializer(value)
148
149     def normalize_modified(self, value):
150         return self.datetime_normalizer(value)
151
152     def serialize_start(self, value):
153         return self.timestamp_serializer(value)
154
155     def deserialize_start(self, value):
156         return self.timestamp_deserializer(value)
157
158     def normalize_start(self, value):
159         return self.datetime_normalizer(value)
160
161     def serialize_end(self, value):
162         return self.timestamp_serializer(value)
163
164     def deserialize_end(self, value):
165         return self.timestamp_deserializer(value)
166
167     def normalize_end(self, value):
168         return self.datetime_normalizer(value)
169
170     def serialize_due(self, value):
171         return self.timestamp_serializer(value)
172
173     def deserialize_due(self, value):
174         return self.timestamp_deserializer(value)
175
176     def normalize_due(self, value):
177         return self.datetime_normalizer(value)
178
179     def serialize_scheduled(self, value):
180         return self.timestamp_serializer(value)
181
182     def deserialize_scheduled(self, value):
183         return self.timestamp_deserializer(value)
184
185     def normalize_scheduled(self, value):
186         return self.datetime_normalizer(value)
187
188     def serialize_until(self, value):
189         return self.timestamp_serializer(value)
190
191     def deserialize_until(self, value):
192         return self.timestamp_deserializer(value)
193
194     def normalize_until(self, value):
195         return self.datetime_normalizer(value)
196
197     def serialize_wait(self, value):
198         return self.timestamp_serializer(value)
199
200     def deserialize_wait(self, value):
201         return self.timestamp_deserializer(value)
202
203     def normalize_wait(self, value):
204         return self.datetime_normalizer(value)
205
206     def serialize_annotations(self, value):
207         value = value if value is not None else []
208
209         # This may seem weird, but it's correct, we want to export
210         # a list of dicts as serialized value
211         serialized_annotations = [json.loads(annotation.export_data())
212                                   for annotation in value]
213         return serialized_annotations if serialized_annotations else ''
214
215     def deserialize_annotations(self, data):
216         return [TaskAnnotation(self, d) for d in data] if data else []
217
218     def serialize_tags(self, tags):
219         return ','.join(tags) if tags else ''
220
221     def deserialize_tags(self, tags):
222         if isinstance(tags, six.string_types):
223             return tags.split(',') if tags else []
224         return tags or []
225
226     def serialize_depends(self, value):
227         # Return the list of uuids
228         value = value if value is not None else set()
229         return ','.join(task['uuid'] for task in value)
230
231     def deserialize_depends(self, raw_uuids):
232         raw_uuids = raw_uuids or []  # Convert None to empty list
233
234         # TW 2.4.4 encodes list of dependencies as a single string
235         if type(raw_uuids) is not list:
236             uuids = raw_uuids.split(',')
237         # TW 2.4.5 and later exports them as a list, no conversion needed
238         else:
239             uuids = raw_uuids
240
241         return set(self.warrior.tasks.get(uuid=uuid) for uuid in uuids if uuid)
242
243     def datetime_normalizer(self, value):
244         """
245         Normalizes date/datetime value (considered to come from user input)
246         to localized datetime value. Following conversions happen:
247
248         naive date -> localized datetime with the same date, and time=midnight
249         naive datetime -> localized datetime with the same value
250         localized datetime -> localized datetime (no conversion)
251         """
252
253         if (isinstance(value, datetime.date)
254             and not isinstance(value, datetime.datetime)):
255             # Convert to local midnight
256             value_full = datetime.datetime.combine(value, datetime.time.min)
257             localized = local_zone.localize(value_full)
258         elif isinstance(value, datetime.datetime):
259             if value.tzinfo is None:
260                 # Convert to localized datetime object
261                 localized = local_zone.localize(value)
262             else:
263                 # If the value is already localized, there is no need to change
264                 # time zone at this point. Also None is a valid value too.
265                 localized = value
266         elif (isinstance(value, six.string_types)
267                 and self.warrior.version >= VERSION_2_4_0):
268             # For strings, use 'task calc' to evaluate the string to datetime
269             # available since TW 2.4.0
270             args = value.split()
271             result = self.warrior.execute_command(['calc'] + args)
272             naive = datetime.datetime.strptime(result[0], DATE_FORMAT_CALC)
273             localized = local_zone.localize(naive)
274         else:
275             raise ValueError("Provided value could not be converted to "
276                              "datetime, its type is not supported: {}"
277                              .format(type(value)))
278
279         return localized
280
281     def normalize_uuid(self, value):
282         # Enforce sane UUID
283         if not isinstance(value, six.string_types) or value == '':
284             raise ValueError("UUID must be a valid non-empty string, "
285                              "not: {}".format(value))
286
287         return value
288
289
290 class TaskResource(SerializingObject):
291     read_only_fields = []
292
293     def _load_data(self, data):
294         self._data = dict((key, self._deserialize(key, value))
295                           for key, value in data.items())
296         # We need to use a copy for original data, so that changes
297         # are not propagated.
298         self._original_data = copy.deepcopy(self._data)
299
300     def _update_data(self, data, update_original=False, remove_missing=False):
301         """
302         Low level update of the internal _data dict. Data which are coming as
303         updates should already be serialized. If update_original is True, the
304         original_data dict is updated as well.
305         """
306         self._data.update(dict((key, self._deserialize(key, value))
307                                for key, value in data.items()))
308
309         # In certain situations, we want to treat missing keys as removals
310         if remove_missing:
311             for key in set(self._data.keys()) - set(data.keys()):
312                 self._data[key] = None
313
314         if update_original:
315             self._original_data = copy.deepcopy(self._data)
316
317
318     def __getitem__(self, key):
319         # This is a workaround to make TaskResource non-iterable
320         # over simple index-based iteration
321         try:
322             int(key)
323             raise StopIteration
324         except ValueError:
325             pass
326
327         if key not in self._data:
328             self._data[key] = self._deserialize(key, None)
329
330         return self._data.get(key)
331
332     def __setitem__(self, key, value):
333         if key in self.read_only_fields:
334             raise RuntimeError('Field \'%s\' is read-only' % key)
335
336         # Normalize the user input before saving it
337         value = self._normalize(key, value)
338         self._data[key] = value
339
340     def __str__(self):
341         s = six.text_type(self.__unicode__())
342         if not six.PY3:
343             s = s.encode('utf-8')
344         return s
345
346     def __repr__(self):
347         return str(self)
348
349     def export_data(self):
350         """
351         Exports current data contained in the Task as JSON
352         """
353
354         # We need to remove spaces for TW-1504, use custom separators
355         data_tuples = ((key, self._serialize(key, value))
356                        for key, value in six.iteritems(self._data))
357
358         # Empty string denotes empty serialized value, we do not want
359         # to pass that to TaskWarrior.
360         data_tuples = filter(lambda t: t[1] is not '', data_tuples)
361         data = dict(data_tuples)
362         return json.dumps(data, separators=(',',':'))
363
364     @property
365     def _modified_fields(self):
366         writable_fields = set(self._data.keys()) - set(self.read_only_fields)
367         for key in writable_fields:
368             new_value = self._data.get(key)
369             old_value = self._original_data.get(key)
370
371             # Make sure not to mark data removal as modified field if the
372             # field originally had some empty value
373             if key in self._data and not new_value and not old_value:
374                 continue
375
376             if new_value != old_value:
377                 yield key
378
379     @property
380     def modified(self):
381         return bool(list(self._modified_fields))
382
383
384 class TaskAnnotation(TaskResource):
385     read_only_fields = ['entry', 'description']
386
387     def __init__(self, task, data=None):
388         self.task = task
389         self._load_data(data or dict())
390         super(TaskAnnotation, self).__init__(task.warrior)
391
392     def remove(self):
393         self.task.remove_annotation(self)
394
395     def __unicode__(self):
396         return self['description']
397
398     def __eq__(self, other):
399         # consider 2 annotations equal if they belong to the same task, and
400         # their data dics are the same
401         return self.task == other.task and self._data == other._data
402
403     __repr__ = __unicode__
404
405
406 class Task(TaskResource):
407     read_only_fields = ['id', 'entry', 'urgency', 'uuid', 'modified']
408
409     class DoesNotExist(Exception):
410         pass
411
412     class CompletedTask(Exception):
413         """
414         Raised when the operation cannot be performed on the completed task.
415         """
416         pass
417
418     class DeletedTask(Exception):
419         """
420         Raised when the operation cannot be performed on the deleted task.
421         """
422         pass
423
424     class ActiveTask(Exception):
425         """
426         Raised when the operation cannot be performed on the active task.
427         """
428         pass
429
430     class InactiveTask(Exception):
431         """
432         Raised when the operation cannot be performed on an inactive task.
433         """
434         pass
435
436     class NotSaved(Exception):
437         """
438         Raised when the operation cannot be performed on the task, because
439         it has not been saved to TaskWarrior yet.
440         """
441         pass
442
443     @classmethod
444     def from_input(cls, input_file=sys.stdin, modify=None, warrior=None):
445         """
446         Creates a Task object, directly from the stdin, by reading one line.
447         If modify=True, two lines are used, first line interpreted as the
448         original state of the Task object, and second line as its new,
449         modified value. This is consistent with the TaskWarrior's hook
450         system.
451
452         Object created by this method should not be saved, deleted
453         or refreshed, as t could create a infinite loop. For this
454         reason, TaskWarrior instance is set to None.
455
456         Input_file argument can be used to specify the input file,
457         but defaults to sys.stdin.
458         """
459
460         # Detect the hook type if not given directly
461         name = os.path.basename(sys.argv[0])
462         modify = name.startswith('on-modify') if modify is None else modify
463
464         # Create the TaskWarrior instance if none passed
465         if warrior is None:
466             hook_parent_dir = os.path.dirname(os.path.dirname(sys.argv[0]))
467             warrior = TaskWarrior(data_location=hook_parent_dir)
468
469         # TaskWarrior instance is set to None
470         task = cls(warrior)
471
472         # Load the data from the input
473         task._load_data(json.loads(input_file.readline().strip()))
474
475         # If this is a on-modify event, we are provided with additional
476         # line of input, which provides updated data
477         if modify:
478             task._update_data(json.loads(input_file.readline().strip()),
479                               remove_missing=True)
480
481         return task
482
483     def __init__(self, warrior, **kwargs):
484         super(Task, self).__init__(warrior)
485
486         # Check that user is not able to set read-only value in __init__
487         for key in kwargs.keys():
488             if key in self.read_only_fields:
489                 raise RuntimeError('Field \'%s\' is read-only' % key)
490
491         # We serialize the data in kwargs so that users of the library
492         # do not have to pass different data formats via __setitem__ and
493         # __init__ methods, that would be confusing
494
495         # Rather unfortunate syntax due to python2.6 comaptiblity
496         self._data = dict((key, self._normalize(key, value))
497                           for (key, value) in six.iteritems(kwargs))
498         self._original_data = copy.deepcopy(self._data)
499
500         # Provide read only access to the original data
501         self.original = ReadOnlyDictView(self._original_data)
502
503     def __unicode__(self):
504         return self['description']
505
506     def __eq__(self, other):
507         if self['uuid'] and other['uuid']:
508             # For saved Tasks, just define equality by equality of uuids
509             return self['uuid'] == other['uuid']
510         else:
511             # If the tasks are not saved, compare the actual instances
512             return id(self) == id(other)
513
514
515     def __hash__(self):
516         if self['uuid']:
517             # For saved Tasks, just define equality by equality of uuids
518             return self['uuid'].__hash__()
519         else:
520             # If the tasks are not saved, return hash of instance id
521             return id(self).__hash__()
522
523     @property
524     def completed(self):
525         return self['status'] == six.text_type('completed')
526
527     @property
528     def deleted(self):
529         return self['status'] == six.text_type('deleted')
530
531     @property
532     def waiting(self):
533         return self['status'] == six.text_type('waiting')
534
535     @property
536     def pending(self):
537         return self['status'] == six.text_type('pending')
538
539     @property
540     def active(self):
541         return self['start'] is not None
542
543     @property
544     def saved(self):
545         return self['uuid'] is not None or self['id'] is not None
546
547     def serialize_depends(self, cur_dependencies):
548         # Check that all the tasks are saved
549         for task in (cur_dependencies or set()):
550             if not task.saved:
551                 raise Task.NotSaved('Task \'%s\' needs to be saved before '
552                                     'it can be set as dependency.' % task)
553
554         return super(Task, self).serialize_depends(cur_dependencies)
555
556     def format_depends(self):
557         # We need to generate added and removed dependencies list,
558         # since Taskwarrior does not accept redefining dependencies.
559
560         # This cannot be part of serialize_depends, since we need
561         # to keep a list of all depedencies in the _data dictionary,
562         # not just currently added/removed ones
563
564         old_dependencies = self._original_data.get('depends', set())
565
566         added = self['depends'] - old_dependencies
567         removed = old_dependencies - self['depends']
568
569         # Removed dependencies need to be prefixed with '-'
570         return 'depends:' + ','.join(
571                 [t['uuid'] for t in added] +
572                 ['-' + t['uuid'] for t in removed]
573             )
574
575     def format_description(self):
576         # Task version older than 2.4.0 ignores first word of the
577         # task description if description: prefix is used
578         if self.warrior.version < VERSION_2_4_0:
579             return self._data['description']
580         else:
581             return six.u("description:'{0}'").format(self._data['description'] or '')
582
583     def delete(self):
584         if not self.saved:
585             raise Task.NotSaved("Task needs to be saved before it can be deleted")
586
587         # Refresh the status, and raise exception if the task is deleted
588         self.refresh(only_fields=['status'])
589
590         if self.deleted:
591             raise Task.DeletedTask("Task was already deleted")
592
593         self.warrior.execute_command([self['uuid'], 'delete'])
594
595         # Refresh the status again, so that we have updated info stored
596         self.refresh(only_fields=['status', 'start', 'end'])
597
598     def start(self):
599         if not self.saved:
600             raise Task.NotSaved("Task needs to be saved before it can be started")
601
602         # Refresh, and raise exception if task is already completed/deleted
603         self.refresh(only_fields=['status'])
604
605         if self.completed:
606             raise Task.CompletedTask("Cannot start a completed task")
607         elif self.deleted:
608             raise Task.DeletedTask("Deleted task cannot be started")
609         elif self.active:
610             raise Task.ActiveTask("Task is already active")
611
612         self.warrior.execute_command([self['uuid'], 'start'])
613
614         # Refresh the status again, so that we have updated info stored
615         self.refresh(only_fields=['status', 'start'])
616
617     def stop(self):
618         if not self.saved:
619             raise Task.NotSaved("Task needs to be saved before it can be stopped")
620
621         # Refresh, and raise exception if task is already completed/deleted
622         self.refresh(only_fields=['status'])
623
624         if not self.active:
625             raise Task.InactiveTask("Cannot stop an inactive task")
626
627         self.warrior.execute_command([self['uuid'], 'stop'])
628
629         # Refresh the status again, so that we have updated info stored
630         self.refresh(only_fields=['status', 'start'])
631
632     def done(self):
633         if not self.saved:
634             raise Task.NotSaved("Task needs to be saved before it can be completed")
635
636         # Refresh, and raise exception if task is already completed/deleted
637         self.refresh(only_fields=['status'])
638
639         if self.completed:
640             raise Task.CompletedTask("Cannot complete a completed task")
641         elif self.deleted:
642             raise Task.DeletedTask("Deleted task cannot be completed")
643
644         # Older versions of TW do not stop active task at completion
645         if self.warrior.version < VERSION_2_4_0 and self.active:
646             self.stop()
647
648         self.warrior.execute_command([self['uuid'], 'done'])
649
650         # Refresh the status again, so that we have updated info stored
651         self.refresh(only_fields=['status', 'start', 'end'])
652
653     def save(self):
654         if self.saved and not self.modified:
655             return
656
657         args = [self['uuid'], 'modify'] if self.saved else ['add']
658         args.extend(self._get_modified_fields_as_args())
659         output = self.warrior.execute_command(args)
660
661         # Parse out the new ID, if the task is being added for the first time
662         if not self.saved:
663             id_lines = [l for l in output if l.startswith('Created task ')]
664
665             # Complain loudly if it seems that more tasks were created
666             # Should not happen
667             if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
668                 raise TaskWarriorException("Unexpected output when creating "
669                                            "task: %s" % '\n'.join(id_lines))
670
671             # Circumvent the ID storage, since ID is considered read-only
672             identifier = id_lines[0].split(' ')[2].rstrip('.')
673
674             # Identifier can be either ID or UUID for completed tasks
675             try:
676                 self._data['id'] = int(identifier)
677             except ValueError:
678                 self._data['uuid'] = identifier
679
680         # Refreshing is very important here, as not only modification time
681         # is updated, but arbitrary attribute may have changed due hooks
682         # altering the data before saving
683         self.refresh(after_save=True)
684
685     def add_annotation(self, annotation):
686         if not self.saved:
687             raise Task.NotSaved("Task needs to be saved to add annotation")
688
689         args = [self['uuid'], 'annotate', annotation]
690         self.warrior.execute_command(args)
691         self.refresh(only_fields=['annotations'])
692
693     def remove_annotation(self, annotation):
694         if not self.saved:
695             raise Task.NotSaved("Task needs to be saved to remove annotation")
696
697         if isinstance(annotation, TaskAnnotation):
698             annotation = annotation['description']
699         args = [self['uuid'], 'denotate', annotation]
700         self.warrior.execute_command(args)
701         self.refresh(only_fields=['annotations'])
702
703     def _get_modified_fields_as_args(self):
704         args = []
705
706         def add_field(field):
707             # Add the output of format_field method to args list (defaults to
708             # field:value)
709             serialized_value = self._serialize(field, self._data[field])
710
711             # Empty values should not be enclosed in quotation marks, see
712             # TW-1510
713             if serialized_value is '':
714                 escaped_serialized_value = ''
715             else:
716                 escaped_serialized_value = six.u("'{0}'").format(serialized_value)
717
718             format_default = lambda: six.u("{0}:{1}").format(field,
719                                                       escaped_serialized_value)
720
721             format_func = getattr(self, 'format_{0}'.format(field),
722                                   format_default)
723
724             args.append(format_func())
725
726         # If we're modifying saved task, simply pass on all modified fields
727         if self.saved:
728             for field in self._modified_fields:
729                 add_field(field)
730         # For new tasks, pass all fields that make sense
731         else:
732             for field in self._data.keys():
733                 if field in self.read_only_fields:
734                     continue
735                 add_field(field)
736
737         return args
738
739     def refresh(self, only_fields=None, after_save=False):
740         # Raise error when trying to refresh a task that has not been saved
741         if not self.saved:
742             raise Task.NotSaved("Task needs to be saved to be refreshed")
743
744         # We need to use ID as backup for uuid here for the refreshes
745         # of newly saved tasks. Any other place in the code is fine
746         # with using UUID only.
747         args = [self['uuid'] or self['id'], 'export']
748         output = self.warrior.execute_command(args)
749
750         def valid(output):
751             return len(output) == 1 and output[0].startswith('{')
752
753         # For older TW versions attempt to uniquely locate the task
754         # using the data we have if it has been just saved.
755         # This can happen when adding a completed task on older TW versions.
756         if (not valid(output) and self.warrior.version < VERSION_2_4_5
757                 and after_save):
758
759             # Make a copy, removing ID and UUID. It's most likely invalid
760             # (ID 0) if it failed to match a unique task.
761             data = copy.deepcopy(self._data)
762             data.pop('id', None)
763             data.pop('uuid', None)
764
765             taskfilter = TaskFilter(self.warrior)
766             for key, value in data.items():
767                 taskfilter.add_filter_param(key, value)
768
769             output = self.warrior.execute_command(['export', '--'] +
770                 taskfilter.get_filter_params())
771
772         # If more than 1 task has been matched still, raise an exception
773         if not valid(output):
774             raise TaskWarriorException(
775                 "Unique identifiers {0} with description: {1} matches "
776                 "multiple tasks: {2}".format(
777                 self['uuid'] or self['id'], self['description'], output)
778             )
779
780         new_data = json.loads(output[0])
781         if only_fields:
782             to_update = dict(
783                 [(k, new_data.get(k)) for k in only_fields])
784             self._update_data(to_update, update_original=True)
785         else:
786             self._load_data(new_data)
787
788 class TaskFilter(SerializingObject):
789     """
790     A set of parameters to filter the task list with.
791     """
792
793     def __init__(self, warrior, filter_params=None):
794         self.filter_params = filter_params or []
795         super(TaskFilter, self).__init__(warrior)
796
797     def add_filter(self, filter_str):
798         self.filter_params.append(filter_str)
799
800     def add_filter_param(self, key, value):
801         key = key.replace('__', '.')
802
803         # Replace the value with empty string, since that is the
804         # convention in TW for empty values
805         attribute_key = key.split('.')[0]
806
807         # Since this is user input, we need to normalize before we serialize
808         value = self._normalize(attribute_key, value)
809         value = self._serialize(attribute_key, value)
810
811         # If we are filtering by uuid:, do not use uuid keyword
812         # due to TW-1452 bug
813         if key == 'uuid':
814             self.filter_params.insert(0, value)
815         else:
816             # Surround value with aphostrophes unless it's a empty string
817             value = "'%s'" % value if value else ''
818
819             # We enforce equality match by using 'is' (or 'none') modifier
820             # Without using this syntax, filter fails due to TW-1479
821             # which is, however, fixed in 2.4.5
822             if self.warrior.version < VERSION_2_4_5:
823                 modifier = '.is' if value else '.none'
824                 key = key + modifier if '.' not in key else key
825
826             self.filter_params.append(six.u("{0}:{1}").format(key, value))
827
828     def get_filter_params(self):
829         return [f for f in self.filter_params if f]
830
831     def clone(self):
832         c = self.__class__(self.warrior)
833         c.filter_params = list(self.filter_params)
834         return c
835
836
837 class TaskQuerySet(object):
838     """
839     Represents a lazy lookup for a task objects.
840     """
841
842     def __init__(self, warrior=None, filter_obj=None):
843         self.warrior = warrior
844         self._result_cache = None
845         self.filter_obj = filter_obj or TaskFilter(warrior)
846
847     def __deepcopy__(self, memo):
848         """
849         Deep copy of a QuerySet doesn't populate the cache
850         """
851         obj = self.__class__()
852         for k, v in self.__dict__.items():
853             if k in ('_iter', '_result_cache'):
854                 obj.__dict__[k] = None
855             else:
856                 obj.__dict__[k] = copy.deepcopy(v, memo)
857         return obj
858
859     def __repr__(self):
860         data = list(self[:REPR_OUTPUT_SIZE + 1])
861         if len(data) > REPR_OUTPUT_SIZE:
862             data[-1] = "...(remaining elements truncated)..."
863         return repr(data)
864
865     def __len__(self):
866         if self._result_cache is None:
867             self._result_cache = list(self)
868         return len(self._result_cache)
869
870     def __iter__(self):
871         if self._result_cache is None:
872             self._result_cache = self._execute()
873         return iter(self._result_cache)
874
875     def __getitem__(self, k):
876         if self._result_cache is None:
877             self._result_cache = list(self)
878         return self._result_cache.__getitem__(k)
879
880     def __bool__(self):
881         if self._result_cache is not None:
882             return bool(self._result_cache)
883         try:
884             next(iter(self))
885         except StopIteration:
886             return False
887         return True
888
889     def __nonzero__(self):
890         return type(self).__bool__(self)
891
892     def _clone(self, klass=None, **kwargs):
893         if klass is None:
894             klass = self.__class__
895         filter_obj = self.filter_obj.clone()
896         c = klass(warrior=self.warrior, filter_obj=filter_obj)
897         c.__dict__.update(kwargs)
898         return c
899
900     def _execute(self):
901         """
902         Fetch the tasks which match the current filters.
903         """
904         return self.warrior.filter_tasks(self.filter_obj)
905
906     def all(self):
907         """
908         Returns a new TaskQuerySet that is a copy of the current one.
909         """
910         return self._clone()
911
912     def pending(self):
913         return self.filter(status=PENDING)
914
915     def completed(self):
916         return self.filter(status=COMPLETED)
917
918     def filter(self, *args, **kwargs):
919         """
920         Returns a new TaskQuerySet with the given filters added.
921         """
922         clone = self._clone()
923         for f in args:
924             clone.filter_obj.add_filter(f)
925         for key, value in kwargs.items():
926             clone.filter_obj.add_filter_param(key, value)
927         return clone
928
929     def get(self, **kwargs):
930         """
931         Performs the query and returns a single object matching the given
932         keyword arguments.
933         """
934         clone = self.filter(**kwargs)
935         num = len(clone)
936         if num == 1:
937             return clone._result_cache[0]
938         if not num:
939             raise Task.DoesNotExist(
940                 'Task matching query does not exist. '
941                 'Lookup parameters were {0}'.format(kwargs))
942         raise ValueError(
943             'get() returned more than one Task -- it returned {0}! '
944             'Lookup parameters were {1}'.format(num, kwargs))