[Checkins] SVN: grok/branches/faassen-index/src/grok/ Implement
application directive so that indexes are associated with
Martijn Faassen
faassen at infrae.com
Tue Apr 17 17:44:48 EDT 2007
Log message for revision 74218:
Implement application directive so that indexes are associated with
particular applications or interfaces, not *all* applications.
Changed:
U grok/branches/faassen-index/src/grok/__init__.py
U grok/branches/faassen-index/src/grok/components.py
U grok/branches/faassen-index/src/grok/directive.py
U grok/branches/faassen-index/src/grok/ftests/catalog/indexes.py
A grok/branches/faassen-index/src/grok/ftests/catalog/indexes_app_interface.py
U grok/branches/faassen-index/src/grok/ftests/catalog/indexes_class.py
U grok/branches/faassen-index/src/grok/ftests/catalog/indexes_name.py
A grok/branches/faassen-index/src/grok/ftests/catalog/indexes_no_app.py
U grok/branches/faassen-index/src/grok/meta.py
-=-
Modified: grok/branches/faassen-index/src/grok/__init__.py
===================================================================
--- grok/branches/faassen-index/src/grok/__init__.py 2007-04-17 20:48:27 UTC (rev 74217)
+++ grok/branches/faassen-index/src/grok/__init__.py 2007-04-17 21:44:47 UTC (rev 74218)
@@ -37,7 +37,7 @@
from grok.components import Indexes
from grok.directive import (context, name, template, templatedir, provides,
baseclass, global_utility, local_utility,
- define_permission, require)
+ define_permission, require, application)
from grok._grok import do_grok as grok # Avoid name clash within _grok
from grok._grok import SubscribeDecorator as subscribe
from grok.error import GrokError, GrokImportError
Modified: grok/branches/faassen-index/src/grok/components.py
===================================================================
--- grok/branches/faassen-index/src/grok/components.py 2007-04-17 20:48:27 UTC (rev 74217)
+++ grok/branches/faassen-index/src/grok/components.py 2007-04-17 21:44:47 UTC (rev 74218)
@@ -452,15 +452,13 @@
def __init__(self, name, bases=(), attrs=None):
if attrs is None:
return
- # make sure we take over __grok_context__
- context = attrs.get('__grok_context__')
- if context is not None:
- self.__grok_context__ = context
- # and __grok_name__
- name = attrs.get('__grok_name__')
- if name is not None:
- self.__grok_name__ = name
-
+ # make sure we take over a bunch of possible attributes
+ for name in ['__grok_context__', '__grok_name__',
+ '__grok_application__']:
+ value = attrs.get(name)
+ if value is not None:
+ setattr(self, name, value)
+ # now read and store indexes
indexes = {}
for name, value in attrs.items():
if not interfaces.IIndexDefinition.providedBy(value):
Modified: grok/branches/faassen-index/src/grok/directive.py
===================================================================
--- grok/branches/faassen-index/src/grok/directive.py 2007-04-17 20:48:27 UTC (rev 74217)
+++ grok/branches/faassen-index/src/grok/directive.py 2007-04-17 21:44:47 UTC (rev 74218)
@@ -277,3 +277,6 @@
define_permission = MultipleTextDirective('grok.define_permission',
ModuleDirectiveContext())
require = RequireDirective('grok.require', ClassDirectiveContext())
+application = InterfaceOrClassDirective('grok.application',
+ ClassDirectiveContext())
+
Modified: grok/branches/faassen-index/src/grok/ftests/catalog/indexes.py
===================================================================
--- grok/branches/faassen-index/src/grok/ftests/catalog/indexes.py 2007-04-17 20:48:27 UTC (rev 74217)
+++ grok/branches/faassen-index/src/grok/ftests/catalog/indexes.py 2007-04-17 21:44:47 UTC (rev 74218)
@@ -7,7 +7,7 @@
Let's set up a site in which we manage a couple of objects::
- >>> from grok.ftests.catalog.indexes import Herd, Mammoth
+ >>> from grok.ftests.catalog.indexes import Herd, Herd2, Mammoth
>>> herd = Herd()
>>> getRootFolder()['herd'] = herd
>>> from zope.app.component.hooks import setSite
@@ -21,7 +21,7 @@
We are able to query the catalog::
>>> from zope.app.catalog.interfaces import ICatalog
- >>> from zope.component import getUtility
+ >>> from zope.component import getUtility, queryUtility
>>> catalog = getUtility(ICatalog)
>>> for obj in catalog.searchResults(name=('Beta', 'Beta')):
... print obj.name
@@ -39,8 +39,18 @@
['Alpha']
>>> sortedResults(catalog, message='bye')
['Beta']
+
+Note that another application that we did not register the
+indexes for won't have a catalog available::
+
+ >>> herd2 = Herd2()
+ >>> getRootFolder()['herd2'] = herd2
+ >>> setSite(herd2)
+ >>> queryUtility(ICatalog, default=None) is None
+ True
+ >>> setSite(herd)
-Nuke the catalog and initds in the end, so as not to confuse
+Nuke the catalog and intids in the end, so as not to confuse
other tests::
>>> sm = herd.getSiteManager()
@@ -62,6 +72,12 @@
import grok
from grok import index
+class Herd(grok.Container, grok.Application):
+ pass
+
+class Herd2(grok.Container, grok.Application):
+ pass
+
class IMammoth(Interface):
name = schema.TextLine(title=u'Name')
age = schema.Int(title=u'Age')
@@ -69,6 +85,7 @@
"""Message the mammoth has for the world."""
class MammothIndexes(grok.Indexes):
+ grok.application(Herd)
grok.context(IMammoth)
name = index.Field()
@@ -86,5 +103,3 @@
def message(self):
return self._message
-class Herd(grok.Container, grok.Application):
- pass
Added: grok/branches/faassen-index/src/grok/ftests/catalog/indexes_app_interface.py
===================================================================
--- grok/branches/faassen-index/src/grok/ftests/catalog/indexes_app_interface.py 2007-04-17 20:48:27 UTC (rev 74217)
+++ grok/branches/faassen-index/src/grok/ftests/catalog/indexes_app_interface.py 2007-04-17 21:44:47 UTC (rev 74218)
@@ -0,0 +1,100 @@
+"""
+Grok allows you to set up catalog indexes in your application with a
+special indexes declaration. Here we see how we can register indexes for
+an interface instead of an application directly.
+
+ >>> import grok
+ >>> grok.grok('grok.ftests.catalog.indexes_app_interface')
+
+Let's set up a site in which we manage a couple of objects::
+
+ >>> from grok.ftests.catalog.indexes_app_interface import Herd, Herd2, Mammoth
+ >>> herd = Herd()
+ >>> getRootFolder()['herd'] = herd
+ >>> from zope.app.component.hooks import setSite
+ >>> setSite(herd)
+
+We are able to find the catalog::
+
+ >>> from zope.app.catalog.interfaces import ICatalog
+ >>> from zope.component import getUtility
+ >>> catalog = getUtility(ICatalog)
+ >>> catalog is not None
+ True
+ >>> catalog.get('name') is not None
+ True
+
+Nuke the catalog and intids for this site, so as not to confuse
+other tests::
+
+ >>> 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
+
+Now let's create another application providing the same interface::
+
+ >>> herd2 = Herd2()
+ >>> getRootFolder()['herd2'] = herd2
+ >>> setSite(herd2)
+ >>> catalog = getUtility(ICatalog)
+ >>> catalog is not None
+ True
+ >>> catalog.get('name') is not None
+ True
+
+Nuke the catalog and intids in the end, so as not to confuse
+other tests::
+
+ >>> sm = herd2.getSiteManager()
+ >>> sm.unregisterUtility(catalog, provided=ICatalog)
+ True
+ >>> 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 IHerd(Interface):
+ pass
+
+class Herd(grok.Container, grok.Application):
+ grok.implements(IHerd)
+
+class Herd2(grok.Container, grok.Application):
+ grok.implements(IHerd)
+
+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.application(IHerd)
+ grok.context(IMammoth)
+
+ name = index.Field()
+ age = index.Field()
+ message = index.Text()
+
+class Mammoth(grok.Model):
+ grok.implements(IMammoth)
+
+ def __init__(self, name, age, message):
+ self.name = name
+ self.age = age
+ self._message = message
+
+ def message(self):
+ return self._message
Modified: grok/branches/faassen-index/src/grok/ftests/catalog/indexes_class.py
===================================================================
--- grok/branches/faassen-index/src/grok/ftests/catalog/indexes_class.py 2007-04-17 20:48:27 UTC (rev 74217)
+++ grok/branches/faassen-index/src/grok/ftests/catalog/indexes_class.py 2007-04-17 21:44:47 UTC (rev 74218)
@@ -8,7 +8,7 @@
Let's set up a site in which we manage a couple of objects::
- >>> from grok.ftests.catalog.indexes import Herd, Mammoth
+ >>> from grok.ftests.catalog.indexes_class import Herd, Mammoth
>>> herd = Herd()
>>> getRootFolder()['herd'] = herd
>>> from zope.app.component.hooks import setSite
@@ -41,7 +41,7 @@
>>> sortedResults(catalog, message='bye')
['Beta']
-Nuke the catalog and initds in the end, so as not to confuse
+Nuke the catalog and intids in the end, so as not to confuse
other tests::
>>> sm = herd.getSiteManager()
@@ -59,6 +59,9 @@
import grok
from grok import index
+class Herd(grok.Container, grok.Application):
+ pass
+
class Mammoth(grok.Model):
def __init__(self, name, age, message):
self.name = name
@@ -70,10 +73,9 @@
class MammothIndexes(grok.Indexes):
grok.context(Mammoth)
-
+ grok.application(Herd)
+
name = index.Field()
age = index.Field()
message = index.Text()
-class Herd(grok.Container, grok.Application):
- pass
Modified: grok/branches/faassen-index/src/grok/ftests/catalog/indexes_name.py
===================================================================
--- grok/branches/faassen-index/src/grok/ftests/catalog/indexes_name.py 2007-04-17 20:48:27 UTC (rev 74217)
+++ grok/branches/faassen-index/src/grok/ftests/catalog/indexes_name.py 2007-04-17 21:44:47 UTC (rev 74218)
@@ -8,7 +8,7 @@
Let's set up a site in which we manage a couple of objects::
- >>> from grok.ftests.catalog.indexes import Herd, Mammoth
+ >>> from grok.ftests.catalog.indexes_name import Herd, Mammoth
>>> herd = Herd()
>>> getRootFolder()['herd'] = herd
>>> from zope.app.component.hooks import setSite
@@ -23,7 +23,7 @@
>>> catalog
<zope.app.catalog.catalog.Catalog object at ...>
-Nuke the catalog and initds in the end, so as not to confuse
+Nuke the catalog and intids in the end, so as not to confuse
other tests::
>>> sm = herd.getSiteManager()
@@ -41,6 +41,9 @@
import grok
from grok import index
+class Herd(grok.Container, grok.Application):
+ pass
+
class Mammoth(grok.Model):
def __init__(self, name, age, message):
self.name = name
@@ -53,10 +56,8 @@
class MammothIndexes(grok.Indexes):
grok.context(Mammoth)
grok.name('foo_catalog')
+ grok.application(Herd)
name = index.Field()
age = index.Field()
message = index.Text()
-
-class Herd(grok.Container, grok.Application):
- pass
Added: grok/branches/faassen-index/src/grok/ftests/catalog/indexes_no_app.py
===================================================================
--- grok/branches/faassen-index/src/grok/ftests/catalog/indexes_no_app.py 2007-04-17 20:48:27 UTC (rev 74217)
+++ grok/branches/faassen-index/src/grok/ftests/catalog/indexes_no_app.py 2007-04-17 21:44:47 UTC (rev 74218)
@@ -0,0 +1,30 @@
+"""
+Grok allows you to set up catalog indexes in your application with a
+special indexes declaration. We do need to specify an application for
+the Indexes however, otherwise we get a GrokError:
+
+ >>> import grok
+ >>> grok.grok('grok.ftests.catalog.indexes_no_app')
+ Traceback (most recent call last):
+ ...
+ GrokError: No application specified for grok.Indexes subclass in module
+ <module 'grok.ftests.catalog.indexes_no_app' from ...>.
+ Use grok.application() to specify.
+
+"""
+import grok
+from grok import index
+
+class Herd(grok.Container, grok.Application):
+ pass
+
+class Mammoth(grok.Model):
+ pass
+
+class MammothIndexes(grok.Indexes):
+ grok.context(Mammoth)
+ grok.name('foo_catalog')
+
+ name = index.Field()
+ age = index.Field()
+ message = index.Text()
Modified: grok/branches/faassen-index/src/grok/meta.py
===================================================================
--- grok/branches/faassen-index/src/grok/meta.py 2007-04-17 20:48:27 UTC (rev 74217)
+++ grok/branches/faassen-index/src/grok/meta.py 2007-04-17 21:44:47 UTC (rev 74218)
@@ -496,6 +496,12 @@
component_class = components.IndexesClass
def register(self, context, name, factory, module_info, templates):
+ application = util.class_annotation(factory, 'grok.application', None)
+ if application is None:
+ raise GrokError("No application specified for grok.Indexes "
+ "subclass in module %r. "
+ "Use grok.application() to specify." % module_info.getModule(),
+ factory)
indexes = util.class_annotation(factory, 'grok.indexes', None)
if indexes is None:
return
@@ -503,7 +509,7 @@
catalog_name = util.class_annotation(factory, 'grok.name', u'')
zope.component.provideHandler(
IndexesSetupSubscriber(catalog_name, indexes, context),
- adapts=(grok.interfaces.IApplication,
+ adapts=(application,
grok.IObjectAddedEvent))
class IndexesSetupSubscriber(object):
More information about the Checkins
mailing list