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

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