adaptation Package

adaptation Package

Adaptation package.

Copyright:2013 Enthought, Inc.

adaptation_error Module

Exception raised when a requested adaptation is not possible.

exception traits.adaptation.adaptation_error.AdaptationError[source]

Bases: exceptions.TypeError

Exception raised when a requested adaptation is not possible.

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 (bool) – True if the object provides the protocol, otherwise False.

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

Attempt to adapt an object to a given protocol.

Parameters:
  • adaptee – The object that we want to adapt.
  • to_protocol – The protocol that the want to adapt the object to.

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 (if default=Adaptation error), or default is returned (as in the default value passed to ‘getattr’ etc).

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.adaptation_manager = <traits.adaptation.adaptation_manager.AdaptationManager object>

The default global adaptation manager.

PROVIDED FOR BACKWARD COMPATIBILITY ONLY, IT SHOULD NEVER BE USED DIRECTLY. If you must use a global adaptation manager, use the functions get_global_adaptation_manager, reset_global_adaptation_manager, set_global_adaptation_manager.

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?

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.

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.

traits.adaptation.adapter.adapts(from_, to, extra=None, factory=None, cached=False, when='')[source]

A class advisor for declaring adapters.

Parameters:
  • from_ (type or interface) – What the adapter adapts from, or a list of such types or interfaces (the ‘_’ suffix is used because ‘from’ is a Python keyword).
  • to (type or interface) – What the adapter adapts to, or a list of such types or interfaces.
  • factory (callable) – An (optional) factory for actually creating the adapters. This is any callable that takes a single argument which is the object to be adapted. The factory should return an adapter if it can perform the adaptation and None if it cannot.
  • cached (bool) – Should the adapters be cached? If an adapter is cached, then the factory will produce at most one adapter per instance.
  • when (str) – A Python expression that selects which instances of a particular type can be adapted by this factory. The expression is evaluated in a namespace that contains a single name adaptee, which is bound to the object to be adapted (e.g., ‘adaptee.is_folder’).

Note

The cached and when arguments are ignored if factory is specified.

cached_adapter_factory Module

An adapter factory that caches adapters per instance.

class traits.adaptation.cached_adapter_factory.CachedAdapterFactory[source]

Bases: traits.has_traits.HasTraits

An adapter factory that caches adapters per instance.

We provide this class to provide the caching functionality of the old traits ‘adapts’ implementation. However, note that the cache will not be cleared unless you take care of cleaning the ‘adaptee’ trait once your adapter are deleted.

This class will be removed when the ‘adapts’ function is removed.

factory = adapter

A callable that actually creates the 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.

is_empty = Property(Bool)

True if the cache is empty, otherwise False.

This method is mostly here to help testing - the framework does not rely on it for any other purpose.