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

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