/usr/share/pyshared/zope/browserpage/metaconfigure.py is in python-zope.browserpage 4.0.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ##############################################################################
#
# Copyright (c) 2001, 2002 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 page ZCML configuration code
"""
import os
from zope.component import queryMultiAdapter
from zope.component.interface import provideInterface
from zope.component.zcml import handler
from zope.interface import implementer, classImplements, Interface
from zope.publisher.interfaces import NotFound
from zope.security.checker import CheckerPublic, Checker, defineChecker
from zope.configuration.exceptions import ConfigurationError
from zope.pagetemplate.engine import Engine
from zope.pagetemplate.engine import _Engine
from zope.pagetemplate.engine import TrustedEngine
from zope.pagetemplate.engine import _TrustedEngine
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from zope.publisher.interfaces.browser import IBrowserPublisher
from zope.publisher.browser import BrowserView
from zope.browserpage.simpleviewclass import SimpleViewClass
from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile
try:
from zope.browsermenu.metaconfigure import menuItemDirective
except ImportError:
menuItemDirective = None
# There are three cases we want to suport:
#
# Named view without pages (single-page view)
#
# <browser:page
# for=".IContact.IContactInfo."
# name="info.html"
# template="info.pt"
# class=".ContactInfoView."
# permission="zope.View"
# />
#
# Unnamed view with pages (multi-page view)
#
# <browser:pages
# for=".IContact."
# class=".ContactEditView."
# permission="ZopeProducts.Contact.ManageContacts"
# >
#
# <browser:page name="edit.html" template="edit.pt" />
# <browser:page name="editAction.html" attribute="action" />
# </browser:pages>
#
# Named view with pages (add view is a special case of this)
#
# <browser:view
# name="ZopeProducts.Contact"
# for="Zope.App.OFS.Container.IAdding."
# class=".ContactAddView."
# permission="ZopeProducts.Contact.ManageContacts"
# >
#
# <browser:page name="add.html" template="add.pt" />
# <browser:page name="action.html" attribute="action" />
# </browser:view>
# We'll also provide a convenience directive for add views:
#
# <browser:add
# name="ZopeProducts.Contact"
# class=".ContactAddView."
# permission="ZopeProducts.Contact.ManageContacts"
# >
#
# <browser:page name="add.html" template="add.pt" />
# <browser:page name="action.html" attribute="action" />
# </browser:view>
# page
def page(_context, name, permission, for_=Interface,
layer=IDefaultBrowserLayer, template=None, class_=None,
allowed_interface=None, allowed_attributes=None,
attribute='__call__', menu=None, title=None,
):
_handle_menu(_context, menu, title, [for_], name, permission, layer)
required = {}
permission = _handle_permission(_context, permission)
if not (class_ or template):
raise ConfigurationError("Must specify a class or template")
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)
required['__getitem__'] = permission
# TODO: new __name__ attribute must be tested
if class_:
if attribute != '__call__':
if not hasattr(class_, attribute):
raise ConfigurationError(
"The provided class doesn't have the specified attribute "
)
if template:
# class and template
new_class = SimpleViewClass(template, bases=(class_, ), name=name)
else:
if not hasattr(class_, 'browserDefault'):
cdict = {
'browserDefault':
lambda self, request: (getattr(self, attribute), ())
}
else:
cdict = {}
cdict['__name__'] = name
cdict['__page_attribute__'] = attribute
new_class = type(class_.__name__, (class_, simple,), cdict)
if hasattr(class_, '__implements__'):
classImplements(new_class, IBrowserPublisher)
else:
# template
new_class = SimpleViewClass(template, name=name)
for n in (attribute, 'browserDefault', '__call__', 'publishTraverse'):
required[n] = permission
_handle_allowed_interface(_context, allowed_interface, permission,
required)
_handle_allowed_attributes(_context, allowed_attributes, permission,
required)
_handle_for(_context, for_)
defineChecker(new_class, Checker(required))
_context.action(
discriminator = ('view', (for_, layer), name, IBrowserRequest),
callable = handler,
args = ('registerAdapter',
new_class, (for_, layer), Interface, name, _context.info),
)
# pages, which are just a short-hand for multiple page directives.
# Note that a class might want to access one of the defined
# templates. If it does though, it should use getMultiAdapter.
class pages(object):
def __init__(self, _context, permission, for_=Interface,
layer=IDefaultBrowserLayer, class_=None,
allowed_interface=None, allowed_attributes=None,
):
self.opts = dict(for_=for_, permission=permission,
layer=layer, class_=class_,
allowed_interface=allowed_interface,
allowed_attributes=allowed_attributes,
)
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))
def __call__(self):
return ()
# view (named view with pages)
# This is a different case. We actually build a class with attributes
# for all of the given pages.
class view(object):
default = None
def __init__(self, _context, permission, for_=Interface,
name='', layer=IDefaultBrowserLayer, class_=None,
allowed_interface=None, allowed_attributes=None,
menu=None, title=None, provides=Interface,
):
_handle_menu(_context, menu, title, [for_], name, permission, layer)
permission = _handle_permission(_context, permission)
self.args = (_context, name, (for_, layer), permission, class_,
allowed_interface, allowed_attributes)
self.pages = []
self.menu = menu
self.provides = provides
def page(self, _context, name, attribute=None, template=None):
if template:
template = os.path.abspath(_context.path(template))
if not os.path.isfile(template):
raise ConfigurationError("No such file", template)
else:
if not attribute:
raise ConfigurationError(
"Must specify either a template or an attribute name")
self.pages.append((name, attribute, template))
return ()
def defaultPage(self, _context, name):
self.default = name
return ()
def __call__(self):
(_context, name, (for_, layer), permission, class_,
allowed_interface, allowed_attributes) = self.args
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 = queryMultiAdapter((self, request), name=name)
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 = queryMultiAdapter((self, request), name=name)
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_, simple)
else:
bases = (simple,)
try:
cname = str(name)
except:
cname = "GeneratedClass"
cdict['__name__'] = name
newclass = type(cname, bases, cdict)
for n in ('publishTraverse', 'browserDefault', '__call__'):
required[n] = permission
_handle_allowed_interface(_context, allowed_interface, permission,
required)
_handle_allowed_attributes(_context, allowed_attributes, permission,
required)
_handle_for(_context, for_)
defineChecker(newclass, Checker(required))
if self.provides is not None:
_context.action(
discriminator = None,
callable = provideInterface,
args = ('', self.provides)
)
_context.action(
discriminator = ('view', (for_, layer), name, self.provides),
callable = handler,
args = ('registerAdapter',
newclass, (for_, layer), self.provides, name,
_context.info),
)
def _handle_menu(_context, menu, title, for_, name, permission,
layer=IDefaultBrowserLayer):
if menu or title:
if not (menu and title):
raise ConfigurationError(
"If either menu or title are specified, they must "
"both be specified.")
if len(for_) != 1:
raise ConfigurationError(
"Menus can be specified only for single-view, not for "
"multi-views.")
if menuItemDirective is None:
import warnings
warnings.warn_explicit(
'Page directive used with "menu" argument, while "zope.browsermenu" '
'package is not installed. Doing nothing.',
UserWarning,
_context.info.file, _context.info.line)
return []
return menuItemDirective(
_context, menu, for_[0], '@@' + name, title,
permission=permission, layer=layer)
return []
def _handle_permission(_context, permission):
if permission == 'zope.Public':
permission = CheckerPublic
return permission
def _handle_allowed_interface(_context, allowed_interface, permission,
required):
# Allow access for all names defined by named interfaces
if allowed_interface:
for i in allowed_interface:
_context.action(
discriminator = None,
callable = provideInterface,
args = (None, i)
)
for name in i:
required[name] = permission
def _handle_allowed_attributes(_context, allowed_attributes, permission,
required):
# Allow access for all named attributes
if allowed_attributes:
for name in allowed_attributes:
required[name] = permission
def _handle_for(_context, for_):
if for_ is not None:
_context.action(
discriminator = None,
callable = provideInterface,
args = ('', for_)
)
@implementer(IBrowserPublisher)
class simple(BrowserView):
def publishTraverse(self, request, name):
raise NotFound(self, name, request)
def __call__(self, *a, **k):
# If a class doesn't provide it's own call, then get the attribute
# given by the browser default.
attr = self.__page_attribute__
if attr == '__call__':
raise AttributeError("__call__")
meth = getattr(self, attr)
return meth(*a, **k)
def providesCallable(class_):
if hasattr(class_, '__call__'):
for c in class_.__mro__:
if '__call__' in c.__dict__:
return True
return False
def expressiontype(_context, name, handler):
_context.action(
discriminator = ("tales:expressiontype", name),
callable = registerType,
args = (name, handler)
)
def registerType(name, handler):
Engine.registerType(name, handler)
TrustedEngine.registerType(name, handler)
def clear():
Engine.__init__()
_Engine(Engine)
TrustedEngine.__init__()
_TrustedEngine(TrustedEngine)
try:
from zope.testing.cleanup import addCleanUp
except ImportError:
pass
else:
addCleanUp(clear)
|