Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/util/journalpickler.py
diff options
context:
space:
mode:
Diffstat (limited to 'util/journalpickler.py')
-rw-r--r--util/journalpickler.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/util/journalpickler.py b/util/journalpickler.py
new file mode 100644
index 0000000..36f9311
--- /dev/null
+++ b/util/journalpickler.py
@@ -0,0 +1,47 @@
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+#
+
+import pickle # TODO -> why does cPickle bitch about needing persistence.Jokebook etc ?
+
+
+class JournalPickler:
+ '''Works with util.Persistence to persist objects to the Sugar Journal'''
+
+ def __init__(self, obj = None):
+ pass
+
+ def __set_dirty(self, obj, is_dirty):
+ obj.__dirty__ = False
+ for name, prop in obj.__properties__:
+ if prop.fget(obj).__class__ is list:
+ for item in prop.fget(obj):
+ if hasattr(item, '__dirty__'):
+ self.__set_dirty(item, is_dirty)
+
+
+ def dumps(self, obj, deep_dump):
+ self.__set_dirty(obj, False)
+ pickled = pickle.dumps(obj)
+ return pickled
+
+
+ def loads(self, pickled):
+ obj = pickle.loads(pickled)
+ self.__set_dirty(obj, False)
+ return obj
+
+
+dumps = lambda obj, deep=False : JournalPickler(obj).dumps(obj, deep)
+loads = JournalPickler().loads