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

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