diff options
| -rw-r--r-- | CHANGES.txt | 7 | ||||
| -rw-r--r-- | pyramid/config.py | 27 | ||||
| -rw-r--r-- | pyramid/tests/test_config.py | 19 |
3 files changed, 48 insertions, 5 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index e00c59a1f..27a723fb4 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -22,6 +22,13 @@ Features - Add ``add_directive`` method to configurator, which allows framework extenders to add methods to the configurator (ala ZCML directives). +- When ``Configurator.include`` is passed a *module* as an argument, it + defaults to attempting to find and use a callable named ``includeme`` + within that module. This makes it possible to use + ``config.include('some.module')`` rather than + ``config.include('some.module.somefunc')`` as long as the include function + within ``some.module`` is named ``includeme``. + Paster Templates ---------------- diff --git a/pyramid/config.py b/pyramid/config.py index b144b8fe3..9b8a7c191 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -511,9 +511,12 @@ class Configurator(object): Values allowed to be presented via the ``*callables`` argument to this method: any callable Python object or any :term:`dotted Python - name` which resolves to a callable Python object. + name` which resolves to a callable Python object. It may also be a + Python :term:`module`, in which case, the module will be searched for + a callable named ``includeme``, which will be treated as the + configuration callable. - For example, if the ``configure`` function below lives in a module + For example, if the ``includeme`` function below lives in a module named ``myapp.myconfig``: .. code-block:: python @@ -525,7 +528,7 @@ class Configurator(object): from pyramid.response import Response return Response('OK') - def configure(config): + def includeme(config): config.add_view(my_view) You might cause it be included within your Pyramid application like @@ -538,7 +541,19 @@ class Configurator(object): def main(global_config, **settings): config = Configurator() - config.include('myapp.myconfig.configure') + config.include('myapp.myconfig.includeme') + + Because the function is named ``includeme``, the function name can + also be omitted from the dotted name reference: + + .. code-block:: python + :linenos: + + from pyramid.config import Configurator + + def main(global_config, **settings): + config = Configurator() + config.include('myapp.myconfig') Included configuration statements will be overridden by local configuration statements if an included callable causes a @@ -551,9 +566,11 @@ class Configurator(object): for c in callables: c = self.maybe_dotted(c) - sourcefile = inspect.getsourcefile(c) module = inspect.getmodule(c) + if module is c: + c = getattr(module, 'includeme') spec = module.__name__ + ':' + c.__name__ + sourcefile = inspect.getsourcefile(c) if _context.processSpec(spec): context = GroupingContextDecorator(_context) context.basepath = os.path.dirname(sourcefile) diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py index 61a665f5a..dde839aea 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -658,6 +658,23 @@ class ConfiguratorTests(unittest.TestCase): self.assertEqual(context_after.includepath, ()) self.failUnless(context_after is context_before) + def test_include_with_module_defaults_to_includeme(self): + from pyramid import tests + config = self._makeOne() + context_before = config._make_context() + config._ctx = context_before + config.include('pyramid.tests.test_config') + context_after = config._ctx + actions = context_after.actions + self.assertEqual(len(actions), 1) + self.assertEqual( + actions[0][:3], + ('discrim', None, tests), + ) + self.assertEqual(context_after.basepath, None) + self.assertEqual(context_after.includepath, ()) + self.failUnless(context_after is context_before) + def test_with_context(self): config = self._makeOne() ctx = config._make_context() @@ -5021,6 +5038,8 @@ class DummyHandler(object): # pragma: no cover def dummy_include(config): config.action('discrim', None, config.package) +includeme = dummy_include + def dummy_extend(config, discrim): config.action(discrim, None, config.package) |
