]> 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:

Task: Make Task object non-iterable
[etc/taskwarrior.git] / tasklib / task.py
index 3860f8efd7b7309b409ee9fd0fbcfa5ac83e49ef..096f656d7d9e903ecebd3b6a2c36f58b568afb79 100644 (file)
@@ -29,9 +29,19 @@ class TaskResource(object):
 
     def _load_data(self, data):
         self._data = data
 
     def _load_data(self, data):
         self._data = data
-        self._original_data = data
+        # We need to use a copy for original data, so that changes
+        # are not propagated
+        self._original_data = data.copy()
 
     def __getitem__(self, key):
 
     def __getitem__(self, key):
+        # This is a workaround to make TaskResource non-iterable
+        # over simple index-based iteration
+        try:
+            int(key)
+            raise StopIteration
+        except ValueError:
+            pass
+
         hydrate_func = getattr(self, 'deserialize_{0}'.format(key),
                                lambda x: x)
         return hydrate_func(self._data.get(key))
         hydrate_func = getattr(self, 'deserialize_{0}'.format(key),
                                lambda x: x)
         return hydrate_func(self._data.get(key))
@@ -276,7 +286,10 @@ class Task(TaskResource):
             self._original_data.update(to_update)
         else:
             self._data = new_data
             self._original_data.update(to_update)
         else:
             self._data = new_data
-            self._original_data = new_data
+            # We need to create a clone for original_data though
+            # Shallow copy is alright, since data dict uses only
+            # primitive data types
+            self._original_data = new_data.copy()
 
 
 class TaskFilter(object):
 
 
 class TaskFilter(object):