From 5a972bc6a0c608395a495eb12e63020e2295ef6d Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Thu, 11 Nov 2010 15:59:40 -0500 Subject: Features -------- - The ``settings`` object which used to be available only when ``request.settings.get_settings`` was called is now available as ``registry.settings`` (e.g. ``request.registry.settings`` in view code). Deprecations ------------ - Obtaining the ``settings`` object via ``registry.{get|query}Utility(ISettings)`` is now deprecated. Instead, obtain the ``settings`` object via the ``registry.settings`` attribute. A backwards compatibility shim was added to the registry object to register the settings object as an ISettings utility when ``setattr(registry, 'settings', foo)`` is called, but it will be removed in a later release. - Obtaining the ``settings`` object via ``pyramid.settings.get_settings`` is now deprecated. Obtain it as the ``settings`` attribute of the registry now (obtain the registry via ``pyramid.threadlocal.get_registry`` or as ``request.registry``). --- CHANGES.txt | 19 +++++++++++++++++++ pyramid/configuration.py | 18 +++++++++--------- pyramid/registry.py | 13 +++++++++++++ pyramid/renderers.py | 3 +-- pyramid/router.py | 3 +-- pyramid/settings.py | 2 +- pyramid/tests/test_configuration.py | 9 +++------ pyramid/tests/test_i18n.py | 3 +-- pyramid/tests/test_registry.py | 10 ++++++++++ pyramid/tests/test_renderers.py | 6 ++---- pyramid/tests/test_router.py | 3 +-- pyramid/tests/test_settings.py | 3 +-- 12 files changed, 62 insertions(+), 30 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 7b181f2c5..6c8972d55 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -17,6 +17,10 @@ Features ``route_url``. These are simple passthroughs for their respective functions in ``pyramid.url``. +- The ``settings`` object which used to be available only when + ``request.settings.get_settings`` was called is now available as + ``registry.settings`` (e.g. ``request.registry.settings`` in view code). + Bug Fixes --------- @@ -42,6 +46,21 @@ Documentation - Documented the ``matchdict`` and ``matched_route`` attributes of the request object in the Request API documentation. +Deprecations +------------ + +- Obtaining the ``settings`` object via + ``registry.{get|query}Utility(ISettings)`` is now deprecated. Instead, + obtain the ``settings`` object via the ``registry.settings`` attribute. A + backwards compatibility shim was added to the registry object to register + the settings object as an ISettings utility when ``setattr(registry, + 'settings', foo)`` is called, but it will be removed in a later release. + +- Obtaining the ``settings`` object via ``pyramid.settings.get_settings`` is + now deprecated. Obtain it as the ``settings`` attribute of the registry + now (obtain the registry via ``pyramid.threadlocal.get_registry`` or as + ``request.registry``). + 1.0a2 (2010-11-09) ================== diff --git a/pyramid/configuration.py b/pyramid/configuration.py index 0a3041b9d..aa436b4d7 100644 --- a/pyramid/configuration.py +++ b/pyramid/configuration.py @@ -34,7 +34,6 @@ from pyramid.interfaces import IRootFactory from pyramid.interfaces import IRouteRequest from pyramid.interfaces import IRoutesMapper from pyramid.interfaces import ISecuredView -from pyramid.interfaces import ISettings from pyramid.interfaces import IStaticURLInfo from pyramid.interfaces import ITranslationDirectories from pyramid.interfaces import ITraverser @@ -127,8 +126,8 @@ class Configurator(object): If the ``settings`` argument is passed, it should be a Python dictionary representing the deployment settings for this application. These are later retrievable using the - :meth:`pyramid.configuration.Configurator.get_settings` and - :func:`pyramid.settings.get_settings` APIs. + :meth:`pyramid.registry.Registry.settings` attribute or the + :func:`pyramid.settings.get_settings` API. If the ``root_factory`` argument is passed, it should be an object representing the default :term:`root factory` for your application @@ -244,7 +243,7 @@ class Configurator(object): def _set_settings(self, mapping): settings = Settings(mapping or {}) - self.registry.registerUtility(settings, ISettings) + self.registry.settings = settings return settings def _set_root_factory(self, factory): @@ -288,7 +287,7 @@ class Configurator(object): view = self.maybe_dotted(view) authn_policy = self.registry.queryUtility(IAuthenticationPolicy) authz_policy = self.registry.queryUtility(IAuthorizationPolicy) - settings = self.registry.queryUtility(ISettings) + settings = self.registry.settings logger = self.registry.queryUtility(IDebugLogger) mapped_view = _map_view(view, attr, renderer, self.registry) owrapped_view = _owrap_view(mapped_view, viewname, wrapper_viewname) @@ -593,7 +592,7 @@ class Configurator(object): """ if settings is None: settings = {} - utility = self.registry.queryUtility(ISettings) + utility = self.registry.settings if utility is None: utility = self._set_settings(settings) utility.update(settings) @@ -610,9 +609,10 @@ class Configurator(object): .. note:: For backwards compatibility, dictionary keys can also be looked up as attributes of the settings object. - .. note:: the :class:`pyramid.settings.get_settings` function - performs the same duty.""" - return self.registry.queryUtility(ISettings) + .. note:: the :class:`pyramid.settings.get_settings` and function + performs the same duty and the settings attribute can also be + accessed as :attr:`pyramid.registry.Registry.settings`""" + return self.registry.settings def make_wsgi_app(self): """ Returns a :app:`Pyramid` WSGI application representing diff --git a/pyramid/registry.py b/pyramid/registry.py index e935ac165..226df92db 100644 --- a/pyramid/registry.py +++ b/pyramid/registry.py @@ -1,10 +1,12 @@ from zope.component.registry import Components +from pyramid.interfaces import ISettings class Registry(Components, dict): # for optimization purposes, if no listeners are listening, don't try # to notify them has_listeners = False + _settings = None def registerSubscriptionAdapter(self, *arg, **kw): result = Components.registerSubscriptionAdapter(self, *arg, **kw) @@ -21,4 +23,15 @@ class Registry(Components, dict): # iterating over subscribers assures they get executed [ _ for _ in self.subscribers(events, None) ] + # backwards compatibility for code that wants to look up a settings + # object via ``registry.getUtility(ISettings)`` + def _get_settings(self): + return self._settings + + def _set_settings(self, settings): + self.registerUtility(settings, ISettings) + self._settings = settings + + settings = property(_get_settings, _set_settings) + global_registry = Registry('global') diff --git a/pyramid/renderers.py b/pyramid/renderers.py index 2b6f675df..0c2a7c5db 100644 --- a/pyramid/renderers.py +++ b/pyramid/renderers.py @@ -8,7 +8,6 @@ from pyramid.interfaces import IRendererGlobalsFactory from pyramid.interfaces import IRendererFactory from pyramid.interfaces import IResponseFactory from pyramid.interfaces import ITemplateRenderer -from pyramid.interfaces import ISettings from pyramid.interfaces import IRendererInfo from pyramid.compat import json @@ -231,7 +230,7 @@ class RendererHelper(object): @reify def settings(self): - settings = self.registry.queryUtility(ISettings) + settings = self.registry.settings return settings @reify diff --git a/pyramid/router.py b/pyramid/router.py index fb14374ee..972c05b62 100644 --- a/pyramid/router.py +++ b/pyramid/router.py @@ -9,7 +9,6 @@ from pyramid.interfaces import IRouteRequest from pyramid.interfaces import IRouter from pyramid.interfaces import IRequestFactory from pyramid.interfaces import IRoutesMapper -from pyramid.interfaces import ISettings from pyramid.interfaces import ITraverser from pyramid.interfaces import IView from pyramid.interfaces import IViewClassifier @@ -42,7 +41,7 @@ class Router(object): self.request_factory = q(IRequestFactory, default=Request) self.root_policy = self.root_factory # b/w compat self.registry = registry - settings = q(ISettings) + settings = registry.settings if settings is not None: self.debug_notfound = settings['debug_notfound'] diff --git a/pyramid/settings.py b/pyramid/settings.py index ed2c3a281..96ad3336a 100644 --- a/pyramid/settings.py +++ b/pyramid/settings.py @@ -77,7 +77,7 @@ def get_settings(): performs the same duty. """ reg = get_current_registry() - return reg.queryUtility(ISettings) + return reg.settings def asbool(s): """ Return the boolean value ``True`` if the case-lowered value of string diff --git a/pyramid/tests/test_configuration.py b/pyramid/tests/test_configuration.py index 17726320d..ded17cb33 100644 --- a/pyramid/tests/test_configuration.py +++ b/pyramid/tests/test_configuration.py @@ -77,8 +77,7 @@ class ConfiguratorTests(unittest.TestCase): config.registry.registerUtility(policy, IAuthorizationPolicy) def _registerSettings(self, config, **settings): - from pyramid.interfaces import ISettings - config.registry.registerUtility(settings, ISettings) + config.registry.settings = settings def test_ctor_no_registry(self): import sys @@ -472,20 +471,18 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(config.get_settings(), None) def test_get_settings_withsettings(self): - from pyramid.interfaces import ISettings settings = {'a':1} config = self._makeOne() - config.registry.registerUtility(settings, ISettings) + config.registry.settings = settings self.assertEqual(config.get_settings(), settings) def test_add_settings_settings_already_registered(self): from pyramid.registry import Registry - from pyramid.interfaces import ISettings reg = Registry() config = self._makeOne(reg) config._set_settings({'a':1}) config.add_settings({'b':2}) - settings = reg.getUtility(ISettings) + settings = reg.settings self.assertEqual(settings['a'], 1) self.assertEqual(settings['b'], 2) diff --git a/pyramid/tests/test_i18n.py b/pyramid/tests/test_i18n.py index e975013b7..12cf185b4 100644 --- a/pyramid/tests/test_i18n.py +++ b/pyramid/tests/test_i18n.py @@ -87,8 +87,7 @@ class Test_negotiate_locale_name(unittest.TestCase): from pyramid.threadlocal import get_current_registry registry = get_current_registry() settings = {'default_locale_name':'settings'} - from pyramid.interfaces import ISettings - registry.registerUtility(settings, ISettings) + registry.settings = settings request = DummyRequest() request.registry = registry result = self._callFUT(request) diff --git a/pyramid/tests/test_registry.py b/pyramid/tests/test_registry.py index 7e6d1b67f..3d94cb645 100644 --- a/pyramid/tests/test_registry.py +++ b/pyramid/tests/test_registry.py @@ -28,6 +28,16 @@ class TestRegistry(unittest.TestCase): [IDummyEvent], Interface) self.assertEqual(registry.has_listeners, True) + def test__get_settings(self): + registry = self._makeOne() + registry._settings = 'foo' + self.assertEqual(registry.settings, 'foo') + + def test__set_settings(self): + registry = self._makeOne() + registry.settings = 'foo' + self.assertEqual(registry._settings, 'foo') + class DummyModule: __path__ = "foo" __name__ = "dummy" diff --git a/pyramid/tests/test_renderers.py b/pyramid/tests/test_renderers.py index 38219b242..b4a6892db 100644 --- a/pyramid/tests/test_renderers.py +++ b/pyramid/tests/test_renderers.py @@ -192,10 +192,9 @@ class TestRendererFromName(unittest.TestCase): def test_it(self): from pyramid.threadlocal import get_current_registry - from pyramid.interfaces import ISettings registry = get_current_registry() settings = {} - registry.registerUtility(settings, ISettings) + registry.settings = settings from pyramid.interfaces import IRendererFactory import os here = os.path.dirname(os.path.abspath(__file__)) @@ -213,10 +212,9 @@ class TestRendererFromName(unittest.TestCase): def test_it_with_package(self): import pyramid from pyramid.threadlocal import get_current_registry - from pyramid.interfaces import ISettings registry = get_current_registry() settings = {} - registry.registerUtility(settings, ISettings) + registry.settings = settings from pyramid.interfaces import IRendererFactory import os here = os.path.dirname(os.path.abspath(__file__)) diff --git a/pyramid/tests/test_router.py b/pyramid/tests/test_router.py index 84ac18f53..fc2d87630 100644 --- a/pyramid/tests/test_router.py +++ b/pyramid/tests/test_router.py @@ -34,10 +34,9 @@ class TestRouter(unittest.TestCase): return logger def _registerSettings(self, **kw): - from pyramid.interfaces import ISettings settings = {'debug_authorization':False, 'debug_notfound':False} settings.update(kw) - self.registry.registerUtility(settings, ISettings) + self.registry.settings = settings def _registerTraverserFactory(self, context, view_name='', subpath=None, traversed=None, virtual_root=None, diff --git a/pyramid/tests/test_settings.py b/pyramid/tests/test_settings.py index 90ed9419b..12b1174de 100644 --- a/pyramid/tests/test_settings.py +++ b/pyramid/tests/test_settings.py @@ -196,9 +196,8 @@ class TestGetSettings(unittest.TestCase): self.assertEqual(self._callFUT(), None) def test_it_withsettings(self): - from pyramid.interfaces import ISettings settings = {'a':1} - self.config.registry.registerUtility(settings, ISettings) + self.config.registry.settings = settings self.assertEqual(self._callFUT(), settings) class Test_asbool(unittest.TestCase): -- cgit v1.2.3