summaryrefslogtreecommitdiff
path: root/repoze/bfg/zcml.py
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2009-09-20 22:13:11 +0000
committerChris McDonough <chrism@agendaless.com>2009-09-20 22:13:11 +0000
commite4610566d881f707c01d266a7e336084029c83e4 (patch)
tree0a2001361b14e5b0b7521164e67560e2763e4c3c /repoze/bfg/zcml.py
parent160f01439dc3e0d865b2e77bb4a9a7c9e7a16c1a (diff)
downloadpyramid-e4610566d881f707c01d266a7e336084029c83e4.tar.gz
pyramid-e4610566d881f707c01d266a7e336084029c83e4.tar.bz2
pyramid-e4610566d881f707c01d266a7e336084029c83e4.zip
- The way ``bfg_view`` declarations are scanned for has been modified.
This should have no external effects. - An object implementing the ``IRenderer`` interface (and ``ITemplateRenderer`, which is a subclass of ``IRenderer``) must now accept an extra ``system`` argument in its ``__call__`` method implementation. Values computed by the system (as opposed to by the view) are passed by the system in the ``system`` parameter, which will always be a dictionary. Keys in the dictionary include: ``view`` (the view object that returned the value), ``renderer_name`` (the template name or simple name of the renderer), ``context`` (the context object passed to the view), and ``request`` (the request object passed to the view). Previously only ITemplateRenderers received system arguments as elements inside the main ``value`` dictionary.
Diffstat (limited to 'repoze/bfg/zcml.py')
-rw-r--r--repoze/bfg/zcml.py77
1 files changed, 50 insertions, 27 deletions
diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py
index a3ddd8527..d5e7ffd67 100644
--- a/repoze/bfg/zcml.py
+++ b/repoze/bfg/zcml.py
@@ -1,3 +1,4 @@
+import inspect
import sys
import types
@@ -20,6 +21,7 @@ from zope.schema import Int
from zope.schema import TextLine
import martian
+import martian.core
from repoze.bfg.interfaces import IAuthenticationPolicy
from repoze.bfg.interfaces import IAuthorizationPolicy
@@ -557,20 +559,6 @@ def renderer(_context, factory, name=''):
sm.registerUtility(factory, IRendererFactory, name=name)
_context.action(discriminator=(IRendererFactory, name))
-class IScanDirective(Interface):
- package = GlobalObject(
- title=u"The package we'd like to scan.",
- required=True,
- )
-
-def scan(_context, package, martian=martian):
- # martian overrideable only for unit tests
- module_grokker = martian.ModuleGrokker()
- module_grokker.register(BFGViewFunctionGrokker())
- martian.grok_dotted_name(package.__name__, grokker=module_grokker,
- context=_context, exclude_filter=exclude)
-
-
class IStaticDirective(Interface):
name = TextLine(
title=u"The URL prefix of the static view",
@@ -598,21 +586,25 @@ def static(_context, name, path, cache_max_age=3600):
route(_context, name, "%s*subpath" % name, view=view,
view_for=StaticRootFactory, factory=StaticRootFactory(path))
-################# utility stuff ####################
-
-def zcml_configure(name, package):
- context = zope.configuration.config.ConfigurationMachine()
- xmlconfig.registerCommonDirectives(context)
- context.package = package
- xmlconfig.include(context, name, package)
- context.execute_actions(clear=False)
- return context.actions
+class IScanDirective(Interface):
+ package = GlobalObject(
+ title=u"The package we'd like to scan.",
+ required=True,
+ )
-file_configure = zcml_configure # backwards compat (>0.8.1)
+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())
+ module_grokker = martian.ModuleGrokker(grokker=multi_grokker)
+ martian.grok_dotted_name(package.__name__, grokker=module_grokker,
+ context=_context, exclude_filter=exclude)
-class BFGViewFunctionGrokker(martian.InstanceGrokker):
- martian.component(types.FunctionType)
+################# utility stuff ####################
+class BFGViewGrokker(object):
def grok(self, name, obj, **kw):
if hasattr(obj, '__is_bfg_view__'):
permission = obj.__permission__
@@ -624,21 +616,52 @@ class BFGViewFunctionGrokker(martian.InstanceGrokker):
request_param = obj.__request_param__
containment = obj.__containment__
wrapper = obj.__wrapper_viewname__
+ attr = obj.__attr__
+ renderer = obj.__renderer__
context = kw['context']
view(context, permission=permission, for_=for_,
view=obj, name=name, request_type=request_type,
route_name=route_name, request_method=request_method,
request_param=request_param, containment=containment,
- wrapper=wrapper)
+ attr=attr, renderer=renderer, wrapper=wrapper)
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)"""
+def zcml_configure(name, package):
+ context = zope.configuration.config.ConfigurationMachine()
+ xmlconfig.registerCommonDirectives(context)
+ context.package = package
+ xmlconfig.include(context, name, package)
+ context.execute_actions(clear=False)
+ return context.actions
+
+file_configure = zcml_configure # backwards compat (>0.8.1)
+