<s> import inspect <EOL> import logging <EOL> import sys <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> HANDSHAKE_DISPATCHER = "<STR_LIT>" <EOL> CONFIG_DISPATCHER = "<STR_LIT>" <EOL> MAIN_DISPATCHER = "<STR_LIT>" <EOL> DEAD_DISPATCHER = "<STR_LIT>" <EOL> class _Caller ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dispatchers , ev_source ) : <EOL> """<STR_LIT>""" <EOL> self . dispatchers = dispatchers <EOL> self . ev_source = ev_source <EOL> def set_ev_cls ( ev_cls , dispatchers = None ) : <EOL> def _set_ev_cls_dec ( handler ) : <EOL> if '<STR_LIT>' not in dir ( handler ) : <EOL> handler . callers = { } <EOL> for e in _listify ( ev_cls ) : <EOL> handler . callers [ e ] = _Caller ( _listify ( dispatchers ) , e . __module__ ) <EOL> return handler <EOL> return _set_ev_cls_dec <EOL> def set_ev_handler ( ev_cls , dispatchers = None ) : <EOL> def _set_ev_cls_dec ( handler ) : <EOL> if '<STR_LIT>' not in dir ( handler ) : <EOL> handler . callers = { } <EOL> for e in _listify ( ev_cls ) : <EOL> handler . callers [ e ] = _Caller ( _listify ( dispatchers ) , None ) <EOL> return handler <EOL> return _set_ev_cls_dec <EOL> def _has_caller ( meth ) : <EOL> return hasattr ( meth , '<STR_LIT>' ) <EOL> def _listify ( may_list ) : <EOL> if may_list is None : <EOL> may_list = [ ] <EOL> if not isinstance ( may_list , list ) : <EOL> may_list = [ may_list ] <EOL> return may_list <EOL> def register_instance ( i ) : <EOL> for _k , m in inspect . getmembers ( i , inspect . ismethod ) : <EOL> if _has_caller ( m ) : <EOL> for ev_cls , c in m . callers . items ( ) : <EOL> i . register_handler ( ev_cls , m ) <EOL> def _is_method ( f ) : <EOL> return inspect . isfunction ( f ) or inspect . ismethod ( f ) <EOL> def get_dependent_services ( cls ) : <EOL> services = [ ] <EOL> for _k , m in inspect . getmembers ( cls , _is_method ) : <EOL> if _has_caller ( m ) : <EOL> for ev_cls , c in m . callers . items ( ) : <EOL> service = getattr ( sys . modules [ ev_cls . __module__ ] , <EOL> '<STR_LIT>' , None ) <EOL> if service : <EOL> if cls . __module__ != service : <EOL> services . append ( service ) <EOL> m = sys . modules [ cls . __module__ ] <EOL> services . extend ( getattr ( m , '<STR_LIT>' , [ ] ) ) <EOL> services = list ( set ( services ) ) <EOL> return services <EOL> def register_service ( service ) : <EOL> """<STR_LIT>""" <EOL> frm = inspect . stack ( ) [ <NUM_LIT:1> ] <EOL> m = inspect . getmodule ( frm [ <NUM_LIT:0> ] ) <EOL> m . _SERVICE_NAME = service </s>
