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