aglyph.integration.cherrypy — Integrating Aglyph with CherryPy¶
Classes and utilities for integrating Aglyph with CherryPy.
New in version 2.1.0.
An example using aglyph.context.XMLContext:
from aglyph.assembler import Assembler
from aglyph.context import XMLContext
from aglyph.integration.cherrypy import AglyphDIPlugin
import cherrypy
context = XMLContext("my-aglyph-context.xml")
assembler = Assembler(context)
cherrypy.engine.aglyph = AglyphDIPlugin(cherrypy.engine, assembler)
cherrypy.engine.aglyph.subscribe()
An example using aglyph.binder.Binder:
from aglyph.integration.cherrypy import AglyphDIPlugin
from bindings import binder
import cherrypy
cherrypy.engine.aglyph = AglyphDIPlugin(cherrypy.engine, binder)
cherrypy.engine.aglyph.subscribe()
In either scenario, you can now use Aglyph to assemble components in your CherryPy application by publishing an “aglyph-assemble” event to the Web Site Process Bus. This event requires an Aglyph component specification (either an ID or an object whose dotted name is a component ID):
...
my_obj = cherrypy.engine.publish("aglyph-assemble", "my-id").pop()
...
- class aglyph.integration.cherrypy.AglyphDIPlugin(bus, assembler, eager_init=True)[source]¶
Bases: cherrypy.process.plugins.SimplePlugin
A CherryPy plugin that provides Aglyph dependency injection support to CherryPy applications.
The Aglyph DI plugin subscribes to the following channels:
- aglyph-assemble
- Publish a component ID to this channel to assemble the component.
- aglyph-init-singletons
- Publish to this channel to pre-assemble and cache all singleton components.
- aglyph-clear-singletons
- Publish to this channel to clear all cached singleton components.
- aglyph-init-borgs
- Publish to this channel to pre-assemble and cache the shared-states of all borg components.
- aglyph-clear-borgs
- Publish to this channel to clear all cached borg components.
- aglyph-clear-weakrefs
- Publish to this channel to clear all cached weakref components.
Parameters: - bus (cherrypy.process.wspbus.Bus) – the CherryPy Web Site Process Bus
- assembler (aglyph.assembler.Assembler) – the configured Aglyph assembler (or binder)
- eager_init (bool) – if True, all singleton and borg components in the assembler’s context will be pre-assembed and cached when the Aglyph DI plugin is started
- start()[source]¶
Subscribe to all Aglyph DI channels.
- aglyph-assemble
- Publish a component ID to this channel to assemble the component.
- aglyph-init-singletons
- Publish to this channel to pre-assemble and cache all singleton components.
- aglyph-clear-singletons
- Publish to this channel to clear all cached singleton components.
- aglyph-init-borgs
- Publish to this channel to pre-assemble and cache the shared-states of all borg components.
- aglyph-clear-borgs
- Publish to this channel to clear all cached borg components.
- aglyph-clear-weakrefs
- Publish to this channel to clear all cached weakref components.
Note
If eager_init is True, all singleton and borg components are pre-assembled and cached before the channels are subscribed.
- stop()[source]¶
Unsubscribe from all Aglyph DI channels.
Note
After all Aglyph DI channels have been unsubscribed, the singleton, borg, and weakref caches are automatically cleared.
- assemble(component_spec)[source]¶
Return the object assembled according to component_spec.
Parameters: component_spec – a string representing a component dotted name or unique ID; or an importable class, function, or module Returns: a complete object with all of its resolved dependencies This method handles messages published to the aglyph-assemble channel.
- init_singletons()[source]¶
Assemble and cache all singleton component objects.
Returns: the initialized singleton component IDs Return type: list This method handles messages published to the aglyph-init-singletons channel.
- clear_singletons()[source]¶
Evict all cached singleton component objects.
Returns: the evicted singleton component IDs Return type: list This method handles messages published to the aglyph-clear-singletons channel.
- init_borgs()[source]¶
Assemble and cache the shared-states for all borg component objects.
Returns: the initialized borg component IDs Return type: list This method handles messages published to the aglyph-init-borgs channel.