def sync_with_taskwarrior(self): # This is called at the point where all the tasks in the vim # are already synced. This should load the tasks from TW matching # the filter, and add the tasks that are new. Optionally remove the # tasks that are not longer belonging there. # Get sets of tasks to add and to delete to_add, to_del = self.get_tasks_to_add_and_del() # Remove tasks that no longer match the filter for task in to_del: # Find matching vimwikitasks in the self.tasks set # There might be more if the viewport contained multiple # representations of the same task if task.saved: matching_vimwikitasks = [ t for t in self.tasks if t.uuid == short.ShortUUID(task['uuid'], task.backend) ] else: # For the tasks that are not saved yet, only one # representation can exist, so use object-comparison matching_vimwikitasks = [ t for t in self.tasks if t.task == task ] # Remove the tasks from viewport's set and from buffer for vimwikitask in matching_vimwikitasks: self.tasks.remove(vimwikitask) self.cache.remove_line(vimwikitask['line_number']) # Add the tasks that match the filter and are not listed added_tasks = 0 existing_tasks = len(self.tasks) sorted_to_add = list(to_add) sorted_to_add.sort(key=lambda x: x['entry']) for task in sorted_to_add: added_tasks += 1 added_at = self.line_number + existing_tasks + added_tasks # Add the task object to cache self.cache.task[short.ShortUUID(task['uuid'], self.tw)] = task # Create the VimwikiTask vimwikitask = vwtask.VimwikiTask.from_task(self.cache, task) vimwikitask['line_number'] = added_at self.tasks.add(vimwikitask) # Update the buffer self.cache.insert_line(str(vimwikitask), added_at) # Save it to cache self.cache.vwtask[added_at] = vimwikitask sort.TaskSorter(self.cache, self.tasks, self.sort).execute()
def load_tasks(self): raw_task_info = [] # Load the tasks in batch, all in given TaskWarrior instance for line in self.buffer: match = re.search(regexp.GENERIC_TASK, line) if not match: continue tw = self.warriors[match.group('source') or 'default'] uuid = match.group('uuid') if not uuid: continue raw_task_info.append((uuid, tw)) for tw in self.warriors.values(): # Select all tasks in the files that have UUIDs uuids = [uuid for uuid, task_tw in raw_task_info if task_tw == tw] # If no task in the file contains UUID, we have no job here if not uuids: continue # Get them out of TaskWarrior at once tasks = tw.tasks.filter() for uuid in uuids: tasks = tasks.filter(uuid=uuid) # Update each task in the cache for task in tasks: key = short.ShortUUID(task['uuid'], tw) self.task[key] = task