summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGael Pasgrimaud <gael@gawel.org>2011-01-12 21:38:06 +0100
committerGael Pasgrimaud <gael@gawel.org>2011-01-12 21:38:06 +0100
commit2422ceae0750c65cc1e4ea097143f4c71fbba348 (patch)
tree6f9e739c6f6e81805ed3ca94f12f801f19954b8a
parentcad4e3c917017d43c382df213a1c045523e247b8 (diff)
downloadpyramid-2422ceae0750c65cc1e4ea097143f4c71fbba348.tar.gz
pyramid-2422ceae0750c65cc1e4ea097143f4c71fbba348.tar.bz2
pyramid-2422ceae0750c65cc1e4ea097143f4c71fbba348.zip
store extends in context and add attributes to instance
-rw-r--r--pyramid/config.py19
-rw-r--r--pyramid/tests/test_config.py16
2 files changed, 26 insertions, 9 deletions
diff --git a/pyramid/config.py b/pyramid/config.py
index 60197a4c6..8e709b607 100644
--- a/pyramid/config.py
+++ b/pyramid/config.py
@@ -275,6 +275,7 @@ class Configurator(object):
default_permission=None,
session_factory=None,
default_view_mapper=None,
+ extends = None,
autocommit=False,
):
if package is None:
@@ -302,6 +303,8 @@ class Configurator(object):
session_factory=session_factory,
default_view_mapper=default_view_mapper,
)
+ if extends:
+ self.extend(*extends)
def _set_settings(self, mapping):
settings = Settings(mapping or {})
@@ -425,6 +428,7 @@ class Configurator(object):
context = PyramidConfigurationMachine()
registerCommonDirectives(context)
context.registry = self.registry
+ context.extends = []
context.autocommit = autocommit
return context
@@ -572,7 +576,7 @@ class Configurator(object):
module = inspect.getmodule(c)
spec = module.__name__ + ':' + name
if _context.processSpec(spec):
- if hasattr(klass, name):
+ if hasattr(self, name):
raise ConfigurationError(
"Configurator already have a method named %s" % name)
context = GroupingContextDecorator(_context)
@@ -580,10 +584,13 @@ class Configurator(object):
context.includepath = _context.includepath + (spec,)
context.package = package_of(module)
config = klass.with_context(context)
- def extend_wrapper(*args, **kwargs):
- c(config, *args, **kwargs)
- extend_wrapper.__name__ = name
- setattr(klass, name, staticmethod(extend_wrapper))
+ wrapped = action_method(c)
+ def wrapper(*args, **kwargs):
+ return wrapped(config, *args, **kwargs)
+ wrapper.__name__ = name
+ wrapper.__doc__ = c.__doc__
+ self.__dict__[name] = wrapper
+ context.extends.append(c)
@classmethod
def with_context(cls, context):
@@ -592,7 +599,7 @@ class Configurator(object):
:meth:`pyramid.config.Configurator.include` to obtain a configurator
with 'the right' context. Returns a new Configurator instance."""
configurator = cls(registry=context.registry, package=context.package,
- autocommit=context.autocommit)
+ extends=context.extends, autocommit=context.autocommit)
configurator._ctx = context
return configurator
diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py
index a16323313..6b5bfbf94 100644
--- a/pyramid/tests/test_config.py
+++ b/pyramid/tests/test_config.py
@@ -3220,8 +3220,7 @@ class TestConfiguratorExtender(unittest.TestCase):
def setUp(self):
from pyramid.config import Configurator
- class Config(Configurator): pass
- self.config = Config()
+ self.config = Configurator()
def test_extend_with_dotted_name(self):
from pyramid import tests
@@ -3241,6 +3240,8 @@ class TestConfiguratorExtender(unittest.TestCase):
self.assertEqual(context_after.basepath, None)
self.assertEqual(context_after.includepath, ())
self.failUnless(context_after is context_before)
+ self.assertEqual(len(context_before.extends), 1)
+ self.assertEqual(context_before.extends, context_after.extends)
def test_extend_with_python_callable(self):
from pyramid import tests
@@ -3260,9 +3261,10 @@ class TestConfiguratorExtender(unittest.TestCase):
self.assertEqual(context_after.basepath, None)
self.assertEqual(context_after.includepath, ())
self.failUnless(context_after is context_before)
+ self.assertEqual(len(context_before.extends), 1)
+ self.assertEqual(context_before.extends, context_after.extends)
def test_extend_conflict(self):
- from pyramid import tests
from pyramid.exceptions import ConfigurationError
config = self.config
context_before = config._make_context()
@@ -3270,6 +3272,14 @@ class TestConfiguratorExtender(unittest.TestCase):
config.extend(dummy_extend)
self.assertRaises(ConfigurationError, config.extend, 'pyramid.tests.dummy_extend')
+ def test_extend_no_conflict_with_two_instance(self):
+ from pyramid.config import Configurator
+ config = self.config
+ config.extend(dummy_extend)
+ config2 = Configurator()
+ config2.extend('pyramid.tests.dummy_extend')
+ self.failUnless(config._ctx.extends != config2._ctx.extends)
+
class TestViewDeriver(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()