aglyph.component — Defining components and their dependencies

The classes in this module are used to define components and their dependencies.

class aglyph.component.Strategy[source]

Bases: object

Define the component assembly strategies recognized by Aglyph.

The default component assembly strategy for Aglyph is Strategy.PROTOTYPE (“prototype”).

PROTOTYPE

a new instance of the component is always created, initialized, wired, and returned

SINGLETON

only one instance is created, initialized, and wired; the instance is cached the first time the component is assembled, and subsequent assembly requests return the cached instance

BORG

a new instance is always created; the internal state is cached the first time the component is assembled, and thereafter all new instances share this same state

class aglyph.component.Reference[source]

Bases: unicode

A place-holder used to refer to another aglyph.component.Component.

A Reference is used as an alias to identify a component that is a dependency of another component. The value of a Reference can be either a dotted-name or a user-provided unique ID.

A Reference value MUST correspond to a component ID in the same context.

A Reference can be used as an argument for an aglyph.component.Evaluator, and can be assembled directly by an aglyph.assembler.Assembler.

Warning

In Python versions < 3.0, a Reference representing a dotted-name must consist only of characters in the ASCII subset of the source encoding (see PEP 0263).

But in Python versions >= 3.0, a Reference representing a dotted-name may contain non-ASCII characters (see PEP 3131).

Because of this difference, the super class of Reference is “dynamic” with respect to the version of Python under which Aglyph is running (unicode under Python 2.5 - 2.7, str under Python >= 3.0). This documentation shows the base class as unicode because the documentation generator runs under Python 2.7.

class aglyph.component.Evaluator(func, *args, **keywords)[source]

Bases: object

Enable lazy creation of objects.

An Evaluator is similar to a functools.partial() in that they both collect a function and related arguments into a callable object with a simplified signature that can be called repeatedly to produce a new object.

Unlike a partial function, an Evaluator may have arguments that are not truly “frozen,” in the sense that any argument may be defined as an aglyph.component.Reference, a functools.partial(), or even another Evaluator, which needs to be resolved (i.e. assembled/called) before calling func.

An Evaluator is initialized similar to a functools.partial():

func must be a callable object that returns new objects, args is a tuple of positional arguments, and keywords is a mapping of keyword arguments.

func[source]

a read-only property for the callable

args[source]

a read-only property for the positional arguments

keywords[source]

a read-only property for the keyword arguments

class aglyph.component.Component(component_id, dotted_name=None, strategy='prototype')[source]

Bases: object

Define a component and the dependencies needed to create a new object of that component at runtime.

Only a component ID is strictly required to define a component.

component_id must be a valid “relative_module.identifier” dotted-name string or a user-provided unique component identifier.

dotted_name, if provided, must be a valid “relative_module.identifier” dotted-name string. If it is not provided, component_id is assumed to be a dotted-name.

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

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

Warning

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

Once a Component instance is initialized, the init_args (list), init_keywords (dict), and attributes (dict) members can be modified in-place to define the dependencies that must be injected into objects of this component at assembly time. For example:

component = Component("http.client.HTTPConnection")
component.init_args.append("www.ninthtest.net")
component.init_args.append(80)
component.init_keywords["strict"] = True
component.attributes["set_debuglevel"] = 1

In Aglyph, a component may:

component_id[source]

a read-only property for the component ID

dotted_name[source]

a read-only property for the component dotted-name

strategy[source]

a read-only property for the component assembly strategy

Previous topic

aglyph.cache — Simple object caching support for Aglyph

Next topic

aglyph.context — Defining component contexts

This Page