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 ReadOnlyDictView(object):
33 Provides simplified read-only view upon dict object.
36 def __init__(self, viewed_dict):
37 self.viewed_dict = viewed_dict
39 def __getitem__(self, key):
40 return copy.deepcopy(self.viewed_dict.__getitem__(key))
42 def __contains__(self, k):
43 return self.viewed_dict.__contains__(k)
46 for value in self.viewed_dict:
47 yield copy.deepcopy(value)
50 return len(self.viewed_dict)
52 def get(self, key, default=None):
53 return copy.deepcopy(self.viewed_dict.get(key, default))
56 return [copy.deepcopy(v) for v in self.viewed_dict.items()]
59 return [copy.deepcopy(v) for v in self.viewed_dict.values()]
62 class SerializingObject(object):
64 Common ancestor for TaskResource & TaskFilter, since they both
65 need to serialize arguments.
67 Serializing method should hold the following contract:
68 - any empty value (meaning removal of the attribute)
69 is deserialized into a empty string
70 - None denotes a empty value for any attribute
72 Deserializing method should hold the following contract:
73 - None denotes a empty value for any attribute (however,
74 this is here as a safeguard, TaskWarrior currently does
75 not export empty-valued attributes) if the attribute
76 is not iterable (e.g. list or set), in which case
77 a empty iterable should be used.
79 Normalizing methods should hold the following contract:
80 - They are used to validate and normalize the user input.
81 Any attribute value that comes from the user (during Task
82 initialization, assignign values to Task attributes, or
83 filtering by user-provided values of attributes) is first
84 validated and normalized using the normalize_{key} method.
85 - If validation or normalization fails, normalizer is expected
89 def _deserialize(self, key, value):
90 hydrate_func = getattr(self, 'deserialize_{0}'.format(key),
91 lambda x: x if x != '' else None)
92 return hydrate_func(value)
94 def _serialize(self, key, value):
95 dehydrate_func = getattr(self, 'serialize_{0}'.format(key),
96 lambda x: x if x is not None else '')
97 return dehydrate_func(value)
99 def _normalize(self, key, value):
101 Use normalize_<key> methods to normalize user input. Any user
102 input will be normalized at the moment it is used as filter,
103 or entered as a value of Task attribute.
106 normalize_func = getattr(self, 'normalize_{0}'.format(key),
109 return normalize_func(value)
111 def timestamp_serializer(self, date):
115 # Any serialized timestamp should be localized, we need to
116 # convert to UTC before converting to string (DATE_FORMAT uses UTC)
117 date = date.astimezone(pytz.utc)
119 return date.strftime(DATE_FORMAT)
121 def timestamp_deserializer(self, date_str):
125 # Return timestamp localized in the local zone
126 naive_timestamp = datetime.datetime.strptime(date_str, DATE_FORMAT)
127 localized_timestamp = pytz.utc.localize(naive_timestamp)
128 return localized_timestamp.astimezone(local_zone)
130 def serialize_entry(self, value):
131 return self.timestamp_serializer(value)
133 def deserialize_entry(self, value):
134 return self.timestamp_deserializer(value)
136 def normalize_entry(self, value):
137 return self.datetime_normalizer(value)
139 def serialize_modified(self, value):
140 return self.timestamp_serializer(value)
142 def deserialize_modified(self, value):
143 return self.timestamp_deserializer(value)
145 def normalize_modified(self, value):
146 return self.datetime_normalizer(value)
148 def serialize_due(self, value):
149 return self.timestamp_serializer(value)
151 def deserialize_due(self, value):
152 return self.timestamp_deserializer(value)
154 def normalize_due(self, value):
155 return self.datetime_normalizer(value)
157 def serialize_scheduled(self, value):
158 return self.timestamp_serializer(value)
160 def deserialize_scheduled(self, value):
161 return self.timestamp_deserializer(value)
163 def normalize_scheduled(self, value):
164 return self.datetime_normalizer(value)
166 def serialize_until(self, value):
167 return self.timestamp_serializer(value)
169 def deserialize_until(self, value):
170 return self.timestamp_deserializer(value)
172 def normalize_until(self, value):
173 return self.datetime_normalizer(value)
175 def serialize_wait(self, value):
176 return self.timestamp_serializer(value)
178 def deserialize_wait(self, value):
179 return self.timestamp_deserializer(value)
181 def normalize_wait(self, value):
182 return self.datetime_normalizer(value)
184 def serialize_annotations(self, value):
185 value = value if value is not None else []
187 # This may seem weird, but it's correct, we want to export
188 # a list of dicts as serialized value
189 serialized_annotations = [json.loads(annotation.export_data())
190 for annotation in value]
191 return serialized_annotations if serialized_annotations else ''
193 def deserialize_annotations(self, data):
194 return [TaskAnnotation(self, d) for d in data] if data else []
196 def serialize_tags(self, tags):
197 return ','.join(tags) if tags else ''
199 def deserialize_tags(self, tags):
200 if isinstance(tags, six.string_types):
201 return tags.split(',') if tags else []
204 def serialize_depends(self, value):
205 # Return the list of uuids
206 value = value if value is not None else set()
207 return ','.join(task['uuid'] for task in value)
209 def deserialize_depends(self, raw_uuids):
210 raw_uuids = raw_uuids or '' # Convert None to empty string
211 uuids = raw_uuids.split(',')
212 return set(self.warrior.tasks.get(uuid=uuid) for uuid in uuids if uuid)
214 def datetime_normalizer(self, value):
216 Normalizes date/datetime value (considered to come from user input)
217 to localized datetime value. Following conversions happen:
219 naive date -> localized datetime with the same date, and time=midnight
220 naive datetime -> localized datetime with the same value
221 localized datetime -> localized datetime (no conversion)
224 if (isinstance(value, datetime.date)
225 and not isinstance(value, datetime.datetime)):
226 # Convert to local midnight
227 value_full = datetime.datetime.combine(value, datetime.time.min)
228 localized = local_zone.localize(value_full)
229 elif isinstance(value, datetime.datetime) and value.tzinfo is None:
230 # Convert to localized datetime object
231 localized = local_zone.localize(value)
233 # If the value is already localized, there is no need to change
234 # time zone at this point. Also None is a valid value too.
239 def normalize_uuid(self, value):
241 if not isinstance(value, six.text_type) or value == '':
242 raise ValueError("UUID must be a valid non-empty string.")
247 class TaskResource(SerializingObject):
248 read_only_fields = []
250 def _load_data(self, data):
251 self._data = dict((key, self._deserialize(key, value))
252 for key, value in data.items())
253 # We need to use a copy for original data, so that changes
254 # are not propagated.
255 self._original_data = copy.deepcopy(self._data)
257 def _update_data(self, data, update_original=False):
259 Low level update of the internal _data dict. Data which are coming as
260 updates should already be serialized. If update_original is True, the
261 original_data dict is updated as well.
263 self._data.update(dict((key, self._deserialize(key, value))
264 for key, value in data.items()))
267 self._original_data = copy.deepcopy(self._data)
270 def __getitem__(self, key):
271 # This is a workaround to make TaskResource non-iterable
272 # over simple index-based iteration
279 if key not in self._data:
280 self._data[key] = self._deserialize(key, None)
282 return self._data.get(key)
284 def __setitem__(self, key, value):
285 if key in self.read_only_fields:
286 raise RuntimeError('Field \'%s\' is read-only' % key)
288 # Normalize the user input before saving it
289 value = self._normalize(key, value)
290 self._data[key] = value
293 s = six.text_type(self.__unicode__())
295 s = s.encode('utf-8')
301 def export_data(self):
303 Exports current data contained in the Task as JSON
306 # We need to remove spaces for TW-1504, use custom separators
307 data_tuples = ((key, self._serialize(key, value))
308 for key, value in six.iteritems(self._data))
310 # Empty string denotes empty serialized value, we do not want
311 # to pass that to TaskWarrior.
312 data_tuples = filter(lambda t: t[1] is not '', data_tuples)
313 data = dict(data_tuples)
314 return json.dumps(data, separators=(',',':'))
317 def _modified_fields(self):
318 writable_fields = set(self._data.keys()) - set(self.read_only_fields)
319 for key in writable_fields:
320 new_value = self._data.get(key)
321 old_value = self._original_data.get(key)
323 # Make sure not to mark data removal as modified field if the
324 # field originally had some empty value
325 if key in self._data and not new_value and not old_value:
328 if new_value != old_value:
333 return bool(list(self._modified_fields))
336 class TaskAnnotation(TaskResource):
337 read_only_fields = ['entry', 'description']
339 def __init__(self, task, data={}):
341 self._load_data(data)
344 self.task.remove_annotation(self)
346 def __unicode__(self):
347 return self['description']
349 def __eq__(self, other):
350 # consider 2 annotations equal if they belong to the same task, and
351 # their data dics are the same
352 return self.task == other.task and self._data == other._data
354 __repr__ = __unicode__
357 class Task(TaskResource):
358 read_only_fields = ['id', 'entry', 'urgency', 'uuid', 'modified']
360 class DoesNotExist(Exception):
363 class CompletedTask(Exception):
365 Raised when the operation cannot be performed on the completed task.
369 class DeletedTask(Exception):
371 Raised when the operation cannot be performed on the deleted task.
375 class NotSaved(Exception):
377 Raised when the operation cannot be performed on the task, because
378 it has not been saved to TaskWarrior yet.
383 def from_input(cls, input_file=sys.stdin, modify=None):
385 Creates a Task object, directly from the stdin, by reading one line.
386 If modify=True, two lines are used, first line interpreted as the
387 original state of the Task object, and second line as its new,
388 modified value. This is consistent with the TaskWarrior's hook
391 Object created by this method should not be saved, deleted
392 or refreshed, as t could create a infinite loop. For this
393 reason, TaskWarrior instance is set to None.
395 Input_file argument can be used to specify the input file,
396 but defaults to sys.stdin.
399 # TaskWarrior instance is set to None
402 # Detect the hook type if not given directly
403 name = os.path.basename(sys.argv[0])
404 modify = name.startswith('on-modify') if modify is None else modify
406 # Load the data from the input
407 task._load_data(json.loads(input_file.readline().strip()))
409 # If this is a on-modify event, we are provided with additional
410 # line of input, which provides updated data
412 task._update_data(json.loads(input_file.readline().strip()))
416 def __init__(self, warrior, **kwargs):
417 self.warrior = warrior
419 # Check that user is not able to set read-only value in __init__
420 for key in kwargs.keys():
421 if key in self.read_only_fields:
422 raise RuntimeError('Field \'%s\' is read-only' % key)
424 # We serialize the data in kwargs so that users of the library
425 # do not have to pass different data formats via __setitem__ and
426 # __init__ methods, that would be confusing
428 # Rather unfortunate syntax due to python2.6 comaptiblity
429 self._data = dict((key, self._normalize(key, value))
430 for (key, value) in six.iteritems(kwargs))
431 self._original_data = copy.deepcopy(self._data)
433 # Provide read only access to the original data
434 self.original = ReadOnlyDictView(self._original_data)
436 def __unicode__(self):
437 return self['description']
439 def __eq__(self, other):
440 if self['uuid'] and other['uuid']:
441 # For saved Tasks, just define equality by equality of uuids
442 return self['uuid'] == other['uuid']
444 # If the tasks are not saved, compare the actual instances
445 return id(self) == id(other)
450 # For saved Tasks, just define equality by equality of uuids
451 return self['uuid'].__hash__()
453 # If the tasks are not saved, return hash of instance id
454 return id(self).__hash__()
458 return self['status'] == six.text_type('completed')
462 return self['status'] == six.text_type('deleted')
466 return self['status'] == six.text_type('waiting')
470 return self['status'] == six.text_type('pending')
474 return self['uuid'] is not None or self['id'] is not None
476 def serialize_depends(self, cur_dependencies):
477 # Check that all the tasks are saved
478 for task in (cur_dependencies or set()):
480 raise Task.NotSaved('Task \'%s\' needs to be saved before '
481 'it can be set as dependency.' % task)
483 return super(Task, self).serialize_depends(cur_dependencies)
485 def format_depends(self):
486 # We need to generate added and removed dependencies list,
487 # since Taskwarrior does not accept redefining dependencies.
489 # This cannot be part of serialize_depends, since we need
490 # to keep a list of all depedencies in the _data dictionary,
491 # not just currently added/removed ones
493 old_dependencies = self._original_data.get('depends', set())
495 added = self['depends'] - old_dependencies
496 removed = old_dependencies - self['depends']
498 # Removed dependencies need to be prefixed with '-'
499 return 'depends:' + ','.join(
500 [t['uuid'] for t in added] +
501 ['-' + t['uuid'] for t in removed]
504 def format_description(self):
505 # Task version older than 2.4.0 ignores first word of the
506 # task description if description: prefix is used
507 if self.warrior.version < VERSION_2_4_0:
508 return self._data['description']
510 return "description:'{0}'".format(self._data['description'] or '')
514 raise Task.NotSaved("Task needs to be saved before it can be deleted")
516 # Refresh the status, and raise exception if the task is deleted
517 self.refresh(only_fields=['status'])
520 raise Task.DeletedTask("Task was already deleted")
522 self.warrior.execute_command([self['uuid'], 'delete'])
524 # Refresh the status again, so that we have updated info stored
525 self.refresh(only_fields=['status'])
530 raise Task.NotSaved("Task needs to be saved before it can be completed")
532 # Refresh, and raise exception if task is already completed/deleted
533 self.refresh(only_fields=['status'])
536 raise Task.CompletedTask("Cannot complete a completed task")
538 raise Task.DeletedTask("Deleted task cannot be completed")
540 self.warrior.execute_command([self['uuid'], 'done'])
542 # Refresh the status again, so that we have updated info stored
543 self.refresh(only_fields=['status'])
546 if self.saved and not self.modified:
549 args = [self['uuid'], 'modify'] if self.saved else ['add']
550 args.extend(self._get_modified_fields_as_args())
551 output = self.warrior.execute_command(args)
553 # Parse out the new ID, if the task is being added for the first time
555 id_lines = [l for l in output if l.startswith('Created task ')]
557 # Complain loudly if it seems that more tasks were created
559 if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
560 raise TaskWarriorException("Unexpected output when creating "
561 "task: %s" % '\n'.join(id_lines))
563 # Circumvent the ID storage, since ID is considered read-only
564 self._data['id'] = int(id_lines[0].split(' ')[2].rstrip('.'))
566 # Refreshing is very important here, as not only modification time
567 # is updated, but arbitrary attribute may have changed due hooks
568 # altering the data before saving
571 def add_annotation(self, annotation):
573 raise Task.NotSaved("Task needs to be saved to add annotation")
575 args = [self['uuid'], 'annotate', annotation]
576 self.warrior.execute_command(args)
577 self.refresh(only_fields=['annotations'])
579 def remove_annotation(self, annotation):
581 raise Task.NotSaved("Task needs to be saved to remove annotation")
583 if isinstance(annotation, TaskAnnotation):
584 annotation = annotation['description']
585 args = [self['uuid'], 'denotate', annotation]
586 self.warrior.execute_command(args)
587 self.refresh(only_fields=['annotations'])
589 def _get_modified_fields_as_args(self):
592 def add_field(field):
593 # Add the output of format_field method to args list (defaults to
595 serialized_value = self._serialize(field, self._data[field])
597 # Empty values should not be enclosed in quotation marks, see
599 if serialized_value is '':
600 escaped_serialized_value = ''
602 escaped_serialized_value = "'{0}'".format(serialized_value)
604 format_default = lambda: "{0}:{1}".format(field,
605 escaped_serialized_value)
607 format_func = getattr(self, 'format_{0}'.format(field),
610 args.append(format_func())
612 # If we're modifying saved task, simply pass on all modified fields
614 for field in self._modified_fields:
616 # For new tasks, pass all fields that make sense
618 for field in self._data.keys():
619 if field in self.read_only_fields:
625 def refresh(self, only_fields=[]):
626 # Raise error when trying to refresh a task that has not been saved
628 raise Task.NotSaved("Task needs to be saved to be refreshed")
630 # We need to use ID as backup for uuid here for the refreshes
631 # of newly saved tasks. Any other place in the code is fine
632 # with using UUID only.
633 args = [self['uuid'] or self['id'], 'export']
634 new_data = json.loads(self.warrior.execute_command(args)[0])
637 [(k, new_data.get(k)) for k in only_fields])
638 self._update_data(to_update, update_original=True)
640 self._load_data(new_data)
642 class TaskFilter(SerializingObject):
644 A set of parameters to filter the task list with.
647 def __init__(self, filter_params=[]):
648 self.filter_params = filter_params
650 def add_filter(self, filter_str):
651 self.filter_params.append(filter_str)
653 def add_filter_param(self, key, value):
654 key = key.replace('__', '.')
656 # Replace the value with empty string, since that is the
657 # convention in TW for empty values
658 attribute_key = key.split('.')[0]
660 # Since this is user input, we need to normalize before we serialize
661 value = self._normalize(key, value)
662 value = self._serialize(attribute_key, value)
664 # If we are filtering by uuid:, do not use uuid keyword
667 self.filter_params.insert(0, value)
669 # Surround value with aphostrophes unless it's a empty string
670 value = "'%s'" % value if value else ''
672 # We enforce equality match by using 'is' (or 'none') modifier
673 # Without using this syntax, filter fails due to TW-1479
674 modifier = '.is' if value else '.none'
675 key = key + modifier if '.' not in key else key
677 self.filter_params.append("{0}:{1}".format(key, value))
679 def get_filter_params(self):
680 return [f for f in self.filter_params if f]
684 c.filter_params = list(self.filter_params)
688 class TaskQuerySet(object):
690 Represents a lazy lookup for a task objects.
693 def __init__(self, warrior=None, filter_obj=None):
694 self.warrior = warrior
695 self._result_cache = None
696 self.filter_obj = filter_obj or TaskFilter()
698 def __deepcopy__(self, memo):
700 Deep copy of a QuerySet doesn't populate the cache
702 obj = self.__class__()
703 for k, v in self.__dict__.items():
704 if k in ('_iter', '_result_cache'):
705 obj.__dict__[k] = None
707 obj.__dict__[k] = copy.deepcopy(v, memo)
711 data = list(self[:REPR_OUTPUT_SIZE + 1])
712 if len(data) > REPR_OUTPUT_SIZE:
713 data[-1] = "...(remaining elements truncated)..."
717 if self._result_cache is None:
718 self._result_cache = list(self)
719 return len(self._result_cache)
722 if self._result_cache is None:
723 self._result_cache = self._execute()
724 return iter(self._result_cache)
726 def __getitem__(self, k):
727 if self._result_cache is None:
728 self._result_cache = list(self)
729 return self._result_cache.__getitem__(k)
732 if self._result_cache is not None:
733 return bool(self._result_cache)
736 except StopIteration:
740 def __nonzero__(self):
741 return type(self).__bool__(self)
743 def _clone(self, klass=None, **kwargs):
745 klass = self.__class__
746 filter_obj = self.filter_obj.clone()
747 c = klass(warrior=self.warrior, filter_obj=filter_obj)
748 c.__dict__.update(kwargs)
753 Fetch the tasks which match the current filters.
755 return self.warrior.filter_tasks(self.filter_obj)
759 Returns a new TaskQuerySet that is a copy of the current one.
764 return self.filter(status=PENDING)
767 return self.filter(status=COMPLETED)
769 def filter(self, *args, **kwargs):
771 Returns a new TaskQuerySet with the given filters added.
773 clone = self._clone()
775 clone.filter_obj.add_filter(f)
776 for key, value in kwargs.items():
777 clone.filter_obj.add_filter_param(key, value)
780 def get(self, **kwargs):
782 Performs the query and returns a single object matching the given
785 clone = self.filter(**kwargs)
788 return clone._result_cache[0]
790 raise Task.DoesNotExist(
791 'Task matching query does not exist. '
792 'Lookup parameters were {0}'.format(kwargs))
794 'get() returned more than one Task -- it returned {0}! '
795 'Lookup parameters were {1}'.format(num, kwargs))
798 class TaskWarrior(object):
799 def __init__(self, data_location='~/.task', create=True):
800 data_location = os.path.expanduser(data_location)
801 if create and not os.path.exists(data_location):
802 os.makedirs(data_location)
804 'data.location': os.path.expanduser(data_location),
805 'confirmation': 'no',
806 'dependency.confirmation': 'no', # See TW-1483 or taskrc man page
807 'recurrence.confirmation': 'no', # Necessary for modifying R tasks
809 self.tasks = TaskQuerySet(self)
810 self.version = self._get_version()
812 def _get_command_args(self, args, config_override={}):
813 command_args = ['task', 'rc:/']
814 config = self.config.copy()
815 config.update(config_override)
816 for item in config.items():
817 command_args.append('rc.{0}={1}'.format(*item))
818 command_args.extend(map(str, args))
821 def _get_version(self):
822 p = subprocess.Popen(
823 ['task', '--version'],
824 stdout=subprocess.PIPE,
825 stderr=subprocess.PIPE)
826 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
827 return stdout.strip('\n')
829 def execute_command(self, args, config_override={}, allow_failure=True):
830 command_args = self._get_command_args(
831 args, config_override=config_override)
832 logger.debug(' '.join(command_args))
833 p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
834 stderr=subprocess.PIPE)
835 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
836 if p.returncode and allow_failure:
838 error_msg = stderr.strip().splitlines()[-1]
840 error_msg = stdout.strip()
841 raise TaskWarriorException(error_msg)
842 return stdout.strip().split('\n')
844 def enforce_recurrence(self):
845 # Run arbitrary report command which will trigger generation
846 # of recurrent tasks.
847 # TODO: Make a version dependant enforcement once
849 self.execute_command(['next'], allow_failure=False)
851 def filter_tasks(self, filter_obj):
852 self.enforce_recurrence()
853 args = ['export', '--'] + filter_obj.get_filter_params()
855 for line in self.execute_command(args):
857 data = line.strip(',')
859 filtered_task = Task(self)
860 filtered_task._load_data(json.loads(data))
861 tasks.append(filtered_task)
863 raise TaskWarriorException('Invalid JSON: %s' % data)
866 def merge_with(self, path, push=False):
867 path = path.rstrip('/') + '/'
868 self.execute_command(['merge', path], config_override={
869 'merge.autopush': 'yes' if push else 'no',
873 self.execute_command(['undo'])