aglyph.integration.cherrypy — Integrating Aglyph with CherryPy — Aglyph 3.0.0 documentation

# `aglyph.integration.cherrypy` — Integrating Aglyph with CherryPy

Release: | 3.0.0

Classes and utilities for integrating Aglyph with CherryPy.

New in version 2.1.0.

An example using XML configuration:

    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 the fluent configuration API:

    from aglyph.integration.cherrypy import AglyphDIPlugin
    from aglyph.context import Context
    import cherrypy

    context = Context("my-aglyph-context")
    context.template(...)
    context.prototype(...)
    context.singleton(...)
    context.borg(...)
    context.weakref(...)
    # and so on
    assembler = Assembler(context)

    cherrypy.engine.aglyph = AglyphDIPlugin(cherrypy.engine, assembler)
    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)

  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

  `eager_init`

    Return the current value of the eager initialization flag.

  `start`()

    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`()

    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)

    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`()

    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`()

    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`()

    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.

  `clear_borgs`()

    Evict all cached borg component shared-states.

    Returns: | the evicted borg component IDs
    Return type: | `list`

    This method handles messages published to the aglyph-clear-borgs channel.

  `clear_weakrefs`()

    Evict all cached weakref component objects.

    Returns: | the evicted weakref component IDs
    Return type: | `list`

    This method handles messages published to the aglyph-clear-weakrefs channel.