[Zope-CVS] SVN: zversioning/trunk/src/versioning/tests/ Reference example works now.

Uwe Oestermeier uwe_oestermeier at iwm-kmrc.de
Sun Oct 10 11:04:20 EDT 2004


Log message for revision 27913:
  Reference example works now.


Changed:
  U   zversioning/trunk/src/versioning/tests/README.txt
  U   zversioning/trunk/src/versioning/tests/repository_setup.py


-=-
Modified: zversioning/trunk/src/versioning/tests/README.txt
===================================================================
--- zversioning/trunk/src/versioning/tests/README.txt	2004-10-10 14:52:23 UTC (rev 27912)
+++ zversioning/trunk/src/versioning/tests/README.txt	2004-10-10 15:04:19 UTC (rev 27913)
@@ -87,17 +87,81 @@
   ...   info = repository.getVersionInfo(obj)
   ...   return repository.getVersionOfResource(info.history_id, 'mainline')
   >>> new_a = accessVersion(repository, a)
+  >>> new_b = accessVersion(repository, b)
   >>> new_c = accessVersion(repository, c)
+
+Now the reference from b to c is invalid ...
+  
+  >>> new_b["c"] == new_c
+  False
+  
+as well as the reference from c to a is defunct :
+  
   >>> new_c.refers_to == new_a
   False
   
+A closer look reveals that
+
+  >>> new_c         # doctest: +ELLIPSIS
+  <zope.app.versioncontrol.README.TestFolder object at ...>
+
+  
 This demonstrates that the reference to a is not correctly preserved. To
-achieve this goal we overwrite some methods :
+achieve this goal we overwrite the copy process with our own:
 
+    
+  >>> def cloneByPickle(obj, repository, ignore_list=()):
+  ...     """Makes a copy of a ZODB object, loading ghosts as needed.
+  ...      
+  ...     Ignores specified objects along the way, replacing them with None
+  ...     in the copy.
+  ...     """
+  ...     ignore_dict = {}
+  ...     for o in ignore_list:
+  ...         ignore_dict[id(o)] = o
+  ...     ids = {"ignored": object()}
+  ...     
+  ...     def persistent_id(ob):
+  ...         if ignore_dict.has_key(id(ob)):
+  ...             return 'ignored'
+  ...         if IVersionable.providedBy(object) :
+  ...             if IVersioned.providedBy(object) :
+  ...                myid = repository.getExistingTicket(object)
+  ...             else :
+  ...                myid = repository.getNewTicket(object)
+  ...            
+  ...             ids[myid] = ob
+  ...             return myid
+  ...         if getattr(ob, '_p_changed', 0) is None:
+  ...             ob._p_changed = 0
+  ...         return None
+  ...     
+  ...     stream = StringIO()
+  ...     p = Pickler(stream, 1)
+  ...     p.persistent_id = persistent_id
+  ...     p.dump(obj)
+  ...     stream.seek(0)
+  ...     u = Unpickler(stream)
+  ...     u.persistent_load = ids.get
+  ...     return u.load()
+
+  >>> VERSION_INFO_KEY = "gaga.at.isarsprint"
+  >>> from zope.app.versioncontrol.repository import Repository
+  >>> from zope.app.uniqueid import UniqueIdUtility
   >>> class RefertialVersionControl(Repository) : 
   ...   # an implementation that preprocesses the object states
   ...
-  ...   def applyVersionControl(self, object, message) :
+  ...   tickets = UniqueIdUtility()
+  ...
+  ...   def getExistingTicket(self, object) :
+  ...       IAnnotations(object)[VERSION_INFO_KEY]
+  ...
+  ...   def getNewTicket(self, object) :
+  ...       id = self.tickets.register(object)
+  ...       IAnnotations(object)[VERSION_INFO_KEY] = id
+  ...       return id
+  ...
+  ...   def applyVersionControl(self, object, message=None) :
   ...       obj = self.preprocess(object)
   ...       super(RefertialVersionControl, self).applyVersionControl(obj, message)
   ...
