[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/apidoc/component. Improved getRealFactory()'s capabilities to remove all wrappers until the

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Nov 10 06:40:39 EST 2005


Log message for revision 40022:
  Improved getRealFactory()'s capabilities to remove all wrappers until the 
  original factory is found. Added missing tests for the function.
  

Changed:
  U   Zope3/trunk/src/zope/app/apidoc/component.py
  U   Zope3/trunk/src/zope/app/apidoc/component.txt

-=-
Modified: Zope3/trunk/src/zope/app/apidoc/component.py
===================================================================
--- Zope3/trunk/src/zope/app/apidoc/component.py	2005-11-10 02:46:57 UTC (rev 40021)
+++ Zope3/trunk/src/zope/app/apidoc/component.py	2005-11-10 11:40:37 UTC (rev 40022)
@@ -140,11 +140,14 @@
     Sometimes the original factory is masked by functions. If the function
     keeps track of the original factory, use it.
     """
-    if isinstance(factory, types.FunctionType) and hasattr(factory, 'factory'):
-        return factory.factory
-    elif not hasattr(factory, '__name__'):
-        # We have an instance
+    # Remove all wrappers until none are found anymore.
+    while hasattr(factory, 'factory'):
+        factory = factory.factory
+
+    # If we have an instance, return its class
+    if not hasattr(factory, '__name__'):
         return factory.__class__
+
     return factory
 
 

Modified: Zope3/trunk/src/zope/app/apidoc/component.txt
===================================================================
--- Zope3/trunk/src/zope/app/apidoc/component.txt	2005-11-10 02:46:57 UTC (rev 40021)
+++ Zope3/trunk/src/zope/app/apidoc/component.txt	2005-11-10 11:40:37 UTC (rev 40022)
@@ -208,6 +208,61 @@
                        <zope.app.apidoc.doctest.MyFooBar object at ...>, '')]
 
 
+`getRealFactory(factory)`
+-------------------------
+
+During registration, factories are commonly masked by wrapper functions. Also,
+factories are sometimes also `IFactory` instances, which are not referencable,
+so that we would like to return the class. If the wrapper objects/functions
+play nice, then they provide a `factory` attribute that points to the next
+wrapper or the original factory.
+
+The task of this function is to remove all the factory wrappers and make sure
+that the returned factory is referencable.
+
+  >>> class Factory(object):
+  ...     pass
+
+  >>> def wrapper1(*args):
+  ...     return Factory(*args)
+  >>> wrapper1.factory = Factory
+
+  >>> def wrapper2(*args):
+  ...     return wrapper1(*args)
+  >>> wrapper2.factory = wrapper1
+
+So whether we pass in `Factory`,
+
+  >>> component.getRealFactory(Factory)
+  <class 'zope.app.apidoc.doctest.Factory'>
+
+`wrapper1`,
+
+  >>> component.getRealFactory(wrapper1)
+  <class 'zope.app.apidoc.doctest.Factory'>
+
+or `wrapper2`,
+
+  >>> component.getRealFactory(wrapper2)
+  <class 'zope.app.apidoc.doctest.Factory'>
+
+the answer should always be the `Factory` class. Next we are going to pass in
+an instance, and again we should get our class aas a result:
+
+  >>> factory = Factory()
+  >>> component.getRealFactory(factory)
+  <class 'zope.app.apidoc.doctest.Factory'>
+
+Even, if the factory instance is wrapped, we should get the factory class:
+
+  >>> def wrapper3(*args):
+  ...     return factory(*args)
+  >>> wrapper3.factory = factory
+
+  >>> component.getRealFactory(wrapper3)
+  <class 'zope.app.apidoc.doctest.Factory'>
+
+
 `getInterfaceInfoDictionary(iface)`
 -----------------------------------
 



More information about the Zope3-Checkins mailing list