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

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