-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.py
187 lines (149 loc) · 6.49 KB
/
bootstrap.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
##############################################################################
#
# Copyright (c) 2002, 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.
#
##############################################################################
"""Bootstrap code.
This module contains code to bootstrap a Zope3 instance. For example
it makes sure a root folder exists and creates one if necessary.
"""
import logging
import transaction
import warnings
import zope.component.interfaces
import zope.event
import zope.lifecycleevent
import zope.processlifetime
from zope.app.publication.zopepublication import ZopePublication
from zope.container.interfaces import INameChooser
from zope.security.management import getSecurityPolicy
from zope.security.simplepolicies import ParanoidSecurityPolicy
from zope.site import site
from zope.site.folder import rootFolder
from zope.traversing.api import traverse
_marker = object()
def ensureObject(root_folder, object_name, object_type, object_factory,
asObject=_marker):
"""Check that there's a basic object in the site
manager. If not, add one.
Return the name abdded, if we added an object, otherwise None.
"""
if asObject is not _marker:
warnings.warn("asObject argument is deprecated and will be "
"removed in Zope 3.6", DeprecationWarning, 2)
package = getSiteManagerDefault(root_folder)
valid_objects = [ name
for name in package
if object_type.providedBy(package[name]) ]
if valid_objects:
return None
name = object_name
obj = object_factory()
package[name] = obj
return obj
def ensureUtility(root_folder, interface, utility_type,
utility_factory, name='', asObject=_marker, **kw):
"""Add a utility to the top site manager
Returns the name added or ``None`` if nothing was added.
"""
if asObject is not _marker:
warnings.warn("asObject argument is deprecated and will be "
"removed in Zope 3.6", DeprecationWarning, 2)
sm = root_folder.getSiteManager()
utils = [reg for reg in sm.registeredUtilities()
if (reg.provided.isOrExtends(interface) and reg.name == name)]
if len(utils) == 0:
return addConfigureUtility(
root_folder, interface, utility_type, utility_factory,
name, asObject, **kw
)
else:
return None
def addConfigureUtility(
root_folder, interface, utility_type, utility_factory, name='',
asObject=_marker, **kw):
"""Add and configure a utility to the root folder."""
if asObject is not _marker:
warnings.warn("asObject argument is deprecated and will be "
"removed in Zope 3.6", DeprecationWarning, 2)
utility = addUtility(root_folder, utility_type, utility_factory, **kw)
root_folder.getSiteManager().registerUtility(utility, interface, name)
return utility
def addUtility(root_folder, utility_type, utility_factory,
asObject=_marker, **kw):
""" Add a Utility to the root folder's site manager.
The utility is added to the default package and activated.
"""
if asObject is not _marker:
warnings.warn("asObject argument is deprecated and will be "
"removed in Zope 3.6", DeprecationWarning, 2)
package = getSiteManagerDefault(root_folder)
chooser = INameChooser(package)
utility = utility_factory()
name = chooser.chooseName(utility_type, utility)
package[name] = utility
# the utility might have been location-proxied; we need the name
# information (__name__) so let's get it back again from the
# container
utility = package[name]
# Set additional attributes on the utility
for k, v in kw.items():
setattr(utility, k, v)
return utility
def getSiteManagerDefault(root_folder):
package = traverse(root_folder.getSiteManager(), 'default')
return package
def getInformationFromEvent(event):
""" Extracts information from the event
Return a tuple containing
- db
- connection open from the db
- root connection object
- the root_folder object
"""
db = event.database
connection = db.open()
root = connection.root()
root_folder = root.get(ZopePublication.root_name, None)
return db, connection, root, root_folder
######################################################################
######################################################################
def bootStrapSubscriber(event):
"""The actual subscriber to the bootstrap IDataBaseOpenedEvent
Boostrap a Zope3 instance given a database object This first checks if the
root folder exists and has a site manager. If it exists, nothing else
is changed. If no root folder exists, one is added.
"""
db, connection, root, root_folder = getInformationFromEvent(event)
if root_folder is None:
# ugh... we depend on the root folder implementation
root_folder = rootFolder()
zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(root_folder))
root[ZopePublication.root_name] = root_folder
if not zope.component.interfaces.ISite.providedBy(root_folder):
site_manager = site.LocalSiteManager(root_folder)
root_folder.setSiteManager(site_manager)
transaction.commit()
connection.close()
zope.event.notify(zope.processlifetime.DatabaseOpenedWithRoot(db))
########################################################################
########################################################################
def checkSecurityPolicy(event):
"""Warn if the configured security policy is ParanoidSecurityPolicy
Between Zope X3 3.0 and Zope 3.1, the security policy configuration
was refactored and now it needs to be included from site.zcml.
"""
if getSecurityPolicy() is ParanoidSecurityPolicy:
logging.getLogger('zope.app.appsetup').warning(
'Security policy is not configured.\n'
'Please make sure that securitypolicy.zcml is included'
' in site.zcml immediately\n'
'before principals.zcml')