<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> from zope . interface import interface , declarations <EOL> from zope . interface . adapter import AdapterRegistry <EOL> from twisted . python . compat import NativeStringIO <EOL> from twisted . python import reflect <EOL> globalRegistry = AdapterRegistry ( ) <EOL> ALLOW_DUPLICATES = <NUM_LIT:0> <EOL> if getattr ( AdapterRegistry , '<STR_LIT>' , None ) is None : <EOL> def _registered ( registry , required , provided ) : <EOL> """<STR_LIT>""" <EOL> return registry . get ( required ) . selfImplied . get ( provided , { } ) . get ( '<STR_LIT>' ) <EOL> else : <EOL> def _registered ( registry , required , provided ) : <EOL> """<STR_LIT>""" <EOL> return registry . registered ( [ required ] , provided ) <EOL> def registerAdapter ( adapterFactory , origInterface , * interfaceClasses ) : <EOL> """<STR_LIT>""" <EOL> self = globalRegistry <EOL> assert interfaceClasses , "<STR_LIT>" <EOL> global ALLOW_DUPLICATES <EOL> if not isinstance ( origInterface , interface . InterfaceClass ) : <EOL> origInterface = declarations . implementedBy ( origInterface ) <EOL> for interfaceClass in interfaceClasses : <EOL> factory = _registered ( self , origInterface , interfaceClass ) <EOL> if factory is not None and not ALLOW_DUPLICATES : <EOL> raise ValueError ( "<STR_LIT>" % ( factory , ) ) <EOL> for interfaceClass in interfaceClasses : <EOL> self . register ( [ origInterface ] , interfaceClass , '<STR_LIT>' , adapterFactory ) <EOL> def getAdapterFactory ( fromInterface , toInterface , default ) : <EOL> """<STR_LIT>""" <EOL> self = globalRegistry <EOL> if not isinstance ( fromInterface , interface . InterfaceClass ) : <EOL> fromInterface = declarations . implementedBy ( fromInterface ) <EOL> factory = self . lookup1 ( fromInterface , toInterface ) <EOL> if factory is None : <EOL> factory = default <EOL> return factory <EOL> def _addHook ( registry ) : <EOL> """<STR_LIT>""" <EOL> lookup = registry . lookup1 <EOL> def _hook ( iface , ob ) : <EOL> factory = lookup ( declarations . providedBy ( ob ) , iface ) <EOL> if factory is None : <EOL> return None <EOL> else : <EOL> return factory ( ob ) <EOL> interface . adapter_hooks . append ( _hook ) <EOL> return _hook <EOL> def _removeHook ( hook ) : <EOL> """<STR_LIT>""" <EOL> interface . adapter_hooks . remove ( hook ) <EOL> _addHook ( globalRegistry ) <EOL> def getRegistry ( ) : <EOL> """<STR_LIT>""" <EOL> return globalRegistry <EOL> CannotAdapt = TypeError <EOL> class Adapter : <EOL> """<STR_LIT>""" <EOL> temporaryAdapter = <NUM_LIT:0> <EOL> multiComponent = <NUM_LIT:1> <EOL> def __init__ ( self , original ) : <EOL> """<STR_LIT>""" <EOL> self . original = original <EOL> def __conform__ ( self , interface ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self . original , "<STR_LIT>" ) : <EOL> return self . original . __conform__ ( interface ) <EOL> return None <EOL> def isuper ( self , iface , adapter ) : <EOL> """<STR_LIT>""" <EOL> return self . original . isuper ( iface , adapter ) <EOL> class Componentized : <EOL> """<STR_LIT>""" <EOL> persistenceVersion = <NUM_LIT:1> <EOL> def __init__ ( self ) : <EOL> self . _adapterCache = { } <EOL> def locateAdapterClass ( self , klass , interfaceClass , default ) : <EOL> return getAdapterFactory ( klass , interfaceClass , default ) <EOL> def setAdapter ( self , interfaceClass , adapterClass ) : <EOL> """<STR_LIT>""" <EOL> self . setComponent ( interfaceClass , adapterClass ( self ) ) <EOL> def addAdapter ( self , adapterClass , ignoreClass = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> adapt = adapterClass ( self ) <EOL> self . addComponent ( adapt , ignoreClass ) <EOL> return adapt <EOL> def setComponent ( self , interfaceClass , component ) : <EOL> """<STR_LIT>""" <EOL> self . _adapterCache [ reflect . qual ( interfaceClass ) ] = component <EOL> def addComponent ( self , component , ignoreClass = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> for iface in declarations . providedBy ( component ) : <EOL> if ( ignoreClass or <EOL> ( self . locateAdapterClass ( self . __class__ , iface , None ) <EOL> == component . __class__ ) ) : <EOL> self . _adapterCache [ reflect . qual ( iface ) ] = component <EOL> def unsetComponent ( self , interfaceClass ) : <EOL> """<STR_LIT>""" <EOL> del self . _adapterCache [ reflect . qual ( interfaceClass ) ] <EOL> def removeComponent ( self , component ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> for k , v in list ( self . _adapterCache . items ( ) ) : <EOL> if v is component : <EOL> del self . _adapterCache [ k ] <EOL> l . append ( reflect . namedObject ( k ) ) <EOL> return l <EOL> def getComponent ( self , interface , default = None ) : <EOL> """<STR_LIT>""" <EOL> k = reflect . qual ( interface ) <EOL> if k in self . _adapterCache : <EOL> return self . _adapterCache [ k ] <EOL> else : <EOL> adapter = interface . __adapt__ ( self ) <EOL> if adapter is not None and not ( <EOL> hasattr ( adapter , "<STR_LIT>" ) and <EOL> adapter . temporaryAdapter ) : <EOL> self . _adapterCache [ k ] = adapter <EOL> if ( hasattr ( adapter , "<STR_LIT>" ) and <EOL> adapter . multiComponent ) : <EOL> self . addComponent ( adapter ) <EOL> if adapter is None : <EOL> return default <EOL> return adapter <EOL> def __conform__ ( self , interface ) : <EOL> return self . getComponent ( interface ) <EOL> class ReprableComponentized ( Componentized ) : <EOL> def __init__ ( self ) : <EOL> Componentized . __init__ ( self ) <EOL> def __repr__ ( self ) : <EOL> from pprint import pprint <EOL> sio = NativeStringIO ( ) <EOL> pprint ( self . _adapterCache , sio ) <EOL> return sio . getvalue ( ) <EOL> def proxyForInterface ( iface , originalAttribute = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , original ) : <EOL> setattr ( self , originalAttribute , original ) <EOL> contents = { "<STR_LIT>" : __init__ } <EOL> for name in iface : <EOL> contents [ name ] = _ProxyDescriptor ( name , originalAttribute ) <EOL> proxy = type ( "<STR_LIT>" <EOL> % ( reflect . qual ( iface ) , ) , ( object , ) , contents ) <EOL> declarations . classImplements ( proxy , iface ) <EOL> return proxy <EOL> class _ProxiedClassMethod ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , methodName , originalAttribute ) : <EOL> self . methodName = methodName <EOL> self . originalAttribute = originalAttribute <EOL> def __call__ ( self , oself , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> original = getattr ( oself , self . originalAttribute ) <EOL> actualMethod = getattr ( original , self . methodName ) <EOL> return actualMethod ( * args , ** kw ) <EOL> class _ProxyDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attributeName , originalAttribute ) : <EOL> self . attributeName = attributeName <EOL> self . originalAttribute = originalAttribute <EOL> def __get__ ( self , oself , type = None ) : <EOL> """<STR_LIT>""" <EOL> if oself is None : <EOL> return _ProxiedClassMethod ( self . attributeName , <EOL> self . originalAttribute ) <EOL> original = getattr ( oself , self . originalAttribute ) <EOL> return getattr ( original , self . attributeName ) <EOL> def __set__ ( self , oself , value ) : <EOL> """<STR_LIT>""" <EOL> original = getattr ( oself , self . originalAttribute ) <EOL> setattr ( original , self . attributeName , value ) <EOL> def __delete__ ( self , oself ) : <EOL> """<STR_LIT>""" <EOL> original = getattr ( oself , self . originalAttribute ) <EOL> delattr ( original , self . attributeName ) <EOL> __all__ = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] </s>
