]> git.madduck.net Git - etc/taskwarrior.git/blobdiff - tasklib/task.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Add serialize/deserialize for fields and implement for due date
[etc/taskwarrior.git] / tasklib / task.py
index cd4b92f1d3af491bc001229406ac26f3e509c5a7..b78ed32fb4aa0aee25e9c17a4f74302ff66f39e7 100644 (file)
@@ -1,4 +1,5 @@
 import copy
+import datetime
 import json
 import os
 import subprocess
@@ -6,7 +7,10 @@ import tempfile
 import uuid
 
 
+DATE_FORMAT = '%Y%m%dT%H%M%SZ'
+
 REPR_OUTPUT_SIZE = 10
+
 PENDING = 'pending'
 
 
@@ -24,7 +28,7 @@ class Task(object):
         self._data = data
 
     def __getitem__(self, key):
-        return self._data.get(key)
+        return self._get_field(key)
 
     def __setitem__(self, key, val):
         self._data[key] = val
@@ -32,6 +36,22 @@ class Task(object):
     def __unicode__(self):
         return self._data.get('description')
 
+    def _get_field(self, key):
+        hydrate_func = getattr(self, 'deserialize_{0}'.format(key), lambda x:x)
+        return hydrate_func(self._data.get(key))
+
+    def _set_field(self, key, value):
+        dehydrate_func = getattr(self, 'serialize_{0}'.format(key), lambda x:x)
+        self._data[key] = dehydrate_func(value)
+
+    def serialize_due(self, date):
+        return date.strftime(DATE_FORMAT)
+
+    def deserialize_due(self, date_str):
+        if not date_str:
+            return None
+        return datetime.datetime.strptime(date_str, DATE_FORMAT)
+
     def regenerate_uuid(self):
         self['uuid'] = str(uuid.uuid4())