Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/util/journalpickler.py
blob: 36f931154235eeae725eaf5c60a0594ddb6327d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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