-
-
-class TaskWarrior(object):
- def __init__(self, data_location='~/.task', create=True):
- data_location = os.path.expanduser(data_location)
- if create and not os.path.exists(data_location):
- os.makedirs(data_location)
- self.config = {
- 'data.location': os.path.expanduser(data_location),
- 'confirmation': 'no',
- }
- self.tasks = TaskQuerySet(self)
- self.version = self._get_version()
-
- def _get_command_args(self, args, config_override={}):
- command_args = ['task', 'rc:/']
- config = self.config.copy()
- config.update(config_override)
- for item in config.items():
- command_args.append('rc.{0}={1}'.format(*item))
- command_args.extend(map(str, args))
- return command_args
-
- def _get_version(self):
- p = subprocess.Popen(
- ['task', '--version'],
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
- return stdout.strip('\n')
-
- def execute_command(self, args, config_override={}):
- command_args = self._get_command_args(
- args, config_override=config_override)
- logger.debug(' '.join(command_args))
- p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
- if p.returncode:
- if stderr.strip():
- error_msg = stderr.strip().splitlines()[-1]
- else:
- error_msg = stdout.strip()
- raise TaskWarriorException(error_msg)
- return stdout.strip().split('\n')
-
- def filter_tasks(self, filter_obj):
- args = ['export', '--'] + filter_obj.get_filter_params()
- tasks = []
- for line in self.execute_command(args):
- if line:
- data = line.strip(',')
- try:
- filtered_task = Task(self)
- filtered_task._load_data(json.loads(data))
- tasks.append(filtered_task)
- except ValueError:
- raise TaskWarriorException('Invalid JSON: %s' % data)
- return tasks
-
- def merge_with(self, path, push=False):
- path = path.rstrip('/') + '/'
- self.execute_command(['merge', path], config_override={
- 'merge.autopush': 'yes' if push else 'no',
- })
-
- def undo(self):
- self.execute_command(['undo'])