#NEWLINE# def adapts( from_ , to , extra = None , factory = None , cached = False , when = '' ) : #NEWLINE# #INDENT# " A class advisor for declaring adapters.\n\n    Parameters\n    ----------\n    from_ : type or interface\n        What the adapter adapts *from*, or a list of such types or interfaces\n        (the '_' suffix is used because 'from' is a Python keyword).\n    to : type or interface\n        What the adapter adapts *to*, or a list of such types or interfaces.\n    factory : callable\n        An (optional) factory for actually creating the adapters. This is\n        any callable that takes a single argument which is the object to\n        be adapted. The factory should return an adapter if it can\n        perform the adaptation and **None** if it cannot.\n    cached : bool\n        Should the adapters be cached? If an adapter is cached, then the\n        factory will produce at most one adapter per instance.\n    when : str\n        A Python expression that selects which instances of a particular type\n        can be adapted by this factory. The expression is evaluated in a\n        namespace that contains a single name *adaptee*, which is bound to the\n        object to be adapted (e.g., 'adaptee.is_folder').\n\n    Note\n    ----\n    The ``cached`` and ``when`` arguments are ignored if ``factory`` is\n    specified.\n\n    " #NEWLINE# from traits.adaptation.api import register_factory #NEWLINE# from traits.adaptation.cached_adapter_factory import CachedAdapterFactory #NEWLINE# from traits.protocols.advice import addClassAdvisor #NEWLINE# if ( extra is not None ) : #NEWLINE# #INDENT# ( adapter , from_ , to ) = ( from_ , to , extra ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# adapter = None #NEWLINE# #UNINDENT# @ deprecated ( "use the 'register_factory' function from 'traits.api' instead" ) #NEWLINE# def callback( klass ) : #NEWLINE# #INDENT# ' Called when the class has been created. ' #NEWLINE# if ( type ( from_ ) is not list ) : #NEWLINE# #INDENT# from_protocols = [ from_ ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# from_protocols = from_ #NEWLINE# #UNINDENT# if ( type ( to ) is not list ) : #NEWLINE# #INDENT# to_protocols = [ to ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# to_protocols = to #NEWLINE# #UNINDENT# if ( factory is None ) : #NEWLINE# #INDENT# adapter_factory = klass #NEWLINE# if ( when != '' ) : #NEWLINE# #INDENT# def _conditional_factory( adaptee , * args , **kw ) : #NEWLINE# #INDENT# namespace = { #NEWLINE# #INDENT# 'adaptee' : adaptee , #NEWLINE# #UNINDENT# } #NEWLINE# if eval ( when , namespace , namespace ) : #NEWLINE# #INDENT# return klass ( adaptee , * args , ** kw ) #NEWLINE# #UNINDENT# return None #NEWLINE# #UNINDENT# adapter_factory = _conditional_factory #NEWLINE# #UNINDENT# if cached : #NEWLINE# #INDENT# adapter_factory = CachedAdapterFactory ( factory = adapter_factory ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# adapter_factory = factory #NEWLINE# #UNINDENT# for from_protocol in from_protocols : #NEWLINE# #INDENT# for to_protocol in to_protocols : #NEWLINE# #INDENT# register_factory ( adapter_factory , from_protocol , to_protocol ) #NEWLINE# #UNINDENT# #UNINDENT# for to_protocol in to_protocols : #NEWLINE# #INDENT# if isinstance ( klass , type ) : #NEWLINE# #INDENT# type ( to_protocol ) . register ( to_protocol , klass ) #NEWLINE# #UNINDENT# #UNINDENT# return klass #NEWLINE# #UNINDENT# if ( adapter is not None ) : #NEWLINE# #INDENT# callback ( adapter ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# addClassAdvisor ( from_ )