[8057] | 1 | ## $Id: zcml.py 11999 2014-11-20 03:06:02Z uli $ |
---|
[7576] | 2 | ## |
---|
[8057] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[7576] | 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. |
---|
[8057] | 8 | ## |
---|
[7576] | 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. |
---|
[8057] | 13 | ## |
---|
[7576] | 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 | ## |
---|
[11999] | 18 | import os |
---|
[7576] | 19 | from zope.component.zcml import handler |
---|
[11999] | 20 | from zope.configuration.exceptions import ConfigurationError |
---|
[11992] | 21 | from waeup.ikoba.interfaces import IDataCenterConfig, IPayPalConfig |
---|
[7576] | 22 | |
---|
[11993] | 23 | |
---|
[7576] | 24 | def data_center_conf(context, path): |
---|
| 25 | """Handler for ZCML ``datacenter`` directive. |
---|
| 26 | |
---|
| 27 | Registers a global utility under IDataCenterConfig and containing |
---|
| 28 | a dictionary with (currently) only one entry: `path`. |
---|
| 29 | |
---|
| 30 | The directive can be put into site.zcml like this: |
---|
| 31 | |
---|
| 32 | - Add to the header: |
---|
[11949] | 33 | ``xmlns:ikoba="http://namespaces.waeup.org/ikoba"`` |
---|
[7576] | 34 | |
---|
[11949] | 35 | - Then, after including waeup.ikoba: |
---|
| 36 | ``<ikoba:datacenter path="some/existing/file/path" />`` |
---|
[7576] | 37 | |
---|
| 38 | In a running instance (where some directive like above was |
---|
| 39 | processed during startup), one can then ask for the |
---|
| 40 | IDataCenterConfig utility: |
---|
| 41 | |
---|
[11949] | 42 | >>> from waeup.ikoba.interfaces import IDataCenterConfig |
---|
[7576] | 43 | >>> from zope.component import getUtility |
---|
| 44 | >>> getUtility(IDataCenterConfig) |
---|
| 45 | {'path': 'some/existing/file/path'} |
---|
| 46 | |
---|
| 47 | """ |
---|
| 48 | context.action( |
---|
[11993] | 49 | discriminator=('utility', IDataCenterConfig, ''), |
---|
| 50 | callable=handler, |
---|
| 51 | args=('registerUtility', |
---|
| 52 | {'path': path}, IDataCenterConfig, '') |
---|
[7576] | 53 | ) |
---|
[11992] | 54 | |
---|
| 55 | |
---|
[11999] | 56 | def paypal_handler(path): |
---|
| 57 | """ZCML handler that registers paypal configuration. |
---|
| 58 | |
---|
| 59 | We expect paypal credentials written down in a config file. The |
---|
| 60 | path to this config file can be set with the ZCML `paypalconf` |
---|
| 61 | directive, which is handled here. |
---|
| 62 | |
---|
| 63 | This handler requires the given path to exist and to be a file. |
---|
| 64 | |
---|
| 65 | If the file exists and is readable, a dict with ``path`` set to |
---|
| 66 | the given path is registered as a global unnamed utility for the |
---|
| 67 | `IPayPalConfig` interface. |
---|
| 68 | |
---|
| 69 | We do not check more (syntax, etc. of a found config) as this is |
---|
| 70 | beyond the scope of ZCML configuration. ZCML is responsible for |
---|
| 71 | finding _some_ paypal configuration file, not for the values found |
---|
| 72 | (or not found) within this file. |
---|
| 73 | """ |
---|
| 74 | if not os.path.exists(path): |
---|
| 75 | raise ConfigurationError("No such file: %s" % path) |
---|
| 76 | if not os.path.isfile(path): |
---|
| 77 | raise ConfigurationError("Not a regular file: %s" % path) |
---|
| 78 | return handler( |
---|
| 79 | 'registerUtility', {'path': path}, IPayPalConfig, '') |
---|
| 80 | |
---|
| 81 | |
---|
[11992] | 82 | def paypal_conf(context, path): |
---|
| 83 | """Handler for ZCML paypalconf directive. |
---|
[11999] | 84 | |
---|
| 85 | This handler registers the `paypal_handler` above to perform the |
---|
| 86 | real configuration action. |
---|
[11992] | 87 | """ |
---|
| 88 | context.action( |
---|
[11993] | 89 | discriminator=('utility', IPayPalConfig, ''), |
---|
[11999] | 90 | callable=paypal_handler, |
---|
| 91 | args=(path, ) |
---|
[11992] | 92 | ) |
---|