diff options
| -rw-r--r-- | CHANGES.txt | 7 | ||||
| -rw-r--r-- | docs/narr/hooks.rst | 8 | ||||
| -rw-r--r-- | pyramid/config/__init__.py | 2 | ||||
| -rw-r--r-- | pyramid/config/adapters.py | 3 | ||||
| -rw-r--r-- | pyramid/config/routes.py | 5 | ||||
| -rw-r--r-- | pyramid/config/views.py | 5 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_util.py | 44 |
7 files changed, 60 insertions, 14 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 9d34786e2..3a78f6950 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,6 +1,13 @@ Next Release ============ +Features +-------- + +- Users can now provide dotted Python names to + ``add_{view,route,subscriber}_predicates`` instead of the predicate factory + directly. + Bug Fixes --------- diff --git a/docs/narr/hooks.rst b/docs/narr/hooks.rst index 8ffda1a5f..a7dc3e78b 100644 --- a/docs/narr/hooks.rst +++ b/docs/narr/hooks.rst @@ -1384,9 +1384,11 @@ The first argument to :meth:`pyramid.config.Configurator.add_view_predicate`, the name, is a string representing the name that is expected to be passed to ``view_config`` (or its imperative analogue ``add_view``). -The second argument is a view or route predicate factory. A view or route -predicate factory is most often a class with a constructor (``__init__``), a -``text`` method, a ``phash`` method and a ``__call__`` method. For example: +The second argument is a view or route predicate factory, or a :term:`dotted +Python name` which refers to a view or route predicate factory. A view or +route predicate factory is most often a class with a constructor +(``__init__``), a ``text`` method, a ``phash`` method and a ``__call__`` +method. For example: .. code-block:: python :linenos: diff --git a/pyramid/config/__init__.py b/pyramid/config/__init__.py index d4557d6b1..f1f24fbc1 100644 --- a/pyramid/config/__init__.py +++ b/pyramid/config/__init__.py @@ -512,7 +512,7 @@ class Configurator( '%s predicate named %s' % (type, name), '%s predicate' % type) intr['name'] = name - intr['factory'] = factory + intr['factory'] = self.maybe_dotted(factory) intr['weighs_more_than'] = weighs_more_than intr['weighs_less_than'] = weighs_less_than def register(): diff --git a/pyramid/config/adapters.py b/pyramid/config/adapters.py index 5573b6748..0a74e76b4 100644 --- a/pyramid/config/adapters.py +++ b/pyramid/config/adapters.py @@ -147,7 +147,8 @@ class AdaptersConfiguratorMixin(object): Python identifier (it will be used as a ``**predicates`` keyword argument to :meth:`~pyramid.config.Configurator.add_subscriber`). - ``factory`` should be a :term:`predicate factory`. + ``factory`` should be a :term:`predicate factory` or :term:`dotted + Python name` which refers to a predicate factory. See :ref:`subscriber_predicates` for more information. diff --git a/pyramid/config/routes.py b/pyramid/config/routes.py index 0ed370c94..14dd87d0f 100644 --- a/pyramid/config/routes.py +++ b/pyramid/config/routes.py @@ -255,6 +255,8 @@ class RoutesConfiguratorMixin(object): custom_predicates + .. deprecated:: 1.5 + This value should be a sequence of references to custom predicate callables. Use custom predicates when no set of predefined predicates does what you need. Custom predicates @@ -523,7 +525,8 @@ class RoutesConfiguratorMixin(object): Python identifier (it will be used as a keyword argument to ``add_view``). - ``factory`` should be a :term:`predicate factory`. + ``factory`` should be a :term:`predicate factory` or :term:`dotted + Python name` which refers to a predicate factory. See :ref:`view_and_route_predicates` for more information. diff --git a/pyramid/config/views.py b/pyramid/config/views.py index 707c84043..243c017fa 100644 --- a/pyramid/config/views.py +++ b/pyramid/config/views.py @@ -1027,6 +1027,8 @@ class ViewsConfiguratorMixin(object): custom_predicates + .. deprecated:: 1.5 + This value should be a sequence of references to custom predicate callables. Use custom predicates when no set of predefined predicates do what you need. Custom predicates can be combined with @@ -1369,7 +1371,8 @@ class ViewsConfiguratorMixin(object): Python identifier (it will be used as a keyword argument to ``add_view`` by others). - ``factory`` should be a :term:`predicate factory`. + ``factory`` should be a :term:`predicate factory` or :term:`dotted + Python name` which refers to a predicate factory. See :ref:`view_and_route_predicates` for more information. """ diff --git a/pyramid/tests/test_config/test_util.py b/pyramid/tests/test_config/test_util.py index f6cd414fb..93d5c8895 100644 --- a/pyramid/tests/test_config/test_util.py +++ b/pyramid/tests/test_config/test_util.py @@ -380,8 +380,8 @@ class TestPredicateList(unittest.TestCase): self.assertEqual(predicates[2].text(), '!header header') self.assertEqual(predicates[1](None, request), True) self.assertEqual(predicates[2](None, request), True) - - + + class Test_takes_one_arg(unittest.TestCase): def _callFUT(self, view, attr=None, argname=None): from pyramid.config.util import takes_one_arg @@ -560,7 +560,7 @@ class Test_takes_one_arg(unittest.TestCase): class Foo: pass foo = Foo() self.assertFalse(self._callFUT(foo)) - + def test_method_onearg_named_request(self): class Foo: def method(self, request): @@ -586,11 +586,41 @@ class TestNotted(unittest.TestCase): self.assertEqual(inst.text(), '') self.assertEqual(inst.phash(), '') self.assertEqual(inst(None, None), True) - + +class TestDotted(unittest.TestCase): + def _makeOne(self, *arg, **kw): + self.action_called = False + from pyramid.config import Configurator + config = Configurator(*arg, **kw) + return config + + def test_it_without_dots(self): + config = self._makeOne() + + def _fakeAction(discriminator, callable=None, args=(), kw=None, order=0, introspectables=(), **extra): + self.assertEqual(len(introspectables), 1) + self.assertEqual(introspectables[0]['name'], 'testing') + self.assertEqual(introspectables[0]['factory'], DummyPredicate) + + config.action = _fakeAction + config._add_predicate('route', 'testing', DummyPredicate) + + def test_it_with_dots(self): + config = self._makeOne() + + def _fakeAction(discriminator, callable=None, args=(), kw=None, order=0, introspectables=(), **extra): + self.assertEqual(len(introspectables), 1) + self.assertEqual(introspectables[0]['name'], 'testing') + self.assertEqual(introspectables[0]['factory'], DummyPredicate) + + config.action = _fakeAction + config._add_predicate('route', 'testing', 'pyramid.tests.test_config.test_util.DummyPredicate') + + class DummyPredicate(object): def __init__(self, result): self.result = result - + def text(self): return self.result @@ -598,7 +628,7 @@ class DummyPredicate(object): def __call__(self, context, request): return True - + class DummyCustomPredicate(object): def __init__(self): self.__text__ = 'custom predicate' @@ -626,4 +656,4 @@ class DummyRequest: class DummyConfigurator(object): def maybe_dotted(self, thing): return thing - + |
