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.
1 from __future__ import print_function
13 DATE_FORMAT = '%Y%m%dT%H%M%SZ'
16 COMPLETED = 'completed'
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')
23 logger = logging.getLogger(__name__)
24 local_zone = tzlocal.get_localzone()
27 class TaskWarriorException(Exception):
31 class SerializingObject(object):
33 Common ancestor for TaskResource & TaskFilter, since they both
34 need to serialize arguments.
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
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.
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)
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)
59 def _normalize(self, key, value):
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.
66 normalize_func = getattr(self, 'normalize_{0}'.format(key),
69 return normalize_func(value)
71 def timestamp_serializer(self, date):
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)
79 return date.strftime(DATE_FORMAT)
81 def timestamp_deserializer(self, date_str):
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)
90 def serialize_entry(self, value):
91 return self.timestamp_serializer(value)
93 def deserialize_entry(self, value):
94 return self.timestamp_deserializer(value)
96 def normalize_entry(self, value):
97 return self.datetime_normalizer(value)
99 def serialize_modified(self, value):
100 return self.timestamp_serializer(value)
102 def deserialize_modified(self, value):
103 return self.timestamp_deserializer(value)
105 def normalize_modified(self, value):
106 return self.datetime_normalizer(value)
108 def serialize_due(self, value):
109 return self.timestamp_serializer(value)
111 def deserialize_due(self, value):
112 return self.timestamp_deserializer(value)
114 def normalize_due(self, value):
115 return self.datetime_normalizer(value)
117 def serialize_scheduled(self, value):
118 return self.timestamp_serializer(value)
120 def deserialize_scheduled(self, value):
121 return self.timestamp_deserializer(value)
123 def normalize_scheduled(self, value):
124 return self.datetime_normalizer(value)
126 def serialize_until(self, value):
127 return self.timestamp_serializer(value)
129 def deserialize_until(self, value):
130 return self.timestamp_deserializer(value)
132 def normalize_until(self, value):
133 return self.datetime_normalizer(value)
135 def serialize_wait(self, value):
136 return self.timestamp_serializer(value)
138 def deserialize_wait(self, value):
139 return self.timestamp_deserializer(value)
141 def normalize_wait(self, value):
142 return self.datetime_normalizer(value)
144 def serialize_annotations(self, value):
145 value = value if value is not None else []
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 ''
153 def deserialize_annotations(self, data):
154 return [TaskAnnotation(self, d) for d in data] if data else []
156 def serialize_tags(self, tags):
157 return ','.join(tags) if tags else ''
159 def deserialize_tags(self, tags):
160 if isinstance(tags, six.string_types):
161 return tags.split(',') if tags else []
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)
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)
174 def datetime_normalizer(self, value):
176 Normalizes date/datetime value (considered to come from user input)
177 to localized datetime value. Following conversions happen:
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)
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)
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.
201 class TaskResource(SerializingObject):
202 read_only_fields = []
204 def _load_data(self, data):
205 self._data = dict((key, self._deserialize(key, value))
206 for key, value in data.items())
207 # We need to use a copy for original data, so that changes
208 # are not propagated.
209 self._original_data = copy.deepcopy(self._data)
211 def _update_data(self, data, update_original=False):
213 Low level update of the internal _data dict. Data which are coming as
214 updates should already be serialized. If update_original is True, the
215 original_data dict is updated as well.
217 self._data.update(dict((key, self._deserialize(key, value))
218 for key, value in data.items()))
221 self._original_data = copy.deepcopy(self._data)
224 def __getitem__(self, key):
225 # This is a workaround to make TaskResource non-iterable
226 # over simple index-based iteration
233 if key not in self._data:
234 self._data[key] = self._deserialize(key, None)
236 return self._data.get(key)
238 def __setitem__(self, key, value):
239 if key in self.read_only_fields:
240 raise RuntimeError('Field \'%s\' is read-only' % key)
242 # Normalize the user input before saving it
243 value = self._normalize(key, value)
244 self._data[key] = value
247 s = six.text_type(self.__unicode__())
249 s = s.encode('utf-8')
255 def export_data(self):
257 Exports current data contained in the Task as JSON
260 # We need to remove spaces for TW-1504, use custom separators
261 data_tuples = ((key, self._serialize(key, value))
262 for key, value in six.iteritems(self._data))
264 # Empty string denotes empty serialized value, we do not want
265 # to pass that to TaskWarrior.
266 data_tuples = filter(lambda t: t[1] is not '', data_tuples)
267 data = dict(data_tuples)
268 return json.dumps(data, separators=(',',':'))
271 def _modified_fields(self):
272 writable_fields = set(self._data.keys()) - set(self.read_only_fields)
273 for key in writable_fields:
274 new_value = self._data.get(key)
275 old_value = self._original_data.get(key)
277 # Make sure not to mark data removal as modified field if the
278 # field originally had some empty value
279 if key in self._data and not new_value and not old_value:
282 if new_value != old_value:
287 return bool(list(self._modified_fields))
290 class TaskAnnotation(TaskResource):
291 read_only_fields = ['entry', 'description']
293 def __init__(self, task, data={}):
295 self._load_data(data)
298 self.task.remove_annotation(self)
300 def __unicode__(self):
301 return self['description']
303 def __eq__(self, other):
304 # consider 2 annotations equal if they belong to the same task, and
305 # their data dics are the same
306 return self.task == other.task and self._data == other._data
308 __repr__ = __unicode__
311 class Task(TaskResource):
312 read_only_fields = ['id', 'entry', 'urgency', 'uuid', 'modified']
314 class DoesNotExist(Exception):
317 class CompletedTask(Exception):
319 Raised when the operation cannot be performed on the completed task.
323 class DeletedTask(Exception):
325 Raised when the operation cannot be performed on the deleted task.
329 class NotSaved(Exception):
331 Raised when the operation cannot be performed on the task, because
332 it has not been saved to TaskWarrior yet.
337 def from_input(cls, input_file=sys.stdin, modify=None):
339 Creates a Task object, directly from the stdin, by reading one line.
340 If modify=True, two lines are used, first line interpreted as the
341 original state of the Task object, and second line as its new,
342 modified value. This is consistent with the TaskWarrior's hook
345 Object created by this method should not be saved, deleted
346 or refreshed, as t could create a infinite loop. For this
347 reason, TaskWarrior instance is set to None.
349 Input_file argument can be used to specify the input file,
350 but defaults to sys.stdin.
353 # TaskWarrior instance is set to None
356 # Detect the hook type if not given directly
357 name = os.path.basename(sys.argv[0])
358 modify = name.startswith('on-modify') if modify is None else modify
360 # Load the data from the input
361 task._load_data(json.loads(input_file.readline().strip()))
363 # If this is a on-modify event, we are provided with additional
364 # line of input, which provides updated data
366 task._update_data(json.loads(input_file.readline().strip()))
370 def __init__(self, warrior, **kwargs):
371 self.warrior = warrior
373 # Check that user is not able to set read-only value in __init__
374 for key in kwargs.keys():
375 if key in self.read_only_fields:
376 raise RuntimeError('Field \'%s\' is read-only' % key)
378 # We serialize the data in kwargs so that users of the library
379 # do not have to pass different data formats via __setitem__ and
380 # __init__ methods, that would be confusing
382 # Rather unfortunate syntax due to python2.6 comaptiblity
383 self._data = dict((key, self._normalize(key, value))
384 for (key, value) in six.iteritems(kwargs))
385 self._original_data = copy.deepcopy(self._data)
387 def __unicode__(self):
388 return self['description']
390 def __eq__(self, other):
391 if self['uuid'] and other['uuid']:
392 # For saved Tasks, just define equality by equality of uuids
393 return self['uuid'] == other['uuid']
395 # If the tasks are not saved, compare the actual instances
396 return id(self) == id(other)
401 # For saved Tasks, just define equality by equality of uuids
402 return self['uuid'].__hash__()
404 # If the tasks are not saved, return hash of instance id
405 return id(self).__hash__()
409 return self['status'] == six.text_type('completed')
413 return self['status'] == six.text_type('deleted')
417 return self['status'] == six.text_type('waiting')
421 return self['status'] == six.text_type('pending')
425 return self['uuid'] is not None or self['id'] is not None
427 def serialize_depends(self, cur_dependencies):
428 # Check that all the tasks are saved
429 for task in (cur_dependencies or set()):
431 raise Task.NotSaved('Task \'%s\' needs to be saved before '
432 'it can be set as dependency.' % task)
434 return super(Task, self).serialize_depends(cur_dependencies)
436 def format_depends(self):
437 # We need to generate added and removed dependencies list,
438 # since Taskwarrior does not accept redefining dependencies.
440 # This cannot be part of serialize_depends, since we need
441 # to keep a list of all depedencies in the _data dictionary,
442 # not just currently added/removed ones
444 old_dependencies = self._original_data.get('depends', set())
446 added = self['depends'] - old_dependencies
447 removed = old_dependencies - self['depends']
449 # Removed dependencies need to be prefixed with '-'
450 return 'depends:' + ','.join(
451 [t['uuid'] for t in added] +
452 ['-' + t['uuid'] for t in removed]
455 def format_description(self):
456 # Task version older than 2.4.0 ignores first word of the
457 # task description if description: prefix is used
458 if self.warrior.version < VERSION_2_4_0:
459 return self._data['description']
461 return "description:'{0}'".format(self._data['description'] or '')
465 raise Task.NotSaved("Task needs to be saved before it can be deleted")
467 # Refresh the status, and raise exception if the task is deleted
468 self.refresh(only_fields=['status'])
471 raise Task.DeletedTask("Task was already deleted")
473 self.warrior.execute_command([self['uuid'], 'delete'])
475 # Refresh the status again, so that we have updated info stored
476 self.refresh(only_fields=['status'])
481 raise Task.NotSaved("Task needs to be saved before it can be completed")
483 # Refresh, and raise exception if task is already completed/deleted
484 self.refresh(only_fields=['status'])
487 raise Task.CompletedTask("Cannot complete a completed task")
489 raise Task.DeletedTask("Deleted task cannot be completed")
491 self.warrior.execute_command([self['uuid'], 'done'])
493 # Refresh the status again, so that we have updated info stored
494 self.refresh(only_fields=['status'])
497 if self.saved and not self.modified:
500 args = [self['uuid'], 'modify'] if self.saved else ['add']
501 args.extend(self._get_modified_fields_as_args())
502 output = self.warrior.execute_command(args)
504 # Parse out the new ID, if the task is being added for the first time
506 id_lines = [l for l in output if l.startswith('Created task ')]
508 # Complain loudly if it seems that more tasks were created
510 if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
511 raise TaskWarriorException("Unexpected output when creating "
512 "task: %s" % '\n'.join(id_lines))
514 # Circumvent the ID storage, since ID is considered read-only
515 self._data['id'] = int(id_lines[0].split(' ')[2].rstrip('.'))
517 # Refreshing is very important here, as not only modification time
518 # is updated, but arbitrary attribute may have changed due hooks
519 # altering the data before saving
522 def add_annotation(self, annotation):
524 raise Task.NotSaved("Task needs to be saved to add annotation")
526 args = [self['uuid'], 'annotate', annotation]
527 self.warrior.execute_command(args)
528 self.refresh(only_fields=['annotations'])
530 def remove_annotation(self, annotation):
532 raise Task.NotSaved("Task needs to be saved to remove annotation")
534 if isinstance(annotation, TaskAnnotation):
535 annotation = annotation['description']
536 args = [self['uuid'], 'denotate', annotation]
537 self.warrior.execute_command(args)
538 self.refresh(only_fields=['annotations'])
540 def _get_modified_fields_as_args(self):
543 def add_field(field):
544 # Add the output of format_field method to args list (defaults to
546 serialized_value = self._serialize(field, self._data[field])
548 # Empty values should not be enclosed in quotation marks, see
550 if serialized_value is '':
551 escaped_serialized_value = ''
553 escaped_serialized_value = "'{0}'".format(serialized_value)
555 format_default = lambda: "{0}:{1}".format(field,
556 escaped_serialized_value)
558 format_func = getattr(self, 'format_{0}'.format(field),
561 args.append(format_func())
563 # If we're modifying saved task, simply pass on all modified fields
565 for field in self._modified_fields:
567 # For new tasks, pass all fields that make sense
569 for field in self._data.keys():
570 if field in self.read_only_fields:
576 def refresh(self, only_fields=[]):
577 # Raise error when trying to refresh a task that has not been saved
579 raise Task.NotSaved("Task needs to be saved to be refreshed")
581 # We need to use ID as backup for uuid here for the refreshes
582 # of newly saved tasks. Any other place in the code is fine
583 # with using UUID only.
584 args = [self['uuid'] or self['id'], 'export']
585 new_data = json.loads(self.warrior.execute_command(args)[0])
588 [(k, new_data.get(k)) for k in only_fields])
589 self._update_data(to_update, update_original=True)
591 self._load_data(new_data)
593 class TaskFilter(SerializingObject):
595 A set of parameters to filter the task list with.
598 def __init__(self, filter_params=[]):
599 self.filter_params = filter_params
601 def add_filter(self, filter_str):
602 self.filter_params.append(filter_str)
604 def add_filter_param(self, key, value):
605 key = key.replace('__', '.')
607 # Replace the value with empty string, since that is the
608 # convention in TW for empty values
609 attribute_key = key.split('.')[0]
611 # Since this is user input, we need to normalize before we serialize
612 value = self._normalize(key, value)
613 value = self._serialize(attribute_key, value)
615 # If we are filtering by uuid:, do not use uuid keyword
618 self.filter_params.insert(0, value)
620 # Surround value with aphostrophes unless it's a empty string
621 value = "'%s'" % value if value else ''
623 # We enforce equality match by using 'is' (or 'none') modifier
624 # Without using this syntax, filter fails due to TW-1479
625 modifier = '.is' if value else '.none'
626 key = key + modifier if '.' not in key else key
628 self.filter_params.append("{0}:{1}".format(key, value))
630 def get_filter_params(self):
631 return [f for f in self.filter_params if f]
635 c.filter_params = list(self.filter_params)
639 class TaskQuerySet(object):
641 Represents a lazy lookup for a task objects.
644 def __init__(self, warrior=None, filter_obj=None):
645 self.warrior = warrior
646 self._result_cache = None
647 self.filter_obj = filter_obj or TaskFilter()
649 def __deepcopy__(self, memo):
651 Deep copy of a QuerySet doesn't populate the cache
653 obj = self.__class__()
654 for k, v in self.__dict__.items():
655 if k in ('_iter', '_result_cache'):
656 obj.__dict__[k] = None
658 obj.__dict__[k] = copy.deepcopy(v, memo)
662 data = list(self[:REPR_OUTPUT_SIZE + 1])
663 if len(data) > REPR_OUTPUT_SIZE:
664 data[-1] = "...(remaining elements truncated)..."
668 if self._result_cache is None:
669 self._result_cache = list(self)
670 return len(self._result_cache)
673 if self._result_cache is None:
674 self._result_cache = self._execute()
675 return iter(self._result_cache)
677 def __getitem__(self, k):
678 if self._result_cache is None:
679 self._result_cache = list(self)
680 return self._result_cache.__getitem__(k)
683 if self._result_cache is not None:
684 return bool(self._result_cache)
687 except StopIteration:
691 def __nonzero__(self):
692 return type(self).__bool__(self)
694 def _clone(self, klass=None, **kwargs):
696 klass = self.__class__
697 filter_obj = self.filter_obj.clone()
698 c = klass(warrior=self.warrior, filter_obj=filter_obj)
699 c.__dict__.update(kwargs)
704 Fetch the tasks which match the current filters.
706 return self.warrior.filter_tasks(self.filter_obj)
710 Returns a new TaskQuerySet that is a copy of the current one.
715 return self.filter(status=PENDING)
718 return self.filter(status=COMPLETED)
720 def filter(self, *args, **kwargs):
722 Returns a new TaskQuerySet with the given filters added.
724 clone = self._clone()
726 clone.filter_obj.add_filter(f)
727 for key, value in kwargs.items():
728 clone.filter_obj.add_filter_param(key, value)
731 def get(self, **kwargs):
733 Performs the query and returns a single object matching the given
736 clone = self.filter(**kwargs)
739 return clone._result_cache[0]
741 raise Task.DoesNotExist(
742 'Task matching query does not exist. '
743 'Lookup parameters were {0}'.format(kwargs))
745 'get() returned more than one Task -- it returned {0}! '
746 'Lookup parameters were {1}'.format(num, kwargs))
749 class TaskWarrior(object):
750 def __init__(self, data_location='~/.task', create=True):
751 data_location = os.path.expanduser(data_location)
752 if create and not os.path.exists(data_location):
753 os.makedirs(data_location)
755 'data.location': os.path.expanduser(data_location),
756 'confirmation': 'no',
757 'dependency.confirmation': 'no', # See TW-1483 or taskrc man page
758 'recurrence.confirmation': 'no', # Necessary for modifying R tasks
760 self.tasks = TaskQuerySet(self)
761 self.version = self._get_version()
763 def _get_command_args(self, args, config_override={}):
764 command_args = ['task', 'rc:/']
765 config = self.config.copy()
766 config.update(config_override)
767 for item in config.items():
768 command_args.append('rc.{0}={1}'.format(*item))
769 command_args.extend(map(str, args))
772 def _get_version(self):
773 p = subprocess.Popen(
774 ['task', '--version'],
775 stdout=subprocess.PIPE,
776 stderr=subprocess.PIPE)
777 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
778 return stdout.strip('\n')
780 def execute_command(self, args, config_override={}, allow_failure=True):
781 command_args = self._get_command_args(
782 args, config_override=config_override)
783 logger.debug(' '.join(command_args))
784 p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
785 stderr=subprocess.PIPE)
786 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
787 if p.returncode and allow_failure:
789 error_msg = stderr.strip().splitlines()[-1]
791 error_msg = stdout.strip()
792 raise TaskWarriorException(error_msg)
793 return stdout.strip().split('\n')
795 def filter_tasks(self, filter_obj):
796 args = ['export', '--'] + filter_obj.get_filter_params()
798 for line in self.execute_command(args):
800 data = line.strip(',')
802 filtered_task = Task(self)
803 filtered_task._load_data(json.loads(data))
804 tasks.append(filtered_task)
806 raise TaskWarriorException('Invalid JSON: %s' % data)
809 def merge_with(self, path, push=False):
810 path = path.rstrip('/') + '/'
811 self.execute_command(['merge', path], config_override={
812 'merge.autopush': 'yes' if push else 'no',
816 self.execute_command(['undo'])