-
-
-class TaskWarrior(object):
- DEFAULT_FILTERS = {
- 'status': 'pending',
- }
-
- def __init__(self, data_location='~/.task', create=True):
- if not os.path.exists(data_location):
- os.makedirs(data_location)
- self.config = {
- 'data.location': os.path.expanduser(data_location),
- }
- self.tasks = TaskQuerySet(self)
-
- def _generate_command(self, command):
- args = ['task', 'rc:/']
- for item in self.config.items():
- args.append('rc.{0}={1}'.format(*item))
- args.append(command)
- return ' '.join(args)
-
- def _execute_command(self, command):
- p = subprocess.Popen(self._generate_command(command), shell=True,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- stdout, stderr = p.communicate()
- if p.returncode:
- raise TaskWarriorException(stderr.strip())
- return stdout.strip().split('\n')
-
- def _format_filter_kwarg(self, kwarg):
- key, val = kwarg[0], kwarg[1]
- key = key.replace('__', '.')
- return '{0}:{1}'.format(key, val)
-
- def _execute_filter(self, filter_obj):
- filter_commands = ' '.join(map(self._format_filter_kwarg,
- filter_obj.get_filter_params()))
- command = '{0} export'.format(filter_commands)
- tasks = []
- for line in self._execute_command(command):
- if line:
- tasks.append(Task(self, json.loads(line.strip(','))))
- return tasks
-
- def add_task(self, description, project=None):
- args = ['add', description]
- if project is not None:
- args.append('project:{0}'.format(project))
- self._execute_command(' '.join(args))
-
- def delete_task(self, task_id):
- self._execute_command('{0} rc.confirmation:no delete'.format(task_id))
-
- def complete_task(self, task_id):
- self._execute_command('{0} done'.format(task_id))
-
- def import_tasks(self, tasks):
- fd, path = tempfile.mkstemp()
- with open(path, 'w') as f:
- f.write(json.dumps(tasks))
- self._execute_command('import {0}'.format(path))