From 84a58a9fec53e9b7fca604a413001a13065ee2d6 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Tue, 7 Dec 2010 19:09:14 -0500 Subject: add a with_context method for use by ZCML directives --- pyramid/configuration.py | 6 +++ pyramid/zcml.py | 103 ++++++++--------------------------------------- 2 files changed, 23 insertions(+), 86 deletions(-) diff --git a/pyramid/configuration.py b/pyramid/configuration.py index 269d7604d..60c0220f8 100644 --- a/pyramid/configuration.py +++ b/pyramid/configuration.py @@ -269,6 +269,12 @@ class Configurator(object): session_factory=session_factory, ) + @classmethod + def with_context(cls, context): + configurator = cls(registry=context.registry, package=context.package) + configurator._ctx = context + return configurator + def _action(self, discriminator, callable=None, args=(), kw=None, order=0): """ Register an action which will be executed during a commit. """ if kw is None: diff --git a/pyramid/zcml.py b/pyramid/zcml.py index 81c230250..d0db53bee 100644 --- a/pyramid/zcml.py +++ b/pyramid/zcml.py @@ -163,18 +163,13 @@ def view( cacheable=True, # not used, here for b/w compat < 0.8 ): - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - if renderer is not None: package = getattr(_context, 'package', None) renderer = {'name':renderer, 'package':package} context = context or for_ - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.add_view( permission=permission, context=context, view=view, name=name, request_type=request_type, route_name=route_name, @@ -261,11 +256,6 @@ def route(_context, # these are route predicates; if they do not match, the next route # in the routelist will be tried - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - if view_context is None: view_context = view_for or for_ @@ -278,7 +268,7 @@ def route(_context, if pattern is None: raise ConfigurationError('route directive must include a "pattern"') - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.add_route( name, pattern, @@ -327,11 +317,7 @@ def notfound(_context, renderer=None, wrapper=None): - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.set_notfound_view(view=view, attr=attr, renderer=renderer, wrapper=wrapper) @@ -342,11 +328,7 @@ def forbidden(_context, renderer=None, wrapper=None): - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.set_forbidden_view(view=view, attr=attr, renderer=renderer, wrapper=wrapper) @@ -366,12 +348,7 @@ class IResourceDirective(Interface): required=True) def resource(_context, to_override, override_with, _override=None): - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.override_resource(to_override, override_with, _override=_override) class IRepozeWho1AuthenticationPolicyDirective(Interface): @@ -385,11 +362,7 @@ def repozewho1authenticationpolicy(_context, identifier_name='auth_tkt', callback=callback) # authentication policies must be registered eagerly so they can # be found by the view registration machinery - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config._set_authentication_policy(policy) class IRemoteUserAuthenticationPolicyDirective(Interface): @@ -403,11 +376,7 @@ def remoteuserauthenticationpolicy(_context, environ_key='REMOTE_USER', callback=callback) # authentication policies must be registered eagerly so they can # be found by the view registration machinery - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config._set_authentication_policy(policy) class IAuthTktAuthenticationPolicyDirective(Interface): @@ -449,11 +418,7 @@ def authtktauthenticationpolicy(_context, raise ConfigurationError(str(why)) # authentication policies must be registered eagerly so they can # be found by the view registration machinery - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config._set_authentication_policy(policy) class IACLAuthorizationPolicyDirective(Interface): @@ -463,11 +428,7 @@ def aclauthorizationpolicy(_context): policy = ACLAuthorizationPolicy() # authorization policies must be registered eagerly so they can be # found by the view registration machinery - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config._set_authorization_policy(policy) class IRendererDirective(Interface): @@ -482,11 +443,7 @@ class IRendererDirective(Interface): def renderer(_context, factory, name=''): # renderer factories must be registered eagerly so they can be # found by the view machinery - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.add_renderer(name, factory) class IStaticDirective(Interface): @@ -517,11 +474,7 @@ def static(_context, name, path, cache_max_age=3600, permission='__no_permission_required__'): """ Handle ``static`` ZCML directives """ - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.add_static_view(name, path, cache_max_age=cache_max_age, permission=permission) @@ -532,11 +485,7 @@ class IScanDirective(Interface): ) def scan(_context, package): - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.scan(package) class ITranslationDirDirective(Interface): @@ -548,12 +497,7 @@ class ITranslationDirDirective(Interface): def translationdir(_context, dir): path = path_spec(_context, dir) - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.add_translation_dirs(path) class ILocaleNegotiatorDirective(Interface): @@ -564,11 +508,7 @@ class ILocaleNegotiatorDirective(Interface): ) def localenegotiator(_context, negotiator): - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.set_locale_negotiator(negotiator) class IAdapterDirective(Interface): @@ -707,17 +647,12 @@ def subscriber(_context, for_=None, factory=None, handler=None, provides=None): for_ = tuple(for_) - try: - registry = _context.registry - except AttributeError: # pragma: no cover (b/c) - registry = get_current_registry() - - config = Configurator(registry=registry, package=_context.package, - _ctx=_context) + config = Configurator.with_context(_context) if handler is not None: config.add_subscriber(handler, for_) else: + registry = _context.registry _context.action( discriminator = None, callable = registry.registerSubscriptionAdapter, @@ -799,11 +734,7 @@ def default_permission(_context, name): """ Register a default permission name """ # the default permission must be registered eagerly so it can # be found by the view registration machinery - try: - reg = _context.registry - except AttributeError: # pragma: no cover (b/c) - reg = get_current_registry() - config = Configurator(reg, package=_context.package, _ctx=_context) + config = Configurator.with_context(_context) config.set_default_permission(name) def path_spec(context, path): -- cgit v1.2.3