[Zope3-checkins] CVS: Zope3/src/zope/app/container - contained.py:1.1.2.7

Jim Fulton jim at zope.com
Wed Sep 17 18:30:09 EDT 2003


Update of /cvs-repository/Zope3/src/zope/app/container
In directory cvs.zope.org:/tmp/cvs-serv22879/src/zope/app/container

Modified Files:
      Tag: parentgeddon-branch
	contained.py 
Log Message:
Provided automativ data conversion for parentgeddon.


=== Zope3/src/zope/app/container/contained.py 1.1.2.6 => 1.1.2.7 ===
--- Zope3/src/zope/app/container/contained.py:1.1.2.6	Wed Sep 17 09:08:59 2003
+++ Zope3/src/zope/app/container/contained.py	Wed Sep 17 18:29:38 2003
@@ -442,7 +442,7 @@
         publish(container, event)
         modified(container)    
 
-
+fixing_up = False
 def uncontained(object, container, name=None):
     """Clear the containment relationship between the object amd the container
 
@@ -530,8 +530,16 @@
     3
 
     """
-    oldparent = object.__parent__
-    oldname = object.__name__
+    try:
+        oldparent = object.__parent__
+        oldname = object.__name__
+    except AttributeError:
+        # The old object doesn't implements IContained
+        # Maybe we're converting old data:
+        if not fixing_up:
+            raise
+        oldparent = None
+        oldname = None
 
     if oldparent is not container or oldname != name:
         if oldparent is not None or oldname is not None:
@@ -692,6 +700,97 @@
     __providedBy__ = DecoratorSpecificationDescriptor()
 
     __Security_checker__ = DecoratedSecurityCheckerDescriptor()
+
+
+##############################################################################
+# Parentgeddon fixup:
+
+from zope.app.event.function import Subscriber
+from zope.app.interfaces.content.folder import IRootFolder
+from zope.app.interfaces.container import IWriteContainer
+from zope.app.interfaces.services.service import IPossibleSite
+from zope.app.interfaces.container import IContainer
+from transaction import get_transaction
+from zope.component.exceptions import ComponentLookupError
+from zope.app.interfaces.services.registration import IRegistry, INameRegistry
+from zope.app.services.registration import RegistrationStack
+
+def parentgeddonFixup(event):
+    """Fixup databases to work with the result of parentgeddon.
+    """
+    database = event.database
+    connection = database.open()
+    app = connection.root().get('Application')
+    if app is None:
+        # No old data
+        return
+
+    if IRootFolder.isImplementedBy(app):
+        # already did fixup
+        return
+
+    print "Fixing up data for parentgeddon"
+
+    zope.interface.directlyProvides(app, IRootFolder)
+
+    global fixing_up
+    fixing_up = True
+
+    try:
+        fixcontainer(app)
+        get_transaction().commit()
+    finally:
+        get_transaction().abort()
+        fixing_up = False
+        connection.close()
+
+parentgeddonFixup = Subscriber(parentgeddonFixup)
+
+def fixcontainer(container):
+
+    # Step 1: fix items:
+    for name in container:
+        ob = container[name]
+        
+        if not IContained.isImplementedBy(ob):
+            # remove the old item and reassign it
+            del container[name]
+            ob = contained(ob, container, name)
+            container[name] = ob
+        elif ob.__parent__ is not container:
+            ob.__parent__ = container
+            ob.__name__ = name
+
+        if IContainer.isImplementedBy(ob):
+            fixcontainer(ob)
+
+        if IRegistry.isImplementedBy(ob):
+            fixregistry(ob)
     
-    
+    if IPossibleSite.isImplementedBy(container):
+        try:
+            sm = container.getSiteManager()
+        except ComponentLookupError:
+            pass # nothing to do
+        else:
+            fixupsitemanager(sm, container)
+
+def fixupsitemanager(sm, container):
+    sm.__parent__ = container
+    sm.__name__ = '++etc++site'
+    sm._SampleContainer__data = sm.Packages._SampleContainer__data
+    del sm.Packages
+    fixregistry(sm)
+    fixcontainer(sm)
+
+def fixregistry(registry):
+    if INameRegistry.isImplementedBy(registry):
+        for name in registry.listRegistrationNames():
+            stack = registry.queryRegistrations(name)
+            stack.__parent__ = registry
+        return
+    for data in registry.getRegisteredMatching():
+        for ob in data:
+            if isinstance(ob, RegistrationStack):
+                ob.__parent__ = registry
     




More information about the Zope3-Checkins mailing list