]> 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 modified property non-private
[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=False):
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         # Load the data from the input
230         task._load_data(json.loads(input_file.readline().strip()))
231
232         # If this is a on-modify event, we are provided with additional
233         # line of input, which provides updated data
234         if modify:
235             task._update_data(json.loads(input_file.readline().strip()))
236
237         return task
238
239     def __init__(self, warrior, **kwargs):
240         self.warrior = warrior
241
242         # Check that user is not able to set read-only value in __init__
243         for key in kwargs.keys():
244             if key in self.read_only_fields:
245                 raise RuntimeError('Field \'%s\' is read-only' % key)
246
247         # We serialize the data in kwargs so that users of the library
248         # do not have to pass different data formats via __setitem__ and
249         # __init__ methods, that would be confusing
250
251         # Rather unfortunate syntax due to python2.6 comaptiblity
252         self._load_data(dict((key, self._serialize(key, value))
253                         for (key, value) in six.iteritems(kwargs)))
254
255     def __unicode__(self):
256         return self['description']
257
258     def __eq__(self, other):
259         if self['uuid'] and other['uuid']:
260             # For saved Tasks, just define equality by equality of uuids
261             return self['uuid'] == other['uuid']
262         else:
263             # If the tasks are not saved, compare the actual instances
264             return id(self) == id(other)
265
266
267     def __hash__(self):
268         if self['uuid']:
269             # For saved Tasks, just define equality by equality of uuids
270             return self['uuid'].__hash__()
271         else:
272             # If the tasks are not saved, return hash of instance id
273             return id(self).__hash__()
274
275     @property
276     def _modified_fields(self):
277         writable_fields = set(self._data.keys()) - set(self.read_only_fields)
278         for key in writable_fields:
279             if self._data.get(key) != self._original_data.get(key):
280                 yield key
281
282     @property
283     def modified(self):
284         return bool(list(self._modified_fields))
285
286     @property
287     def completed(self):
288         return self['status'] == six.text_type('completed')
289
290     @property
291     def deleted(self):
292         return self['status'] == six.text_type('deleted')
293
294     @property
295     def waiting(self):
296         return self['status'] == six.text_type('waiting')
297
298     @property
299     def pending(self):
300         return self['status'] == six.text_type('pending')
301
302     @property
303     def saved(self):
304         return self['uuid'] is not None or self['id'] is not None
305
306     def serialize_depends(self, cur_dependencies):
307         # Check that all the tasks are saved
308         for task in cur_dependencies:
309             if not task.saved:
310                 raise Task.NotSaved('Task \'%s\' needs to be saved before '
311                                     'it can be set as dependency.' % task)
312
313         return super(Task, self).serialize_depends(cur_dependencies)
314
315     def format_depends(self):
316         # We need to generate added and removed dependencies list,
317         # since Taskwarrior does not accept redefining dependencies.
318
319         # This cannot be part of serialize_depends, since we need
320         # to keep a list of all depedencies in the _data dictionary,
321         # not just currently added/removed ones
322
323         old_dependencies = self._original_data.get('depends', set())
324
325         added = self['depends'] - old_dependencies
326         removed = old_dependencies - self['depends']
327
328         # Removed dependencies need to be prefixed with '-'
329         return 'depends:' + ','.join(
330                 [t['uuid'] for t in added] +
331                 ['-' + t['uuid'] for t in removed]
332             )
333
334     def format_description(self):
335         # Task version older than 2.4.0 ignores first word of the
336         # task description if description: prefix is used
337         if self.warrior.version < VERSION_2_4_0:
338             return self._data['description']
339         else:
340             return "description:'{0}'".format(self._data['description'] or '')
341
342     def delete(self):
343         if not self.saved:
344             raise Task.NotSaved("Task needs to be saved before it can be deleted")
345
346         # Refresh the status, and raise exception if the task is deleted
347         self.refresh(only_fields=['status'])
348
349         if self.deleted:
350             raise Task.DeletedTask("Task was already deleted")
351
352         self.warrior.execute_command([self['uuid'], 'delete'])
353
354         # Refresh the status again, so that we have updated info stored
355         self.refresh(only_fields=['status'])
356
357
358     def done(self):
359         if not self.saved:
360             raise Task.NotSaved("Task needs to be saved before it can be completed")
361
362         # Refresh, and raise exception if task is already completed/deleted
363         self.refresh(only_fields=['status'])
364
365         if self.completed:
366             raise Task.CompletedTask("Cannot complete a completed task")
367         elif self.deleted:
368             raise Task.DeletedTask("Deleted task cannot be completed")
369
370         self.warrior.execute_command([self['uuid'], 'done'])
371
372         # Refresh the status again, so that we have updated info stored
373         self.refresh(only_fields=['status'])
374
375     def save(self):
376         if self.saved and not self.modified:
377             return
378
379         args = [self['uuid'], 'modify'] if self.saved else ['add']
380         args.extend(self._get_modified_fields_as_args())
381         output = self.warrior.execute_command(args)
382
383         # Parse out the new ID, if the task is being added for the first time
384         if not self.saved:
385             id_lines = [l for l in output if l.startswith('Created task ')]
386
387             # Complain loudly if it seems that more tasks were created
388             # Should not happen
389             if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
390                 raise TaskWarriorException("Unexpected output when creating "
391                                            "task: %s" % '\n'.join(id_lines))
392
393             # Circumvent the ID storage, since ID is considered read-only
394             self._data['id'] = int(id_lines[0].split(' ')[2].rstrip('.'))
395
396         # Refreshing is very important here, as not only modification time
397         # is updated, but arbitrary attribute may have changed due hooks
398         # altering the data before saving
399         self.refresh()
400
401     def add_annotation(self, annotation):
402         if not self.saved:
403             raise Task.NotSaved("Task needs to be saved to add annotation")
404
405         args = [self['uuid'], 'annotate', annotation]
406         self.warrior.execute_command(args)
407         self.refresh(only_fields=['annotations'])
408
409     def remove_annotation(self, annotation):
410         if not self.saved:
411             raise Task.NotSaved("Task needs to be saved to remove annotation")
412
413         if isinstance(annotation, TaskAnnotation):
414             annotation = annotation['description']
415         args = [self['uuid'], 'denotate', annotation]
416         self.warrior.execute_command(args)
417         self.refresh(only_fields=['annotations'])
418
419     def _get_modified_fields_as_args(self):
420         args = []
421
422         def add_field(field):
423             # Add the output of format_field method to args list (defaults to
424             # field:value)
425             serialized_value = self._serialize(field, self._data[field]) or ''
426             format_default = lambda: "{0}:{1}".format(
427                 field,
428                 "'{0}'".format(serialized_value) if serialized_value else ''
429             )
430             format_func = getattr(self, 'format_{0}'.format(field),
431                                   format_default)
432             args.append(format_func())
433
434         # If we're modifying saved task, simply pass on all modified fields
435         if self.saved:
436             for field in self._modified_fields:
437                 add_field(field)
438         # For new tasks, pass all fields that make sense
439         else:
440             for field in self._data.keys():
441                 if field in self.read_only_fields:
442                     continue
443                 add_field(field)
444
445         return args
446
447     def refresh(self, only_fields=[]):
448         # Raise error when trying to refresh a task that has not been saved
449         if not self.saved:
450             raise Task.NotSaved("Task needs to be saved to be refreshed")
451
452         # We need to use ID as backup for uuid here for the refreshes
453         # of newly saved tasks. Any other place in the code is fine
454         # with using UUID only.
455         args = [self['uuid'] or self['id'], 'export']
456         new_data = json.loads(self.warrior.execute_command(args)[0])
457         if only_fields:
458             to_update = dict(
459                 [(k, new_data.get(k)) for k in only_fields])
460             self._update_data(to_update, update_original=True)
461         else:
462             self._load_data(new_data)
463
464     def export_data(self):
465         """
466         Exports current data contained in the Task as JSON
467         """
468
469         # We need to remove spaces for TW-1504, use custom separators
470         return json.dumps(self._data, separators=(',',':'))
471
472 class TaskFilter(SerializingObject):
473     """
474     A set of parameters to filter the task list with.
475     """
476
477     def __init__(self, filter_params=[]):
478         self.filter_params = filter_params
479
480     def add_filter(self, filter_str):
481         self.filter_params.append(filter_str)
482
483     def add_filter_param(self, key, value):
484         key = key.replace('__', '.')
485
486         # Replace the value with empty string, since that is the
487         # convention in TW for empty values
488         attribute_key = key.split('.')[0]
489         value = self._serialize(attribute_key, value)
490
491         # If we are filtering by uuid:, do not use uuid keyword
492         # due to TW-1452 bug
493         if key == 'uuid':
494             self.filter_params.insert(0, value)
495         else:
496             # Surround value with aphostrophes unless it's a empty string
497             value = "'%s'" % value if value else ''
498
499             # We enforce equality match by using 'is' (or 'none') modifier
500             # Without using this syntax, filter fails due to TW-1479
501             modifier = '.is' if value else '.none'
502             key = key + modifier if '.' not in key else key
503
504             self.filter_params.append("{0}:{1}".format(key, value))
505
506     def get_filter_params(self):
507         return [f for f in self.filter_params if f]
508
509     def clone(self):
510         c = self.__class__()
511         c.filter_params = list(self.filter_params)
512         return c
513
514
515 class TaskQuerySet(object):
516     """
517     Represents a lazy lookup for a task objects.
518     """
519
520     def __init__(self, warrior=None, filter_obj=None):
521         self.warrior = warrior
522         self._result_cache = None
523         self.filter_obj = filter_obj or TaskFilter()
524
525     def __deepcopy__(self, memo):
526         """
527         Deep copy of a QuerySet doesn't populate the cache
528         """
529         obj = self.__class__()
530         for k, v in self.__dict__.items():
531             if k in ('_iter', '_result_cache'):
532                 obj.__dict__[k] = None
533             else:
534                 obj.__dict__[k] = copy.deepcopy(v, memo)
535         return obj
536
537     def __repr__(self):
538         data = list(self[:REPR_OUTPUT_SIZE + 1])
539         if len(data) > REPR_OUTPUT_SIZE:
540             data[-1] = "...(remaining elements truncated)..."
541         return repr(data)
542
543     def __len__(self):
544         if self._result_cache is None:
545             self._result_cache = list(self)
546         return len(self._result_cache)
547
548     def __iter__(self):
549         if self._result_cache is None:
550             self._result_cache = self._execute()
551         return iter(self._result_cache)
552
553     def __getitem__(self, k):
554         if self._result_cache is None:
555             self._result_cache = list(self)
556         return self._result_cache.__getitem__(k)
557
558     def __bool__(self):
559         if self._result_cache is not None:
560             return bool(self._result_cache)
561         try:
562             next(iter(self))
563         except StopIteration:
564             return False
565         return True
566
567     def __nonzero__(self):
568         return type(self).__bool__(self)
569
570     def _clone(self, klass=None, **kwargs):
571         if klass is None:
572             klass = self.__class__
573         filter_obj = self.filter_obj.clone()
574         c = klass(warrior=self.warrior, filter_obj=filter_obj)
575         c.__dict__.update(kwargs)
576         return c
577
578     def _execute(self):
579         """
580         Fetch the tasks which match the current filters.
581         """
582         return self.warrior.filter_tasks(self.filter_obj)
583
584     def all(self):
585         """
586         Returns a new TaskQuerySet that is a copy of the current one.
587         """
588         return self._clone()
589
590     def pending(self):
591         return self.filter(status=PENDING)
592
593     def completed(self):
594         return self.filter(status=COMPLETED)
595
596     def filter(self, *args, **kwargs):
597         """
598         Returns a new TaskQuerySet with the given filters added.
599         """
600         clone = self._clone()
601         for f in args:
602             clone.filter_obj.add_filter(f)
603         for key, value in kwargs.items():
604             clone.filter_obj.add_filter_param(key, value)
605         return clone
606
607     def get(self, **kwargs):
608         """
609         Performs the query and returns a single object matching the given
610         keyword arguments.
611         """
612         clone = self.filter(**kwargs)
613         num = len(clone)
614         if num == 1:
615             return clone._result_cache[0]
616         if not num:
617             raise Task.DoesNotExist(
618                 'Task matching query does not exist. '
619                 'Lookup parameters were {0}'.format(kwargs))
620         raise ValueError(
621             'get() returned more than one Task -- it returned {0}! '
622             'Lookup parameters were {1}'.format(num, kwargs))
623
624
625 class TaskWarrior(object):
626     def __init__(self, data_location='~/.task', create=True):
627         data_location = os.path.expanduser(data_location)
628         if create and not os.path.exists(data_location):
629             os.makedirs(data_location)
630         self.config = {
631             'data.location': os.path.expanduser(data_location),
632             'confirmation': 'no',
633             'dependency.confirmation': 'no', # See TW-1483 or taskrc man page
634         }
635         self.tasks = TaskQuerySet(self)
636         self.version = self._get_version()
637
638     def _get_command_args(self, args, config_override={}):
639         command_args = ['task', 'rc:/']
640         config = self.config.copy()
641         config.update(config_override)
642         for item in config.items():
643             command_args.append('rc.{0}={1}'.format(*item))
644         command_args.extend(map(str, args))
645         return command_args
646
647     def _get_version(self):
648         p = subprocess.Popen(
649                 ['task', '--version'],
650                 stdout=subprocess.PIPE,
651                 stderr=subprocess.PIPE)
652         stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
653         return stdout.strip('\n')
654
655     def execute_command(self, args, config_override={}):
656         command_args = self._get_command_args(
657             args, config_override=config_override)
658         logger.debug(' '.join(command_args))
659         p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
660                              stderr=subprocess.PIPE)
661         stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
662         if p.returncode:
663             if stderr.strip():
664                 error_msg = stderr.strip().splitlines()[-1]
665             else:
666                 error_msg = stdout.strip()
667             raise TaskWarriorException(error_msg)
668         return stdout.strip().split('\n')
669
670     def filter_tasks(self, filter_obj):
671         args = ['export', '--'] + filter_obj.get_filter_params()
672         tasks = []
673         for line in self.execute_command(args):
674             if line:
675                 data = line.strip(',')
676                 try:
677                     filtered_task = Task(self)
678                     filtered_task._load_data(json.loads(data))
679                     tasks.append(filtered_task)
680                 except ValueError:
681                     raise TaskWarriorException('Invalid JSON: %s' % data)
682         return tasks
683
684     def merge_with(self, path, push=False):
685         path = path.rstrip('/') + '/'
686         self.execute_command(['merge', path], config_override={
687             'merge.autopush': 'yes' if push else 'no',
688         })
689
690     def undo(self):
691         self.execute_command(['undo'])