/usr/share/pyshared/zope/browserresource/metaconfigure.py is in python-zope.browserresource 3.12.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 | ##############################################################################
#
# 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.
#
##############################################################################
"""ZCML directive handlers for browser resources
"""
import os
from zope.component import queryUtility
from zope.component.interface import provideInterface
from zope.component.zcml import handler
from zope.configuration.exceptions import ConfigurationError
from zope.interface import Interface, implements, classProvides
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.publisher.interfaces.browser import IDefaultBrowserLayer
from zope.security.checker import CheckerPublic, NamesChecker, Checker
from zope.security.proxy import Proxy
from zope.browserresource.directory import DirectoryResourceFactory
from zope.browserresource.file import File, FileResourceFactory
from zope.browserresource.i18nfile import I18nFileResourceFactory
from zope.browserresource.icon import IconViewFactory
from zope.browserresource.interfaces import IResourceFactory
from zope.browserresource.interfaces import IResourceFactoryFactory
allowed_names = ('GET', 'HEAD', 'publishTraverse', 'browserDefault',
'request', '__call__')
class ResourceFactoryWrapper(object):
implements(IResourceFactory)
classProvides(IResourceFactoryFactory)
def __init__(self, factory, checker, name):
self.__factory = factory
self.__checker = checker
self.__name = name
def __call__(self, request):
resource = self.__factory(request)
resource.__Security_checker__ = self.__checker
resource.__name__ = self.__name
return resource
def resource(_context, name, layer=IDefaultBrowserLayer,
permission='zope.Public', factory=None,
file=None, image=None, template=None):
if permission == 'zope.Public':
permission = CheckerPublic
checker = NamesChecker(allowed_names, permission)
if (factory and (file or image or template)) or \
(file and (factory or image or template)) or \
(image and (factory or file or template)) or \
(template and (factory or file or image)):
raise ConfigurationError(
"Must use exactly one of factory or file or image or template"
" attributes for resource directives"
)
if image or template:
import warnings
warnings.warn_explicit(
'The "template" and "image" attributes of resource '
'directive are deprecated in favor of pluggable '
'file resource factories based on file extensions. '
'Use the "file" attribute instead.',
DeprecationWarning,
_context.info.file, _context.info.line)
if image:
file = image
elif template:
file = template
_context.action(
discriminator = ('resource', name, IBrowserRequest, layer),
callable = resourceHandler,
args = (name, layer, checker, factory, file, _context.info),
)
def resourceHandler(name, layer, checker, factory, file, context_info):
if factory is not None:
factory = ResourceFactoryWrapper(factory, checker, name)
else:
ext = os.path.splitext(os.path.normcase(file))[1][1:]
factory_factory = queryUtility(IResourceFactoryFactory, ext,
FileResourceFactory)
factory = factory_factory(file, checker, name)
handler('registerAdapter', factory, (layer,), Interface, name, context_info)
def resourceDirectory(_context, name, directory, layer=IDefaultBrowserLayer,
permission='zope.Public'):
if permission == 'zope.Public':
permission = CheckerPublic
checker = NamesChecker(allowed_names + ('__getitem__', 'get'),
permission)
if not os.path.isdir(directory):
raise ConfigurationError(
"Directory %s does not exist" % directory
)
factory = DirectoryResourceFactory(directory, checker, name)
_context.action(
discriminator = ('resource', name, IBrowserRequest, layer),
callable = handler,
args = ('registerAdapter',
factory, (layer,), Interface, name, _context.info),
)
def icon(_context, name, for_, file=None, resource=None,
layer=IDefaultBrowserLayer, title=None,
width=16, height=16):
iname = for_.getName()
if title is None:
title = iname
if title.startswith('I'):
title = title[1:] # Remove leading 'I'
if file is not None and resource is not None:
raise ConfigurationError(
"Can't use more than one of file, and resource "
"attributes for icon directives"
)
elif file is not None:
resource = '-'.join(for_.__module__.split('.'))
resource = "%s-%s-%s" % (resource, iname, name)
ext = os.path.splitext(file)[1]
if ext:
resource += ext
# give this module another name, so we can use the "resource" directive
# in it that won't conflict with our local variable with the same name.
from zope.browserresource import metaconfigure
metaconfigure.resource(_context, file=file, name=resource, layer=layer)
elif resource is None:
raise ConfigurationError(
"At least one of the file, and resource "
"attributes for resource directives must be specified"
)
vfactory = IconViewFactory(resource, title, width, height)
_context.action(
discriminator = ('view', name, vfactory, layer),
callable = handler,
args = ('registerAdapter',
vfactory, (for_, layer), Interface, name, _context.info)
)
_context.action(
discriminator = None,
callable = provideInterface,
args = (for_.__module__+'.'+for_.getName(),
for_)
)
class I18nResource(object):
type = IBrowserRequest
default_allowed_attributes = '__call__'
def __init__(self, _context, name=None, defaultLanguage='en',
layer=IDefaultBrowserLayer, permission=None):
self._context = _context
self.name = name
self.defaultLanguage = defaultLanguage
self.layer = layer
self.permission = permission
self.__data = {}
def translation(self, _context, language, file=None, image=None):
if file is not None and image is not None:
raise ConfigurationError(
"Can't use more than one of file, and image "
"attributes for resource directives"
)
elif file is None and image is None:
raise ConfigurationError(
"At least one of the file, and image "
"attributes for resource directives must be specified"
)
if image is not None:
import warnings
warnings.warn_explicit(
'The "image" attribute of i18n-resource directive is '
'deprecated in favor of simple files. Use the "file" '
'attribute instead.',
DeprecationWarning,
_context.info.file, _context.info.line)
file = image
self.__data[language] = File(_context.path(file), self.name)
def __call__(self, require = None):
if self.name is None:
return
if self.defaultLanguage not in self.__data:
raise ConfigurationError(
"A translation for the default language (%s) "
"must be specified" % self.defaultLanguage
)
permission = self.permission
factory = I18nFileResourceFactory(self.__data, self.defaultLanguage)
if permission:
if require is None:
require = {}
if permission == 'zope.Public':
permission = CheckerPublic
if require:
checker = Checker(require)
factory = self._proxyFactory(factory, checker)
self._context.action(
discriminator = ('i18n-resource', self.name, self.type, self.layer),
callable = handler,
args = ('registerAdapter',
factory, (self.layer,), Interface, self.name,
self._context.info)
)
def _proxyFactory(self, factory, checker):
def proxyView(request,
factory=factory, checker=checker):
resource = factory(request)
# We need this in case the resource gets unwrapped and
# needs to be rewrapped
resource.__Security_checker__ = checker
return Proxy(resource, checker)
return proxyView
|