<s> """<STR_LIT>""" <EOL> import re <EOL> from . . core import exc , meta <EOL> from . . core import backend <EOL> from . . utils . misc import minimal_logger <EOL> LOG = minimal_logger ( __name__ ) <EOL> class HandlerManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , use_backend_globals = False ) : <EOL> if use_backend_globals is True : <EOL> self . __handlers__ = backend . __handlers__ <EOL> else : <EOL> self . __handlers__ = { } <EOL> def get ( self , handler_type , handler_label , * args ) : <EOL> """<STR_LIT>""" <EOL> if handler_type not in self . __handlers__ : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> handler_type ) <EOL> if handler_label in self . __handlers__ [ handler_type ] : <EOL> return self . __handlers__ [ handler_type ] [ handler_label ] <EOL> elif len ( args ) > <NUM_LIT:0> : <EOL> return args [ <NUM_LIT:0> ] <EOL> else : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> ( handler_type , handler_label ) ) <EOL> def list ( self , handler_type ) : <EOL> """<STR_LIT>""" <EOL> if handler_type not in self . __handlers__ : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> handler_type ) <EOL> res = [ ] <EOL> for label in self . __handlers__ [ handler_type ] : <EOL> if label == '<STR_LIT>' : <EOL> continue <EOL> res . append ( self . __handlers__ [ handler_type ] [ label ] ) <EOL> return res <EOL> def define ( self , interface ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( interface , '<STR_LIT>' ) : <EOL> raise exc . InterfaceError ( "<STR_LIT>" % interface + <EOL> "<STR_LIT>" ) <EOL> if not hasattr ( interface . IMeta , '<STR_LIT:label>' ) : <EOL> raise exc . InterfaceError ( "<STR_LIT>" % interface + <EOL> "<STR_LIT>" ) <EOL> LOG . debug ( "<STR_LIT>" % <EOL> ( interface . IMeta . label , interface . __name__ ) ) <EOL> if interface . IMeta . label in self . __handlers__ : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> interface . IMeta . label ) <EOL> self . __handlers__ [ interface . IMeta . label ] = { <EOL> '<STR_LIT>' : interface <EOL> } <EOL> def defined ( self , handler_type ) : <EOL> """<STR_LIT>""" <EOL> if handler_type in self . __handlers__ : <EOL> return True <EOL> else : <EOL> return False <EOL> def register ( self , handler_obj ) : <EOL> """<STR_LIT>""" <EOL> orig_obj = handler_obj <EOL> obj = orig_obj ( ) <EOL> if not hasattr ( obj . _meta , '<STR_LIT:label>' ) or not obj . _meta . label : <EOL> raise exc . InterfaceError ( "<STR_LIT>" % orig_obj + <EOL> "<STR_LIT>" ) <EOL> if not hasattr ( obj . _meta , '<STR_LIT>' ) or not obj . _meta . interface : <EOL> raise exc . InterfaceError ( "<STR_LIT>" % orig_obj + <EOL> "<STR_LIT>" ) <EOL> orig_obj . Meta . label = re . sub ( '<STR_LIT:->' , '<STR_LIT:_>' , obj . _meta . label ) <EOL> obj . _meta . label = re . sub ( '<STR_LIT:->' , '<STR_LIT:_>' , obj . _meta . label ) <EOL> handler_type = obj . _meta . interface . IMeta . label <EOL> LOG . debug ( "<STR_LIT>" % <EOL> ( orig_obj , handler_type , obj . _meta . label ) ) <EOL> if handler_type not in self . __handlers__ : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> handler_type ) <EOL> if obj . _meta . label in self . __handlers__ [ handler_type ] and self . __handlers__ [ handler_type ] [ obj . _meta . label ] != obj : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> ( handler_type , obj . _meta . label ) ) <EOL> interface = self . __handlers__ [ handler_type ] [ '<STR_LIT>' ] <EOL> if hasattr ( interface . IMeta , '<STR_LIT>' ) : <EOL> interface . IMeta ( ) . validator ( obj ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" % <EOL> interface ) <EOL> self . __handlers__ [ handler_type ] [ obj . Meta . label ] = orig_obj <EOL> def registered ( self , handler_type , handler_label ) : <EOL> """<STR_LIT>""" <EOL> if handler_type in self . __handlers__ and handler_label in self . __handlers__ [ handler_type ] : <EOL> return True <EOL> return False <EOL> def resolve ( self , handler_type , handler_def , raise_error = True ) : <EOL> """<STR_LIT>""" <EOL> han = None <EOL> if type ( handler_def ) == str : <EOL> han = self . get ( handler_type , handler_def ) ( ) <EOL> elif hasattr ( handler_def , '<STR_LIT>' ) : <EOL> if not self . registered ( handler_type , handler_def . _meta . label ) : <EOL> self . register ( handler_def . __class__ ) <EOL> han = handler_def <EOL> elif hasattr ( handler_def , '<STR_LIT:Meta>' ) : <EOL> han = handler_def ( ) <EOL> if not self . registered ( handler_type , han . _meta . label ) : <EOL> self . register ( handler_def ) <EOL> msg = "<STR_LIT>" % ( handler_def , handler_type ) <EOL> if han is not None : <EOL> return han <EOL> elif han is None and raise_error : <EOL> raise exc . FrameworkError ( msg ) <EOL> elif han is None : <EOL> LOG . debug ( msg ) <EOL> return None <EOL> class CementBaseHandler ( meta . MetaMixin ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> """<STR_LIT>""" <EOL> label = None <EOL> """<STR_LIT>""" <EOL> interface = None <EOL> """<STR_LIT>""" <EOL> config_section = None <EOL> """<STR_LIT>""" <EOL> config_defaults = None <EOL> """<STR_LIT>""" <EOL> overridable = False <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kw ) : <EOL> super ( CementBaseHandler , self ) . __init__ ( ** kw ) <EOL> self . app = None <EOL> def _setup ( self , app_obj ) : <EOL> """<STR_LIT>""" <EOL> self . app = app_obj <EOL> if self . _meta . config_section is None : <EOL> self . _meta . config_section = "<STR_LIT>" % ( self . _meta . interface . IMeta . label , self . _meta . label ) <EOL> if self . _meta . config_defaults is not None : <EOL> LOG . debug ( "<STR_LIT>" % self + <EOL> "<STR_LIT>" % self . _meta . config_section ) <EOL> dict_obj = dict ( ) <EOL> dict_obj [ self . _meta . config_section ] = self . _meta . config_defaults <EOL> self . app . config . merge ( dict_obj , override = False ) <EOL> def get ( handler_type , handler_label , * args ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if handler_type not in backend . __handlers__ : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> handler_type ) <EOL> if handler_label in backend . __handlers__ [ handler_type ] : <EOL> return backend . __handlers__ [ handler_type ] [ handler_label ] <EOL> elif len ( args ) > <NUM_LIT:0> : <EOL> return args [ <NUM_LIT:0> ] <EOL> else : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> ( handler_type , handler_label ) ) <EOL> def list ( handler_type ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if handler_type not in backend . __handlers__ : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> handler_type ) <EOL> res = [ ] <EOL> for label in backend . __handlers__ [ handler_type ] : <EOL> if label == '<STR_LIT>' : <EOL> continue <EOL> res . append ( backend . __handlers__ [ handler_type ] [ label ] ) <EOL> return res <EOL> def define ( interface ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if not hasattr ( interface , '<STR_LIT>' ) : <EOL> raise exc . InterfaceError ( "<STR_LIT>" % interface + <EOL> "<STR_LIT>" ) <EOL> if not hasattr ( interface . IMeta , '<STR_LIT:label>' ) : <EOL> raise exc . InterfaceError ( "<STR_LIT>" % interface + <EOL> "<STR_LIT>" ) <EOL> LOG . debug ( "<STR_LIT>" % <EOL> ( interface . IMeta . label , interface . __name__ ) ) <EOL> if interface . IMeta . label in backend . __handlers__ : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> interface . IMeta . label ) <EOL> backend . __handlers__ [ interface . IMeta . label ] = { '<STR_LIT>' : interface } <EOL> def defined ( handler_type ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if handler_type in backend . __handlers__ : <EOL> return True <EOL> else : <EOL> return False <EOL> def register ( handler_obj ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> orig_obj = handler_obj <EOL> obj = orig_obj ( ) <EOL> if not hasattr ( obj . _meta , '<STR_LIT:label>' ) or not obj . _meta . label : <EOL> raise exc . InterfaceError ( "<STR_LIT>" % orig_obj + <EOL> "<STR_LIT>" ) <EOL> if not hasattr ( obj . _meta , '<STR_LIT>' ) or not obj . _meta . interface : <EOL> raise exc . InterfaceError ( "<STR_LIT>" % orig_obj + <EOL> "<STR_LIT>" ) <EOL> orig_obj . Meta . label = re . sub ( '<STR_LIT:->' , '<STR_LIT:_>' , obj . _meta . label ) <EOL> obj . _meta . label = re . sub ( '<STR_LIT:->' , '<STR_LIT:_>' , obj . _meta . label ) <EOL> handler_type = obj . _meta . interface . IMeta . label <EOL> LOG . debug ( "<STR_LIT>" % <EOL> ( orig_obj , handler_type , obj . _meta . label ) ) <EOL> if handler_type not in backend . __handlers__ : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> handler_type ) <EOL> if obj . _meta . label in backend . __handlers__ [ handler_type ] and backend . __handlers__ [ handler_type ] [ obj . _meta . label ] != obj : <EOL> raise exc . FrameworkError ( "<STR_LIT>" % <EOL> ( handler_type , obj . _meta . label ) ) <EOL> interface = backend . __handlers__ [ handler_type ] [ '<STR_LIT>' ] <EOL> if hasattr ( interface . IMeta , '<STR_LIT>' ) : <EOL> interface . IMeta ( ) . validator ( obj ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" % <EOL> interface ) <EOL> backend . __handlers__ [ handler_type ] [ obj . Meta . label ] = orig_obj <EOL> def registered ( handler_type , handler_label ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if handler_type in backend . __handlers__ and handler_label in backend . __handlers__ [ handler_type ] : <EOL> return True <EOL> return False <EOL> def resolve ( handler_type , handler_def , raise_error = True ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> han = None <EOL> if type ( handler_def ) == str : <EOL> han = get ( handler_type , handler_def ) ( ) <EOL> elif hasattr ( handler_def , '<STR_LIT>' ) : <EOL> if not registered ( handler_type , handler_def . _meta . label ) : <EOL> register ( handler_def . __class__ ) <EOL> han = handler_def <EOL> elif hasattr ( handler_def , '<STR_LIT:Meta>' ) : <EOL> han = handler_def ( ) <EOL> if not registered ( handler_type , han . _meta . label ) : <EOL> register ( handler_def ) <EOL> msg = "<STR_LIT>" % ( handler_def , handler_type ) <EOL> if han is not None : <EOL> return han <EOL> elif han is None and raise_error : <EOL> raise exc . FrameworkError ( msg ) <EOL> elif han is None : <EOL> LOG . debug ( msg ) <EOL> return None </s>
