diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-09-20 22:56:37 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-09-20 22:56:37 +0000 |
| commit | 022873d909055040a39f24fe3df34f4c154f9102 (patch) | |
| tree | 964cc5a9dde24f86f101cfabb15a5841de0f07f6 | |
| parent | e4610566d881f707c01d266a7e336084029c83e4 (diff) | |
| download | pyramid-022873d909055040a39f24fe3df34f4c154f9102.tar.gz pyramid-022873d909055040a39f24fe3df34f4c154f9102.tar.bz2 pyramid-022873d909055040a39f24fe3df34f4c154f9102.zip | |
Make instance grokking work again.
| -rw-r--r-- | repoze/bfg/tests/grokkedapp/__init__.py | 7 | ||||
| -rw-r--r-- | repoze/bfg/tests/grokkedapp/another.py | 7 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_integration.py | 30 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_zcml.py | 10 | ||||
| -rw-r--r-- | repoze/bfg/zcml.py | 39 |
5 files changed, 58 insertions, 35 deletions
diff --git a/repoze/bfg/tests/grokkedapp/__init__.py b/repoze/bfg/tests/grokkedapp/__init__.py index c58ee428a..f4a924292 100644 --- a/repoze/bfg/tests/grokkedapp/__init__.py +++ b/repoze/bfg/tests/grokkedapp/__init__.py @@ -29,6 +29,13 @@ class grokked_class(object): grokked_class = bfg_view(name='grokked_class')(grokked_class) +class Foo(object): + def __call__(self, context, request): + return 'grokked_instance' + +grokked_instance = Foo() +grokked_instance = bfg_view(name='grokked_instance')(grokked_instance) + # ungrokkable A = 1 diff --git a/repoze/bfg/tests/grokkedapp/another.py b/repoze/bfg/tests/grokkedapp/another.py index b97110ab0..94be9f74a 100644 --- a/repoze/bfg/tests/grokkedapp/another.py +++ b/repoze/bfg/tests/grokkedapp/another.py @@ -29,6 +29,13 @@ class grokked_class(object): grokked_class = bfg_view(name='another_grokked_class')(grokked_class) +class Foo(object): + def __call__(self, context, request): + return 'another_grokked_instance' + +grokked_instance = Foo() +grokked_instance = bfg_view(name='another_grokked_instance')(grokked_instance) + # ungrokkable A = 1 diff --git a/repoze/bfg/tests/test_integration.py b/repoze/bfg/tests/test_integration.py index bcd29d294..928e94429 100644 --- a/repoze/bfg/tests/test_integration.py +++ b/repoze/bfg/tests/test_integration.py @@ -121,36 +121,48 @@ class TestGrokkedApp(unittest.TestCase): action = actions[-3] self.assertEqual(action[0][1], None) - self.assertEqual(action[0][2], 'another_grokked_class') + self.assertEqual(action[0][2], 'another_grokked_instance') self.assertEqual(action[0][3], IRequest) self.assertEqual(action[0][4], IView) action = actions[-4] self.assertEqual(action[0][1], None) - self.assertEqual(action[0][2], 'another') + self.assertEqual(action[0][2], 'another_grokked_class') self.assertEqual(action[0][3], IRequest) self.assertEqual(action[0][4], IView) action = actions[-5] self.assertEqual(action[0][1], None) - self.assertEqual(action[0][2], 'oldstyle_grokked_class') + self.assertEqual(action[0][2], 'another') self.assertEqual(action[0][3], IRequest) self.assertEqual(action[0][4], IView) - + action = actions[-6] self.assertEqual(action[0][1], None) - self.assertEqual(action[0][2], '') + self.assertEqual(action[0][2], 'oldstyle_grokked_class') self.assertEqual(action[0][3], IRequest) self.assertEqual(action[0][4], IView) action = actions[-7] self.assertEqual(action[0][1], None) - self.assertEqual(action[0][2], 'grokked_class') + self.assertEqual(action[0][2], '') self.assertEqual(action[0][3], IRequest) self.assertEqual(action[0][4], IView) action = actions[-8] self.assertEqual(action[0][1], None) + self.assertEqual(action[0][2], 'grokked_instance') + self.assertEqual(action[0][3], IRequest) + self.assertEqual(action[0][4], IView) + + action = actions[-9] + self.assertEqual(action[0][1], None) + self.assertEqual(action[0][2], 'grokked_class') + self.assertEqual(action[0][3], IRequest) + self.assertEqual(action[0][4], IView) + + action = actions[-10] + self.assertEqual(action[0][1], None) self.assertEqual(action[0][2], '') self.assertEqual(action[0][3], IRequest) self.assertEqual(action[0][4], IView) @@ -170,6 +182,9 @@ class TestGrokkedApp(unittest.TestCase): result= render_view_to_response(ctx, req, 'grokked_class') self.assertEqual(result, 'grokked_class') + result= render_view_to_response(ctx, req, 'grokked_instance') + self.assertEqual(result, 'grokked_instance') + result= render_view_to_response(ctx, req, 'oldstyle_grokked_class') self.assertEqual(result, 'oldstyle_grokked_class') @@ -184,6 +199,9 @@ class TestGrokkedApp(unittest.TestCase): result= render_view_to_response(ctx, req, 'another_grokked_class') self.assertEqual(result, 'another_grokked_class') + result= render_view_to_response(ctx, req, 'another_grokked_instance') + self.assertEqual(result, 'another_grokked_instance') + result= render_view_to_response(ctx, req, 'another_oldstyle_grokked_class') self.assertEqual(result, 'another_oldstyle_grokked_class') diff --git a/repoze/bfg/tests/test_zcml.py b/repoze/bfg/tests/test_zcml.py index 79354417f..5be8b7fb2 100644 --- a/repoze/bfg/tests/test_zcml.py +++ b/repoze/bfg/tests/test_zcml.py @@ -1808,7 +1808,7 @@ class TestZCMLConfigure(unittest.TestCase): self.assertRaises(IOError, self._callFUT, 'configure.zcml', self.module) -class TestBFGViewFunctionGrokker(unittest.TestCase): +class TestBFGViewGrokker(unittest.TestCase): def setUp(self): cleanUp() @@ -1816,8 +1816,8 @@ class TestBFGViewFunctionGrokker(unittest.TestCase): cleanUp() def _getTargetClass(self): - from repoze.bfg.zcml import BFGViewFunctionGrokker - return BFGViewFunctionGrokker + from repoze.bfg.zcml import BFGViewGrokker + return BFGViewGrokker def _makeOne(self, *arg, **kw): return self._getTargetClass()(*arg, **kw) @@ -1881,7 +1881,7 @@ class TestZCMLScanDirective(unittest.TestCase): return scan(context, package, martian) def test_it(self): - from repoze.bfg.zcml import SimpleMultiGrokker + from repoze.bfg.zcml import BFGMultiGrokker from repoze.bfg.zcml import exclude martian = DummyMartianModule() module_grokker = DummyModuleGrokker() @@ -1889,7 +1889,7 @@ class TestZCMLScanDirective(unittest.TestCase): self._callFUT(None, dummy_module, martian) self.assertEqual(martian.name, 'dummy') multi_grokker = martian.module_grokker.multi_grokker - self.assertEqual(multi_grokker.__class__, SimpleMultiGrokker) + self.assertEqual(multi_grokker.__class__, BFGMultiGrokker) self.assertEqual(martian.context, None) self.assertEqual(martian.exclude_filter, exclude) diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py index d5e7ffd67..d597f860f 100644 --- a/repoze/bfg/zcml.py +++ b/repoze/bfg/zcml.py @@ -594,17 +594,27 @@ class IScanDirective(Interface): def scan(_context, package, martian=martian): # martian overrideable only for unit tests - multi_grokker = SimpleMultiGrokker() - multi_grokker.register(BFGViewFunctionGrokker()) - multi_grokker.register(BFGViewNewStyleClassGrokker()) - multi_grokker.register(BFGViewOldStyleClassGrokker()) + multi_grokker = BFGMultiGrokker() + multi_grokker.register(BFGViewGrokker()) +## multi_grokker.register(BFGViewNewStyleClassGrokker()) +## multi_grokker.register(BFGViewOldStyleClassGrokker()) module_grokker = martian.ModuleGrokker(grokker=multi_grokker) martian.grok_dotted_name(package.__name__, grokker=module_grokker, context=_context, exclude_filter=exclude) ################# utility stuff #################### -class BFGViewGrokker(object): +class BFGViewMarker(object): + pass + +class BFGMultiGrokker(martian.core.MultiInstanceOrClassGrokkerBase): + def get_bases(self, obj): + if hasattr(obj, '__is_bfg_view__'): + return [BFGViewMarker] + return [] + +class BFGViewGrokker(martian.InstanceGrokker): + martian.component(BFGViewMarker) def grok(self, name, obj, **kw): if hasattr(obj, '__is_bfg_view__'): permission = obj.__permission__ @@ -627,30 +637,11 @@ class BFGViewGrokker(object): return True return False -class BFGViewFunctionGrokker(BFGViewGrokker, martian.InstanceGrokker): - martian.component(types.FunctionType) - -class BFGViewOldStyleClassGrokker(BFGViewGrokker, martian.InstanceGrokker): - martian.component(types.ClassType) - -class BFGViewNewStyleClassGrokker(BFGViewGrokker, martian.InstanceGrokker): - martian.component(type) - def exclude(name): if name.startswith('.'): return True return False -class SimpleMultiGrokker(martian.core.MultiInstanceOrClassGrokkerBase): - # this is an amalgam of martian.core.MultiInstanceGrokker and - # martian.core.MultiClassGrokker. - def get_bases(self, obj): - if type(obj) is types.ModuleType: - return [] - if hasattr(obj, '__class__'): - return inspect.getmro(obj.__class__) - return [types.ClassType] - class Uncacheable(object): """ Include in discriminators of actions which are not cacheable; this class only exists for backwards compatibility (<0.8.1)""" |
