Package AccessEngine :: Module AEAccAdapter
[hide private]
[frames] | no frames]

Module AEAccAdapter

source code

Defines machinery for stating interfaces and adapting objects to those interfaces on demand.


Authors:
Peter Parente, Frank Zenker
Organizations: Copyright:

License: The BSD License All rights reserved. This program and the accompanying materials are made available under the terms of the BSD license which accompanies this distribution, and is available at http://www.opensource.org/licenses/bsd-license.php

Classes [hide private]
  AdaptationError
Error raised when a suitable adapter could not be found.
  Interface
Base class for all interfaces.
  AEAccAdapter
Base class for all adapter classes.
  PORAdapter
Convenience base class for AEAccAdapters for AEPors.
Functions [hide private]
 
registerAdapters(adapters=[])
Registers all AEAccAdapters in the caller's local namespace.
source code
 
_add(adapter, interface, condition=None)
Adds an adapter to the registry.
source code
 
_get(interface, subject)
Gets an appropriate adapter to the desired interface for the given subject.
source code
Variables [hide private]
  log = logging.getLogger('Adapt')
dictionary _adapters = {}
Lists of adapters and conditions under which they should be applied keyed by the interface provided
dictionary _default_adapters = {}
Adapters to be used when no better adapter is available for a given subject or interface keyed by the interface provided
Function Details [hide private]

registerAdapters(adapters=[])

source code 

Registers all AEAccAdapters in the caller's local namespace.

An adapter must be a class deriving from AEAccAdapter and should have a class variable named provides containing a list of Interfaces that the adapter implements. An adapter can also expose a when attribute containing a condition under which the adapter can be used. If when is not given, the adapter is used as the default for the given interface. Only one default may be registered per interface.

Note: This function uses a small hack to get the caller's locals. This hack can be removed in favor of the caller passing in its locals dictionary explicitly, but that places more responsibility on the caller who may already forget to call this function to begin with.

_add(adapter, interface, condition=None)

source code 

Adds an adapter to the registry.

If condition is specified, the adapter is added to the _adapters dictionary. If no condition is given, the adapter is set as the default for the given interface in the _default_adapters dictionary.
Parameters:
  • adapter (AEAccAdapter) - Instance to be registered as an AEAccAdapter
  • interface (Interface) - Interface provided by the AEAccAdapter
  • condition (string) - Function expressing a condition under which the adapter can be applied to a subject. The subject is available in the variable subject in the condition. A value of None implies the AEAccAdapter is a default to be used when no other AEAccAdapter is available for this interface.

_get(interface, subject)

source code 

Gets an appropriate adapter to the desired interface for the given subject.

First tries to find an AEAccAdapter to the Interface by satisfying one of the conditions of the registered AEAccAdapters. If that fails, trys to use a default AEAccAdapter to the Interface. If no default is registered, raises an exception.
Parameters:
  • interface (Interface) - Interface to which the subject should be adapted
  • subject (object) - Object to adapt to the interface
Raises: