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.
   1 from __future__ import print_function
 
  13 DATE_FORMAT = '%Y%m%dT%H%M%SZ'
 
  16 COMPLETED = 'completed'
 
  18 VERSION_2_1_0 = six.u('2.1.0')
 
  19 VERSION_2_2_0 = six.u('2.2.0')
 
  20 VERSION_2_3_0 = six.u('2.3.0')
 
  21 VERSION_2_4_0 = six.u('2.4.0')
 
  23 logger = logging.getLogger(__name__)
 
  24 local_zone = tzlocal.get_localzone()
 
  27 class TaskWarriorException(Exception):
 
  31 class ReadOnlyDictView(object):
 
  33     Provides simplified read-only view upon dict object.
 
  36     def __init__(self, viewed_dict):
 
  37         self.viewed_dict = viewed_dict
 
  39     def __getitem__(self, key):
 
  40         return copy.deepcopy(self.viewed_dict.__getitem__(key))
 
  42     def __contains__(self, k):
 
  43         return self.viewed_dict.__contains__(k)
 
  46         for value in self.viewed_dict:
 
  47             yield copy.deepcopy(value)
 
  50         return len(self.viewed_dict)
 
  52     def get(self, key, default=None):
 
  53         return copy.deepcopy(self.viewed_dict.get(key, default))
 
  56         return [copy.deepcopy(v) for v in self.viewed_dict.items()]
 
  59         return [copy.deepcopy(v) for v in self.viewed_dict.values()]
 
  62 class SerializingObject(object):
 
  64     Common ancestor for TaskResource & TaskFilter, since they both
 
  65     need to serialize arguments.
 
  67     Serializing method should hold the following contract:
 
  68       - any empty value (meaning removal of the attribute)
 
  69         is deserialized into a empty string
 
  70       - None denotes a empty value for any attribute
 
  72     Deserializing method should hold the following contract:
 
  73       - None denotes a empty value for any attribute (however,
 
  74         this is here as a safeguard, TaskWarrior currently does
 
  75         not export empty-valued attributes) if the attribute
 
  76         is not iterable (e.g. list or set), in which case
 
  77         a empty iterable should be used.
 
  79     Normalizing methods should hold the following contract:
 
  80       - They are used to validate and normalize the user input.
 
  81         Any attribute value that comes from the user (during Task
 
  82         initialization, assignign values to Task attributes, or
 
  83         filtering by user-provided values of attributes) is first
 
  84         validated and normalized using the normalize_{key} method.
 
  85       - If validation or normalization fails, normalizer is expected
 
  89     def _deserialize(self, key, value):
 
  90         hydrate_func = getattr(self, 'deserialize_{0}'.format(key),
 
  91                                lambda x: x if x != '' else None)
 
  92         return hydrate_func(value)
 
  94     def _serialize(self, key, value):
 
  95         dehydrate_func = getattr(self, 'serialize_{0}'.format(key),
 
  96                                  lambda x: x if x is not None else '')
 
  97         return dehydrate_func(value)
 
  99     def _normalize(self, key, value):
 
 101         Use normalize_<key> methods to normalize user input. Any user
 
 102         input will be normalized at the moment it is used as filter,
 
 103         or entered as a value of Task attribute.
 
 106         # None value should not be converted by normalizer
 
 110         normalize_func = getattr(self, 'normalize_{0}'.format(key),
 
 113         return normalize_func(value)
 
 115     def timestamp_serializer(self, date):
 
 119         # Any serialized timestamp should be localized, we need to
 
 120         # convert to UTC before converting to string (DATE_FORMAT uses UTC)
 
 121         date = date.astimezone(pytz.utc)
 
 123         return date.strftime(DATE_FORMAT)
 
 125     def timestamp_deserializer(self, date_str):
 
 129         # Return timestamp localized in the local zone
 
 130         naive_timestamp = datetime.datetime.strptime(date_str, DATE_FORMAT)
 
 131         localized_timestamp = pytz.utc.localize(naive_timestamp)
 
 132         return localized_timestamp.astimezone(local_zone)
 
 134     def serialize_entry(self, value):
 
 135         return self.timestamp_serializer(value)
 
 137     def deserialize_entry(self, value):
 
 138         return self.timestamp_deserializer(value)
 
 140     def normalize_entry(self, value):
 
 141         return self.datetime_normalizer(value)
 
 143     def serialize_modified(self, value):
 
 144         return self.timestamp_serializer(value)
 
 146     def deserialize_modified(self, value):
 
 147         return self.timestamp_deserializer(value)
 
 149     def normalize_modified(self, value):
 
 150         return self.datetime_normalizer(value)
 
 152     def serialize_due(self, value):
 
 153         return self.timestamp_serializer(value)
 
 155     def deserialize_due(self, value):
 
 156         return self.timestamp_deserializer(value)
 
 158     def normalize_due(self, value):
 
 159         return self.datetime_normalizer(value)
 
 161     def serialize_scheduled(self, value):
 
 162         return self.timestamp_serializer(value)
 
 164     def deserialize_scheduled(self, value):
 
 165         return self.timestamp_deserializer(value)
 
 167     def normalize_scheduled(self, value):
 
 168         return self.datetime_normalizer(value)
 
 170     def serialize_until(self, value):
 
 171         return self.timestamp_serializer(value)
 
 173     def deserialize_until(self, value):
 
 174         return self.timestamp_deserializer(value)
 
 176     def normalize_until(self, value):
 
 177         return self.datetime_normalizer(value)
 
 179     def serialize_wait(self, value):
 
 180         return self.timestamp_serializer(value)
 
 182     def deserialize_wait(self, value):
 
 183         return self.timestamp_deserializer(value)
 
 185     def normalize_wait(self, value):
 
 186         return self.datetime_normalizer(value)
 
 188     def serialize_annotations(self, value):
 
 189         value = value if value is not None else []
 
 191         # This may seem weird, but it's correct, we want to export
 
 192         # a list of dicts as serialized value
 
 193         serialized_annotations = [json.loads(annotation.export_data())
 
 194                                   for annotation in value]
 
 195         return serialized_annotations if serialized_annotations else ''
 
 197     def deserialize_annotations(self, data):
 
 198         return [TaskAnnotation(self, d) for d in data] if data else []
 
 200     def serialize_tags(self, tags):
 
 201         return ','.join(tags) if tags else ''
 
 203     def deserialize_tags(self, tags):
 
 204         if isinstance(tags, six.string_types):
 
 205             return tags.split(',') if tags else []
 
 208     def serialize_depends(self, value):
 
 209         # Return the list of uuids
 
 210         value = value if value is not None else set()
 
 211         return ','.join(task['uuid'] for task in value)
 
 213     def deserialize_depends(self, raw_uuids):
 
 214         raw_uuids = raw_uuids or ''  # Convert None to empty string
 
 215         uuids = raw_uuids.split(',')
 
 216         return set(self.warrior.tasks.get(uuid=uuid) for uuid in uuids if uuid)
 
 218     def datetime_normalizer(self, value):
 
 220         Normalizes date/datetime value (considered to come from user input)
 
 221         to localized datetime value. Following conversions happen:
 
 223         naive date -> localized datetime with the same date, and time=midnight
 
 224         naive datetime -> localized datetime with the same value
 
 225         localized datetime -> localized datetime (no conversion)
 
 228         if (isinstance(value, datetime.date)
 
 229             and not isinstance(value, datetime.datetime)):
 
 230             # Convert to local midnight
 
 231             value_full = datetime.datetime.combine(value, datetime.time.min)
 
 232             localized = local_zone.localize(value_full)
 
 233         elif isinstance(value, datetime.datetime) and value.tzinfo is None:
 
 234             # Convert to localized datetime object
 
 235             localized = local_zone.localize(value)
 
 237             # If the value is already localized, there is no need to change
 
 238             # time zone at this point. Also None is a valid value too.
 
 243     def normalize_uuid(self, value):
 
 245         if not isinstance(value, six.string_types) or value == '':
 
 246             raise ValueError("UUID must be a valid non-empty string, "
 
 247                              "not: {}".format(value))
 
 252 class TaskResource(SerializingObject):
 
 253     read_only_fields = []
 
 255     def _load_data(self, data):
 
 256         self._data = dict((key, self._deserialize(key, value))
 
 257                           for key, value in data.items())
 
 258         # We need to use a copy for original data, so that changes
 
 259         # are not propagated.
 
 260         self._original_data = copy.deepcopy(self._data)
 
 262     def _update_data(self, data, update_original=False):
 
 264         Low level update of the internal _data dict. Data which are coming as
 
 265         updates should already be serialized. If update_original is True, the
 
 266         original_data dict is updated as well.
 
 268         self._data.update(dict((key, self._deserialize(key, value))
 
 269                                for key, value in data.items()))
 
 272             self._original_data = copy.deepcopy(self._data)
 
 275     def __getitem__(self, key):
 
 276         # This is a workaround to make TaskResource non-iterable
 
 277         # over simple index-based iteration
 
 284         if key not in self._data:
 
 285             self._data[key] = self._deserialize(key, None)
 
 287         return self._data.get(key)
 
 289     def __setitem__(self, key, value):
 
 290         if key in self.read_only_fields:
 
 291             raise RuntimeError('Field \'%s\' is read-only' % key)
 
 293         # Normalize the user input before saving it
 
 294         value = self._normalize(key, value)
 
 295         self._data[key] = value
 
 298         s = six.text_type(self.__unicode__())
 
 300             s = s.encode('utf-8')
 
 306     def export_data(self):
 
 308         Exports current data contained in the Task as JSON
 
 311         # We need to remove spaces for TW-1504, use custom separators
 
 312         data_tuples = ((key, self._serialize(key, value))
 
 313                        for key, value in six.iteritems(self._data))
 
 315         # Empty string denotes empty serialized value, we do not want
 
 316         # to pass that to TaskWarrior.
 
 317         data_tuples = filter(lambda t: t[1] is not '', data_tuples)
 
 318         data = dict(data_tuples)
 
 319         return json.dumps(data, separators=(',',':'))
 
 322     def _modified_fields(self):
 
 323         writable_fields = set(self._data.keys()) - set(self.read_only_fields)
 
 324         for key in writable_fields:
 
 325             new_value = self._data.get(key)
 
 326             old_value = self._original_data.get(key)
 
 328             # Make sure not to mark data removal as modified field if the
 
 329             # field originally had some empty value
 
 330             if key in self._data and not new_value and not old_value:
 
 333             if new_value != old_value:
 
 338         return bool(list(self._modified_fields))
 
 341 class TaskAnnotation(TaskResource):
 
 342     read_only_fields = ['entry', 'description']
 
 344     def __init__(self, task, data={}):
 
 346         self._load_data(data)
 
 349         self.task.remove_annotation(self)
 
 351     def __unicode__(self):
 
 352         return self['description']
 
 354     def __eq__(self, other):
 
 355         # consider 2 annotations equal if they belong to the same task, and
 
 356         # their data dics are the same
 
 357         return self.task == other.task and self._data == other._data
 
 359     __repr__ = __unicode__
 
 362 class Task(TaskResource):
 
 363     read_only_fields = ['id', 'entry', 'urgency', 'uuid', 'modified']
 
 365     class DoesNotExist(Exception):
 
 368     class CompletedTask(Exception):
 
 370         Raised when the operation cannot be performed on the completed task.
 
 374     class DeletedTask(Exception):
 
 376         Raised when the operation cannot be performed on the deleted task.
 
 380     class NotSaved(Exception):
 
 382         Raised when the operation cannot be performed on the task, because
 
 383         it has not been saved to TaskWarrior yet.
 
 388     def from_input(cls, input_file=sys.stdin, modify=None, warrior=None):
 
 390         Creates a Task object, directly from the stdin, by reading one line.
 
 391         If modify=True, two lines are used, first line interpreted as the
 
 392         original state of the Task object, and second line as its new,
 
 393         modified value. This is consistent with the TaskWarrior's hook
 
 396         Object created by this method should not be saved, deleted
 
 397         or refreshed, as t could create a infinite loop. For this
 
 398         reason, TaskWarrior instance is set to None.
 
 400         Input_file argument can be used to specify the input file,
 
 401         but defaults to sys.stdin.
 
 404         # Detect the hook type if not given directly
 
 405         name = os.path.basename(sys.argv[0])
 
 406         modify = name.startswith('on-modify') if modify is None else modify
 
 408         # Create the TaskWarrior instance if none passed
 
 410             hook_parent_dir = os.path.dirname(os.path.dirname(sys.argv[0]))
 
 411             warrior = TaskWarrior(data_location=hook_parent_dir)
 
 413         # TaskWarrior instance is set to None
 
 416         # Load the data from the input
 
 417         task._load_data(json.loads(input_file.readline().strip()))
 
 419         # If this is a on-modify event, we are provided with additional
 
 420         # line of input, which provides updated data
 
 422             task._update_data(json.loads(input_file.readline().strip()))
 
 426     def __init__(self, warrior, **kwargs):
 
 427         self.warrior = warrior
 
 429         # Check that user is not able to set read-only value in __init__
 
 430         for key in kwargs.keys():
 
 431             if key in self.read_only_fields:
 
 432                 raise RuntimeError('Field \'%s\' is read-only' % key)
 
 434         # We serialize the data in kwargs so that users of the library
 
 435         # do not have to pass different data formats via __setitem__ and
 
 436         # __init__ methods, that would be confusing
 
 438         # Rather unfortunate syntax due to python2.6 comaptiblity
 
 439         self._data = dict((key, self._normalize(key, value))
 
 440                           for (key, value) in six.iteritems(kwargs))
 
 441         self._original_data = copy.deepcopy(self._data)
 
 443         # Provide read only access to the original data
 
 444         self.original = ReadOnlyDictView(self._original_data)
 
 446     def __unicode__(self):
 
 447         return self['description']
 
 449     def __eq__(self, other):
 
 450         if self['uuid'] and other['uuid']:
 
 451             # For saved Tasks, just define equality by equality of uuids
 
 452             return self['uuid'] == other['uuid']
 
 454             # If the tasks are not saved, compare the actual instances
 
 455             return id(self) == id(other)
 
 460             # For saved Tasks, just define equality by equality of uuids
 
 461             return self['uuid'].__hash__()
 
 463             # If the tasks are not saved, return hash of instance id
 
 464             return id(self).__hash__()
 
 468         return self['status'] == six.text_type('completed')
 
 472         return self['status'] == six.text_type('deleted')
 
 476         return self['status'] == six.text_type('waiting')
 
 480         return self['status'] == six.text_type('pending')
 
 484         return self['uuid'] is not None or self['id'] is not None
 
 486     def serialize_depends(self, cur_dependencies):
 
 487         # Check that all the tasks are saved
 
 488         for task in (cur_dependencies or set()):
 
 490                 raise Task.NotSaved('Task \'%s\' needs to be saved before '
 
 491                                     'it can be set as dependency.' % task)
 
 493         return super(Task, self).serialize_depends(cur_dependencies)
 
 495     def format_depends(self):
 
 496         # We need to generate added and removed dependencies list,
 
 497         # since Taskwarrior does not accept redefining dependencies.
 
 499         # This cannot be part of serialize_depends, since we need
 
 500         # to keep a list of all depedencies in the _data dictionary,
 
 501         # not just currently added/removed ones
 
 503         old_dependencies = self._original_data.get('depends', set())
 
 505         added = self['depends'] - old_dependencies
 
 506         removed = old_dependencies - self['depends']
 
 508         # Removed dependencies need to be prefixed with '-'
 
 509         return 'depends:' + ','.join(
 
 510                 [t['uuid'] for t in added] +
 
 511                 ['-' + t['uuid'] for t in removed]
 
 514     def format_description(self):
 
 515         # Task version older than 2.4.0 ignores first word of the
 
 516         # task description if description: prefix is used
 
 517         if self.warrior.version < VERSION_2_4_0:
 
 518             return self._data['description']
 
 520             return "description:'{0}'".format(self._data['description'] or '')
 
 524             raise Task.NotSaved("Task needs to be saved before it can be deleted")
 
 526         # Refresh the status, and raise exception if the task is deleted
 
 527         self.refresh(only_fields=['status'])
 
 530             raise Task.DeletedTask("Task was already deleted")
 
 532         self.warrior.execute_command([self['uuid'], 'delete'])
 
 534         # Refresh the status again, so that we have updated info stored
 
 535         self.refresh(only_fields=['status'])
 
 539             raise Task.NotSaved("Task needs to be saved before it can be started")
 
 541         # Refresh, and raise exception if task is already completed/deleted
 
 542         self.refresh(only_fields=['status'])
 
 545             raise Task.CompletedTask("Cannot start a completed task")
 
 547             raise Task.DeletedTask("Deleted task cannot be started")
 
 549         self.warrior.execute_command([self['uuid'], 'start'])
 
 551         # Refresh the status again, so that we have updated info stored
 
 552         self.refresh(only_fields=['status'])
 
 556             raise Task.NotSaved("Task needs to be saved before it can be completed")
 
 558         # Refresh, and raise exception if task is already completed/deleted
 
 559         self.refresh(only_fields=['status'])
 
 562             raise Task.CompletedTask("Cannot complete a completed task")
 
 564             raise Task.DeletedTask("Deleted task cannot be completed")
 
 566         self.warrior.execute_command([self['uuid'], 'done'])
 
 568         # Refresh the status again, so that we have updated info stored
 
 569         self.refresh(only_fields=['status'])
 
 572         if self.saved and not self.modified:
 
 575         args = [self['uuid'], 'modify'] if self.saved else ['add']
 
 576         args.extend(self._get_modified_fields_as_args())
 
 577         output = self.warrior.execute_command(args)
 
 579         # Parse out the new ID, if the task is being added for the first time
 
 581             id_lines = [l for l in output if l.startswith('Created task ')]
 
 583             # Complain loudly if it seems that more tasks were created
 
 585             if len(id_lines) != 1 or len(id_lines[0].split(' ')) != 3:
 
 586                 raise TaskWarriorException("Unexpected output when creating "
 
 587                                            "task: %s" % '\n'.join(id_lines))
 
 589             # Circumvent the ID storage, since ID is considered read-only
 
 590             self._data['id'] = int(id_lines[0].split(' ')[2].rstrip('.'))
 
 592         # Refreshing is very important here, as not only modification time
 
 593         # is updated, but arbitrary attribute may have changed due hooks
 
 594         # altering the data before saving
 
 597     def add_annotation(self, annotation):
 
 599             raise Task.NotSaved("Task needs to be saved to add annotation")
 
 601         args = [self['uuid'], 'annotate', annotation]
 
 602         self.warrior.execute_command(args)
 
 603         self.refresh(only_fields=['annotations'])
 
 605     def remove_annotation(self, annotation):
 
 607             raise Task.NotSaved("Task needs to be saved to remove annotation")
 
 609         if isinstance(annotation, TaskAnnotation):
 
 610             annotation = annotation['description']
 
 611         args = [self['uuid'], 'denotate', annotation]
 
 612         self.warrior.execute_command(args)
 
 613         self.refresh(only_fields=['annotations'])
 
 615     def _get_modified_fields_as_args(self):
 
 618         def add_field(field):
 
 619             # Add the output of format_field method to args list (defaults to
 
 621             serialized_value = self._serialize(field, self._data[field])
 
 623             # Empty values should not be enclosed in quotation marks, see
 
 625             if serialized_value is '':
 
 626                 escaped_serialized_value = ''
 
 628                 escaped_serialized_value = "'{0}'".format(serialized_value)
 
 630             format_default = lambda: "{0}:{1}".format(field,
 
 631                                                       escaped_serialized_value)
 
 633             format_func = getattr(self, 'format_{0}'.format(field),
 
 636             args.append(format_func())
 
 638         # If we're modifying saved task, simply pass on all modified fields
 
 640             for field in self._modified_fields:
 
 642         # For new tasks, pass all fields that make sense
 
 644             for field in self._data.keys():
 
 645                 if field in self.read_only_fields:
 
 651     def refresh(self, only_fields=[]):
 
 652         # Raise error when trying to refresh a task that has not been saved
 
 654             raise Task.NotSaved("Task needs to be saved to be refreshed")
 
 656         # We need to use ID as backup for uuid here for the refreshes
 
 657         # of newly saved tasks. Any other place in the code is fine
 
 658         # with using UUID only.
 
 659         args = [self['uuid'] or self['id'], 'export']
 
 660         new_data = json.loads(self.warrior.execute_command(args)[0])
 
 663                 [(k, new_data.get(k)) for k in only_fields])
 
 664             self._update_data(to_update, update_original=True)
 
 666             self._load_data(new_data)
 
 668 class TaskFilter(SerializingObject):
 
 670     A set of parameters to filter the task list with.
 
 673     def __init__(self, filter_params=[]):
 
 674         self.filter_params = filter_params
 
 676     def add_filter(self, filter_str):
 
 677         self.filter_params.append(filter_str)
 
 679     def add_filter_param(self, key, value):
 
 680         key = key.replace('__', '.')
 
 682         # Replace the value with empty string, since that is the
 
 683         # convention in TW for empty values
 
 684         attribute_key = key.split('.')[0]
 
 686         # Since this is user input, we need to normalize before we serialize
 
 687         value = self._normalize(key, value)
 
 688         value = self._serialize(attribute_key, value)
 
 690         # If we are filtering by uuid:, do not use uuid keyword
 
 693             self.filter_params.insert(0, value)
 
 695             # Surround value with aphostrophes unless it's a empty string
 
 696             value = "'%s'" % value if value else ''
 
 698             # We enforce equality match by using 'is' (or 'none') modifier
 
 699             # Without using this syntax, filter fails due to TW-1479
 
 700             modifier = '.is' if value else '.none'
 
 701             key = key + modifier if '.' not in key else key
 
 703             self.filter_params.append("{0}:{1}".format(key, value))
 
 705     def get_filter_params(self):
 
 706         return [f for f in self.filter_params if f]
 
 710         c.filter_params = list(self.filter_params)
 
 714 class TaskQuerySet(object):
 
 716     Represents a lazy lookup for a task objects.
 
 719     def __init__(self, warrior=None, filter_obj=None):
 
 720         self.warrior = warrior
 
 721         self._result_cache = None
 
 722         self.filter_obj = filter_obj or TaskFilter()
 
 724     def __deepcopy__(self, memo):
 
 726         Deep copy of a QuerySet doesn't populate the cache
 
 728         obj = self.__class__()
 
 729         for k, v in self.__dict__.items():
 
 730             if k in ('_iter', '_result_cache'):
 
 731                 obj.__dict__[k] = None
 
 733                 obj.__dict__[k] = copy.deepcopy(v, memo)
 
 737         data = list(self[:REPR_OUTPUT_SIZE + 1])
 
 738         if len(data) > REPR_OUTPUT_SIZE:
 
 739             data[-1] = "...(remaining elements truncated)..."
 
 743         if self._result_cache is None:
 
 744             self._result_cache = list(self)
 
 745         return len(self._result_cache)
 
 748         if self._result_cache is None:
 
 749             self._result_cache = self._execute()
 
 750         return iter(self._result_cache)
 
 752     def __getitem__(self, k):
 
 753         if self._result_cache is None:
 
 754             self._result_cache = list(self)
 
 755         return self._result_cache.__getitem__(k)
 
 758         if self._result_cache is not None:
 
 759             return bool(self._result_cache)
 
 762         except StopIteration:
 
 766     def __nonzero__(self):
 
 767         return type(self).__bool__(self)
 
 769     def _clone(self, klass=None, **kwargs):
 
 771             klass = self.__class__
 
 772         filter_obj = self.filter_obj.clone()
 
 773         c = klass(warrior=self.warrior, filter_obj=filter_obj)
 
 774         c.__dict__.update(kwargs)
 
 779         Fetch the tasks which match the current filters.
 
 781         return self.warrior.filter_tasks(self.filter_obj)
 
 785         Returns a new TaskQuerySet that is a copy of the current one.
 
 790         return self.filter(status=PENDING)
 
 793         return self.filter(status=COMPLETED)
 
 795     def filter(self, *args, **kwargs):
 
 797         Returns a new TaskQuerySet with the given filters added.
 
 799         clone = self._clone()
 
 801             clone.filter_obj.add_filter(f)
 
 802         for key, value in kwargs.items():
 
 803             clone.filter_obj.add_filter_param(key, value)
 
 806     def get(self, **kwargs):
 
 808         Performs the query and returns a single object matching the given
 
 811         clone = self.filter(**kwargs)
 
 814             return clone._result_cache[0]
 
 816             raise Task.DoesNotExist(
 
 817                 'Task matching query does not exist. '
 
 818                 'Lookup parameters were {0}'.format(kwargs))
 
 820             'get() returned more than one Task -- it returned {0}! '
 
 821             'Lookup parameters were {1}'.format(num, kwargs))
 
 824 class TaskWarrior(object):
 
 825     def __init__(self, data_location='~/.task', create=True):
 
 826         data_location = os.path.expanduser(data_location)
 
 827         if create and not os.path.exists(data_location):
 
 828             os.makedirs(data_location)
 
 830             'data.location': os.path.expanduser(data_location),
 
 831             'confirmation': 'no',
 
 832             'dependency.confirmation': 'no',  # See TW-1483 or taskrc man page
 
 833             'recurrence.confirmation': 'no',  # Necessary for modifying R tasks
 
 835         self.tasks = TaskQuerySet(self)
 
 836         self.version = self._get_version()
 
 838     def _get_command_args(self, args, config_override={}):
 
 839         command_args = ['task', 'rc:/']
 
 840         config = self.config.copy()
 
 841         config.update(config_override)
 
 842         for item in config.items():
 
 843             command_args.append('rc.{0}={1}'.format(*item))
 
 844         command_args.extend(map(str, args))
 
 847     def _get_version(self):
 
 848         p = subprocess.Popen(
 
 849                 ['task', '--version'],
 
 850                 stdout=subprocess.PIPE,
 
 851                 stderr=subprocess.PIPE)
 
 852         stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
 
 853         return stdout.strip('\n')
 
 855     def execute_command(self, args, config_override={}, allow_failure=True):
 
 856         command_args = self._get_command_args(
 
 857             args, config_override=config_override)
 
 858         logger.debug(' '.join(command_args))
 
 859         p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
 
 860                              stderr=subprocess.PIPE)
 
 861         stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
 
 862         if p.returncode and allow_failure:
 
 864                 error_msg = stderr.strip()
 
 866                 error_msg = stdout.strip()
 
 867             raise TaskWarriorException(error_msg)
 
 868         return stdout.strip().split('\n')
 
 870     def enforce_recurrence(self):
 
 871         # Run arbitrary report command which will trigger generation
 
 872         # of recurrent tasks.
 
 873         # TODO: Make a version dependant enforcement once
 
 875         self.execute_command(['next'], allow_failure=False)
 
 877     def filter_tasks(self, filter_obj):
 
 878         self.enforce_recurrence()
 
 879         args = ['export', '--'] + filter_obj.get_filter_params()
 
 881         for line in self.execute_command(args):
 
 883                 data = line.strip(',')
 
 885                     filtered_task = Task(self)
 
 886                     filtered_task._load_data(json.loads(data))
 
 887                     tasks.append(filtered_task)
 
 889                     raise TaskWarriorException('Invalid JSON: %s' % data)
 
 892     def merge_with(self, path, push=False):
 
 893         path = path.rstrip('/') + '/'
 
 894         self.execute_command(['merge', path], config_override={
 
 895             'merge.autopush': 'yes' if push else 'no',
 
 899         self.execute_command(['undo'])