aglyph.binder — The Aglyph component binder

New in version 1.1.0.

The Aglyph binder provides a much simpler and more compact alternative to aglyph.assembler.Assembler, aglyph.context.Context, and aglyph.component.Component for programmatic configuration of Aglyph.

“Binding” usually results in much less configuration code. An aglyph.binder.Binder also offers the same functionality as an aglyph.assembler.Assembler, so the same object used to configure injection can be used to perform injection, further reducing the amount of “bootstrap” code needed.

For example, the following two blocks exhibit identical behavior:

Using Assembler / Context / Component:

context = Context("context-id")
assembler = Assembler(context)
component = Component("my.package.MyClass")
component.init_args.append("arg")
component.init_keywords["kw"] = "value"
component.attributes["set_spam"] = "eggs"
context.add(component)
my = assembler.assemble("my.package.MyClass")

Using Binder:

binder = Binder()
binder.bind(MyClass).init("arg", kw="value").attributes(set_spam="eggs")
my = binder.lookup(MyClass)
class aglyph.binder.Binder(binder_id=None)[source]

Bases: aglyph.assembler.Assembler

Configure application components and provide assembly services (see lookup()) for those components.

binder_id is a unique identifier for this instance. If it is not provided, a random ID is generated.

binder_id[source]

a read-only property for the binder ID

bind(component_spec, to=None, strategy='prototype')[source]

Define a component by associating the unique ID for component_spec with the dotted-name for to.

component_spec any importable class or unbound function, or a user-defined identifier, that will serve as the lookup() key for objects of the component.

to is the importable class or unbound function, or the dotted-name for the same, that will be called to product objects of the component.

If to is not provided, the component ID and dotted-name will be identical. In this case component_spec must be a class, a function, or a dotted-name.

strategy must be a recognized component assembly strategy, and defaults to aglyph.component.Strategy.PROTOTYPE (“prototype”) if not specified.

Please see aglyph.component.Strategy for a description of the component assembly strategies supported by Aglyph.

Warning

The aglyph.component.Strategy.BORG (“borg”) component assembly strategy is only supported for classes that do not define or inherit __slots__!

Returns:a _Binding object that allows the component dependencies to be defined in chained-call fashion.
lookup(component_spec)[source]

Return an instance of the component specified by component_spec with all of its dependencies provided.

component_spec must be an importable class or unbound function, or a user-defined unique identifier, that was previously bound by a call to bind().

class aglyph.binder._Binding(component)[source]

Bases: object

Note

This class is not intended to be imported or created directly; instances are returned automatically from the Binder.bind() method.

A _Binding allows component dependencies to be defined in chained-call fashion:

Binder().bind(...).init(*args, **keywords).attributes(**keywords)

Or, if you don’t prefer chained calls:

binding = Binder().bind(...)
binding.init(*args, **keywords)
binding.attributes(**keywords)

component is an aglyph.component.Component that was created by a call to Binder.bind().

init(*args, **keywords)[source]

Define the initialization dependencies (i.e. position and/or keyword arguments) for a component.

args are the positional argument dependencies, and keywords are the keyword argument dependencies.

Warning

Multiple calls to this method on the same instance are not cumulative; each call will replace the aglyph.component.Component.init_args list and the aglyph.component.Component.init_keywords map.

attributes(**keywords)[source]

Define the attribute dependencies (i.e. fields, setter methods, and/or properties) for a component.

keywords are the field, setter method, and/or property dependencies. Each keyword name corresponds to the name of a simple attribute (field), setter method, or property.

Warning

Multiple calls to this method on the same instance are not cumulative; each call will replace the aglyph.component.Component.attributes map.

Previous topic

aglyph.assembler — The Aglyph component assembler

Next topic

aglyph.cache — Simple object caching support for Aglyph

This Page