[Checkins] SVN: grok/trunk/src/grok/ Add an optional 'direct'
keyword argument for the global_utility directive.
Martijn Faassen
faassen at infrae.com
Fri Apr 27 13:44:44 EDT 2007
Log message for revision 74864:
Add an optional 'direct' keyword argument for the global_utility directive.
This allows one to register instances or other objects directly as global
utilities, which is handy sometimes.
(cannot call this argument factory as first proposed as it's already in
use for the directive)
Changed:
U grok/trunk/src/grok/directive.py
U grok/trunk/src/grok/meta.py
U grok/trunk/src/grok/tests/utility/utility.py
-=-
Modified: grok/trunk/src/grok/directive.py
===================================================================
--- grok/trunk/src/grok/directive.py 2007-04-27 16:19:36 UTC (rev 74863)
+++ grok/trunk/src/grok/directive.py 2007-04-27 17:44:43 UTC (rev 74864)
@@ -200,7 +200,8 @@
class GlobalUtilityDirective(MultipleTimesDirective):
- def check_arguments(self, factory, provides=None, name=u''):
+ def check_arguments(self, factory, provides=None, name=u'',
+ direct=False):
if provides is not None and not IInterface.providedBy(provides):
raise GrokImportError("You can only pass an interface to the "
"provides argument of %s." % self.name)
@@ -210,9 +211,10 @@
class GlobalUtilityInfo(object):
- def __init__(self, factory, provides=None, name=u''):
+ def __init__(self, factory, provides=None, name=u'', direct=False):
self.factory = factory
-
+ self.direct = direct
+
if provides is None:
provides = util.class_annotation(factory, 'grok.provides', None)
self.provides = provides
Modified: grok/trunk/src/grok/meta.py
===================================================================
--- grok/trunk/src/grok/meta.py 2007-04-27 16:19:36 UTC (rev 74863)
+++ grok/trunk/src/grok/meta.py 2007-04-27 17:44:43 UTC (rev 74864)
@@ -297,7 +297,11 @@
for info in infos:
if info.provides is None:
util.check_implements_one(info.factory)
- component.provideUtility(info.factory(),
+ if info.direct:
+ obj = info.factory
+ else:
+ obj = info.factory()
+ component.provideUtility(obj,
provides=info.provides,
name=info.name)
Modified: grok/trunk/src/grok/tests/utility/utility.py
===================================================================
--- grok/trunk/src/grok/tests/utility/utility.py 2007-04-27 16:19:36 UTC (rev 74863)
+++ grok/trunk/src/grok/tests/utility/utility.py 2007-04-27 17:44:43 UTC (rev 74864)
@@ -97,7 +97,24 @@
True
>>> small3 is not small2 and small3 is not small
True
+
+Normally one registers a utility factory, such as the class, as a
+global utility. It is also possible to register an arbitrary object directly
+as a global utility. You do this by passing a 'direct' argument set to
+'True'. This can be useful if one needs to register functions (such
+as factory functions) that can be looked up as a utility, or if the
+class you want to register as a global utility has an __init__ that
+takes arguments, where you want to do the instantiation yourself.
+Let's look up an instance we registered this way:
+
+ >>> small4 = component.getUtility(ISmallClub, name='smallish')
+ >>> ISmallClub.providedBy(small4)
+ True
+ >>> isinstance(small4, SmallClub)
+ True
+
"""
+
import grok
from zope import interface
@@ -158,3 +175,6 @@
grok.global_utility(SmallClub, provides=ITinyClub)
grok.global_utility(SmallClub, name='small')
+
+grok.global_utility(SmallClub(), name='smallish',
+ direct=True)
More information about the Checkins
mailing list