[Zope-CVS] SVN: zversioning/trunk/gaga.py Storing atomic objects implemented.

Uwe Oestermeier uwe_oestermeier at iwm-kmrc.de
Sun Oct 10 16:15:59 EDT 2004


Log message for revision 27939:
  Storing atomic objects implemented.


Changed:
  U   zversioning/trunk/gaga.py


-=-
Modified: zversioning/trunk/gaga.py
===================================================================
--- zversioning/trunk/gaga.py	2004-10-10 19:58:46 UTC (rev 27938)
+++ zversioning/trunk/gaga.py	2004-10-10 20:15:59 UTC (rev 27939)
@@ -1,31 +1,170 @@
+#! /usr/local/env/python
 from cStringIO import StringIO
-from cPickle import Pickler, Unpickler
+import cPickle
+from cPickle import Pickler, Unpickler, loads
+  
+from persistent import Persistent
+  
+def isVersionable(obj) :
+    return getattr(obj, "__versionable__", False)
+   
+class AtomicPickler(object) :
 
-ids = {}
+    def __init__(self, repository) :
+        stream = self.stream = StringIO()
+        self.repository = repository
+        self.pickler = Pickler(stream, 1)
+        self.pickler.persistent_id = self.persistent_id
+        self.init = True
+       
+    def persistent_id(self, obj) :
+        if self.init :
+            return None
+        if self.isVersionable(obj) :
+            return self.repository.getReferenceId(obj)
+        return obj
+         
+    def dump(self, obj) :
+        self.init = True
+        self.stream.seek(0)
+        self.pickler.dump(obj)
+        return self.stream.getvalue()      
+        
+class AtomicUnpickler(object) :
 
-def persistent_id(ob):
-    myid = id(ob)
-    ids[myid] = ob
-    return myid
+    def __init__(self, repository) :
+        stream = self.stream = StringIO()
+        self.repository = repository
+        self.pickler = Pickler(stream, 1)
+        self.pickler.persistent_id = self.persistent_id
+        self.init = True
+       
+    def persistent_id(self, obj) :
+        if self.init :
+            return None
+        if self.isVersionable(obj) :
+            return self.repository.getReferenceId(obj)
+        return obj
+         
+    def dump(self, obj) :
+        self.init = True
+        self.stream.seek(0)
+        self.pickler.dump(obj)
+        return self.stream.getvalue()      
 
-class Dummy:
+            
+   
+class Repository(object) :
+    """ A simple dict simulation of an arbitrary data store. """
+    
+    counter = 0
+    storage = {}
+    
+    def __init__(self) :
+        self.atomic_pickler = AtomicPickler(self)
+    
+    def getTicket(self, obj) :
+        if hasattr(obj, "__ticket__") :
+            return obj.__ticket__
+        self.counter += 1
+        return self.counter
+        
+    def getReferenceId(self, ticket) :
+        return "guid of %s" % ticket
+  
+    def store(self, ticket, obj) :
+        self.storage[ticket] = self.atomic_pickler.dump(obj)
+        
+    def load(self, ticket) :
+        return cPickle.loads(self.storage[ticket])
+        
+        
+    
+class ReferencePickler :
+    """ Help class to store wrapped references. """
+    
+    def __init__(self, repository) :
+        self.repository = repository
+        self.counter = 0
+             
+              
+    def writeReference(self, obj):
+        print "writeReference", obj
+        self.counter += 1
+        if self.counter == 1 :
+            return None
+        
+        if isVersionable(obj) : 
+            print "isVersionable", obj
+            ticket = self.repository.getTicket(obj)
+            self.repository.store(ticket, obj)
+            return None
+            key = repository.getReferenceId(ticket)
+            print "   new version", key
+            return key
+            
+        return obj
+        
+    def readReference(self, obj) :
+        if isinstance(obj, str) and obj.startswith("our id") :
+            return self.ids[obj]
+        return obj
+                 
+
+class Dummy(object) :
+    
+    __versionable__ = True
+    
     def __getstate__(self):
-        print self.__dict__
+        print "pickling:", self.__dict__
         return self.__dict__
+        
+    def __setstate__(self, state) :
+        print "unpickling:", state
+        self.__dict__ = state
     
+  
+ 
+repository = Repository()
+
+class InnerDummy(Dummy) : pass
+class InnerInnerDummy(Dummy) : pass  
+
+
+innerdummy = InnerDummy()
+innerinnderdummy = InnerInnerDummy()
+innerdummy.level2 = innerinnderdummy
+ 
 dummy = Dummy()
 dummy.__anno__ = {'component1': 'blah', 'component2': 'trallalla'}
 dummy.simple = 3
+dummy.subdummy = innerdummy
 
 stream = StringIO()
 p = Pickler(stream, 1)
-p.persistent_id = persistent_id
+helper = ReferencePickler(repository)
+p.persistent_id = helper.writeReference
 p.dump(dummy)
-print stream
 
-"""
-stream.seek(0)
-u = Unpickler(stream)
-u.persistent_load = ids.get
-u.load()
-"""
\ No newline at end of file
+
+print "Repository"
+for key, value in repository.storage.items() :
+    print key
+    print value
+    print key, ":", len(value), "bytes", repository.load(key)
+    
+#print "gepoeckelt", stream.getvalue()
+
+
+
+
+# stream.seek(0)
+# u = Unpickler(stream)
+# u.persistent_load = helper.sniffed
+# u.noload()
+# 
+# print "sniffed", helper.sniffed
+# 
+
+
+



More information about the Zope-CVS mailing list