traits.adaptation Package

Adaptation package.

traits.adaptation.adaptation_error Module

Exception raised when a requested adaptation is not possible.

exception traits.adaptation.adaptation_error.AdaptationError[source]

Bases: TypeError

Exception raised when a requested adaptation is not possible.

traits.adaptation.adaptation_manager Module

Manages all registered adaptations.

traits.adaptation.adaptation_manager.no_adapter_necessary(adaptee)[source]

An adapter factory used to register that a protocol provides another.

See ‘register_provides’ for details.

class traits.adaptation.adaptation_manager.AdaptationManager[source]

Bases: traits.has_traits.HasTraits

Manages all registered adaptations.

static mro_distance_to_protocol(from_type, to_protocol)[source]

Return the distance in the MRO from ‘from_type’ to ‘to_protocol’.

If from_type provides to_protocol, returns the distance between from_type and the super-most class in the MRO hierarchy providing to_protocol (that’s where the protocol was provided in the first place).

If from_type does not provide to_protocol, return None.

static provides_protocol(type_, protocol)[source]

Does the given type provide (i.e implement) a given protocol?

Parameters
  • type – Python ‘type’.

  • protocol – Either a regular Python class or a traits Interface.

Returns

result – True if the object provides the protocol, otherwise False.

Return type

bool

adapt(adaptee, to_protocol, default=<class 'traits.adaptation.adaptation_error.AdaptationError'>)[source]

Attempt to adapt an object to a given protocol.

If adaptee already provides (i.e. implements) the given protocol then it is simply returned unchanged.

Otherwise, we try to build a chain of adapters that adapt adaptee to to_protocol. If no such adaptation is possible then either an AdaptationError is raised, or default is returned.

Parameters
  • adaptee (object) – The object that we want to adapt.

  • to_protocol (type or interface) – The protocol that the want to adapt adaptee to.

  • default (object, optional) – Object to return if no adaptation is possible. If no default is provided, and adaptation fails, an AdaptationError is raised.

Returns

adapted_object – The original adaptee adapted to the target protocol.

Return type

to_protocol

Raises

AdaptationError – If adaptation is not possible, and no default is given.

register_offer(offer)[source]

Register an offer to adapt from one protocol to another.

register_factory(factory, from_protocol, to_protocol)[source]

Register an adapter factory.

This is a simply a convenience method that creates and registers an ‘AdaptationOffer’ from the given arguments.

register_provides(provider_protocol, protocol)[source]

Register that a protocol provides another.

supports_protocol(obj, protocol)[source]

Does the object support a given protocol?

An object “supports” a protocol if either it “provides” it directly, or it can be adapted to it.

traits.adaptation.adaptation_manager.set_global_adaptation_manager(new_adaptation_manager)[source]

Set the global adaptation manager to the given instance.

traits.adaptation.adaptation_manager.reset_global_adaptation_manager()[source]

Set the global adaptation manager to a new AdaptationManager instance.

traits.adaptation.adaptation_manager.get_global_adaptation_manager()[source]

Set a reference to the global adaptation manager.

traits.adaptation.adaptation_manager.adapt(adaptee, to_protocol, default=<class 'traits.adaptation.adaptation_error.AdaptationError'>)[source]

Attempt to adapt an object to a given protocol.

traits.adaptation.adaptation_manager.register_factory(factory, from_protocol, to_protocol)[source]

Register an adapter factory.

traits.adaptation.adaptation_manager.register_offer(offer)[source]

Register an offer to adapt from one protocol to another.

traits.adaptation.adaptation_manager.register_provides(provider_protocol, protocol)[source]

Register that a protocol provides another.

traits.adaptation.adaptation_manager.supports_protocol(obj, protocol)[source]

Does the object support a given protocol?

traits.adaptation.adaptation_manager.provides_protocol(type_, protocol)[source]

Does the given type provide (i.e implement) a given protocol?

traits.adaptation.adaptation_offer Module

An offer to provide adapters from one protocol to another.

class traits.adaptation.adaptation_offer.AdaptationOffer[source]

Bases: traits.has_traits.HasTraits

An offer to provide adapters from one protocol to another.

An adaptation offer consists of a factory that can create adapters, and the protocols that define what the adapters adapt from and to.

factory = Property(Any)

A factory for creating adapters.

The factory must ba callable that takes exactly one argument which is the object to be adapted (known as the adaptee), and returns an adapter from the from_protocol to the to_protocol.

The factory can be specified as either a callable, or a string in the form ‘foo.bar.baz’ which is turned into an import statement ‘from foo.bar import baz’ and imported when the trait is first accessed.

from_protocol = Property(Any)

Adapters created by the factory adapt from this protocol.

The protocol can be specified as a protocol (class/Interface), or a string in the form ‘foo.bar.baz’ which is turned into an import statement ‘from foo.bar import baz’ and imported when the trait is accessed.

to_protocol = Property(Any)

Adapters created by the factory adapt to this protocol.

The protocol can be specified as a protocol (class/Interface), or a string in the form ‘foo.bar.baz’ which is turned into an import statement ‘from foo.bar import baz’ and imported when the trait is accessed.

traits.adaptation.adapter Module

Base classes for adapters.

Adapters do not have to inherit from these classes, as long as their constructor takes the object to be adapted as the first and only positional argument.

class traits.adaptation.adapter.PurePythonAdapter(adaptee)[source]

Bases: object

Base class for pure Python adapters.

class traits.adaptation.adapter.Adapter(adaptee, **traits)[source]

Bases: traits.has_traits.HasTraits

Base class for adapters with traits.