Version: 8.3.0
gui.adapt Namespace Reference

Classes

class  AdaptationError
 
class  LiskovViolation
 

Functions

def registerAdapterFactory
 
def unregisterAdapterFactory
 
def _adapt_by_registry
 
def adapt
 

Variables

dictionary _adapter_factory_registry = {}
 

Function Documentation

def gui.adapt._adapt_by_registry (   obj,
  protocol,
  alternate 
)
private

Definition at line 33 of file adapt.py.

Referenced by gui.adapt.adapt().

33 
34 def _adapt_by_registry(obj, protocol, alternate):
35  factory = _adapter_factory_registry.get((type(obj), protocol))
36  if factory is None:
37  adapter = alternate
38  else:
39  adapter = factory(obj, protocol, alternate)
40  if adapter is AdaptationError:
41  raise AdaptationError,obj
42  else:
43  return adapter
44 
def gui.adapt.adapt (   obj,
  protocol,
  alternate = AdaptationError 
)

Definition at line 45 of file adapt.py.

References gui.adapt._adapt_by_registry().

Referenced by YACS::ENGINE::RuntimeSALOME.adapt().

45 
46 def adapt(obj, protocol, alternate=AdaptationError):
47 
48  t = type(obj)
49 
50  # (a) first check to see if object has the exact protocol
51  if t is protocol:
52  return obj
53 
54  try:
55  # (b) next check if t.__conform__ exists & likes protocol
56  conform = getattr(t, '__conform__', None)
57  if conform is not None:
58  result = conform(obj, protocol)
59  if result is not None:
60  return result
61 
62  # (c) then check if protocol.__adapt__ exists & likes obj
63  adapt = getattr(type(protocol), '__adapt__', None)
64  if adapt is not None:
65  result = adapt(protocol, obj)
66  if result is not None:
67  return result
68  except LiskovViolation:
69  pass
70  else:
71  # (d) check if object is instance of protocol
72  try:
73  if isinstance(obj, protocol):
74  return obj
75  except:
76  pass
77 
78  # (e) last chance: try the registry
79  return _adapt_by_registry(obj, protocol, alternate)
def gui.adapt.registerAdapterFactory (   objtype,
  protocol,
  factory 
)

Definition at line 27 of file adapt.py.

27 
28 def registerAdapterFactory(objtype, protocol, factory):
29  _adapter_factory_registry[objtype, protocol] = factory
def gui.adapt.unregisterAdapterFactory (   objtype,
  protocol 
)

Definition at line 30 of file adapt.py.

30 
31 def unregisterAdapterFactory(objtype, protocol):
32  del _adapter_factory_registry[objtype, protocol]

Variable Documentation

dictionary gui.adapt._adapter_factory_registry = {}

Definition at line 25 of file adapt.py.