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.task import TaskFilter
9 VERSION_2_1_0 = six.u('2.1.0')
10 VERSION_2_2_0 = six.u('2.2.0')
11 VERSION_2_3_0 = six.u('2.3.0')
12 VERSION_2_4_0 = six.u('2.4.0')
13 VERSION_2_4_1 = six.u('2.4.1')
14 VERSION_2_4_2 = six.u('2.4.2')
15 VERSION_2_4_3 = six.u('2.4.3')
16 VERSION_2_4_4 = six.u('2.4.4')
17 VERSION_2_4_5 = six.u('2.4.5')
20 class Backend(object):
22 filter_class = TaskFilter
25 def filter_tasks(self, filter_obj):
26 """Returns a list of Task objects matching the given filter"""
30 def save_task(self, task):
34 def delete_task(self, task):
38 def start_task(self, task):
42 def stop_task(self, task):
46 def complete_task(self, task):
50 def refresh_task(self, task, after_save=False):
52 Refreshes the given task. Returns new data dict with serialized
58 def annotate_task(self, task, annotation):
62 def denotate_task(self, task, annotation):
67 """Syncs the backend database with the taskd server"""
71 class TaskWarriorException(Exception):
75 class TaskWarrior(object):
76 def __init__(self, data_location=None, create=True, taskrc_location='~/.taskrc'):
77 self.taskrc_location = os.path.expanduser(taskrc_location)
79 # If taskrc does not exist, pass / to use defaults and avoid creating
80 # dummy .taskrc file by TaskWarrior
81 if not os.path.exists(self.taskrc_location):
82 self.taskrc_location = '/'
84 self.version = self._get_version()
87 'dependency.confirmation': 'no', # See TW-1483 or taskrc man page
88 'recurrence.confirmation': 'no', # Necessary for modifying R tasks
90 # Defaults to on since 2.4.5, we expect off during parsing
93 # 2.4.3 onwards supports 0 as infite bulk, otherwise set just
94 # arbitrary big number which is likely to be large enough
95 'bulk': 0 if self.version >= VERSION_2_4_3 else 100000,
98 # Set data.location override if passed via kwarg
99 if data_location is not None:
100 data_location = os.path.expanduser(data_location)
101 if create and not os.path.exists(data_location):
102 os.makedirs(data_location)
103 self.config['data.location'] = data_location
105 self.tasks = TaskQuerySet(self)
107 def _get_command_args(self, args, config_override=None):
108 command_args = ['task', 'rc:{0}'.format(self.taskrc_location)]
109 config = self.config.copy()
110 config.update(config_override or dict())
111 for item in config.items():
112 command_args.append('rc.{0}={1}'.format(*item))
113 command_args.extend(map(six.text_type, args))
116 def _get_version(self):
117 p = subprocess.Popen(
118 ['task', '--version'],
119 stdout=subprocess.PIPE,
120 stderr=subprocess.PIPE)
121 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
122 return stdout.strip('\n')
124 def get_config(self):
125 raw_output = self.execute_command(
127 config_override={'verbose': 'nothing'}
131 config_regex = re.compile(r'^(?P<key>[^\s]+)\s+(?P<value>[^\s].+$)')
133 for line in raw_output:
134 match = config_regex.match(line)
136 config[match.group('key')] = match.group('value').strip()
140 def execute_command(self, args, config_override=None, allow_failure=True,
142 command_args = self._get_command_args(
143 args, config_override=config_override)
144 logger.debug(' '.join(command_args))
145 p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
146 stderr=subprocess.PIPE)
147 stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
148 if p.returncode and allow_failure:
150 error_msg = stderr.strip()
152 error_msg = stdout.strip()
153 raise TaskWarriorException(error_msg)
155 # Return all whole triplet only if explicitly asked for
157 return stdout.rstrip().split('\n')
159 return (stdout.rstrip().split('\n'),
160 stderr.rstrip().split('\n'),
163 def enforce_recurrence(self):
164 # Run arbitrary report command which will trigger generation
165 # of recurrent tasks.
167 # Only necessary for TW up to 2.4.1, fixed in 2.4.2.
168 if self.version < VERSION_2_4_2:
169 self.execute_command(['next'], allow_failure=False)
171 def merge_with(self, path, push=False):
172 path = path.rstrip('/') + '/'
173 self.execute_command(['merge', path], config_override={
174 'merge.autopush': 'yes' if push else 'no',
178 self.execute_command(['undo'])
180 # Backend interface implementation
182 def filter_tasks(self, filter_obj):
183 self.enforce_recurrence()
184 args = ['export', '--'] + filter_obj.get_filter_params()
186 for line in self.execute_command(args):
188 data = line.strip(',')
190 filtered_task = Task(self)
191 filtered_task._load_data(json.loads(data))
192 tasks.append(filtered_task)
194 raise TaskWarriorException('Invalid JSON: %s' % data)
197 def save_task(self, task):
198 """Save a task into TaskWarrior database using add/modify call"""
200 args = [task['uuid'], 'modify'] if task.saved else ['add']
201 args.extend(task._get_modified_fields_as_args())
202 output = self.execute_command(args)
204 # Parse out the new ID, if the task is being added for the first time
206 id_lines = [l for l in output if l.startswith('Created task ')]
208 # Complain loudly if it seems that more tasks were created
210 if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
211 raise TaskWarriorException("Unexpected output when creating "
212 "task: %s" % '\n'.join(id_lines))
214 # Circumvent the ID storage, since ID is considered read-only
215 identifier = id_lines[0].split(' ')[2].rstrip('.')
217 # Identifier can be either ID or UUID for completed tasks
219 task._data['id'] = int(identifier)
221 task._data['uuid'] = identifier
223 # Refreshing is very important here, as not only modification time
224 # is updated, but arbitrary attribute may have changed due hooks
225 # altering the data before saving
226 task.refresh(after_save=True)
228 def delete_task(self, task):
229 self.execute_command([task['uuid'], 'delete'])
231 def start_task(self, task):
232 self.execute_command([task['uuid'], 'start'])
234 def stop_task(self, task):
235 self.execute_command([task['uuid'], 'stop'])
237 def complete_task(self, task):
238 # Older versions of TW do not stop active task at completion
239 if self.version < VERSION_2_4_0 and task.active:
242 self.execute_command([task['uuid'], 'done'])
244 def annotate_task(self, task, annotation):
245 args = [task['uuid'], 'annotate', annotation]
246 self.execute_command(args)
248 def denotate_task(self, task, annotation):
249 args = [task['uuid'], 'denotate', annotation]
250 self.execute_command(args)
252 def refresh_task(self, task, after_save=False):
253 # We need to use ID as backup for uuid here for the refreshes
254 # of newly saved tasks. Any other place in the code is fine
255 # with using UUID only.
256 args = [task['uuid'] or task['id'], 'export']
257 output = self.execute_command(args)
260 return len(output) == 1 and output[0].startswith('{')
262 # For older TW versions attempt to uniquely locate the task
263 # using the data we have if it has been just saved.
264 # This can happen when adding a completed task on older TW versions.
265 if (not valid(output) and self.version < VERSION_2_4_5
268 # Make a copy, removing ID and UUID. It's most likely invalid
269 # (ID 0) if it failed to match a unique task.
270 data = copy.deepcopy(task._data)
272 data.pop('uuid', None)
274 taskfilter = self.filter_class(self)
275 for key, value in data.items():
276 taskfilter.add_filter_param(key, value)
278 output = self.execute_command(['export', '--'] +
279 taskfilter.get_filter_params())
281 # If more than 1 task has been matched still, raise an exception
282 if not valid(output):
283 raise TaskWarriorException(
284 "Unique identifiers {0} with description: {1} matches "
285 "multiple tasks: {2}".format(
286 task['uuid'] or task['id'], task['description'], output)
289 return json.loads(output[0])
292 self.execute_command(['sync'])