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):
32 # We need to use a copy for original data, so that changes
34 self._original_data = data.copy()
36 def __getitem__(self, key):
37 hydrate_func = getattr(self, 'deserialize_{0}'.format(key),
39 return hydrate_func(self._data.get(key))
41 def __setitem__(self, key, value):
42 if key in self.read_only_fields:
43 raise RuntimeError('Field \'%s\' is read-only' % key)
44 dehydrate_func = getattr(self, 'serialize_{0}'.format(key),
46 self._data[key] = dehydrate_func(value)
49 s = six.text_type(self.__unicode__())
58 class TaskAnnotation(TaskResource):
59 read_only_fields = ['entry', 'description']
61 def __init__(self, task, data={}):
65 def deserialize_entry(self, data):
66 return datetime.datetime.strptime(data, DATE_FORMAT) if data else None
68 def serialize_entry(self, date):
69 return date.strftime(DATE_FORMAT) if date else ''
72 self.task.remove_annotation(self)
74 def __unicode__(self):
75 return self['description']
77 __repr__ = __unicode__
80 class Task(TaskResource):
81 read_only_fields = ['id', 'entry', 'urgency', 'uuid']
83 class DoesNotExist(Exception):
86 class CompletedTask(Exception):
88 Raised when the operation cannot be performed on the completed task.
92 class DeletedTask(Exception):
94 Raised when the operation cannot be performed on the deleted task.
98 class NotSaved(Exception):
100 Raised when the operation cannot be performed on the task, because
101 it has not been saved to TaskWarrior yet.
105 def __init__(self, warrior, data={}, **kwargs):
106 self.warrior = warrior
108 # We keep data for backwards compatibility
111 self._load_data(kwargs)
113 def __unicode__(self):
114 return self['description']
116 def __eq__(self, other):
117 return self['uuid'] == other['uuid']
120 return self['uuid'].__hash__()
123 def _modified_fields(self):
124 for key in self._data.keys():
125 if self._data.get(key) != self._original_data.get(key):
130 return self['status'] == six.text_type('completed')
134 return self['status'] == six.text_type('deleted')
138 return self['status'] == six.text_type('waiting')
142 return self['status'] == six.text_type('pending')
146 return self['uuid'] is not None or self['id'] is not None
148 def serialize_due(self, date):
149 return date.strftime(DATE_FORMAT)
151 def deserialize_due(self, date_str):
154 return datetime.datetime.strptime(date_str, DATE_FORMAT)
156 def deserialize_annotations(self, data):
157 return [TaskAnnotation(self, d) for d in data] if data else []
159 def deserialize_tags(self, tags):
160 if isinstance(tags, basestring):
161 return tags.split(',') if tags else []
164 def serialize_tags(self, tags):
165 return ','.join(tags) if tags else ''
169 raise Task.NotSaved("Task needs to be saved before it can be deleted")
171 # Refresh the status, and raise exception if the task is deleted
172 self.refresh(only_fields=['status'])
175 raise Task.DeletedTask("Task was already deleted")
177 self.warrior.execute_command([self['uuid'], 'delete'], config_override={
178 'confirmation': 'no',
181 # Refresh the status again, so that we have updated info stored
182 self.refresh(only_fields=['status'])
187 raise Task.NotSaved("Task needs to be saved before it can be completed")
189 # Refresh, and raise exception if task is already completed/deleted
190 self.refresh(only_fields=['status'])
193 raise Task.CompletedTask("Cannot complete a completed task")
195 raise Task.DeletedTask("Deleted task cannot be completed")
197 self.warrior.execute_command([self['uuid'], 'done'])
199 # Refresh the status again, so that we have updated info stored
200 self.refresh(only_fields=['status'])
203 args = [self['uuid'], 'modify'] if self.saved else ['add']
204 args.extend(self._get_modified_fields_as_args())
205 output = self.warrior.execute_command(args)
207 # Parse out the new ID, if the task is being added for the first time
209 id_lines = [l for l in output if l.startswith('Created task ')]
211 # Complain loudly if it seems that more tasks were created
213 if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
214 raise TaskWarriorException("Unexpected output when creating "
215 "task: %s" % '\n'.join(id_lines))
217 # Circumvent the ID storage, since ID is considered read-only
218 self._data['id'] = int(id_lines[0].split(' ')[2].rstrip('.'))
222 def add_annotation(self, annotation):
224 raise Task.NotSaved("Task needs to be saved to add annotation")
226 args = [self['uuid'], 'annotate', annotation]
227 self.warrior.execute_command(args)
228 self.refresh(only_fields=['annotations'])
230 def remove_annotation(self, annotation):
232 raise Task.NotSaved("Task needs to be saved to add annotation")
234 if isinstance(annotation, TaskAnnotation):
235 annotation = annotation['description']
236 args = [self['uuid'], 'denotate', annotation]
237 self.warrior.execute_command(args)
238 self.refresh(only_fields=['annotations'])
240 def _get_modified_fields_as_args(self):
243 def add_field(field):
244 # Task version older than 2.4.0 ignores first word of the
245 # task description if description: prefix is used
246 if self.warrior.version < VERSION_2_4_0 and field == 'description':
247 args.append(self._data[field])
249 args.append('{0}:{1}'.format(field, self._data[field]))
251 # If we're modifying saved task, simply pass on all modified fields
253 for field in self._modified_fields:
255 # For new tasks, pass all fields that make sense
257 for field in self._data.keys():
258 if field in self.read_only_fields:
264 def refresh(self, only_fields=[]):
265 # Raise error when trying to refresh a task that has not been saved
267 raise Task.NotSaved("Task needs to be saved to be refreshed")
269 # We need to use ID as backup for uuid here for the refreshes
270 # of newly saved tasks. Any other place in the code is fine
271 # with using UUID only.
272 args = [self['uuid'] or self['id'], 'export']
273 new_data = json.loads(self.warrior.execute_command(args)[0])
276 [(k, new_data.get(k)) for k in only_fields])
277 self._data.update(to_update)
278 self._original_data.update(to_update)
280 self._data = new_data
281 # We need to create a clone for original_data though
282 # Shallow copy is alright, since data dict uses only
283 # primitive data types
284 self._original_data = new_data.copy()
287 class TaskFilter(object):
289 A set of parameters to filter the task list with.
292 def __init__(self, filter_params=[]):
293 self.filter_params = filter_params
295 def add_filter(self, filter_str):
296 self.filter_params.append(filter_str)
298 def add_filter_param(self, key, value):
299 key = key.replace('__', '.')
301 # Replace the value with empty string, since that is the
302 # convention in TW for empty values
303 value = value if value is not None else ''
305 # If we are filtering by uuid:, do not use uuid keyword
308 self.filter_params.insert(0, value)
310 self.filter_params.append('{0}:{1}'.format(key, value))
312 def get_filter_params(self):
313 return [f for f in self.filter_params if f]
317 c.filter_params = list(self.filter_params)
321 class TaskQuerySet(object):
323 Represents a lazy lookup for a task objects.
326 def __init__(self, warrior=None, filter_obj=None):
327 self.warrior = warrior
328 self._result_cache = None
329 self.filter_obj = filter_obj or TaskFilter()
331 def __deepcopy__(self, memo):
333 Deep copy of a QuerySet doesn't populate the cache
335 obj = self.__class__()
336 for k, v in self.__dict__.items():
337 if k in ('_iter', '_result_cache'):
338 obj.__dict__[k] = None
340 obj.__dict__[k] = copy.deepcopy(v, memo)
344 data = list(self[:REPR_OUTPUT_SIZE + 1])
345 if len(data) > REPR_OUTPUT_SIZE:
346 data[-1] = "...(remaining elements truncated)..."
350 if self._result_cache is None:
351 self._result_cache = list(self)
352 return len(self._result_cache)
355 if self._result_cache is None:
356 self._result_cache = self._execute()
357 return iter(self._result_cache)
359 def __getitem__(self, k):
360 if self._result_cache is None:
361 self._result_cache = list(self)
362 return self._result_cache.__getitem__(k)
365 if self._result_cache is not None:
366 return bool(self._result_cache)
369 except StopIteration:
373 def __nonzero__(self):
374 return type(self).__bool__(self)
376 def _clone(self, klass=None, **kwargs):
378 klass = self.__class__
379 filter_obj = self.filter_obj.clone()
380 c = klass(warrior=self.warrior, filter_obj=filter_obj)
381 c.__dict__.update(kwargs)
386 Fetch the tasks which match the current filters.
388 return self.warrior.filter_tasks(self.filter_obj)
392 Returns a new TaskQuerySet that is a copy of the current one.
397 return self.filter(status=PENDING)
400 return self.filter(status=COMPLETED)
402 def filter(self, *args, **kwargs):
404 Returns a new TaskQuerySet with the given filters added.
406 clone = self._clone()
408 clone.filter_obj.add_filter(f)
409 for key, value in kwargs.items():
410 clone.filter_obj.add_filter_param(key, value)
413 def get(self, **kwargs):
415 Performs the query and returns a single object matching the given
418 clone = self.filter(**kwargs)
421 return clone._result_cache[0]
423 raise Task.DoesNotExist(
424 'Task matching query does not exist. '
425 'Lookup parameters were {0}'.format(kwargs))
427 'get() returned more than one Task -- it returned {0}! '
428 'Lookup parameters were {1}'.format(num, kwargs))
431 class TaskWarrior(object):
432 def __init__(self, data_location='~/.task', create=True):
433 data_location = os.path.expanduser(data_location)
434 if create and not os.path.exists(data_location):
435 os.makedirs(data_location)
437 'data.location': os.path.expanduser(data_location),
439 self.tasks = TaskQuerySet(self)
440 self.version = self._get_version()
442 def _get_command_args(self, args, config_override={}):
443 command_args = ['task', 'rc:/']
444 config = self.config.copy()
445 config.update(config_override)
446 for item in config.items():
447 command_args.append('rc.{0}={1}'.format(*item))
448 command_args.extend(map(str, args))
451 def _get_version(self):
452 p = subprocess.Popen(
453 ['task', '--version'],
454 stdout=subprocess.PIPE,
455 stderr=subprocess.PIPE)
456 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
457 return stdout.strip('\n')
459 def execute_command(self, args, config_override={}):
460 command_args = self._get_command_args(
461 args, config_override=config_override)
462 logger.debug(' '.join(command_args))
463 p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
464 stderr=subprocess.PIPE)
465 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
468 error_msg = stderr.strip().splitlines()[-1]
470 error_msg = stdout.strip()
471 raise TaskWarriorException(error_msg)
472 return stdout.strip().split('\n')
474 def filter_tasks(self, filter_obj):
475 args = ['export', '--'] + filter_obj.get_filter_params()
477 for line in self.execute_command(args):
479 data = line.strip(',')
481 tasks.append(Task(self, json.loads(data)))
483 raise TaskWarriorException('Invalid JSON: %s' % data)
486 def merge_with(self, path, push=False):
487 path = path.rstrip('/') + '/'
488 self.execute_command(['merge', path], config_override={
489 'merge.autopush': 'yes' if push else 'no',
493 self.execute_command(['undo'], config_override={
494 'confirmation': 'no',