vendor/Five/1.2b-r20590

view metaconfigure.py @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2004, 2005 Zope Corporation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 """Generic Components ZCML Handlers
16 $Id: metaconfigure.py 12884 2005-05-30 13:10:41Z philikon $
17 """
18 from types import ModuleType
20 from zope.interface import classImplements
21 from zope.configuration.exceptions import ConfigurationError
23 from security import CheckerPublic
24 from security import protectName, initializeClass
26 class ContentDirective:
28 def __init__(self, _context, class_):
29 self.__class = class_
30 if isinstance(self.__class, ModuleType):
31 raise ConfigurationError('Content class attribute must be a class')
32 self.__context = _context
34 def implements(self, _context, interface):
35 for interface in interface:
36 _context.action(
37 discriminator = (
38 'five::directive:content', self.__class, object()),
39 callable = classImplements,
40 args = (self.__class, interface),
41 )
42 interface(_context, interface)
44 def require(self, _context, permission=None,
45 attributes=None, interface=None):
46 """Require a the permission to access a specific aspect"""
48 if not (interface or attributes):
49 raise ConfigurationError("Nothing required")
51 if interface:
52 for i in interface:
53 if i:
54 self.__protectByInterface(i, permission)
55 if attributes:
56 self.__protectNames(attributes, permission)
58 def allow(self, _context, attributes=None, interface=None):
59 """Like require, but with permission_id zope.Public"""
60 return self.require(_context, CheckerPublic, attributes, interface)
62 def __protectByInterface(self, interface, permission_id):
63 "Set a permission on names in an interface."
64 for n, d in interface.namesAndDescriptions(1):
65 self.__protectName(n, permission_id)
66 interface(self.__context, interface)
68 def __protectName(self, name, permission_id):
69 "Set a permission on a particular name."
70 self.__context.action(
71 discriminator = ('five:protectName', self.__class, name),
72 callable = protectName,
73 args = (self.__class, name, permission_id)
74 )
76 def __protectNames(self, names, permission_id):
77 "Set a permission on a bunch of names."
78 for name in names:
79 self.__protectName(name, permission_id)
81 def __call__(self):
82 "Handle empty/simple declaration."
83 return self.__context.action(
84 discriminator = ('five:initialize:class', self.__class),
85 callable = initializeClass,
86 args = (self.__class,)
87 )