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.
8 DATE_FORMAT = '%Y%m%dT%H%M%SZ'
12 logger = logging.getLogger(__name__)
15 class TaskWarriorException(Exception):
21 class DoesNotExist(Exception):
24 def __init__(self, warrior, data={}):
25 self.warrior = warrior
28 self._modified_fields = set()
30 def __unicode__(self):
31 return self['description']
33 def __getitem__(self, key):
34 hydrate_func = getattr(self, 'deserialize_{0}'.format(key),
36 return hydrate_func(self._data.get(key))
38 def __setitem__(self, key, value):
39 dehydrate_func = getattr(self, 'serialize_{0}'.format(key),
41 self._data[key] = dehydrate_func(value)
42 self._modified_fields.add(key)
44 def serialize_due(self, date):
45 return date.strftime(DATE_FORMAT)
47 def deserialize_due(self, date_str):
50 return datetime.datetime.strptime(date_str, DATE_FORMAT)
52 def serialize_annotations(self, annotations):
53 ann_list = list(annotations)
55 ann['entry'] = ann['entry'].strftime(DATE_FORMAT)
58 def deserialize_annotations(self, annotations):
59 ann_list = list(annotations)
61 ann['entry'] = datetime.datetime.strptime(
62 ann['entry'], DATE_FORMAT)
65 def deserialize_tags(self, tags):
66 if isinstance(tags, basestring):
67 return tags.split(',') if tags else []
70 def serialize_tags(self, tags):
71 return ','.join(tags) if tags else ''
74 self.warrior.execute_command([self['id'], 'delete'], config_override={
79 self.warrior.execute_comamnd([self['id'], 'done'])
82 args = [self['id'], 'modify'] if self['id'] else ['add']
83 args.extend(self._get_modified_fields_as_args())
84 self.warrior.execute_command(args)
85 self._modified_fields.clear()
87 def _get_modified_fields_as_args(self):
89 for field in self._modified_fields:
90 args.append('{}:{}'.format(field, self._data[field]))
93 __repr__ = __unicode__
96 class TaskFilter(object):
98 A set of parameters to filter the task list with.
101 def __init__(self, filter_params=[]):
102 self.filter_params = filter_params
104 def add_filter(self, filter_str):
105 self.filter_params.append(filter_str)
107 def add_filter_param(self, key, value):
108 key = key.replace('__', '.')
109 self.filter_params.append('{0}:{1}'.format(key, value))
111 def get_filter_params(self):
112 return [f for f in self.filter_params if f]
116 c.filter_params = list(self.filter_params)
120 class TaskQuerySet(object):
122 Represents a lazy lookup for a task objects.
125 def __init__(self, warrior=None, filter_obj=None):
126 self.warrior = warrior
127 self._result_cache = None
128 self.filter_obj = filter_obj or TaskFilter()
130 def __deepcopy__(self, memo):
132 Deep copy of a QuerySet doesn't populate the cache
134 obj = self.__class__()
135 for k, v in self.__dict__.items():
136 if k in ('_iter', '_result_cache'):
137 obj.__dict__[k] = None
139 obj.__dict__[k] = copy.deepcopy(v, memo)
143 data = list(self[:REPR_OUTPUT_SIZE + 1])
144 if len(data) > REPR_OUTPUT_SIZE:
145 data[-1] = "...(remaining elements truncated)..."
149 if self._result_cache is None:
150 self._result_cache = list(self)
151 return len(self._result_cache)
154 if self._result_cache is None:
155 self._result_cache = self._execute()
156 return iter(self._result_cache)
158 def __getitem__(self, k):
159 if self._result_cache is None:
160 self._result_cache = list(self)
161 return self._result_cache.__getitem__(k)
164 if self._result_cache is not None:
165 return bool(self._result_cache)
168 except StopIteration:
172 def __nonzero__(self):
173 return type(self).__bool__(self)
175 def _clone(self, klass=None, **kwargs):
177 klass = self.__class__
178 filter_obj = self.filter_obj.clone()
179 c = klass(warrior=self.warrior, filter_obj=filter_obj)
180 c.__dict__.update(kwargs)
185 Fetch the tasks which match the current filters.
187 return self.warrior.filter_tasks(self.filter_obj)
191 Returns a new TaskQuerySet that is a copy of the current one.
196 return self.filter(status=PENDING)
198 def filter(self, *args, **kwargs):
200 Returns a new TaskQuerySet with the given filters added.
202 clone = self._clone()
204 clone.filter_obj.add_filter(f)
205 for key, value in kwargs.items():
206 clone.filter_obj.add_filter_param(key, value)
209 def get(self, **kwargs):
211 Performs the query and returns a single object matching the given
214 clone = self.filter(**kwargs)
217 return clone._result_cache[0]
219 raise Task.DoesNotExist(
220 'Task matching query does not exist. '
221 'Lookup parameters were {0}'.format(kwargs))
223 'get() returned more than one Task -- it returned {0}! '
224 'Lookup parameters were {1}'.format(num, kwargs))
227 class TaskWarrior(object):
228 def __init__(self, data_location='~/.task', create=True):
229 data_location = os.path.expanduser(data_location)
230 if not os.path.exists(data_location):
231 os.makedirs(data_location)
233 'data.location': os.path.expanduser(data_location),
235 self.tasks = TaskQuerySet(self)
237 def _get_command_args(self, args, config_override={}):
238 command_args = ['task', 'rc:/']
239 config = self.config.copy()
240 config.update(config_override)
241 for item in config.items():
242 command_args.append('rc.{0}={1}'.format(*item))
243 command_args.extend(map(str, args))
246 def execute_command(self, args, config_override={}):
247 command_args = self._get_command_args(
248 args, config_override=config_override)
249 logger.debug(' '.join(command_args))
250 p = subprocess.Popen(command_args, stdout=subprocess.PIPE,
251 stderr=subprocess.PIPE)
252 stdout, stderr = p.communicate()
254 error_msg = stderr.strip().splitlines()[-1]
255 raise TaskWarriorException(error_msg)
256 return stdout.strip().split('\n')
258 def filter_tasks(self, filter_obj):
259 args = ['export', '--'] + filter_obj.get_filter_params()
261 for line in self.execute_command(args):
263 tasks.append(Task(self, json.loads(line.strip(','))))
266 def merge_with(self, path, push=False):
267 path = path.rstrip('/') + '/'
268 self.execute_command(['merge', path], config_override={
269 'merge.autopush': 'yes' if push else 'no',
273 self.execute_command(['undo'], config_override={
274 'confirmation': 'no',