<s> import logging <EOL> import inspect <EOL> from uliweb . utils . common import import_attr <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> HIGH = <NUM_LIT:1> <EOL> MIDDLE = <NUM_LIT:2> <EOL> LOW = <NUM_LIT:3> <EOL> _receivers = { } <EOL> _called = { } <EOL> def reset ( ) : <EOL> global _receivers , _called <EOL> _receivers . clear ( ) <EOL> _called . clear ( ) <EOL> def bind ( topic , signal = None , kind = MIDDLE , nice = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> def f ( func ) : <EOL> if not topic in _receivers : <EOL> receivers = _receivers [ topic ] = [ ] <EOL> else : <EOL> receivers = _receivers [ topic ] <EOL> if nice == - <NUM_LIT:1> : <EOL> if kind == MIDDLE : <EOL> n = <NUM_LIT> <EOL> elif kind == HIGH : <EOL> n = <NUM_LIT:100> <EOL> else : <EOL> n = <NUM_LIT> <EOL> else : <EOL> n = nice <EOL> if callable ( func ) : <EOL> func_name = func . __module__ + '<STR_LIT:.>' + func . __name__ <EOL> func = func <EOL> else : <EOL> func_name = func <EOL> func = None <EOL> _f = ( n , { '<STR_LIT>' : func , '<STR_LIT>' : signal , '<STR_LIT>' : func_name } ) <EOL> receivers . append ( _f ) <EOL> return func <EOL> return f <EOL> def unbind ( topic , func ) : <EOL> """<STR_LIT>""" <EOL> if topic in _receivers : <EOL> receivers = _receivers [ topic ] <EOL> for i in range ( len ( receivers ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> nice , f = receivers [ i ] <EOL> if ( callable ( func ) and f [ '<STR_LIT>' ] == func ) or ( f [ '<STR_LIT>' ] == func ) : <EOL> del receivers [ i ] <EOL> return <EOL> def _test ( kwargs , receiver ) : <EOL> signal = kwargs . get ( '<STR_LIT>' , None ) <EOL> f = receiver [ '<STR_LIT>' ] <EOL> args = inspect . getargspec ( f ) [ <NUM_LIT:0> ] <EOL> if '<STR_LIT>' not in args : <EOL> kwargs . pop ( '<STR_LIT>' , None ) <EOL> _signal = receiver . get ( '<STR_LIT>' ) <EOL> flag = True <EOL> if _signal : <EOL> if isinstance ( _signal , ( tuple , list ) ) : <EOL> if signal not in _signal : <EOL> flag = False <EOL> elif _signal != signal : <EOL> flag = False <EOL> return flag <EOL> def call ( sender , topic , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not topic in _receivers : <EOL> return <EOL> items = _receivers [ topic ] <EOL> def _cmp ( x , y ) : <EOL> return cmp ( x [ <NUM_LIT:0> ] , y [ <NUM_LIT:0> ] ) <EOL> items . sort ( _cmp ) <EOL> i = <NUM_LIT:0> <EOL> while i < len ( items ) : <EOL> nice , f = items [ i ] <EOL> i = i + <NUM_LIT:1> <EOL> _f = f [ '<STR_LIT>' ] <EOL> if not _f : <EOL> try : <EOL> _f = import_attr ( f [ '<STR_LIT>' ] ) <EOL> except ( ImportError , AttributeError ) as e : <EOL> logging . error ( "<STR_LIT>" % f [ '<STR_LIT>' ] ) <EOL> raise <EOL> f [ '<STR_LIT>' ] = _f <EOL> if callable ( _f ) : <EOL> kw = kwargs . copy ( ) <EOL> if not _test ( kw , f ) : <EOL> continue <EOL> try : <EOL> _f ( sender , * args , ** kw ) <EOL> except : <EOL> func = _f . __module__ + '<STR_LIT:.>' + _f . __name__ <EOL> logging . exception ( '<STR_LIT>' % ( topic , func , args , kw ) ) <EOL> raise <EOL> else : <EOL> raise Exception , "<STR_LIT>" % ( topic , _f ) <EOL> def call_once ( sender , topic , * args , ** kwargs ) : <EOL> signal = kwargs . get ( '<STR_LIT>' ) <EOL> if ( topic , signal ) in _called : <EOL> return <EOL> else : <EOL> call ( sender , topic , * args , ** kwargs ) <EOL> _called [ ( topic , signal ) ] = True <EOL> def get ( sender , topic , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not topic in _receivers : <EOL> return <EOL> items = _receivers [ topic ] <EOL> def _cmp ( x , y ) : <EOL> return cmp ( x [ <NUM_LIT:0> ] , y [ <NUM_LIT:0> ] ) <EOL> items . sort ( _cmp ) <EOL> for i in range ( len ( items ) ) : <EOL> nice , f = items [ i ] <EOL> _f = f [ '<STR_LIT>' ] <EOL> if not _f : <EOL> try : <EOL> _f = import_attr ( f [ '<STR_LIT>' ] ) <EOL> except ImportError : <EOL> logging . error ( "<STR_LIT>" % f [ '<STR_LIT>' ] ) <EOL> raise <EOL> f [ '<STR_LIT>' ] = _f <EOL> if callable ( _f ) : <EOL> if not _test ( kwargs , f ) : <EOL> continue <EOL> try : <EOL> v = _f ( sender , * args , ** kwargs ) <EOL> except : <EOL> func = _f . __module__ + '<STR_LIT:.>' + _f . __name__ <EOL> logging . exception ( '<STR_LIT>' % ( topic , func , args , kwargs ) ) <EOL> raise <EOL> if v is not None : <EOL> return v <EOL> else : <EOL> raise "<STR_LIT>" % ( topic , _f ) <EOL> def get_once ( sender , topic , * args , ** kwargs ) : <EOL> signal = kwargs . get ( '<STR_LIT>' ) <EOL> if ( topic , signal ) in _called : <EOL> return _called [ ( topic , signal ) ] <EOL> else : <EOL> r = get ( sender , topic , * args , ** kwargs ) <EOL> _called [ ( topic , signal ) ] = r <EOL> return r <EOL> def print_topics ( ) : <EOL> import pprint <EOL> pprint . pprint ( _receivers ) </s>
