From 358dc276d28fb395a9a742ff53dc66ee115c58ad Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 11 Jan 2009 02:20:52 +0000 Subject: Merge router-simplify branch. Changelog below. Bug Fixes --------- - Fix a bug where the Paste configuration's ``unicode_path_segments`` (and os.environ's ``BFG_UNICODE_PATH_SEGMENTS``) may have been defaulting to false in some circumstances. It now always defaults to true, matching the documentation and intent. - The ``repoze.bfg.traversal.find_model`` API did not work properly when passed a ``path`` argument which was unicode and contained high-order bytes when the ``unicode_path_segments`` or ``BFG_UNICODE_PATH_SEGMENTS`` configuration variables were "true". - A new module was added: ``repoze.bfg.settings``. This contains deployment-settings-related code. Behavior Changes ---------------- - The ``make_app`` callable within ``repoze.bfg.router`` now registers the ``root_policy`` argument as a utility (unnamed, using the new ``repoze.bfg.interfaces.IRootFactory`` as a provides interface) rather than passing it as the first argument to the ``repoze.bfg.router.Router`` class. As a result the ``repoze.bfg.router.Router`` router class only accepts a single argument: ``registry``. The ``repoze.bfg.router.Router`` class retrieves the root policy via a utility lookup now. The ``repoze.bfg.router.make_app`` API also now performs some important application registrations that were previously handled inside ``repoze.bfg.registry.makeRegistry``. - The ``repoze.bfg.settings.Settings`` class (an instance of which is registered as a utility providing ``repoze.bfg.interfaces.ISettings`` when any application is started) now automatically calls ``repoze.bfg.settings.get_options`` on the options passed to its constructor. This means that usage of ``get_options`` within an application's ``make_app`` function is no longer required (the "raw" ``options`` dict or None may be passed). Deprecations ------------ - Moved the ``repoze.bfg.registry.Settings`` class. This has been moved to ``repoze.bfg.settings.Settings``. A deprecation warning is issued when it is imported from the older location. - Moved the ``repoze.bfg.registry.get_options`` function This has been moved to ``repoze.bfg.settings.get_options``. A deprecation warning is issued when it is imported from the older location. - The ``repoze.bfg.interfaces.IRootPolicy`` interface was renamed within the interfaces package. It has been renamed to ``IRootFactory``. A deprecation warning is issued when it is imported from the older location. --- repoze/bfg/registry.py | 100 ++++++++++++++----------------------------------- 1 file changed, 29 insertions(+), 71 deletions(-) (limited to 'repoze/bfg/registry.py') diff --git a/repoze/bfg/registry.py b/repoze/bfg/registry.py index cab95933b..f2f47a889 100644 --- a/repoze/bfg/registry.py +++ b/repoze/bfg/registry.py @@ -1,5 +1,3 @@ -import os -import sys import threading import zope.component @@ -10,12 +8,21 @@ from zope.component.interfaces import IComponentLookup from zope.component.registry import Components from zope.component import getSiteManager as original_getSiteManager -from zope.interface import implements +from zope.deferredimport import deprecated -from repoze.bfg.interfaces import ISettings -from repoze.bfg.interfaces import ILogger from repoze.bfg.zcml import zcml_configure -from repoze.bfg.log import make_stream_logger + +deprecated( + "('from repoze.bfg.registry import Settings' is now " + "deprecated; instead use 'from repoze.bfg.settings import Settings')", + Settings = "repoze.bfg.settings:Settings", + ) + +deprecated( + "('from repoze.bfg.registry import get_options' is now " + "deprecated; instead use 'from repoze.bfg.settings import get_options')", + get_options = "repoze.bfg.settings:get_options", + ) class ThreadLocalRegistryManager(threading.local): registry = getGlobalSiteManager() @@ -36,30 +43,26 @@ def setRegistryManager(manager): # for unit tests registry_manager = manager return old_registry_manager -def makeRegistry(filename, package, options=None, lock=threading.Lock()): - # We push our ZCML-defined configuration into an app-local - # component registry in order to allow more than one bfg app to - # live in the same process space without one unnecessarily - # stomping on the other's component registrations (although I - # suspect directives that have side effects are going to fail). - # The only way to do that currently is to override - # zope.component.getGlobalSiteManager for the duration of the ZCML - # includes. We acquire a lock in case another make_app runs in a - # different thread simultaneously, in a vain attempt to prevent - # mixing of registrations. There's not much we can do about - # non-make_app code that tries to use the global site manager API - # directly in a different thread while we hold the lock. Those - # registrations will end up in our application's registry. +def makeRegistry(filename, package, lock=threading.Lock()): + + """ We push our ZCML-defined configuration into an app-local + component registry in order to allow more than one bfg app to live + in the same process space without one unnecessarily stomping on + the other's component registrations (although I suspect directives + that have side effects are going to fail). The only way to do + that currently is to override zope.component.getGlobalSiteManager + for the duration of the ZCML includes. We acquire a lock in case + another make_app runs in a different thread simultaneously, in a + vain attempt to prevent mixing of registrations. There's not much + we can do about non-makeRegistry code that tries to use the global + site manager API directly in a different thread while we hold the + lock. Those registrations will end up in our application's + registry.""" + lock.acquire() try: registry = Components(package.__name__) registry_manager.set(registry) - if options is None: - options = {} - settings = Settings(options) - registry.registerUtility(settings, ISettings) - debug_logger = make_stream_logger('repoze.bfg.debug', sys.stderr) - registry.registerUtility(debug_logger, ILogger, 'repoze.bfg.debug') original_getSiteManager.sethook(getSiteManager) zope.component.getGlobalSiteManager = registry_manager.get zcml_configure(filename, package=package) @@ -69,15 +72,6 @@ def makeRegistry(filename, package, options=None, lock=threading.Lock()): lock.release() registry_manager.clear() -class Settings(object): - implements(ISettings) - reload_templates = False - debug_notfound = False - debug_authorization = False - unicode_path_segments = True - def __init__(self, options): - self.__dict__.update(options) - def getSiteManager(context=None): if context is None: return registry_manager.get() @@ -87,42 +81,6 @@ def getSiteManager(context=None): except TypeError, error: raise ComponentLookupError(*error.args) -def asbool(s): - s = str(s).strip() - return s.lower() in ('t', 'true', 'y', 'yes', 'on', '1') - -def get_options(kw, environ=os.environ): - """ Update PasteDeploy application settings keywords with - framework-specific key/value pairs (e.g. find - 'BFG_DEBUG_AUTHORIZATION' in os.environ and jam into keyword - args).""" - # environ is passed in for unit tests - eget = environ.get - config_debug_all = kw.get('debug_all', '') - effective_debug_all = asbool(eget('BFG_DEBUG_ALL', - config_debug_all)) - config_debug_auth = kw.get('debug_authorization', '') - effective_debug_auth = asbool(eget('BFG_DEBUG_AUTHORIZATION', - config_debug_auth)) - config_debug_notfound = kw.get('debug_notfound', '') - effective_debug_notfound = asbool(eget('BFG_DEBUG_NOTFOUND', - config_debug_notfound)) - config_reload_templates = kw.get('reload_templates', '') - effective_reload_templates = asbool(eget('BFG_RELOAD_TEMPLATES', - config_reload_templates)) - config_unicode_path_segments = kw.get('unicode_path_segments', '') - effective_unicode_path_segments = asbool(eget('BFG_UNICODE_PATH_SEGMENTS', - config_unicode_path_segments)) - update = { - 'debug_authorization': effective_debug_all or effective_debug_auth, - 'debug_notfound': effective_debug_all or effective_debug_notfound, - 'reload_templates': effective_reload_templates, - 'unicode_path_segments': effective_unicode_path_segments, - } - - kw.update(update) - return kw - from zope.testing.cleanup import addCleanUp try: addCleanUp(original_getSiteManager.reset) -- cgit v1.2.3