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
10 DATE_FORMAT = '%Y%m%dT%H%M%SZ'
13 COMPLETED = 'completed'
15 VERSION_2_1_0 = six.u('2.1.0')
16 VERSION_2_2_0 = six.u('2.2.0')
17 VERSION_2_3_0 = six.u('2.3.0')
18 VERSION_2_4_0 = six.u('2.4.0')
20 logger = logging.getLogger(__name__)
23 class TaskWarriorException(Exception):
27 class TaskResource(object):
30 def _load_data(self, data):
33 def __getitem__(self, key):
34 hydrate_func = getattr(self, 'deserialize_{0}'.format(key),
36 return hydrate_func(self._data.get(key))
38 def __setitem__(self, key, value):
39 if key in self.read_only_fields:
40 raise RuntimeError('Field \'%s\' is read-only' % key)
41 dehydrate_func = getattr(self, 'serialize_{0}'.format(key),
43 self._data[key] = dehydrate_func(value)
44 self._modified_fields.add(key)
47 s = six.text_type(self.__unicode__())
56 class TaskAnnotation(TaskResource):
57 read_only_fields = ['entry', 'description']
59 def __init__(self, task, data={}):
63 def deserialize_entry(self, data):
64 return datetime.datetime.strptime(data, DATE_FORMAT) if data else None
66 def serialize_entry(self, date):
67 return date.strftime(DATE_FORMAT) if date else ''
70 self.task.remove_annotation(self)
72 def __unicode__(self):
73 return self['description']
75 __repr__ = __unicode__
78 class Task(TaskResource):
79 read_only_fields = ['id', 'entry', 'urgency', 'uuid']
81 class DoesNotExist(Exception):
84 class CompletedTask(Exception):
86 Raised when the operation cannot be performed on the completed task.
90 class DeletedTask(Exception):
92 Raised when the operation cannot be performed on the deleted task.
96 class NotSaved(Exception):
98 Raised when the operation cannot be performed on the task, because
99 it has not been saved to TaskWarrior yet.
103 def __init__(self, warrior, data={}, **kwargs):
104 self.warrior = warrior
106 # We keep data for backwards compatibility
109 self._load_data(kwargs)
110 self._modified_fields = set()
112 def __unicode__(self):
113 return self['description']
115 def __eq__(self, other):
116 return self['uuid'] == other['uuid']
119 return self['uuid'].__hash__()
123 return self['status'] == six.text_type('completed')
127 return self['status'] == six.text_type('deleted')
131 return self['status'] == six.text_type('waiting')
135 return self['status'] == six.text_type('pending')
139 return self['uuid'] is not None or self['id'] is not None
141 def serialize_due(self, date):
142 return date.strftime(DATE_FORMAT)
144 def deserialize_due(self, date_str):
147 return datetime.datetime.strptime(date_str, DATE_FORMAT)
149 def deserialize_annotations(self, data):
150 return [TaskAnnotation(self, d) for d in data] if data else []
152 def deserialize_tags(self, tags):
153 if isinstance(tags, basestring):
154 return tags.split(',') if tags else []
157 def serialize_tags(self, tags):
158 return ','.join(tags) if tags else ''
162 raise Task.NotSaved("Task needs to be saved before it can be deleted")
164 # Refresh the status, and raise exception if the task is deleted
165 self.refresh(only_fields=['status'])
168 raise Task.DeletedTask("Task was already deleted")
170 self.warrior.execute_command([self['uuid'], 'delete'], config_override={
171 'confirmation': 'no',
174 # Refresh the status again, so that we have updated info stored
175 self.refresh(only_fields=['status'])
180 raise Task.NotSaved("Task needs to be saved before it can be completed")
182 # Refresh, and raise exception if task is already completed/deleted
183 self.refresh(only_fields=['status'])
186 raise Task.CompletedTask("Cannot complete a completed task")
188 raise Task.DeletedTask("Deleted task cannot be completed")
190 self.warrior.execute_command([self['uuid'], 'done'])
192 # Refresh the status again, so that we have updated info stored
193 self.refresh(only_fields=['status'])
196 args = [self['uuid'], 'modify'] if self.saved else ['add']
197 args.extend(self._get_modified_fields_as_args())
198 output = self.warrior.execute_command(args)
200 # Parse out the new ID, if the task is being added for the first time
202 id_lines = [l for l in output if l.startswith('Created task ')]
204 # Complain loudly if it seems that more tasks were created
206 if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
207 raise TaskWarriorException("Unexpected output when creating "
208 "task: %s" % '\n'.join(id_lines))
210 # Circumvent the ID storage, since ID is considered read-only
211 self._data['id'] = int(id_lines[0].split(' ')[2].rstrip('.'))
213 self._modified_fields.clear()
216 def add_annotation(self, annotation):
218 raise Task.NotSaved("Task needs to be saved to add annotation")
220 args = [self['uuid'], 'annotate', annotation]
221 self.warrior.execute_command(args)
222 self.refresh(only_fields=['annotations'])
224 def remove_annotation(self, annotation):
226 raise Task.NotSaved("Task needs to be saved to add annotation")
228 if isinstance(annotation, TaskAnnotation):
229 annotation = annotation['description']
230 args = [self['uuid'], 'denotate', annotation]
231 self.warrior.execute_command(args)
232 self.refresh(only_fields=['annotations'])
234 def _get_modified_fields_as_args(self):
237 def add_field(field):
238 # Task version older than 2.4.0 ignores first word of the
239 # task description if description: prefix is used
240 if self.warrior.version < VERSION_2_4_0 and field == 'description':
241 args.append(self._data[field])
243 args.append('{0}:{1}'.format(field, self._data[field]))
245 # If we're modifying saved task, simply pass on all modified fields
247 for field in self._modified_fields:
249 # For new tasks, pass all fields that make sense
251 for field in self._data.keys():
252 if field in self.read_only_fields:
258 def refresh(self, only_fields=[]):
259 # Raise error when trying to refresh a task that has not been saved
261 raise Task.NotSaved("Task needs to be saved to be refreshed")
263 # We need to use ID as backup for uuid here for the refreshes
264 # of newly saved tasks. Any other place in the code is fine
265 # with using UUID only.
266 args = [self['uuid'] or self['id'], 'export']
267 new_data = json.loads(self.warrior.execute_command(args)[0])
270 [(k, new_data.get(k)) for k in only_fields])
271 self._data.update(to_update)
273 self._data = new_data
276 class TaskFilter(object):
278 A set of parameters to filter the task list with.
281 def __init__(self, filter_params=[]):
282 self.filter_params = filter_params
284 def add_filter(self, filter_str):
285 self.filter_params.append(filter_str)
287 def add_filter_param(self, key, value):
288 key = key.replace('__', '.')
290 # Replace the value with empty string, since that is the
291 # convention in TW for empty values
292 value = value if value is not None else ''
294 # If we are filtering by uuid:, do not use uuid keyword
297 self.filter_params.insert(0, value)
299 self.filter_params.append('{0}:{1}'.format(key, value))
301 def get_filter_params(self):
302 return [f for f in self.filter_params if f]
306 c.filter_params = list(self.filter_params)
310 class TaskQuerySet(object):
312 Represents a lazy lookup for a task objects.
315 def __init__(self, warrior=None, filter_obj=None):
316 self.warrior = warrior
317 self._result_cache = None
318 self.filter_obj = filter_obj or TaskFilter()
320 def __deepcopy__(self, memo):
322 Deep copy of a QuerySet doesn't populate the cache
324 obj = self.__class__()
325 for k, v in self.__dict__.items():
326 if k in ('_iter', '_result_cache'):
327 obj.__dict__[k] = None
329 obj.__dict__[k] = copy.deepcopy(v, memo)
333 data = list(self[:REPR_OUTPUT_SIZE + 1])
334 if len(data) > REPR_OUTPUT_SIZE:
335 data[-1] = "...(remaining elements truncated)..."
339 if self._result_cache is None:
340 self._result_cache = list(self)
341 return len(self._result_cache)
344 if self._result_cache is None:
345 self._result_cache = self._execute()
346 return iter(self._result_cache)
348 def __getitem__(self, k):
349 if self._result_cache is None:
350 self._result_cache = list(self)
351 return self._result_cache.__getitem__(k)
354 if self._result_cache is not None:
355 return bool(self._result_cache)
358 except StopIteration:
362 def __nonzero__(self):
363 return type(self).__bool__(self)
365 def _clone(self, klass=None, **kwargs):
367 klass = self.__class__
368 filter_obj = self.filter_obj.clone()
369 c = klass(warrior=self.warrior, filter_obj=filter_obj)
370 c.__dict__.update(kwargs)
375 Fetch the tasks which match the current filters.
377 return self.warrior.filter_tasks(self.filter_obj)
381 Returns a new TaskQuerySet that is a copy of the current one.
386 return self.filter(status=PENDING)
389 return self.filter(status=COMPLETED)
391 def filter(self, *args, **kwargs):
393 Returns a new TaskQuerySet with the given filters added.
395 clone = self._clone()
397 clone.filter_obj.add_filter(f)
398 for key, value in kwargs.items():
399 clone.filter_obj.add_filter_param(key, value)
402 def get(self, **kwargs):
404 Performs the query and returns a single object matching the given
407 clone = self.filter(**kwargs)
410 return clone._result_cache[0]
412 raise Task.DoesNotExist(
413 'Task matching query does not exist. '
414 'Lookup parameters were {0}'.format(kwargs))
416 'get() returned more than one Task -- it returned {0}! '
417 'Lookup parameters were {1}'.format(num, kwargs))
420 class TaskWarrior(object):
421 def __init__(self, data_location='~/.task', create=True):
422 data_location = os.path.expanduser(data_location)
423 if create and not os.path.exists(data_location):
424 os.makedirs(data_location)
426 'data.location': os.path.expanduser(data_location),
428 self.tasks = TaskQuerySet(self)
429 self.version = self._get_version()
431 def _get_command_args(self, args, config_override={}):
432 command_args = ['task', 'rc:/']
433 config = self.config.copy()
434 config.update(config_override)
435 for item in config.items():
436 command_args.append('rc.{0}={1}'.format(*item))
437 command_args.extend(map(str, args))
440 def _get_version(self):
441 p = subprocess.Popen(
442 ['task', '--version'],
443 stdout=subprocess.PIPE,
444 stderr=subprocess.PIPE)
445 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
446 return stdout.strip('\n')
448 def execute_command(self, args, config_override={}):
449 command_args = self._get_command_args(
450 args, config_override=config_override)
451 logger.debug(' '.join(command_args))
452 p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
453 stderr=subprocess.PIPE)
454 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
457 error_msg = stderr.strip().splitlines()[-1]
459 error_msg = stdout.strip()
460 raise TaskWarriorException(error_msg)
461 return stdout.strip().split('\n')
463 def filter_tasks(self, filter_obj):
464 args = ['export', '--'] + filter_obj.get_filter_params()
466 for line in self.execute_command(args):
468 data = line.strip(',')
470 tasks.append(Task(self, json.loads(data)))
472 raise TaskWarriorException('Invalid JSON: %s' % data)
475 def merge_with(self, path, push=False):
476 path = path.rstrip('/') + '/'
477 self.execute_command(['merge', path], config_override={
478 'merge.autopush': 'yes' if push else 'no',
482 self.execute_command(['undo'], config_override={
483 'confirmation': 'no',