/
metaconfigure.py
501 lines (423 loc) · 18.7 KB
/
metaconfigure.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
##############################################################################
#
# Copyright (c) 2004, 2005 Zope Foundation 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.
#
##############################################################################
"""Browser directives
Directives to emulate the 'http://namespaces.zope.org/browser'
namespace in ZCML known from zope.app.
"""
import os
from inspect import ismethod
from zope import component
from zope.interface import implements
from zope.interface import Interface
from zope.component.zcml import handler
from zope.component.interface import provideInterface
from zope.configuration.exceptions import ConfigurationError
from zope.publisher.interfaces import NotFound
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from zope.publisher.interfaces.browser import IBrowserPublisher
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.security.zcml import Permission
import zope.browserpage.metaconfigure
from zope.browserpage.metaconfigure import providesCallable
from zope.browserpage.metaconfigure import _handle_menu
from zope.browserpage.metaconfigure import _handle_for
from zope.browserpage.metadirectives import IViewDirective
from AccessControl.class_init import InitializeClass
from AccessControl.security import getSecurityInfo
from AccessControl.security import protectClass
from AccessControl.security import protectName
from AccessControl.security import CheckerPrivateId
from Products.Five.browser import BrowserView
from Products.Five.browser.resource import FileResourceFactory
from Products.Five.browser.resource import ImageResourceFactory
from Products.Five.browser.resource import PageTemplateResourceFactory
from Products.Five.browser.resource import DirectoryResourceFactory
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.Five.metaclass import makeClass
def page(_context, name, permission, for_,
layer=IDefaultBrowserLayer, template=None, class_=None,
allowed_interface=None, allowed_attributes=None,
attribute='__call__', menu=None, title=None,
):
name = str(name) # De-unicode
_handle_menu(_context, menu, title, [for_], name, permission)
if not (class_ or template):
raise ConfigurationError("Must specify a class or template")
if allowed_attributes is None:
allowed_attributes = []
if allowed_interface is not None:
for interface in allowed_interface:
allowed_attributes.extend(interface.names(all=True))
if attribute != '__call__':
if template:
raise ConfigurationError(
"Attribute and template cannot be used together.")
if not class_:
raise ConfigurationError(
"A class must be provided if attribute is used")
if template:
template = os.path.abspath(str(_context.path(template)))
if not os.path.isfile(template):
raise ConfigurationError("No such file", template)
if class_:
if attribute != '__call__':
if not hasattr(class_, attribute):
raise ConfigurationError(
"The provided class doesn't have the specified attribute "
)
cdict = getSecurityInfo(class_)
cdict['__name__'] = name
if template:
new_class = makeClassForTemplate(template, bases=(class_, ),
cdict=cdict, name=name)
elif attribute != "__call__":
# we're supposed to make a page for an attribute (read:
# method) and it's not __call__. We thus need to create a
# new class using our mixin for attributes.
cdict.update({'__page_attribute__': attribute})
new_class = makeClass(class_.__name__,
(class_, ViewMixinForAttributes),
cdict)
# in case the attribute does not provide a docstring,
# ZPublisher refuses to publish it. So, as a workaround,
# we provide a stub docstring
func = getattr(new_class, attribute)
if not func.__doc__:
# cannot test for MethodType/UnboundMethod here
# because of ExtensionClass
if hasattr(func, 'im_func'):
# you can only set a docstring on functions, not
# on method objects
func = func.im_func
func.__doc__ = "Stub docstring to make ZPublisher work"
else:
# we could use the class verbatim here, but we'll execute
# some security declarations on it so we really shouldn't
# modify the original. So, instead we make a new class
# with just one base class -- the original
new_class = makeClass(class_.__name__,
(class_, BrowserView), cdict)
else:
# template
new_class = makeClassForTemplate(template, name=name)
_handle_for(_context, for_)
_context.action(
discriminator = ('view', for_, name, IBrowserRequest, layer),
callable = handler,
args = ('registerAdapter',
new_class, (for_, layer), Interface, name, _context.info),
)
_context.action(
discriminator = ('five:protectClass', new_class),
callable = protectClass,
args = (new_class, permission)
)
if allowed_attributes:
for attr in allowed_attributes:
_context.action(
discriminator = ('five:protectName', new_class, attr),
callable = protectName,
args = (new_class, attr, permission)
)
# Make everything else private
allowed = [attribute] + (allowed_attributes or [])
private_attrs = [name for name in dir(new_class)
if (not name.startswith('_')) and
(name not in allowed) and
ismethod(getattr(new_class, name))]
for attr in private_attrs:
_context.action(
discriminator = ('five:protectName', new_class, attr),
callable = protectName,
args = (new_class, attr, CheckerPrivateId)
)
# Protect the class
_context.action(
discriminator = ('five:initialize:class', new_class),
callable = InitializeClass,
args = (new_class,)
)
class pages(zope.browserpage.metaconfigure.pages):
def page(self, _context, name, attribute='__call__', template=None,
menu=None, title=None):
return page(_context,
name=name,
attribute=attribute,
template=template,
menu=menu, title=title,
**(self.opts))
# view (named view with pages)
class IFiveViewDirective(IViewDirective):
permission = Permission(
title=u"Permission",
description=u"The permission needed to use the view.",
required=False,
)
class view(zope.browserpage.metaconfigure.view):
def __call__(self):
if 7 == len(self.args) and isinstance(self.args[2], tuple):
# zope.browserpage 4.0.0 changd the format of the args
# See https://github.com/zopefoundation/zope.browserpage/commit/b368364f0ce3aef058cc143b2a06572a4666b65b#diff-9b57f9ca40ff82d7998207d13fdd4846L219
(_context, name, (for_, layer), permission, class_,
allowed_interface, allowed_attributes) = self.args
else:
(_context, name, for_, permission, layer, class_,
allowed_interface, allowed_attributes) = self.args
name = str(name) # De-unicode
required = {}
cdict = {}
pages = {}
for pname, attribute, template in self.pages:
if template:
cdict[pname] = ViewPageTemplateFile(template)
if attribute and attribute != name:
cdict[attribute] = cdict[pname]
else:
if not hasattr(class_, attribute):
raise ConfigurationError("Undefined attribute",
attribute)
attribute = attribute or pname
required[pname] = permission
pages[pname] = attribute
# This should go away, but noone seems to remember what to do. :-(
if hasattr(class_, 'publishTraverse'):
def publishTraverse(self, request, name,
pages=pages, getattr=getattr):
if name in pages:
return getattr(self, pages[name])
view = component.queryMultiAdapter((self, request), name=name,
default=None)
if view is not None:
return view
m = class_.publishTraverse.__get__(self)
return m(request, name)
else:
def publishTraverse(self, request, name,
pages=pages, getattr=getattr):
if name in pages:
return getattr(self, pages[name])
view = component.queryMultiAdapter((self, request), name=name,
default=None)
if view is not None:
return view
raise NotFound(self, name, request)
cdict['publishTraverse'] = publishTraverse
if not hasattr(class_, 'browserDefault'):
if self.default or self.pages:
default = self.default or self.pages[0][0]
cdict['browserDefault'] = (
lambda self, request, default=default:
(self, (default, ))
)
elif providesCallable(class_):
cdict['browserDefault'] = (
lambda self, request: (self, ())
)
if class_ is not None:
bases = (class_, ViewMixinForTemplates)
else:
bases = (ViewMixinForTemplates,)
try:
cname = str(name)
except:
cname = "GeneratedClass"
cdict['__name__'] = name
newclass = makeClass(cname, bases, cdict)
_handle_for(_context, for_)
if self.provides is not None:
_context.action(
discriminator = None,
callable = provideInterface,
args = ('', self.provides)
)
_context.action(
discriminator = ('view', for_, name, IBrowserRequest, layer,
self.provides),
callable = handler,
args = ('registerAdapter',
newclass, (for_, layer), self.provides, name,
_context.info),
)
# Security
_context.action(
discriminator = ('five:protectClass', newclass),
callable = protectClass,
args = (newclass, permission)
)
if allowed_attributes:
for attr in allowed_attributes:
_context.action(
discriminator = ('five:protectName', newclass, attr),
callable = protectName,
args = (newclass, attr, permission)
)
# Make everything else private
allowed = allowed_attributes or []
private_attrs = [name for name in dir(newclass)
if (not name.startswith('_')) and
(name not in allowed) and
ismethod(getattr(newclass, name))]
for attr in private_attrs:
_context.action(
discriminator = ('five:protectName', newclass, attr),
callable = protectName,
args = (newclass, attr, CheckerPrivateId, False)
)
# Protect the class
_context.action(
discriminator = ('five:initialize:class', newclass),
callable = InitializeClass,
args = (newclass,)
)
_factory_map = {'image':{'prefix':'ImageResource',
'count':0,
'factory':ImageResourceFactory},
'file':{'prefix':'FileResource',
'count':0,
'factory':FileResourceFactory},
'template':{'prefix':'PageTemplateResource',
'count':0,
'factory':PageTemplateResourceFactory}
}
def resource(_context, name, layer=IDefaultBrowserLayer, permission='zope.Public',
file=None, image=None, template=None):
if ((file and image) or (file and template) or
(image and template) or not (file or image or template)):
raise ConfigurationError(
"Must use exactly one of file or image or template"
"attributes for resource directives"
)
res = file or image or template
res_type = ((file and 'file') or
(image and 'image') or
(template and 'template'))
factory_info = _factory_map.get(res_type)
factory_info['count'] += 1
res_factory = factory_info['factory']
class_name = '%s%s' % (factory_info['prefix'], factory_info['count'])
new_class = makeClass(class_name, (res_factory.resource,), {})
factory = res_factory(name, res, resource_factory=new_class)
_context.action(
discriminator = ('resource', name, IBrowserRequest, layer),
callable = handler,
args = ('registerAdapter',
factory, (layer,), Interface, name, _context.info),
)
_context.action(
discriminator = ('five:protectClass', new_class),
callable = protectClass,
args = (new_class, permission)
)
_context.action(
discriminator = ('five:initialize:class', new_class),
callable = InitializeClass,
args = (new_class,)
)
_rd_map = {ImageResourceFactory:{'prefix':'DirContainedImageResource',
'count':0},
FileResourceFactory:{'prefix':'DirContainedFileResource',
'count':0},
PageTemplateResourceFactory:{'prefix':'DirContainedPTResource',
'count':0},
DirectoryResourceFactory:{'prefix':'DirectoryResource',
'count':0}
}
def resourceDirectory(_context, name, directory, layer=IDefaultBrowserLayer,
permission='zope.Public'):
if not os.path.isdir(directory):
raise ConfigurationError(
"Directory %s does not exist" % directory
)
resource = DirectoryResourceFactory.resource
f_cache = {}
resource_factories = dict(resource.resource_factories)
resource_factories['default'] = resource.default_factory
for ext, factory in resource_factories.items():
if f_cache.get(factory) is not None:
continue
factory_info = _rd_map.get(factory)
factory_info['count'] += 1
class_name = '%s%s' % (factory_info['prefix'], factory_info['count'])
factory_name = '%s%s' % (factory.__name__, factory_info['count'])
f_resource = makeClass(class_name, (factory.resource,), {})
f_cache[factory] = makeClass(factory_name, (factory,),
{'resource':f_resource})
for ext, factory in resource_factories.items():
resource_factories[ext] = f_cache[factory]
default_factory = resource_factories['default']
del resource_factories['default']
cdict = {'resource_factories':resource_factories,
'default_factory':default_factory}
factory_info = _rd_map.get(DirectoryResourceFactory)
factory_info['count'] += 1
class_name = '%s%s' % (factory_info['prefix'], factory_info['count'])
dir_factory = makeClass(class_name, (resource,), cdict)
factory = DirectoryResourceFactory(name, directory,
resource_factory=dir_factory)
new_classes = [dir_factory,
] + [f.resource for f in f_cache.values()]
_context.action(
discriminator = ('resource', name, IBrowserRequest, layer),
callable = handler,
args = ('registerAdapter',
factory, (layer,), Interface, name, _context.info),
)
for new_class in new_classes:
_context.action(
discriminator = ('five:protectClass', new_class),
callable = protectClass,
args = (new_class, permission)
)
_context.action(
discriminator = ('five:initialize:class', new_class),
callable = InitializeClass,
args = (new_class,)
)
class ViewMixinForAttributes(BrowserView,
zope.browserpage.metaconfigure.simple):
# For some reason, the 'simple' baseclass doesn't implement this
# mandatory method (see https://bugs.launchpad.net/zope3/+bug/129296)
def browserDefault(self, request):
return getattr(self, self.__page_attribute__), ()
# __call__ should have the same signature as the original method
@property
def __call__(self):
return getattr(self, self.__page_attribute__)
class ViewMixinForTemplates(BrowserView):
# Cloned from zope.app.pagetemplate.simpleviewclass.simple
implements(IBrowserPublisher)
def browserDefault(self, request):
return self, ()
def publishTraverse(self, request, name):
if name == 'index.html':
return self.index
raise NotFound(self, name, request)
def __getitem__(self, name):
if name == 'macros':
return self.index.macros
return self.index.macros[name]
def __call__(self, *args, **kw):
return self.index(*args, **kw)
def makeClassForTemplate(filename, globals=None, used_for=None,
bases=(), cdict=None, name=u''):
# XXX needs to deal with security from the bases?
if cdict is None:
cdict = {}
cdict.update({'index': ViewPageTemplateFile(filename, globals),
'__name__': name})
bases += (ViewMixinForTemplates,)
class_ = makeClass("SimpleViewClass from %s" % filename, bases, cdict)
if used_for is not None:
class_.__used_for__ = used_for
return class_