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