[7193] | 1 | ## $Id: meta.py 7819 2012-03-08 22:28:46Z henrik $ |
---|
[6138] | 2 | ## |
---|
[7193] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[6138] | 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
[7193] | 8 | ## |
---|
[6138] | 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
[7193] | 13 | ## |
---|
[6138] | 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[7819] | 18 | """Grokkers for Kofa components. |
---|
[6138] | 19 | |
---|
| 20 | Stuff in here is mainly taken from grok.meta, with some modifications |
---|
| 21 | to provide a possibility to update catalogs in a running site. This is |
---|
| 22 | to solve the very common problem of already running sites for which |
---|
| 23 | the software changes and adds catalogs. These catalogs will not be |
---|
| 24 | automatically added in a running site. |
---|
| 25 | |
---|
| 26 | With the following Grokker and the upgrade helper class we can fire |
---|
| 27 | IObjectUpgradedEvents for any existing site and all catalogs |
---|
| 28 | registered will be really created. |
---|
| 29 | |
---|
| 30 | Please note that yet these catalogs are not filled automatically! That |
---|
| 31 | means the catalogs will exist but are empty in the beginning. |
---|
| 32 | |
---|
| 33 | We might solve this second problem by firing extra events like some |
---|
| 34 | ICatalogUpdatedEvent (which still do not exist) or similar. |
---|
| 35 | """ |
---|
| 36 | import grok |
---|
| 37 | import martian |
---|
| 38 | from grok import components |
---|
| 39 | from grok.meta import IndexesSetupSubscriber |
---|
| 40 | from martian.error import GrokError |
---|
| 41 | from zope import component |
---|
[7811] | 42 | from waeup.kofa.interfaces import IObjectUpgradeEvent |
---|
[6138] | 43 | |
---|
| 44 | class IndexesGrokker(martian.InstanceGrokker): |
---|
| 45 | """Grokker for Grok index bundles.""" |
---|
| 46 | martian.component(components.IndexesClass) |
---|
| 47 | |
---|
| 48 | def grok(self, name, factory, module_info, config, **kw): |
---|
| 49 | site = grok.site.bind().get(factory) |
---|
| 50 | context = grok.context.bind().get(factory, module_info.getModule()) |
---|
| 51 | catalog_name = grok.name.bind().get(factory) |
---|
| 52 | |
---|
| 53 | if site is None: |
---|
| 54 | raise GrokError("No site specified for grok.Indexes " |
---|
| 55 | "subclass in module %r. " |
---|
| 56 | "Use grok.site() to specify." |
---|
| 57 | % module_info.getModule(), |
---|
| 58 | factory) |
---|
| 59 | indexes = getattr(factory, '__grok_indexes__', None) |
---|
| 60 | if indexes is None: |
---|
| 61 | return False |
---|
| 62 | |
---|
[7819] | 63 | subscriber = KofaIndexesUpgradeSubscriber( |
---|
[6138] | 64 | catalog_name, indexes, context, module_info) |
---|
| 65 | subscribed = (site, IObjectUpgradeEvent) |
---|
| 66 | config.action( |
---|
| 67 | discriminator=None, |
---|
| 68 | callable=component.provideHandler, |
---|
| 69 | args=(subscriber, subscribed), |
---|
| 70 | ) |
---|
| 71 | return True |
---|
| 72 | |
---|
| 73 | |
---|
[7819] | 74 | class KofaIndexesUpgradeSubscriber(IndexesSetupSubscriber): |
---|
[6138] | 75 | """Helper that sets up indexes when their Grok site is upgraded. |
---|
| 76 | |
---|
| 77 | Each `grok.Indexes` class serves as an assertion that, whenever an |
---|
| 78 | instance of its `grok.site()` is upgraded, the given list of |
---|
| 79 | indexes should be generated if not already created as well. But a |
---|
| 80 | long period of time could elapse between when the application |
---|
| 81 | starts (and its indexes are grokked), and the moment, maybe days |
---|
| 82 | or weeks later, when a new instance of that `grok.Site` is |
---|
| 83 | created. Hence this `IndexesSetupSubscriber`: it can be |
---|
| 84 | instantiated at grokking time with the index information, and then |
---|
| 85 | registered with the Component Architecture as an event that should |
---|
| 86 | be fired later, whenever the right kind of `grok.Site` is |
---|
| 87 | instantiated. At that point its `__call__` method is kicked off |
---|
| 88 | and it makes sure the index catalogs get created properly. |
---|
| 89 | |
---|
| 90 | """ |
---|
| 91 | def __call__(self, site, event): |
---|
| 92 | site.logger.info('Create catalog `%s` if not installed yet.' % ( |
---|
| 93 | self.catalog_name,)) |
---|
| 94 | # make sure we have an intids |
---|
| 95 | self._createIntIds(site) |
---|
| 96 | # get the catalog |
---|
| 97 | catalog = self._createCatalog(site) |
---|
| 98 | # now install indexes |
---|
| 99 | for name, index in self.indexes.items(): |
---|
| 100 | try: |
---|
[7653] | 101 | site.logger.info('Create index `%s` in catalog.' % name) |
---|
[6138] | 102 | index.setup(catalog, name, self.context, self.module_info) |
---|
[7653] | 103 | site.logger.info('Index created.') |
---|
[6138] | 104 | except KeyError: #, DuplicationError: |
---|
[7653] | 105 | site.logger.info('Index `%s` already in catalog.' % name) |
---|
[6138] | 106 | pass |
---|
| 107 | site.logger.info('Catalog `%s` ready.' % self.catalog_name) |
---|