diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-11-28 19:10:23 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-11-28 19:10:23 +0000 |
| commit | 4d534ed4224261529550c53b81628a685facdc46 (patch) | |
| tree | 0d88ed5dabee358236d6ae9102c0cfadf4086c43 | |
| parent | 07a723720d1fea85ec7d0629324c015712b16b4e (diff) | |
| download | pyramid-4d534ed4224261529550c53b81628a685facdc46.tar.gz pyramid-4d534ed4224261529550c53b81628a685facdc46.tar.bz2 pyramid-4d534ed4224261529550c53b81628a685facdc46.zip | |
Allow initial registry setup to be called via a ``setup_registry`` method.
Allow path specifications for renderers which are already resource specifications.
| -rw-r--r-- | repoze/bfg/configuration.py | 38 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_configuration.py | 70 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_zcml.py | 5 | ||||
| -rw-r--r-- | repoze/bfg/zcml.py | 5 |
4 files changed, 105 insertions, 13 deletions
diff --git a/repoze/bfg/configuration.py b/repoze/bfg/configuration.py index 8a6521777..4b235e864 100644 --- a/repoze/bfg/configuration.py +++ b/repoze/bfg/configuration.py @@ -131,19 +131,31 @@ class Configurator(object): if registry is None: registry = Registry(self.package.__name__) self.registry = registry - self._set_settings(settings) - self._set_root_factory(root_factory) - if debug_logger is None: - debug_logger = make_stream_logger('repoze.bfg.debug', - sys.stderr) - registry.registerUtility(debug_logger, IDebugLogger) - registry.registerUtility(debug_logger, IDebugLogger, - 'repoze.bfg.debug') # b /c - if authentication_policy or authorization_policy: - self._set_security_policies(authentication_policy, - authorization_policy) - for name, renderer in renderers: - self.add_renderer(name, renderer) + self.setup_registry( + settings=settings, + root_factory=root_factory, + authentication_policy=authentication_policy, + authorization_policy=authorization_policy, + renderers=renderers, + debug_logger=debug_logger) + + def setup_registry(self, settings=None, root_factory=None, + authentication_policy=None, authorization_policy=None, + renderers=DEFAULT_RENDERERS, debug_logger=None): + self._set_settings(settings) + self._set_root_factory(root_factory) + if debug_logger is None: + debug_logger = make_stream_logger('repoze.bfg.debug', sys.stderr) + registry = self.registry + registry.registerUtility(debug_logger, IDebugLogger) + registry.registerUtility(debug_logger, IDebugLogger, + 'repoze.bfg.debug') # b /c + if authentication_policy or authorization_policy: + self._set_security_policies(authentication_policy, + authorization_policy) + for name, renderer in renderers: + self.add_renderer(name, renderer) + def _set_settings(self, mapping): settings = Settings(mapping or {}) diff --git a/repoze/bfg/tests/test_configuration.py b/repoze/bfg/tests/test_configuration.py index fcce6accd..060f30e3a 100644 --- a/repoze/bfg/tests/test_configuration.py +++ b/repoze/bfg/tests/test_configuration.py @@ -140,6 +140,76 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(config.registry.getUtility(IRendererFactory, 'yeah'), renderer) + def test_setup_registry_custom_settings(self): + from repoze.bfg.registry import Registry + from repoze.bfg.interfaces import ISettings + settings = {'reload_templates':True, + 'mysetting':True} + reg = Registry() + config = self._makeOne(reg) + config.setup_registry(settings=settings) + settings = reg.getUtility(ISettings) + self.assertEqual(settings['reload_templates'], True) + self.assertEqual(settings['debug_authorization'], False) + self.assertEqual(settings['mysetting'], True) + + def test_setup_registry_debug_logger_None_default(self): + from repoze.bfg.registry import Registry + from repoze.bfg.interfaces import IDebugLogger + reg = Registry() + config = self._makeOne(reg) + config.setup_registry() + logger = reg.getUtility(IDebugLogger) + self.assertEqual(logger.name, 'repoze.bfg.debug') + + def test_setup_registry_debug_logger_non_None(self): + from repoze.bfg.registry import Registry + from repoze.bfg.interfaces import IDebugLogger + logger = object() + reg = Registry() + config = self._makeOne(reg) + config.setup_registry(debug_logger=logger) + result = reg.getUtility(IDebugLogger) + self.assertEqual(logger, result) + + def test_setup_registry_authentication_policy(self): + from repoze.bfg.registry import Registry + from repoze.bfg.interfaces import IAuthenticationPolicy + policy = object() + reg = Registry() + config = self._makeOne(reg) + config.setup_registry(authentication_policy=policy) + result = reg.getUtility(IAuthenticationPolicy) + self.assertEqual(policy, result) + + def test_setup_registry_authorization_policy_only(self): + from repoze.bfg.registry import Registry + from zope.configuration.exceptions import ConfigurationError + policy = object() + reg = Registry() + config = self._makeOne(reg) + config = self.assertRaises(ConfigurationError, + config.setup_registry, + authorization_policy=policy) + + def test_setup_registry_default_root_factory(self): + from repoze.bfg.registry import Registry + from repoze.bfg.interfaces import IRootFactory + reg = Registry() + config = self._makeOne(reg) + config.setup_registry() + self.failUnless(reg.getUtility(IRootFactory)) + + def test_setup_registry_alternate_renderers(self): + from repoze.bfg.registry import Registry + from repoze.bfg.interfaces import IRendererFactory + renderer = object() + reg = Registry() + config = self._makeOne(reg) + config.setup_registry(renderers=[('yeah', renderer)]) + self.assertEqual(reg.getUtility(IRendererFactory, 'yeah'), + renderer) + def test_add_subscriber_defaults(self): from zope.interface import implements from zope.interface import Interface diff --git a/repoze/bfg/tests/test_zcml.py b/repoze/bfg/tests/test_zcml.py index e00b02f74..f69afc03b 100644 --- a/repoze/bfg/tests/test_zcml.py +++ b/repoze/bfg/tests/test_zcml.py @@ -939,6 +939,11 @@ class Test_path_spec(unittest.TestCase): result = self._callFUT(context, '/foo.pt') self.assertEqual(result, '/foo.pt') + def test_path_is_already_resource_spec(self): + context = DummyContext() + result = self._callFUT(context, 'repoze.bfg.tests:foo.pt') + self.assertEqual(result, 'repoze.bfg.tests:foo.pt') + class IDummy(Interface): pass diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py index 41945c448..3ccfff66c 100644 --- a/repoze/bfg/zcml.py +++ b/repoze/bfg/zcml.py @@ -1,3 +1,5 @@ +import os + from zope.configuration import xmlconfig from zope.configuration.config import ConfigurationMachine from zope.configuration.exceptions import ConfigurationError @@ -760,6 +762,9 @@ def path_spec(context, path): # absolute path; we prefer registering resource specifications # over absolute paths because these can be overridden by the # resource directive. + if ':' in path and not os.path.isabs(path): + # it's already a resource specification + return path abspath = context.path(path) if hasattr(context, 'package') and context.package: package = context.package |
