diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-12-19 21:14:30 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-12-19 21:14:30 +0000 |
| commit | 251ce7d166fa901b33f20cf46315ec464e3eac64 (patch) | |
| tree | 2cd31c1de76f3a915dfb702cc8c18edff27e60b1 /repoze/bfg/zcml.py | |
| parent | 12daf9e069b66aef5715a14641fc269367c091dd (diff) | |
| download | pyramid-251ce7d166fa901b33f20cf46315ec464e3eac64.tar.gz pyramid-251ce7d166fa901b33f20cf46315ec464e3eac64.tar.bz2 pyramid-251ce7d166fa901b33f20cf46315ec464e3eac64.zip | |
- Add two new APIs to the ``repoze.bfg.configuration.Configurator``
class: ``add_adapter`` and ``add_utility``. These, respectively,
perform the same functions as the ``registerAdapter`` and
``registerUtility`` functions of a ZCA registry. They were added to
allow for a more consistent testing API for applications that make
use of the ZCA directly.
- Cause the ``adapter``, ``utility``, and ``subscriber`` ZCML
directives to use a ``Configurator`` instance and associated
configurator APIs rather than a ZCA registry directly.
Diffstat (limited to 'repoze/bfg/zcml.py')
| -rw-r--r-- | repoze/bfg/zcml.py | 230 |
1 files changed, 110 insertions, 120 deletions
diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py index 7ac32fae2..90f863fb3 100644 --- a/repoze/bfg/zcml.py +++ b/repoze/bfg/zcml.py @@ -543,35 +543,50 @@ def scan(_context, package): args=(package, _context.info) ) -def zcml_configure(name, package): - """ Given a ZCML filename as ``name`` and a Python package as - ``package`` which the filename should be relative to, load the - ZCML into the current ZCML registry. - - .. note:: This feature is new as of :mod:`repoze.bfg` 1.1. +class IAdapterDirective(Interface): + """ + Register an adapter """ - context = ConfigurationMachine() - xmlconfig.registerCommonDirectives(context) - context.package = package - xmlconfig.include(context, name, package) - context.execute_actions(clear=False) # the raison d'etre - return context.actions -file_configure = zcml_configure # backwards compat (>0.8.1) + factory = Tokens( + title=u"Adapter factory/factories", + description=(u"A list of factories (usually just one) that create" + " the adapter instance."), + required=True, + value_type=GlobalObject() + ) -def handler(methodName, *args, **kwargs): - registry = get_current_registry() - method = getattr(registry, methodName) - method(*args, **kwargs) + provides = GlobalInterface( + title=u"Interface the component provides", + description=(u"This attribute specifies the interface the adapter" + " instance must provide."), + required=False, + ) -def adapter(_context, factory, provides=None, for_=None, name=''): + for_ = Tokens( + title=u"Specifications to be adapted", + description=u"This should be a list of interfaces or classes", + required=False, + value_type=GlobalObject( + missing_value=object(), + ), + ) + + name = TextLine( + title=u"Name", + description=(u"Adapters can have names.\n\n" + "This attribute allows you to specify the name for" + " this adapter."), + required=False, + ) +def adapter(_context, factory, provides=None, for_=None, name=''): if for_ is None: if len(factory) == 1: for_ = getattr(factory[0], '__component_adapts__', None) if for_ is None: - raise TypeError("No for attribute was provided and can't " + raise TypeError("No for argument was provided and can't " "determine what the factory adapts.") for_ = tuple(for_) @@ -583,7 +598,7 @@ def adapter(_context, factory, provides=None, for_=None, name=''): provides = p[0] if provides is None: - raise TypeError("Missing 'provides' attribute") + raise TypeError("Missing 'provided' argument") # Generate a single factory from multiple factories: factories = factory @@ -592,31 +607,37 @@ def adapter(_context, factory, provides=None, for_=None, name=''): elif len(factories) < 1: raise ValueError("No factory specified") elif len(factories) > 1 and len(for_) != 1: - raise ValueError("Can't use multiple factories and multiple for") + raise ValueError("Can't use multiple factories and multiple " + "for") else: factory = _rolledUpFactory(factories) - + + reg = get_current_registry() + config = Configurator(reg, package=_context.package) _context.action( discriminator = ('adapter', for_, provides, name), - callable = handler, - args = ('registerAdapter', - factory, for_, provides, name, _context.info), + callable = config.add_adapter, + args = (factory, for_, provides, name, _context.info), ) -class IAdapterDirective(Interface): +class ISubscriberDirective(Interface): """ - Register an adapter + Register a subscriber """ - factory = Tokens( - title=u"Adapter factory/factories", - description=(u"A list of factories (usually just one) that create" - " the adapter instance."), - required=True, - value_type=GlobalObject() + factory = GlobalObject( + title=u"Subscriber factory", + description=u"A factory used to create the subscriber instance.", + required=False, ) - provides = GlobalInterface( + handler = GlobalObject( + title=u"Handler", + description=u"A callable object that handles events.", + required=False, + ) + + provides = GlobalInterface( title=u"Interface the component provides", description=(u"This attribute specifies the interface the adapter" " instance must provide."), @@ -624,23 +645,14 @@ class IAdapterDirective(Interface): ) for_ = Tokens( - title=u"Specifications to be adapted", + title=u"Interfaces or classes that this subscriber depends on", description=u"This should be a list of interfaces or classes", required=False, value_type=GlobalObject( - missing_value=object(), + missing_value = object(), ), ) - name = TextLine( - title=u"Name", - description=(u"Adapters can have names.\n\n" - "This attribute allows you to specify the name for" - " this adapter."), - required=False, - ) - -_handler = handler def subscriber(_context, for_=None, factory=None, handler=None, provides=None): if factory is None: if handler is None: @@ -664,82 +676,22 @@ def subscriber(_context, for_=None, factory=None, handler=None, provides=None): for_ = tuple(for_) + reg = get_current_registry() + config = Configurator(reg, _context.package) + if handler is not None: _context.action( discriminator = None, - callable = _handler, - args = ('registerHandler', - handler, for_, u'', _context.info), + callable = config.add_subscriber, + args = (handler, for_, _context.info), ) else: _context.action( discriminator = None, - callable = _handler, - args = ('registerSubscriptionAdapter', - factory, for_, provides, u'', _context.info), + callable = config.add_subscription_adapter, + args = (factory, for_, provides, _context.info), ) -class ISubscriberDirective(Interface): - """ - Register a subscriber - """ - - factory = GlobalObject( - title=u"Subscriber factory", - description=u"A factory used to create the subscriber instance.", - required=False, - ) - - handler = GlobalObject( - title=u"Handler", - description=u"A callable object that handles events.", - required=False, - ) - - provides = GlobalInterface( - title=u"Interface the component provides", - description=(u"This attribute specifies the interface the adapter" - " instance must provide."), - required=False, - ) - - for_ = Tokens( - title=u"Interfaces or classes that this subscriber depends on", - description=u"This should be a list of interfaces or classes", - required=False, - value_type=GlobalObject( - missing_value = object(), - ), - ) - -def utility(_context, provides=None, component=None, factory=None, name=''): - if factory and component: - raise TypeError("Can't specify factory and component.") - - if provides is None: - if factory: - provides = list(implementedBy(factory)) - else: - provides = list(providedBy(component)) - if len(provides) == 1: - provides = provides[0] - else: - raise TypeError("Missing 'provides' attribute") - - if factory: - kw = dict(factory=factory) - else: - # older component registries don't accept factory as a kwarg, - # so if we don't need it, we don't pass it - kw = {} - - _context.action( - discriminator = ('utility', provides, name), - callable = handler, - args = ('registerUtility', component, provides, name), - kw = kw, - ) - class IUtilityDirective(Interface): """Register a utility.""" @@ -775,14 +727,27 @@ class IUtilityDirective(Interface): required=False, ) -def _rolledUpFactory(factories): - def factory(ob): - for f in factories: - ob = f(ob) - return ob - # Store the original factory for documentation - factory.factory = factories[0] - return factory +def utility(_context, provides=None, component=None, factory=None, name=''): + if factory and component: + raise TypeError("Can't specify factory and component.") + + if provides is None: + if factory: + provides = list(implementedBy(factory)) + else: + provides = list(providedBy(component)) + if len(provides) == 1: + provides = provides[0] + else: + raise TypeError("Missing 'provides' attribute") + + reg = get_current_registry() + config = Configurator(reg, package=_context.package) + _context.action( + discriminator = ('utility', provides, name), + callable = config.add_utility, + args = (component, provides, name, _context.info, factory), + ) def path_spec(context, path): # Convert an absolute path to a resource in a package to a @@ -805,3 +770,28 @@ def path_spec(context, path): relpath.replace(os.path.sep, '/')) return abspath +def zcml_configure(name, package): + """ Given a ZCML filename as ``name`` and a Python package as + ``package`` which the filename should be relative to, load the + ZCML into the current ZCML registry. + + .. note:: This feature is new as of :mod:`repoze.bfg` 1.1. + """ + context = ConfigurationMachine() + xmlconfig.registerCommonDirectives(context) + context.package = package + xmlconfig.include(context, name, package) + context.execute_actions(clear=False) # the raison d'etre + return context.actions + +file_configure = zcml_configure # backwards compat (>0.8.1) + +def _rolledUpFactory(factories): + def factory(ob): + for f in factories: + ob = f(ob) + return ob + # Store the original factory for documentation + factory.factory = factories[0] + return factory + |
