/usr/share/pyshared/zope/viewlet/manager.py is in python-zope.viewlet 3.7.2-0ubuntu4.
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 | ##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################
"""Content Provider Manager implementation
$Id: manager.py 112059 2010-05-05 19:40:35Z tseaver $
"""
__docformat__ = 'restructuredtext'
import zope.component
import zope.interface
import zope.security
import zope.event
from zope.browserpage import ViewPageTemplateFile
from zope.viewlet import interfaces
from zope.location.interfaces import ILocation
from zope.contentprovider.interfaces import BeforeUpdateEvent
class ViewletManagerBase(object):
"""The Viewlet Manager Base
A generic manager class which can be instantiated
"""
zope.interface.implements(interfaces.IViewletManager)
template = None
def __init__(self, context, request, view):
self.__updated = False
self.__parent__ = view
self.context = context
self.request = request
def __getitem__(self, name):
"""See zope.interface.common.mapping.IReadMapping"""
# Find the viewlet
viewlet = zope.component.queryMultiAdapter(
(self.context, self.request, self.__parent__, self),
interfaces.IViewlet, name=name)
# If the viewlet was not found, then raise a lookup error
if viewlet is None:
raise zope.component.interfaces.ComponentLookupError(
'No provider with name `%s` found.' %name)
# If the viewlet cannot be accessed, then raise an
# unauthorized error
if not zope.security.canAccess(viewlet, 'render'):
raise zope.security.interfaces.Unauthorized(
'You are not authorized to access the provider '
'called `%s`.' %name)
# Return the viewlet.
return viewlet
def get(self, name, default=None):
"""See zope.interface.common.mapping.IReadMapping"""
try:
return self[name]
except (zope.component.interfaces.ComponentLookupError,
zope.security.interfaces.Unauthorized):
return default
def __contains__(self, name):
"""See zope.interface.common.mapping.IReadMapping"""
return bool(self.get(name, False))
def filter(self, viewlets):
"""Sort out all content providers
``viewlets`` is a list of tuples of the form (name, viewlet).
"""
# Only return viewlets accessible to the principal
return [(name, viewlet) for name, viewlet in viewlets
if zope.security.canAccess(viewlet, 'render')]
def sort(self, viewlets):
"""Sort the viewlets.
``viewlets`` is a list of tuples of the form (name, viewlet).
"""
# By default, use the standard Python way of doing sorting.
return sorted(viewlets, lambda x, y: cmp(x[1], y[1]))
def update(self):
"""See zope.contentprovider.interfaces.IContentProvider"""
self.__updated = True
# Find all content providers for the region
viewlets = zope.component.getAdapters(
(self.context, self.request, self.__parent__, self),
interfaces.IViewlet)
viewlets = self.filter(viewlets)
viewlets = self.sort(viewlets)
# Just use the viewlets from now on
self.viewlets=[]
for name, viewlet in viewlets:
if ILocation.providedBy(viewlet):
viewlet.__name__ = name
self.viewlets.append(viewlet)
self._updateViewlets()
def _updateViewlets(self):
"""Calls update on all viewlets and fires events"""
for viewlet in self.viewlets:
zope.event.notify(BeforeUpdateEvent(viewlet, self.request))
viewlet.update()
def render(self):
"""See zope.contentprovider.interfaces.IContentProvider"""
# Now render the view
if self.template:
return self.template(viewlets=self.viewlets)
else:
return u'\n'.join([viewlet.render() for viewlet in self.viewlets])
def ViewletManager(name, interface, template=None, bases=()):
attrDict = {'__name__' : name}
if template is not None:
attrDict['template'] = ViewPageTemplateFile(template)
if ViewletManagerBase not in bases:
# Make sure that we do not get a default viewlet manager mixin, if the
# provided base is already a full viewlet manager implementation.
if not (len(bases) == 1 and
interfaces.IViewletManager.implementedBy(bases[0])):
bases = bases + (ViewletManagerBase,)
ViewletManager = type(
'<ViewletManager providing %s>' % interface.getName(), bases, attrDict)
zope.interface.classImplements(ViewletManager, interface)
return ViewletManager
def getWeight((name, viewlet)):
try:
return int(viewlet.weight)
except AttributeError:
return 0
class WeightOrderedViewletManager(ViewletManagerBase):
"""Weight ordered viewlet managers."""
def sort(self, viewlets):
return sorted(viewlets, key=getWeight)
def render(self):
"""See zope.contentprovider.interfaces.IContentProvider"""
# do not render a manager template if no viewlets are avaiable
if not self.viewlets:
return u''
elif self.template:
return self.template(viewlets=self.viewlets)
else:
return u'\n'.join([viewlet.render() for viewlet in self.viewlets])
def isAvailable(viewlet):
try:
return zope.security.canAccess(viewlet, 'render') and viewlet.available
except AttributeError:
return True
class ConditionalViewletManager(WeightOrderedViewletManager):
"""Conditional weight ordered viewlet managers."""
def filter(self, viewlets):
"""Sort out all viewlets which are explicit not available
``viewlets`` is a list of tuples of the form (name, viewlet).
"""
return [(name, viewlet) for name, viewlet in viewlets
if isAvailable(viewlet)]
|