1 | ## $Id: zcml.py 11999 2014-11-20 03:06:02Z uli $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
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. |
---|
8 | ## |
---|
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. |
---|
13 | ## |
---|
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 | ## |
---|
18 | import os |
---|
19 | from zope.component.zcml import handler |
---|
20 | from zope.configuration.exceptions import ConfigurationError |
---|
21 | from waeup.ikoba.interfaces import IDataCenterConfig, IPayPalConfig |
---|
22 | |
---|
23 | |
---|
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: |
---|
33 | ``xmlns:ikoba="http://namespaces.waeup.org/ikoba"`` |
---|
34 | |
---|
35 | - Then, after including waeup.ikoba: |
---|
36 | ``<ikoba:datacenter path="some/existing/file/path" />`` |
---|
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 | |
---|
42 | >>> from waeup.ikoba.interfaces import IDataCenterConfig |
---|
43 | >>> from zope.component import getUtility |
---|
44 | >>> getUtility(IDataCenterConfig) |
---|
45 | {'path': 'some/existing/file/path'} |
---|
46 | |
---|
47 | """ |
---|
48 | context.action( |
---|
49 | discriminator=('utility', IDataCenterConfig, ''), |
---|
50 | callable=handler, |
---|
51 | args=('registerUtility', |
---|
52 | {'path': path}, IDataCenterConfig, '') |
---|
53 | ) |
---|
54 | |
---|
55 | |
---|
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 | |
---|
82 | def paypal_conf(context, path): |
---|
83 | """Handler for ZCML paypalconf directive. |
---|
84 | |
---|
85 | This handler registers the `paypal_handler` above to perform the |
---|
86 | real configuration action. |
---|
87 | """ |
---|
88 | context.action( |
---|
89 | discriminator=('utility', IPayPalConfig, ''), |
---|
90 | callable=paypal_handler, |
---|
91 | args=(path, ) |
---|
92 | ) |
---|