[Checkins]
SVN: Sandbox/ulif/zope.introspector/src/zope/introspector/utilityinfo.txt
Update utilitiy tests to reflect ability to detect local utilities.
Uli Fouquet
uli at gnufix.de
Fri Jun 20 08:36:42 EDT 2008
Log message for revision 87598:
Update utilitiy tests to reflect ability to detect local utilities.
Changed:
U Sandbox/ulif/zope.introspector/src/zope/introspector/utilityinfo.txt
-=-
Modified: Sandbox/ulif/zope.introspector/src/zope/introspector/utilityinfo.txt
===================================================================
--- Sandbox/ulif/zope.introspector/src/zope/introspector/utilityinfo.txt 2008-06-20 12:33:19 UTC (rev 87597)
+++ Sandbox/ulif/zope.introspector/src/zope/introspector/utilityinfo.txt 2008-06-20 12:36:41 UTC (rev 87598)
@@ -16,15 +16,18 @@
>>> class Mammoth(object):
... pass
-We define an utility::
+We define a utility::
>>> from zope.interface import Interface, implements
>>> from zope.component import provideUtility
+ >>> import persistent
+ >>> from zope.app.container.contained import Contained
>>> class ITestUtility(Interface):
... pass
- >>> class TestUtility(object):
+ >>> #class TestUtility(object):
+ >>> class TestUtility(persistent.Persistent, Contained):
... implements(ITestUtility)
@@ -49,12 +52,65 @@
>>> info = UtilityInfo(manfred)
>>> from pprint import pprint
>>> pprint(info.getAllUtilities())
- [{'component': <ZODB.DB.DB object at 0x...>,
- 'name': 'unnamed',
- 'provided': <InterfaceClass ZODB.interfaces.IDatabase>,
+ [{'component': <InterfaceClass zope.component...IUtilityRegistration>,
+ 'name': 'zope.component.interfaces.IUtilityRegistration',
+ 'provided': <InterfaceClass zope.interface.interfaces.IInterface>,
'registry': <BaseGlobalComponents base>},
+ ...
{'component': <TestUtility object at 0x...>,
'name': '',
'provided': <InterfaceClass __builtin__.ITestUtility>,
- 'registry': <BaseGlobalComponents base>}]
+ 'registry': <BaseGlobalComponents base>},
+ ...
+
+Local Utilitites
+================
+
+If an object is stored in a local site, then we should get also the
+locally registered utilities, but only those, that are in one of the
+sites, the object is part of.
+
+We now create a site, where we can register local utilities and store
+objects::
+
+ >>> from zope.app.folder import folder
+ >>> local_site1 = folder.rootFolder()
+ >>> getRootFolder()['local_site'] = local_site1
+
+ >>> from zope.app.component import site
+ >>> sm = site.LocalSiteManager(local_site1)
+ >>> local_site1.setSiteManager(sm)
+
+We register a utility in this site, this time a named one::
+
+ >>> local_utility1 = TestUtility()
+ >>> sm.registerUtility(local_utility1, ITestUtility, 'local_u1')
+
+The utility is available, if we ask the local site manager directly::
+
+ >>> sm.queryUtility(ITestUtility, 'local_u1')
+ <TestUtility object at 0x...>
+
+We place another mammoth in that site. To make it also a placefull
+object, we have also to provide a ``__parent__`` attribute::
+
+ >>> fred = Mammoth()
+ >>> local_site1['fred'] = fred
+ >>> fred.__parent__ = local_site1
+
+Using UtilityInfo we can now get also this newly registered utility::
+
+ >>> info = UtilityInfo(fred)
+ >>> pprint(info.getAllUtilities())
+ [{'component': <TestUtility object at 0x...>,
+ 'name': 'local_u1',
+ 'provided': <InterfaceClass __builtin__.ITestUtility>,
+ 'registry': <LocalSiteManager ++etc++site>},
+ ...
+ {'component': <TestUtility object at 0x...>,
+ 'name': '',
+ 'provided': <InterfaceClass __builtin__.ITestUtility>,
+ 'registry': <BaseGlobalComponents base>},
+ ...]
+
More information about the Checkins
mailing list