[Zope3-checkins] CVS: Zope3/src/zope/proxy/context - __init__.py:1.8.2.2

Steve Alexander steve@cat-box.net
Fri, 16 May 2003 12:18:41 -0400


Update of /cvs-repository/Zope3/src/zope/proxy/context
In directory cvs.zope.org:/tmp/cvs-serv2621/src/zope/proxy/context

Modified Files:
      Tag: stevea-decorators-branch
	__init__.py 
Log Message:
All unit tests now pass.
Added an ensureContextWrapped function to zope.proxy.context.

In code that uses context wrapping, we have two situations:

  * Code such as traversal code wants to ensure that there is a context-
    wrapper, and set the parent and items in the context dict.
    If there is no context wrapper, it should create one, but if there
    is one already, it should be preserved.

  * Code that is the primary source of some information, and wants to
    wrap it and add minimal information (name, usually) before allowing
    the world to see the information. The zopecontainerdecorator is an
    example of this.

To resolve these situations, the former code should use
ensureContextWrapped, which keeps an existing context wrapper if possible,
and updates the context and adds/overwrites items in the context dict.
The latter code can continue to use ContextWrapper.



=== Zope3/src/zope/proxy/context/__init__.py 1.8.2.1 => 1.8.2.2 ===
--- Zope3/src/zope/proxy/context/__init__.py:1.8.2.1	Wed May 14 03:36:10 2003
+++ Zope3/src/zope/proxy/context/__init__.py	Fri May 16 12:18:41 2003
@@ -23,7 +23,7 @@
 from zope.interface import moduleProvides
 from zope.security.proxy import Proxy, getChecker, getObject
 from zope.proxy.context.wrapper import getobject, getdict
-from zope.proxy.context.wrapper import getcontext, getinnercontext
+from zope.proxy.context.wrapper import setcontext, getcontext, getinnercontext
 from zope.proxy.context.wrapper import getinnerwrapper, getbaseobject
 from zope.proxy.context.wrapper import ContextDescriptor, ContextAware
 from zope.proxy.context.wrapper import ContextMethod, ContextProperty
@@ -57,6 +57,19 @@
         _ob = Wrapper(_ob, _parent, **kw)
 
     return _ob
+
+def ensureContextWrapped(_ob, _parent, **kw):
+    # XXX Test me, document me!
+    if type(_ob) is Proxy:
+        unproxied_ob = getObject(_ob)
+    else:
+        unproxied_ob = _ob
+    if type(unproxied_ob) not in wrapperTypes:
+        return ContextWrapper(_ob, _parent, **kw)
+    else:
+        getdict(unproxied_ob).update(kw)
+        setcontext(unproxied_ob, _parent)
+        return _ob
 
 def getWrapperObject(_ob):
     """Remove a context wrapper around an object with data