[Checkins] SVN: grok/branches/faassen-index/src/grok/ Give an error
if we try to index things on an interface that do not
Martijn Faassen
faassen at infrae.com
Fri Apr 20 11:03:40 EDT 2007
Log message for revision 74264:
Give an error if we try to index things on an interface that do not
exist.
Changed:
A grok/branches/faassen-index/src/grok/ftests/catalog/indexes_nonexistent.py
U grok/branches/faassen-index/src/grok/index.py
U grok/branches/faassen-index/src/grok/meta.py
-=-
Added: grok/branches/faassen-index/src/grok/ftests/catalog/indexes_nonexistent.py
===================================================================
--- grok/branches/faassen-index/src/grok/ftests/catalog/indexes_nonexistent.py 2007-04-20 14:52:20 UTC (rev 74263)
+++ grok/branches/faassen-index/src/grok/ftests/catalog/indexes_nonexistent.py 2007-04-20 15:03:40 UTC (rev 74264)
@@ -0,0 +1,65 @@
+"""
+Grok allows you to set up catalog indexes in your application with a
+special indexes declaration. Here we show what happens if you try
+to set up an index for an attribute that does not exist on the interface.
+
+ >>> import grok
+ >>> grok.grok('grok.ftests.catalog.indexes_nonexistent')
+
+Let's set up a site in which we manage a couple of objects::
+
+ >>> from grok.ftests.catalog.indexes_nonexistent import Herd
+ >>> herd = Herd()
+ >>> getRootFolder()['herd'] = herd
+ Traceback (most recent call last):
+ ...
+ GrokError: grok.Indexes in <module
+ 'grok.ftests.catalog.indexes_nonexistent' from ...>
+ refers to an attribute or method 'foo' on interface <InterfaceClass
+ grok.ftests.catalog.indexes_nonexistent.IMammoth>, but this does not
+ exist.
+
+Nuke the catalog and intids in the end, so as not to confuse
+other tests::
+
+ >>> from zope.app.component.hooks import setSite
+ >>> setSite(herd)
+ >>> from zope.app.catalog.interfaces import ICatalog
+ >>> from zope.component import getUtility
+ >>> catalog = getUtility(ICatalog)
+
+
+ >>> sm = herd.getSiteManager()
+ >>> from zope.app.catalog.interfaces import ICatalog
+ >>> sm.unregisterUtility(catalog, provided=ICatalog)
+ True
+ >>> from zope.app.intid.interfaces import IIntIds
+ >>> from zope import component
+ >>> intids = component.getUtility(IIntIds)
+ >>> sm.unregisterUtility(intids, provided=IIntIds)
+ True
+"""
+
+from zope.interface import Interface
+from zope import schema
+
+import grok
+from grok import index
+
+class Herd(grok.Container, grok.Application):
+ pass
+
+class IMammoth(Interface):
+ name = schema.TextLine(title=u'Name')
+ age = schema.Int(title=u'Age')
+ def message():
+ """Message the mammoth has for the world."""
+
+class MammothIndexes(grok.Indexes):
+ grok.site(Herd)
+ grok.context(IMammoth)
+
+ foo = index.Field()
+
+
+
Modified: grok/branches/faassen-index/src/grok/index.py
===================================================================
--- grok/branches/faassen-index/src/grok/index.py 2007-04-20 14:52:20 UTC (rev 74263)
+++ grok/branches/faassen-index/src/grok/index.py 2007-04-20 15:03:40 UTC (rev 74264)
@@ -23,9 +23,16 @@
self._args = args
self._kw = kw
- def setup(self, catalog, name, context):
+ def setup(self, catalog, name, context, module_info):
if IInterface.providedBy(context):
- call = IMethod.providedBy(context[name])
+ try:
+ method = context[name]
+ except KeyError:
+ raise GrokError("grok.Indexes in %r refers to an attribute or "
+ "method %r on interface %r, but this does not "
+ "exist." % (module_info.getModule(),
+ name, context), None)
+ call = IMethod.providedBy(method)
else:
call = callable(getattr(context, name, None))
context = None # no interface lookup
Modified: grok/branches/faassen-index/src/grok/meta.py
===================================================================
--- grok/branches/faassen-index/src/grok/meta.py 2007-04-20 14:52:20 UTC (rev 74263)
+++ grok/branches/faassen-index/src/grok/meta.py 2007-04-20 15:03:40 UTC (rev 74264)
@@ -530,7 +530,7 @@
# now install indexes
for name, index in self.indexes.items():
try:
- index.setup(catalog, name, self.context)
+ index.setup(catalog, name, self.context, self.module_info)
except DuplicationError:
raise GrokError(
"grok.Indexes in module %r causes "
More information about the Checkins
mailing list