Commit cea2d056 authored by Hanno Schlichting's avatar Hanno Schlichting

Simplified `ZPublisher.WSGIPublisher.get_module_info` contract.

parent f86588ef
...@@ -31,6 +31,8 @@ Features Added ...@@ -31,6 +31,8 @@ Features Added
Restructuring Restructuring
+++++++++++++ +++++++++++++
- Simplified `ZPublisher.WSGIPublisher.get_module_info` contract.
- Add new `ZPublisher.utils.recordMetaData` function and use default - Add new `ZPublisher.utils.recordMetaData` function and use default
`transaction.manager` as the transaction manager. `transaction.manager` as the transaction manager.
......
...@@ -100,27 +100,10 @@ def get_module_info(module_name='Zope2'): ...@@ -100,27 +100,10 @@ def get_module_info(module_name='Zope2'):
return info return info
with _MODULE_LOCK: with _MODULE_LOCK:
g = globals() module = __import__(module_name)
module = __import__(module_name, g, g, ('__doc__',))
# Let the app specify a realm
realm = module_name
if _DEFAULT_REALM is not None:
realm = _DEFAULT_REALM
app = getattr(module, 'bobo_application', module) app = getattr(module, 'bobo_application', module)
bobo_before = getattr(module, '__bobo_before__', None) realm = _DEFAULT_REALM if _DEFAULT_REALM is not None else module_name
bobo_after = getattr(module, '__bobo_after__', None) _MODULES[module_name] = info = (app, realm, _DEFAULT_DEBUG_MODE)
error_hook = getattr(module, 'zpublisher_exception_hook', None)
validated_hook = getattr(
module, 'zpublisher_validated_hook', validate_user)
transactions_manager = getattr(
module, 'zpublisher_transactions_manager', transaction.manager)
info = (bobo_before, bobo_after, app, realm, _DEFAULT_DEBUG_MODE,
error_hook, validated_hook, transactions_manager)
_MODULES[module_name] = info
return info return info
...@@ -206,7 +189,7 @@ class WSGIResponse(HTTPResponse): ...@@ -206,7 +189,7 @@ class WSGIResponse(HTTPResponse):
@contextmanager @contextmanager
def transaction_pubevents(tm, request): def transaction_pubevents(request, tm=transaction.manager):
ok_exception = None ok_exception = None
try: try:
setDefaultSkin(request) setDefaultSkin(request)
...@@ -239,21 +222,11 @@ def transaction_pubevents(tm, request): ...@@ -239,21 +222,11 @@ def transaction_pubevents(tm, request):
def publish(request, module_info): def publish(request, module_info):
(bobo_before, obj, realm, debug_mode = module_info
bobo_after,
obj,
realm,
debug_mode,
err_hook,
validated_hook,
transactions_manager) = module_info
request.processInputs() request.processInputs()
response = request.response response = request.response
if bobo_after is not None:
response.after_list += (bobo_after,)
if debug_mode: if debug_mode:
response.debug_mode = debug_mode response.debug_mode = debug_mode
...@@ -261,15 +234,13 @@ def publish(request, module_info): ...@@ -261,15 +234,13 @@ def publish(request, module_info):
response.realm = realm response.realm = realm
noSecurityManager() noSecurityManager()
if bobo_before is not None:
bobo_before()
# Get the path list. # Get the path list.
# According to RFC1738 a trailing space in the path is valid. # According to RFC1738 a trailing space in the path is valid.
path = request.get('PATH_INFO') path = request.get('PATH_INFO')
request['PARENTS'] = [obj] request['PARENTS'] = [obj]
obj = request.traverse(path, validated_hook=validated_hook) obj = request.traverse(path, validated_hook=validate_user)
notify(pubevents.PubAfterTraversal(request)) notify(pubevents.PubAfterTraversal(request))
recordMetaData(obj, request) recordMetaData(obj, request)
...@@ -294,31 +265,22 @@ def publish_module(environ, start_response, ...@@ -294,31 +265,22 @@ def publish_module(environ, start_response,
_response_factory=WSGIResponse, _response_factory=WSGIResponse,
_request=None, _request=None,
_request_factory=WSGIRequest, _request_factory=WSGIRequest,
_module_name='Zope2', _module_name='Zope2'):
):
module_info = get_module_info(_module_name)
transactions_manager = module_info[7]
status = 200 status = 200
with closing(StringIO()) as stdout, closing(StringIO()) as stderr: with closing(StringIO()) as stdout, closing(StringIO()) as stderr:
if _response is None: response = (_response if _response is not None else
response = _response_factory(stdout=stdout, stderr=stderr) _response_factory(stdout=stdout, stderr=stderr))
else:
response = _response
response._http_version = environ['SERVER_PROTOCOL'].split('/')[1] response._http_version = environ['SERVER_PROTOCOL'].split('/')[1]
response._server_version = environ.get('SERVER_SOFTWARE') response._server_version = environ.get('SERVER_SOFTWARE')
if _request is None: request = (_request if _request is not None else
request = _request_factory( _request_factory(environ['wsgi.input'], environ, response))
environ['wsgi.input'], environ, response)
else:
request = _request
with closing(request) as request: with closing(request) as request:
try: try:
with transaction_pubevents(transactions_manager, request): with transaction_pubevents(request):
response = _publish(request, module_info) response = _publish(request, get_module_info(_module_name))
except Unauthorized: except Unauthorized:
response._unauthorized() response._unauthorized()
except HTTPRedirection as exc: except HTTPRedirection as exc:
......
...@@ -155,47 +155,30 @@ class TestPublish(unittest.TestCase): ...@@ -155,47 +155,30 @@ class TestPublish(unittest.TestCase):
def test_wo_REMOTE_USER(self): def test_wo_REMOTE_USER(self):
request = DummyRequest(PATH_INFO='/') request = DummyRequest(PATH_INFO='/')
response = request.response = DummyResponse() response = request.response = DummyResponse()
_before = DummyCallable()
_after = object()
_object = DummyCallable() _object = DummyCallable()
_object._result = 'RESULT' _object._result = 'RESULT'
request._traverse_to = _object request._traverse_to = _object
_realm = 'TESTING' _realm = 'TESTING'
_debug_mode = True _debug_mode = True
_err_hook = DummyCallable() returned = self._callFUT(request, (_object, _realm, _debug_mode))
_validated_hook = object()
_tm = transaction.manager
module_info = (_before, _after, _object, _realm, _debug_mode,
_err_hook, _validated_hook, _tm)
returned = self._callFUT(request, module_info)
self.assertTrue(returned is response) self.assertTrue(returned is response)
self.assertTrue(request._processedInputs) self.assertTrue(request._processedInputs)
self.assertEqual(response.after_list, (_after,))
self.assertTrue(response.debug_mode) self.assertTrue(response.debug_mode)
self.assertEqual(response.realm, 'TESTING') self.assertEqual(response.realm, 'TESTING')
self.assertEqual(_before._called_with, ((), {}))
self.assertEqual(request['PARENTS'], [_object]) self.assertEqual(request['PARENTS'], [_object])
self.assertEqual(request._traversed, ('/', None, _validated_hook)) self.assertEqual(request._traversed[:2], ('/', None))
self.assertEqual(_object._called_with, ((), {})) self.assertEqual(_object._called_with, ((), {}))
self.assertEqual(response._body, 'RESULT') self.assertEqual(response._body, 'RESULT')
self.assertEqual(_err_hook._called_with, None)
def test_w_REMOTE_USER(self): def test_w_REMOTE_USER(self):
request = DummyRequest(PATH_INFO='/', REMOTE_USER='phred') request = DummyRequest(PATH_INFO='/', REMOTE_USER='phred')
response = request.response = DummyResponse() response = request.response = DummyResponse()
_before = DummyCallable()
_after = object()
_object = DummyCallable() _object = DummyCallable()
_object._result = 'RESULT' _object._result = 'RESULT'
request._traverse_to = _object request._traverse_to = _object
_realm = 'TESTING' _realm = 'TESTING'
_debug_mode = True _debug_mode = True
_err_hook = DummyCallable() self._callFUT(request, (_object, _realm, _debug_mode))
_validated_hook = object()
_tm = transaction.manager
module_info = (_before, _after, _object, _realm, _debug_mode,
_err_hook, _validated_hook, _tm)
self._callFUT(request, module_info)
self.assertEqual(response.realm, None) self.assertEqual(response.realm, None)
......
...@@ -17,7 +17,6 @@ from ZPublisher.interfaces import ( ...@@ -17,7 +17,6 @@ from ZPublisher.interfaces import (
IPubAfterTraversal, IPubBeforeCommit, IPubAfterTraversal, IPubBeforeCommit,
IPubBeforeStreaming, IPubBeforeStreaming,
) )
from ZPublisher import Retry
from ZPublisher.WSGIPublisher import publish_module from ZPublisher.WSGIPublisher import publish_module
from ZPublisher.WSGIPublisher import WSGIResponse from ZPublisher.WSGIPublisher import WSGIResponse
...@@ -202,17 +201,5 @@ class _Request(BaseRequest): ...@@ -202,17 +201,5 @@ class _Request(BaseRequest):
# override to get rid of the 'EndRequestEvent' notification # override to get rid of the 'EndRequestEvent' notification
pass pass
# define things necessary for publication # define things necessary for publication
bobo_application = _Application() bobo_application = _Application()
def zpublisher_exception_hook(parent, request, *unused):
action = request.action
if action == 'fail_return':
return 0
if action == 'fail_exception':
raise Exception()
if action == 'conflict':
raise Retry()
raise ValueError('unknown action: %s' % action)
...@@ -20,7 +20,6 @@ from time import asctime ...@@ -20,7 +20,6 @@ from time import asctime
import AccessControl.User import AccessControl.User
from AccessControl.SecurityManagement import newSecurityManager from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import noSecurityManager from AccessControl.SecurityManagement import noSecurityManager
import transaction
import ZODB import ZODB
from zope.deferredimport import deprecated from zope.deferredimport import deprecated
from zope.event import notify from zope.event import notify
...@@ -152,5 +151,3 @@ def startup(): ...@@ -152,5 +151,3 @@ def startup():
startup_time = asctime() startup_time = asctime()
notify(DatabaseOpenedWithRoot(DB)) notify(DatabaseOpenedWithRoot(DB))
Zope2.zpublisher_transactions_manager = transaction.manager
...@@ -31,6 +31,11 @@ deprecated( ...@@ -31,6 +31,11 @@ deprecated(
_began_startup = 0 _began_startup = 0
# Zope2.App.startup.startup() sets the following variables in this module.
bobo_application = None
DB = None
opened = []
def startup_wsgi(): def startup_wsgi():
"""Initialize the Zope Package and provide a published module""" """Initialize the Zope Package and provide a published module"""
...@@ -62,14 +67,3 @@ def _configure_wsgi(): ...@@ -62,14 +67,3 @@ def _configure_wsgi():
configfile = os.environ.get('ZOPE_CONFIG') configfile = os.environ.get('ZOPE_CONFIG')
if configfile is not None: if configfile is not None:
configure_wsgi(configfile) configure_wsgi(configfile)
# Zope2.App.startup.startup() sets the following variables in this module.
DB = None
opened = []
bobo_application = None
zpublisher_transactions_manager = None
zpublisher_validated_hook = None
zpublisher_exception_hook = None
__bobo_after__ = None
__bobo_before__ = None
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment