-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathzcml.py
458 lines (390 loc) · 14.7 KB
/
zcml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
##############################################################################
#
# Copyright (c) 2005 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Component Architecture configuration handlers
$Id$
"""
__docformat__ = "reStructuredText"
import zope.component
import zope.interface
import zope.configuration.fields
import zope.security.zcml
from zope.component.interface import provideInterface
from zope.proxy import ProxyBase, getProxiedObject
from zope.security.proxy import Proxy
from zope.security.checker import InterfaceChecker, CheckerPublic
from zope.security.adapter import LocatingTrustedAdapterFactory
from zope.security.adapter import LocatingUntrustedAdapterFactory
from zope.security.adapter import TrustedAdapterFactory
from zope.i18nmessageid import MessageFactory
_ = MessageFactory('zope')
PublicPermission = 'zope.Public'
def handler(methodName, *args, **kwargs):
method = getattr(zope.component.getGlobalSiteManager(), methodName)
method(*args, **kwargs)
class IBasicComponentInformation(zope.interface.Interface):
component = zope.configuration.fields.GlobalObject(
title=_("Component to use"),
description=_("Python name of the implementation object. This"
" must identify an object in a module using the"
" full dotted name. If specified, the"
" ``factory`` field must be left blank."),
required=False,
)
permission = zope.security.zcml.Permission(
title=_("Permission"),
description=_("Permission required to use this component."),
required=False,
)
factory = zope.configuration.fields.GlobalObject(
title=_("Factory"),
description=_("Python name of a factory which can create the"
" implementation object. This must identify an"
" object in a module using the full dotted name."
" If specified, the ``component`` field must"
" be left blank."),
required=False,
)
class IAdapterDirective(zope.interface.Interface):
"""
Register an adapter
"""
factory = zope.configuration.fields.Tokens(
title=_("Adapter factory/factories"),
description=_("A list of factories (usually just one) that create"
" the adapter instance."),
required=True,
value_type=zope.configuration.fields.GlobalObject()
)
provides = zope.configuration.fields.GlobalInterface(
title=_("Interface the component provides"),
description=_("This attribute specifies the interface the adapter"
" instance must provide."),
required=False,
)
for_ = zope.configuration.fields.Tokens(
title=_("Specifications to be adapted"),
description=_("This should be a list of interfaces or classes"),
required=False,
value_type=zope.configuration.fields.GlobalObject(
missing_value=object(),
),
)
permission = zope.security.zcml.Permission(
title=_("Permission"),
description=_("This adapter is only available, if the principal"
" has this permission."),
required=False,
)
name = zope.schema.TextLine(
title=_("Name"),
description=_("Adapters can have names.\n\n"
"This attribute allows you to specify the name for"
" this adapter."),
required=False,
)
trusted = zope.configuration.fields.Bool(
title=_("Trusted"),
description=_("""Make the adapter a trusted adapter
Trusted adapters have unfettered access to the objects they
adapt. If asked to adapt security-proxied objects, then,
rather than getting an unproxied adapter of security-proxied
objects, you get a security-proxied adapter of unproxied
objects.
"""),
required=False,
default=False,
)
locate = zope.configuration.fields.Bool(
title=_("Locate"),
description=_("""Make the adapter a locatable adapter
Located adapter should be used if a non-public permission
is used.
"""),
required=False,
default=False,
)
def _rolledUpFactory(factories):
# This has to be named 'factory', aparently, so as not to confuse
# apidoc :(
def factory(ob):
for f in factories:
ob = f(ob)
return ob
# Store the original factory for documentation
factory.factory = factories[0]
return factory
def _protectedFactory(original_factory, checker):
# This has to be named 'factory', aparently, so as not to confuse
# apidoc :(
def factory(*args):
ob = original_factory(*args)
try:
ob.__Security_checker__ = checker
except AttributeError:
ob = Proxy(ob, checker)
return ob
factory.factory = original_factory
return factory
def adapter(_context, factory, provides=None, for_=None, permission=None,
name='', trusted=False, locate=False):
if for_ is None:
if len(factory) == 1:
for_ = zope.component.adaptedBy(factory[0])
if for_ is None:
raise TypeError("No for attribute was provided and can't "
"determine what the factory adapts.")
for_ = tuple(for_)
if provides is None:
if len(factory) == 1:
p = list(zope.interface.implementedBy(factory[0]))
if len(p) == 1:
provides = p[0]
if provides is None:
raise TypeError("Missing 'provides' attribute")
# Generate a single factory from multiple factories:
factories = factory
if len(factories) == 1:
factory = factories[0]
elif len(factories) < 1:
raise ValueError("No factory specified")
elif len(factories) > 1 and len(for_) != 1:
raise ValueError("Can't use multiple factories and multiple for")
else:
factory = _rolledUpFactory(factories)
if permission is not None:
if permission == PublicPermission:
permission = CheckerPublic
checker = InterfaceChecker(provides, permission)
factory = _protectedFactory(factory, checker)
# invoke custom adapter factories
if locate or (permission is not None and permission is not CheckerPublic):
if trusted:
factory = LocatingTrustedAdapterFactory(factory)
else:
factory = LocatingUntrustedAdapterFactory(factory)
else:
if trusted:
factory = TrustedAdapterFactory(factory)
_context.action(
discriminator = ('adapter', for_, provides, name),
callable = handler,
args = ('registerAdapter',
factory, for_, provides, name, _context.info),
)
_context.action(
discriminator = None,
callable = provideInterface,
args = ('', provides)
)
if for_:
for iface in for_:
if iface is not None:
_context.action(
discriminator = None,
callable = provideInterface,
args = ('', iface)
)
class ISubscriberDirective(zope.interface.Interface):
"""
Register a subscriber
"""
factory = zope.configuration.fields.GlobalObject(
title=_("Subscriber factory"),
description=_("A factory used to create the subscriber instance."),
required=False,
)
handler = zope.configuration.fields.GlobalObject(
title=_("Handler"),
description=_("A callable object that handles events."),
required=False,
)
provides = zope.configuration.fields.GlobalInterface(
title=_("Interface the component provides"),
description=_("This attribute specifies the interface the adapter"
" instance must provide."),
required=False,
)
for_ = zope.configuration.fields.Tokens(
title=_("Interfaces or classes that this subscriber depends on"),
description=_("This should be a list of interfaces or classes"),
required=False,
value_type=zope.configuration.fields.GlobalObject(
missing_value = object(),
),
)
permission = zope.security.zcml.Permission(
title=_("Permission"),
description=_("This subscriber is only available, if the"
" principal has this permission."),
required=False,
)
trusted = zope.configuration.fields.Bool(
title=_("Trusted"),
description=_("""Make the subscriber a trusted subscriber
Trusted subscribers have unfettered access to the objects they
adapt. If asked to adapt security-proxied objects, then,
rather than getting an unproxied subscriber of security-proxied
objects, you get a security-proxied subscriber of unproxied
objects.
"""),
required=False,
default=False,
)
locate = zope.configuration.fields.Bool(
title=_("Locate"),
description=_("""Make the subscriber a locatable subscriber
Located subscribers should be used if a non-public permission
is used.
"""),
required=False,
default=False,
)
_handler = handler
def subscriber(_context, for_=None, factory=None, handler=None, provides=None,
permission=None, trusted=False, locate=False):
if factory is None:
if handler is None:
raise TypeError("No factory or handler provided")
if provides is not None:
raise TypeError("Cannot use handler with provides")
factory = handler
else:
if handler is not None:
raise TypeError("Cannot use handler with factory")
if provides is None:
raise TypeError(
"You must specify a provided interface when registering "
"a factory")
if for_ is None:
for_ = zope.component.adaptedBy(factory)
if for_ is None:
raise TypeError("No for attribute was provided and can't "
"determine what the factory (or handler) adapts.")
if permission is not None:
if permission == PublicPermission:
permission = CheckerPublic
checker = InterfaceChecker(provides, permission)
factory = _protectedFactory(factory, checker)
for_ = tuple(for_)
# invoke custom adapter factories
if locate or (permission is not None and permission is not CheckerPublic):
if trusted:
factory = LocatingTrustedAdapterFactory(factory)
else:
factory = LocatingUntrustedAdapterFactory(factory)
else:
if trusted:
factory = TrustedAdapterFactory(factory)
if handler is not None:
_context.action(
discriminator = None,
callable = _handler,
args = ('registerHandler',
handler, for_, u'', _context.info),
)
else:
_context.action(
discriminator = None,
callable = _handler,
args = ('registerSubscriptionAdapter',
factory, for_, provides, u'', _context.info),
)
if provides is not None:
_context.action(
discriminator = None,
callable = provideInterface,
args = ('', provides)
)
# For each interface, state that the adapter provides that interface.
for iface in for_:
if iface is not None:
_context.action(
discriminator = None,
callable = provideInterface,
args = ('', iface)
)
class IUtilityDirective(IBasicComponentInformation):
"""Register a utility."""
provides = zope.configuration.fields.GlobalInterface(
title=_("Provided interface"),
description=_("Interface provided by the utility."),
required=False,
)
name = zope.schema.TextLine(
title=_("Name"),
description=_("Name of the registration. This is used by"
" application code when locating a utility."),
required=False,
)
class PermissionProxy(ProxyBase):
__slots__ = ('__Security_checker__', )
def __providedBy__(self):
return zope.interface.providedBy(getProxiedObject(self))
__providedBy__ = property(__providedBy__)
def proxify(ob, checker):
"""Try to get the object proxied with the `checker`, but not too soon
We really don't want to proxy the object unless we need to.
"""
ob = PermissionProxy(ob)
ob.__Security_checker__ = checker
return ob
def utility(_context, provides=None, component=None, factory=None,
permission=None, name=''):
if factory:
if component:
raise TypeError("Can't specify factory and component.")
component = factory()
if provides is None:
provides = list(zope.interface.providedBy(component))
if len(provides) == 1:
provides = provides[0]
else:
raise TypeError("Missing 'provides' attribute")
if permission is not None:
if permission == PublicPermission:
permission = CheckerPublic
checker = InterfaceChecker(provides, permission)
component = proxify(component, checker)
_context.action(
discriminator = ('utility', provides, name),
callable = handler,
args = ('registerUtility', component, provides, name),
)
_context.action(
discriminator = None,
callable = provideInterface,
args = (provides.__module__ + '.' + provides.getName(), provides)
)
class IInterfaceDirective(zope.interface.Interface):
"""
Define an interface
"""
interface = zope.configuration.fields.GlobalInterface(
title=_("Interface"),
required=True,
)
type = zope.configuration.fields.GlobalInterface(
title=_("Interface type"),
required=False,
)
name = zope.schema.TextLine(
title=_("Name"),
required=False,
)
def interface(_context, interface, type=None, name=''):
_context.action(
discriminator = None,
callable = provideInterface,
args = (name, interface, type)
)