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.
11 from .task import Task, TaskQuerySet, ReadOnlyDictView
12 from .filters import TaskWarriorFilter
13 from .serializing import local_zone
15 DATE_FORMAT_CALC = '%Y-%m-%dT%H:%M:%S'
17 logger = logging.getLogger(__name__)
20 class Backend(object):
23 def filter_class(self):
24 """Returns the TaskFilter class used by this backend"""
28 def filter_tasks(self, filter_obj):
29 """Returns a list of Task objects matching the given filter"""
33 def save_task(self, task):
37 def delete_task(self, task):
41 def start_task(self, task):
45 def stop_task(self, task):
49 def complete_task(self, task):
53 def refresh_task(self, task, after_save=False):
55 Refreshes the given task. Returns new data dict with serialized
61 def annotate_task(self, task, annotation):
65 def denotate_task(self, task, annotation):
70 """Syncs the backend database with the taskd server"""
73 def convert_datetime_string(self, value):
75 Converts TW syntax datetime string to a localized datetime
76 object. This method is not mandatory.
81 class TaskWarriorException(Exception):
85 class TaskWarrior(Backend):
87 VERSION_2_1_0 = six.u('2.1.0')
88 VERSION_2_2_0 = six.u('2.2.0')
89 VERSION_2_3_0 = six.u('2.3.0')
90 VERSION_2_4_0 = six.u('2.4.0')
91 VERSION_2_4_1 = six.u('2.4.1')
92 VERSION_2_4_2 = six.u('2.4.2')
93 VERSION_2_4_3 = six.u('2.4.3')
94 VERSION_2_4_4 = six.u('2.4.4')
95 VERSION_2_4_5 = six.u('2.4.5')
97 def __init__(self, data_location=None, create=True, taskrc_location='~/.taskrc'):
98 self.taskrc_location = os.path.expanduser(taskrc_location)
100 # If taskrc does not exist, pass / to use defaults and avoid creating
101 # dummy .taskrc file by TaskWarrior
102 if not os.path.exists(self.taskrc_location):
103 self.taskrc_location = '/'
106 self.version = self._get_version()
108 'confirmation': 'no',
109 'dependency.confirmation': 'no', # See TW-1483 or taskrc man page
110 'recurrence.confirmation': 'no', # Necessary for modifying R tasks
112 # Defaults to on since 2.4.5, we expect off during parsing
115 # 2.4.3 onwards supports 0 as infite bulk, otherwise set just
116 # arbitrary big number which is likely to be large enough
117 'bulk': 0 if self.version >= self.VERSION_2_4_3 else 100000,
120 # Set data.location override if passed via kwarg
121 if data_location is not None:
122 data_location = os.path.expanduser(data_location)
123 if create and not os.path.exists(data_location):
124 os.makedirs(data_location)
125 self.overrides['data.location'] = data_location
127 self.tasks = TaskQuerySet(self)
129 def _get_command_args(self, args, config_override=None):
130 command_args = ['task', 'rc:{0}'.format(self.taskrc_location)]
131 overrides = self.overrides.copy()
132 overrides.update(config_override or dict())
133 for item in overrides.items():
134 command_args.append('rc.{0}={1}'.format(*item))
135 command_args.extend(map(six.text_type, args))
138 def _get_version(self):
139 p = subprocess.Popen(
140 ['task', '--version'],
141 stdout=subprocess.PIPE,
142 stderr=subprocess.PIPE)
143 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
144 return stdout.strip('\n')
146 def _get_modified_task_fields_as_args(self, task):
149 def add_field(field):
150 # Add the output of format_field method to args list (defaults to
152 serialized_value = task._serialize(field, task._data[field])
154 # Empty values should not be enclosed in quotation marks, see
156 if serialized_value is '':
157 escaped_serialized_value = ''
159 escaped_serialized_value = six.u("'{0}'").format(
162 format_default = lambda task: six.u("{0}:{1}").format(
163 field, escaped_serialized_value)
165 format_func = getattr(self, 'format_{0}'.format(field),
168 args.append(format_func(task))
170 # If we're modifying saved task, simply pass on all modified fields
172 for field in task._modified_fields:
174 # For new tasks, pass all fields that make sense
176 for field in task._data.keys():
177 if field in task.read_only_fields:
183 def format_depends(self, task):
184 # We need to generate added and removed dependencies list,
185 # since Taskwarrior does not accept redefining dependencies.
187 # This cannot be part of serialize_depends, since we need
188 # to keep a list of all depedencies in the _data dictionary,
189 # not just currently added/removed ones
191 old_dependencies = task._original_data.get('depends', set())
193 added = task['depends'] - old_dependencies
194 removed = old_dependencies - task['depends']
196 # Removed dependencies need to be prefixed with '-'
197 return 'depends:' + ','.join(
198 [t['uuid'] for t in added] +
199 ['-' + t['uuid'] for t in removed]
202 def format_description(self, task):
203 # Task version older than 2.4.0 ignores first word of the
204 # task description if description: prefix is used
205 if self.version < self.VERSION_2_4_0:
206 return task._data['description']
208 return six.u("description:'{0}'").format(task._data['description'] or '')
210 def convert_datetime_string(self, value):
212 if self.version >= self.VERSION_2_4_0:
213 # For strings, use 'task calc' to evaluate the string to datetime
214 # available since TW 2.4.0
216 result = self.execute_command(['calc'] + args)
217 naive = datetime.datetime.strptime(result[0], DATE_FORMAT_CALC)
218 localized = local_zone.localize(naive)
220 raise ValueError("Provided value could not be converted to "
221 "datetime, its type is not supported: {}"
222 .format(type(value)))
227 def filter_class(self):
228 return TaskWarriorFilter
234 # First, check if memoized information is available
238 # If not, fetch the config using the 'show' command
239 raw_output = self.execute_command(
241 config_override={'verbose': 'nothing'}
245 config_regex = re.compile(r'^(?P<key>[^\s]+)\s+(?P<value>[^\s].+$)')
247 for line in raw_output:
248 match = config_regex.match(line)
250 config[match.group('key')] = match.group('value').strip()
252 # Memoize the config dict
253 self._config = ReadOnlyDictView(config)
257 def execute_command(self, args, config_override=None, allow_failure=True,
259 command_args = self._get_command_args(
260 args, config_override=config_override)
261 logger.debug(' '.join(command_args))
262 p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
263 stderr=subprocess.PIPE)
264 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
265 if p.returncode and allow_failure:
267 error_msg = stderr.strip()
269 error_msg = stdout.strip()
270 raise TaskWarriorException(error_msg)
272 # Return all whole triplet only if explicitly asked for
274 return stdout.rstrip().split('\n')
276 return (stdout.rstrip().split('\n'),
277 stderr.rstrip().split('\n'),
280 def enforce_recurrence(self):
281 # Run arbitrary report command which will trigger generation
282 # of recurrent tasks.
284 # Only necessary for TW up to 2.4.1, fixed in 2.4.2.
285 if self.version < self.VERSION_2_4_2:
286 self.execute_command(['next'], allow_failure=False)
288 def merge_with(self, path, push=False):
289 path = path.rstrip('/') + '/'
290 self.execute_command(['merge', path], config_override={
291 'merge.autopush': 'yes' if push else 'no',
295 self.execute_command(['undo'])
297 # Backend interface implementation
299 def filter_tasks(self, filter_obj):
300 self.enforce_recurrence()
301 args = ['export'] + filter_obj.get_filter_params()
303 for line in self.execute_command(args):
305 data = line.strip(',')
307 filtered_task = Task(self)
308 filtered_task._load_data(json.loads(data))
309 tasks.append(filtered_task)
311 raise TaskWarriorException('Invalid JSON: %s' % data)
314 def save_task(self, task):
315 """Save a task into TaskWarrior database using add/modify call"""
317 args = [task['uuid'], 'modify'] if task.saved else ['add']
318 args.extend(self._get_modified_task_fields_as_args(task))
319 output = self.execute_command(args)
321 # Parse out the new ID, if the task is being added for the first time
323 id_lines = [l for l in output if l.startswith('Created task ')]
325 # Complain loudly if it seems that more tasks were created
327 if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
328 raise TaskWarriorException("Unexpected output when creating "
329 "task: %s" % '\n'.join(id_lines))
331 # Circumvent the ID storage, since ID is considered read-only
332 identifier = id_lines[0].split(' ')[2].rstrip('.')
334 # Identifier can be either ID or UUID for completed tasks
336 task._data['id'] = int(identifier)
338 task._data['uuid'] = identifier
340 # Refreshing is very important here, as not only modification time
341 # is updated, but arbitrary attribute may have changed due hooks
342 # altering the data before saving
343 task.refresh(after_save=True)
345 def delete_task(self, task):
346 self.execute_command([task['uuid'], 'delete'])
348 def start_task(self, task):
349 self.execute_command([task['uuid'], 'start'])
351 def stop_task(self, task):
352 self.execute_command([task['uuid'], 'stop'])
354 def complete_task(self, task):
355 # Older versions of TW do not stop active task at completion
356 if self.version < self.VERSION_2_4_0 and task.active:
359 self.execute_command([task['uuid'], 'done'])
361 def annotate_task(self, task, annotation):
362 args = [task['uuid'], 'annotate', annotation]
363 self.execute_command(args)
365 def denotate_task(self, task, annotation):
366 args = [task['uuid'], 'denotate', annotation]
367 self.execute_command(args)
369 def refresh_task(self, task, after_save=False):
370 # We need to use ID as backup for uuid here for the refreshes
371 # of newly saved tasks. Any other place in the code is fine
372 # with using UUID only.
373 args = [task['uuid'] or task['id'], 'export']
374 output = self.execute_command(args)
377 return len(output) == 1 and output[0].startswith('{')
379 # For older TW versions attempt to uniquely locate the task
380 # using the data we have if it has been just saved.
381 # This can happen when adding a completed task on older TW versions.
382 if (not valid(output) and self.version < self.VERSION_2_4_5
385 # Make a copy, removing ID and UUID. It's most likely invalid
386 # (ID 0) if it failed to match a unique task.
387 data = copy.deepcopy(task._data)
389 data.pop('uuid', None)
391 taskfilter = self.filter_class(self)
392 for key, value in data.items():
393 taskfilter.add_filter_param(key, value)
395 output = self.execute_command(['export'] +
396 taskfilter.get_filter_params())
398 # If more than 1 task has been matched still, raise an exception
399 if not valid(output):
400 raise TaskWarriorException(
401 "Unique identifiers {0} with description: {1} matches "
402 "multiple tasks: {2}".format(
403 task['uuid'] or task['id'], task['description'], output)
406 return json.loads(output[0])
409 self.execute_command(['sync'])