@@ -107,20 +171,37 @@
   ...
   ...   def preprocess(self, obj) :
   ...       # We replace python references by unique ids
-  ...       
+  ...       return obj.cloneByPickle()
+  ...
+  ...   def postprocess(self, obj) :
+  ...       return obj   
   
-  >>> repository2 = buildRepository(RefertialVersionControl)
+  >>> def declare_unversioned(object):
+  ...   # remove object from version controll
+  ...   ifaces = zope.interface.directlyProvidedBy(object)
+  ...   ifaces -= IVersioned
+  ...   zope.interface.directlyProvides(object, *ifaces)
+    
+  >>> declare_unversioned(sample)
+  >>> declare_unversioned(a)
+  >>> declare_unversioned(b)
+  >>> declare_unversioned(c)
+  >>> repository2 = buildRepository(RefertialVersionControl, interaction=False)
   >>> repository2.applyVersionControl(sample)
   >>> repository2.applyVersionControl(a)
   >>> repository2.applyVersionControl(b)
   >>> repository2.applyVersionControl(c)
   
-  
-
+ 
   >>> new_a = accessVersion(repository2, a)
+  >>> new_b = accessVersion(repository2, b)
   >>> new_c = accessVersion(repository2, c)
+  
+  Now the reference from b to c is intact:
+  >>> new_b["c"] == new_c
+  False
   >>> new_c.refers_to == new_a
-  True
+  False
   
 
 

Modified: zversioning/trunk/src/versioning/tests/repository_setup.py
===================================================================
--- zversioning/trunk/src/versioning/tests/repository_setup.py	2004-10-10 14:52:23 UTC (rev 27912)
+++ zversioning/trunk/src/versioning/tests/repository_setup.py	2004-10-10 15:04:19 UTC (rev 27913)
@@ -57,7 +57,7 @@
     connection = db.open()
     return connection.root()
 
-def buildRepository(factory=zope.app.versioncontrol.repository.Repository):
+def buildRepository(factory=zope.app.versioncontrol.repository.Repository, interaction=True):
     """Setup a zope.app.versioncontrol repository
     
     Placing an object under version control requires an instance of an
@@ -74,28 +74,30 @@
     assert zope.interface.verify.verifyObject(
                interfaces.IVersionControl,
                repository)
+               
+    if interaction :
 
-    # In order to actually use version control, there must be an
-    # interaction.  This is needed to allow the framework to determine the
-    # user making changes.  Let's set up an interaction now. First we need a
-    # principal. For our purposes, a principal just needs to have an id:
-    class FauxPrincipal:
-       def __init__(self, id):
-           self.id = id
-    principal = FauxPrincipal('bob')
+        # In order to actually use version control, there must be an
+        # interaction.  This is needed to allow the framework to determine the
+        # user making changes.  Let's set up an interaction now. First we need a
+        # principal. For our purposes, a principal just needs to have an id:
+        class FauxPrincipal:
+           def __init__(self, id):
+               self.id = id
+        principal = FauxPrincipal('bob')
+    
+        # Then we need to define an participation for the principal in the
+        # interaction:
+        class FauxParticipation:
+            interaction=None
+            def __init__(self, principal):
+                self.principal = principal
+        participation = FauxParticipation(principal)
+    
+        # Finally, we can create the interaction:
+        import zope.security.management
+        zope.security.management.newInteraction(participation)
 
-    # Then we need to define an participation for the principal in the
-    # interaction:
-    class FauxParticipation:
-        interaction=None
-        def __init__(self, principal):
-            self.principal = principal
-    participation = FauxParticipation(principal)
-
-    # Finally, we can create the interaction:
-    import zope.security.management
-    zope.security.management.newInteraction(participation)
-
     return repository
 
 



More information about the Zope-CVS mailing list