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.
7 from tasklib.filters import TaskWarriorFilter
8 from tasklib.serializing import local_zone
10 DATE_FORMAT_CALC = '%Y-%m-%dT%H:%M:%S'
12 VERSION_2_1_0 = six.u('2.1.0')
13 VERSION_2_2_0 = six.u('2.2.0')
14 VERSION_2_3_0 = six.u('2.3.0')
15 VERSION_2_4_0 = six.u('2.4.0')
16 VERSION_2_4_1 = six.u('2.4.1')
17 VERSION_2_4_2 = six.u('2.4.2')
18 VERSION_2_4_3 = six.u('2.4.3')
19 VERSION_2_4_4 = six.u('2.4.4')
20 VERSION_2_4_5 = six.u('2.4.5')
23 class Backend(object):
26 def filter_class(self):
27 """Returns the TaskFilter class used by this backend"""
31 def filter_tasks(self, filter_obj):
32 """Returns a list of Task objects matching the given filter"""
36 def save_task(self, task):
40 def delete_task(self, task):
44 def start_task(self, task):
48 def stop_task(self, task):
52 def complete_task(self, task):
56 def refresh_task(self, task, after_save=False):
58 Refreshes the given task. Returns new data dict with serialized
64 def annotate_task(self, task, annotation):
68 def denotate_task(self, task, annotation):
73 """Syncs the backend database with the taskd server"""
76 def convert_datetime_string(self, value):
78 Converts TW syntax datetime string to a localized datetime
79 object. This method is not mandatory.
84 class TaskWarriorException(Exception):
88 class TaskWarrior(object):
89 def __init__(self, data_location=None, create=True, taskrc_location='~/.taskrc'):
90 self.taskrc_location = os.path.expanduser(taskrc_location)
92 # If taskrc does not exist, pass / to use defaults and avoid creating
93 # dummy .taskrc file by TaskWarrior
94 if not os.path.exists(self.taskrc_location):
95 self.taskrc_location = '/'
97 self.version = self._get_version()
100 'dependency.confirmation': 'no', # See TW-1483 or taskrc man page
101 'recurrence.confirmation': 'no', # Necessary for modifying R tasks
103 # Defaults to on since 2.4.5, we expect off during parsing
106 # 2.4.3 onwards supports 0 as infite bulk, otherwise set just
107 # arbitrary big number which is likely to be large enough
108 'bulk': 0 if self.version >= VERSION_2_4_3 else 100000,
111 # Set data.location override if passed via kwarg
112 if data_location is not None:
113 data_location = os.path.expanduser(data_location)
114 if create and not os.path.exists(data_location):
115 os.makedirs(data_location)
116 self.config['data.location'] = data_location
118 self.tasks = TaskQuerySet(self)
120 def _get_command_args(self, args, config_override=None):
121 command_args = ['task', 'rc:{0}'.format(self.taskrc_location)]
122 config = self.config.copy()
123 config.update(config_override or dict())
124 for item in config.items():
125 command_args.append('rc.{0}={1}'.format(*item))
126 command_args.extend(map(six.text_type, args))
129 def _get_version(self):
130 p = subprocess.Popen(
131 ['task', '--version'],
132 stdout=subprocess.PIPE,
133 stderr=subprocess.PIPE)
134 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
135 return stdout.strip('\n')
137 def _get_modified_task_fields_as_args(self, task):
140 def add_field(field):
141 # Add the output of format_field method to args list (defaults to
143 serialized_value = task._serialize(field, task._data[field])
145 # Empty values should not be enclosed in quotation marks, see
147 if serialized_value is '':
148 escaped_serialized_value = ''
150 escaped_serialized_value = six.u("'{0}'").format(serialized_value)
152 format_default = lambda: six.u("{0}:{1}").format(field,
153 escaped_serialized_value)
155 format_func = getattr(self, 'format_{0}'.format(field),
158 args.append(format_func(task))
160 # If we're modifying saved task, simply pass on all modified fields
162 for field in task._modified_fields:
164 # For new tasks, pass all fields that make sense
166 for field in task._data.keys():
167 if field in task.read_only_fields:
173 def format_depends(self, task):
174 # We need to generate added and removed dependencies list,
175 # since Taskwarrior does not accept redefining dependencies.
177 # This cannot be part of serialize_depends, since we need
178 # to keep a list of all depedencies in the _data dictionary,
179 # not just currently added/removed ones
181 old_dependencies = task._original_data.get('depends', set())
183 added = self['depends'] - old_dependencies
184 removed = old_dependencies - self['depends']
186 # Removed dependencies need to be prefixed with '-'
187 return 'depends:' + ','.join(
188 [t['uuid'] for t in added] +
189 ['-' + t['uuid'] for t in removed]
192 def format_description(self, task):
193 # Task version older than 2.4.0 ignores first word of the
194 # task description if description: prefix is used
195 if self.version < VERSION_2_4_0:
196 return task._data['description']
198 return six.u("description:'{0}'").format(task._data['description'] or '')
200 def convert_datetime_string(self, value):
202 if self.version >= VERSION_2_4_0:
203 # For strings, use 'task calc' to evaluate the string to datetime
204 # available since TW 2.4.0
206 result = self.execute_command(['calc'] + args)
207 naive = datetime.datetime.strptime(result[0], DATE_FORMAT_CALC)
208 localized = local_zone.localize(naive)
210 raise ValueError("Provided value could not be converted to "
211 "datetime, its type is not supported: {}"
212 .format(type(value)))
215 def filter_class(self):
216 return TaskWarriorFilter
220 def get_config(self):
221 raw_output = self.execute_command(
223 config_override={'verbose': 'nothing'}
227 config_regex = re.compile(r'^(?P<key>[^\s]+)\s+(?P<value>[^\s].+$)')
229 for line in raw_output:
230 match = config_regex.match(line)
232 config[match.group('key')] = match.group('value').strip()
236 def execute_command(self, args, config_override=None, allow_failure=True,
238 command_args = self._get_command_args(
239 args, config_override=config_override)
240 logger.debug(' '.join(command_args))
241 p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
242 stderr=subprocess.PIPE)
243 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
244 if p.returncode and allow_failure:
246 error_msg = stderr.strip()
248 error_msg = stdout.strip()
249 raise TaskWarriorException(error_msg)
251 # Return all whole triplet only if explicitly asked for
253 return stdout.rstrip().split('\n')
255 return (stdout.rstrip().split('\n'),
256 stderr.rstrip().split('\n'),
259 def enforce_recurrence(self):
260 # Run arbitrary report command which will trigger generation
261 # of recurrent tasks.
263 # Only necessary for TW up to 2.4.1, fixed in 2.4.2.
264 if self.version < VERSION_2_4_2:
265 self.execute_command(['next'], allow_failure=False)
267 def merge_with(self, path, push=False):
268 path = path.rstrip('/') + '/'
269 self.execute_command(['merge', path], config_override={
270 'merge.autopush': 'yes' if push else 'no',
274 self.execute_command(['undo'])
276 # Backend interface implementation
278 def filter_tasks(self, filter_obj):
279 self.enforce_recurrence()
280 args = ['export', '--'] + filter_obj.get_filter_params()
282 for line in self.execute_command(args):
284 data = line.strip(',')
286 filtered_task = Task(self)
287 filtered_task._load_data(json.loads(data))
288 tasks.append(filtered_task)
290 raise TaskWarriorException('Invalid JSON: %s' % data)
293 def save_task(self, task):
294 """Save a task into TaskWarrior database using add/modify call"""
296 args = [task['uuid'], 'modify'] if task.saved else ['add']
297 args.extend(self._get_modified_task_fields_as_args(task))
298 output = self.execute_command(args)
300 # Parse out the new ID, if the task is being added for the first time
302 id_lines = [l for l in output if l.startswith('Created task ')]
304 # Complain loudly if it seems that more tasks were created
306 if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
307 raise TaskWarriorException("Unexpected output when creating "
308 "task: %s" % '\n'.join(id_lines))
310 # Circumvent the ID storage, since ID is considered read-only
311 identifier = id_lines[0].split(' ')[2].rstrip('.')
313 # Identifier can be either ID or UUID for completed tasks
315 task._data['id'] = int(identifier)
317 task._data['uuid'] = identifier
319 # Refreshing is very important here, as not only modification time
320 # is updated, but arbitrary attribute may have changed due hooks
321 # altering the data before saving
322 task.refresh(after_save=True)
324 def delete_task(self, task):
325 self.execute_command([task['uuid'], 'delete'])
327 def start_task(self, task):
328 self.execute_command([task['uuid'], 'start'])
330 def stop_task(self, task):
331 self.execute_command([task['uuid'], 'stop'])
333 def complete_task(self, task):
334 # Older versions of TW do not stop active task at completion
335 if self.version < VERSION_2_4_0 and task.active:
338 self.execute_command([task['uuid'], 'done'])
340 def annotate_task(self, task, annotation):
341 args = [task['uuid'], 'annotate', annotation]
342 self.execute_command(args)
344 def denotate_task(self, task, annotation):
345 args = [task['uuid'], 'denotate', annotation]
346 self.execute_command(args)
348 def refresh_task(self, task, after_save=False):
349 # We need to use ID as backup for uuid here for the refreshes
350 # of newly saved tasks. Any other place in the code is fine
351 # with using UUID only.
352 args = [task['uuid'] or task['id'], 'export']
353 output = self.execute_command(args)
356 return len(output) == 1 and output[0].startswith('{')
358 # For older TW versions attempt to uniquely locate the task
359 # using the data we have if it has been just saved.
360 # This can happen when adding a completed task on older TW versions.
361 if (not valid(output) and self.version < VERSION_2_4_5
364 # Make a copy, removing ID and UUID. It's most likely invalid
365 # (ID 0) if it failed to match a unique task.
366 data = copy.deepcopy(task._data)
368 data.pop('uuid', None)
370 taskfilter = self.filter_class(self)
371 for key, value in data.items():
372 taskfilter.add_filter_param(key, value)
374 output = self.execute_command(['export', '--'] +
375 taskfilter.get_filter_params())
377 # If more than 1 task has been matched still, raise an exception
378 if not valid(output):
379 raise TaskWarriorException(
380 "Unique identifiers {0} with description: {1} matches "
381 "multiple tasks: {2}".format(
382 task['uuid'] or task['id'], task['description'], output)
385 return json.loads(output[0])
388 self.execute_command(['sync'])