/
registration.py
268 lines (209 loc) · 9.28 KB
/
registration.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
##############################################################################
#
# Copyright (c) 2002 Zope Corporation 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.
#
##############################################################################
"""Component registration support
$Id$
"""
from persistent import Persistent
import zope.event
from zope.component import subscribers
from zope.interface import implements
from zope.security.checker import InterfaceChecker, CheckerPublic
from zope.security.proxy import Proxy, removeSecurityProxy
from zope.app import zapi
from zope.app.component.interfaces import registration as interfaces
from zope.app.container.btree import BTreeContainer
from zope.app.container.contained import Contained
from zope.app.dependable.interfaces import IDependable, DependencyError
from zope.app.event import objectevent
from zope.app.location import inside
from zope.app.traversing.interfaces import TraversalError
# BBB: First introduced in 3.1; should go away in 3.3
import bbb
class RegistrationEvent(objectevent.ObjectEvent):
"""An event that is created when a registration-related activity occured."""
implements(interfaces.IRegistrationEvent)
class RegistrationActivatedEvent(RegistrationEvent):
"""An event that is created when a registration is activated."""
implements(interfaces.IRegistrationActivatedEvent)
class RegistrationDeactivatedEvent(RegistrationEvent):
"""An event that is created when a registration is deactivated."""
implements(interfaces.IRegistrationDeactivatedEvent)
class RegistrationStatusProperty(object):
"""A descriptor used to implement `IRegistration`'s `status` property."""
def __get__(self, inst, klass):
registration = inst
if registration is None:
return self
registry = registration.getRegistry()
if registry and registry.registered(registration):
return interfaces.ActiveStatus
return interfaces.InactiveStatus
def __set__(self, inst, value):
registration = inst
registry = registration.getRegistry()
if registry is None:
raise ValueError, 'No registry found.'
if value == interfaces.ActiveStatus:
if not registry.registered(registration):
registry.register(registration)
zope.event.notify(RegistrationActivatedEvent(registration))
elif value == interfaces.InactiveStatus:
if registry.registered(registration):
registry.unregister(registration)
zope.event.notify(RegistrationDeactivatedEvent(registration))
else:
raise ValueError, value
class SimpleRegistration(Persistent, Contained):
"""Registration objects that just contain registration data"""
implements(interfaces.IRegistration,
interfaces.IRegistrationManagerContained)
# See interfaces.IRegistration
status = RegistrationStatusProperty()
def getRegistry(self):
"""See interfaces.IRegistration"""
raise NotImplementedError, \
'This method must be implemented by each specific regstration.'
class ComponentRegistration(bbb.registration.BBBComponentRegistration,
SimpleRegistration):
"""Component registration.
Subclasses should define a getInterface() method returning the interface
of the component.
"""
implements(interfaces.IComponentRegistration)
def __init__(self, component, permission=None):
# BBB: Will go away in 3.3.
super(ComponentRegistration, self).__init__(component, permission)
# self.component = component
if permission == 'zope.Public':
permission = CheckerPublic
self.permission = permission
def _getComponent(self):
if self.permission and self.interface:
checker = InterfaceChecker(self.interface, self.permission)
return Proxy(self._component, checker)
return self._component
def _setComponent(self, component):
# We always want to set the plain component. Untrusted code will
# get back a proxied component anyways.
self._component = removeSecurityProxy(component)
# See zope.app.component.interfaces.registration.IComponentRegistration
component = property(_getComponent, _setComponent)
# See zope.app.component.interfaces.registration.IComponentRegistration
interface = None
def SimpleRegistrationRemoveSubscriber(registration, event):
"""Receive notification of remove event."""
sm = zapi.getSiteManager(registration)
removed = event.object
if (sm == removed) or inside(sm, removed):
# we don't really care if the registration is active, since the site
# is going away.
return
objectstatus = registration.status
if objectstatus == interfaces.ActiveStatus:
try:
objectpath = zapi.getPath(registration)
except: # XXX
objectpath = str(registration)
raise DependencyError("Can't delete active registration (%s)"
% objectpath)
def ComponentRegistrationRemoveSubscriber(componentRegistration, event):
"""Receive notification of remove event."""
component = componentRegistration.component
try:
dependents = IDependable(component)
except TypeError:
return
objectpath = zapi.getPath(componentRegistration)
dependents.removeDependent(objectpath)
def ComponentRegistrationAddSubscriber(componentRegistration, event):
"""Receive notification of add event."""
component = componentRegistration.component
try:
dependents = IDependable(component)
except TypeError:
return
objectpath = zapi.getPath(componentRegistration)
dependents.addDependent(objectpath)
def componentRegistrationEventNotify(componentReg, event):
"""Subscriber to dispatch registration events for components."""
adapters = subscribers((componentReg.component, event), None)
for adapter in adapters:
pass # getting them does the work
def RegisterableMoveSubscriber(registerable, event):
"""A registerable cannot be moved as long as it has registrations in the
registration manager."""
if event.oldParent is not None and event.newParent is not None:
if event.oldParent is not event.newParent:
raise DependencyError(
"Can't move a registered component from its container.")
class Registered(bbb.registration.BBBRegistered, object):
"""An adapter from IRegisterable to IRegistered.
This class is the only place that knows how 'Registered'
data is represented.
"""
implements(interfaces.IRegistered)
__used_for__ = interfaces.IRegisterable
def __init__(self, registerable):
self.registerable = registerable
def registrations(self):
rm = zapi.getParent(self.registerable).registrationManager
return [reg for reg in rm.values()
if (interfaces.IComponentRegistration.providedBy(reg) and
reg.component is self.registerable)]
class RegistrationManager(bbb.registration.BBBRegistrationManager,
BTreeContainer):
"""Registration manager
Manages registrations within a package.
"""
implements(interfaces.IRegistrationManager)
def addRegistration(self, reg):
"See IWriteContainer"
key = self._chooseName('', reg)
self[key] = reg
return key
def _chooseName(self, name, reg):
"""Choose a name for the registration."""
if not name:
name = reg.__class__.__name__
i = 1
chosenName = name
while chosenName in self:
i += 1
chosenName = name + str(i)
return chosenName
class RegisterableContainer(bbb.registration.BBBRegisterableContainer):
"""Mix-in to implement `IRegisterableContainer`"""
implements(interfaces.IRegisterableContainer,
interfaces.IRegisterableContainerContaining)
def __init__(self):
super(RegisterableContainer, self).__init__()
self.__createRegistrationManager()
def __createRegistrationManager(self):
"Create a registration manager and store it as `registrationManager`"
# See interfaces.IRegisterableContainer
self.registrationManager = RegistrationManager()
self.registrationManager.__parent__ = self
self.registrationManager.__name__ = '++registrations++'
zope.event.notify(
objectevent.ObjectCreatedEvent(self.registrationManager))
class RegistrationManagerNamespace(object):
"""Used to traverse to a Registration Manager from a
Registerable Container."""
__used_for__ = interfaces.IRegisterableContainer
def __init__(self, ob, request=None):
self.context = ob.registrationManager
def traverse(self, name, ignore):
if name == '':
return self.context
raise TraversalError(self.context, name)