Skip to content

Commit

Permalink
Add Application initialization
Browse files Browse the repository at this point in the history
When splitting off this product from the main Zope repository, the
Application initialization has been removed. This code brings back the
automatically created site error log.
  • Loading branch information
rbu committed May 18, 2018
1 parent 567acee commit 00e0f7d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -21,7 +21,7 @@
'Acquisition',
'transaction',
'zExceptions',
'Zope2 >= 4.0.dev0',
'Zope2 >= 4.0b5',
'zope.component',
'zope.interface',
'zope.event',
Expand Down
21 changes: 21 additions & 0 deletions src/Products/SiteErrorLog/__init__.py
Expand Up @@ -14,8 +14,29 @@

from . import SiteErrorLog

def commit(note):
import transaction
transaction.get().note(note)
transaction.commit()

def install_errorlog(app):
if hasattr(app, 'error_log'):
return

error_log = SiteErrorLog.SiteErrorLog()
app._setObject('error_log', error_log)
# FIXME explicitly call manage_afterAdd, as sometimes
# events are initialized too late
error_log = app.error_log
error_log.manage_afterAdd(error_log, app)
commit(u'Added site error_log at /error_log')


def initialize(context):
context.registerClass(SiteErrorLog.SiteErrorLog,
constructors=(SiteErrorLog.manage_addErrorLog,),
permission=SiteErrorLog.use_error_logging)

app = context.getApplication() # new API added in Zope 4.0b5
if app is not None:
install_errorlog(app)
87 changes: 87 additions & 0 deletions src/Products/SiteErrorLog/tests/testInitialization.py
@@ -0,0 +1,87 @@
##############################################################################
#
# 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.
#
##############################################################################

import os
import shutil
import tempfile
import unittest

from App.config import getConfiguration, setConfiguration
from OFS.Application import Application, AppInitializer
from Zope2.Startup.options import ZopeWSGIOptions

TEMPNAME = tempfile.mktemp()
TEMPPRODUCTS = os.path.join(TEMPNAME, "Products")

good_cfg = """
instancehome <<INSTANCE_HOME>>
<zodb_db main>
mount-point /
<mappingstorage>
name mappingstorage
</mappingstorage>
</zodb_db>
"""

original_config = None


def getApp():
from App.ZApplication import ZApplicationWrapper
DB = getConfiguration().dbtab.getDatabase('/')
return ZApplicationWrapper(DB, 'Application', Application)()


class TestInitialization(unittest.TestCase):
""" Test the application initialization """

def setUp(self):
global original_config
if original_config is None:
original_config = getConfiguration()
os.makedirs(TEMPNAME)
os.makedirs(TEMPPRODUCTS)

def tearDown(self):
import App.config
App.config.setConfiguration(original_config)
shutil.rmtree(TEMPNAME)
import Products
Products.__path__ = [d for d in Products.__path__
if os.path.exists(d)]

def configure(self, text):
# We have to create a directory of our own since the existence
# of the directory is checked. This handles this in a
# platform-independent way.
config_path = os.path.join(TEMPNAME, 'zope.conf')
with open(config_path, 'w') as fd:
fd.write(text.replace(u"<<INSTANCE_HOME>>", TEMPNAME))

options = ZopeWSGIOptions(config_path)()
config = options.configroot
self.assertEqual(config.instancehome, TEMPNAME)
setConfiguration(config)

def getOne(self):
app = getApp()
return AppInitializer(app)

def test_install_session_data_manager(self):
self.configure(good_cfg)
i = self.getOne()
i.install_products()
app = i.getApp()
self.assertEqual(app.error_log.meta_type, 'Site Error Log')

0 comments on commit 00e0f7d

Please sign in to comment.