<s> """<STR_LIT>""" <EOL> class HookError ( Exception ) : <EOL> "<STR_LIT>" <EOL> def addPre ( klass , name , func ) : <EOL> """<STR_LIT>""" <EOL> _addHook ( klass , name , PRE , func ) <EOL> def addPost ( klass , name , func ) : <EOL> """<STR_LIT>""" <EOL> _addHook ( klass , name , POST , func ) <EOL> def removePre ( klass , name , func ) : <EOL> """<STR_LIT>""" <EOL> _removeHook ( klass , name , PRE , func ) <EOL> def removePost ( klass , name , func ) : <EOL> """<STR_LIT>""" <EOL> _removeHook ( klass , name , POST , func ) <EOL> hooked_func = """<STR_LIT>""" <EOL> _PRE = '<STR_LIT>' <EOL> _POST = '<STR_LIT>' <EOL> _ORIG = '<STR_LIT>' <EOL> def _XXX ( k , n , s ) : <EOL> """<STR_LIT>""" <EOL> x = s % ( k . __module__ . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) , k . __name__ , n ) <EOL> return x <EOL> def PRE ( k , n ) : <EOL> "<STR_LIT>" <EOL> return _XXX ( k , n , _PRE ) <EOL> def POST ( k , n ) : <EOL> "<STR_LIT>" <EOL> return _XXX ( k , n , _POST ) <EOL> def ORIG ( k , n ) : <EOL> "<STR_LIT>" <EOL> return _XXX ( k , n , _ORIG ) <EOL> def _addHook ( klass , name , phase , func ) : <EOL> "<STR_LIT>" <EOL> _enhook ( klass , name ) <EOL> if not hasattr ( klass , phase ( klass , name ) ) : <EOL> setattr ( klass , phase ( klass , name ) , [ ] ) <EOL> phaselist = getattr ( klass , phase ( klass , name ) ) <EOL> phaselist . append ( func ) <EOL> def _removeHook ( klass , name , phase , func ) : <EOL> "<STR_LIT>" <EOL> phaselistname = phase ( klass , name ) <EOL> if not hasattr ( klass , ORIG ( klass , name ) ) : <EOL> raise HookError ( "<STR_LIT>" ) <EOL> phaselist = getattr ( klass , phase ( klass , name ) ) <EOL> try : phaselist . remove ( func ) <EOL> except ValueError : <EOL> raise HookError ( "<STR_LIT>" % <EOL> ( name , klass ) ) <EOL> if not getattr ( klass , PRE ( klass , name ) ) and not getattr ( klass , POST ( klass , name ) ) : <EOL> _dehook ( klass , name ) <EOL> def _enhook ( klass , name ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( klass , ORIG ( klass , name ) ) : <EOL> return <EOL> def newfunc ( * args , ** kw ) : <EOL> for preMethod in getattr ( klass , PRE ( klass , name ) ) : <EOL> preMethod ( * args , ** kw ) <EOL> try : <EOL> return getattr ( klass , ORIG ( klass , name ) ) ( * args , ** kw ) <EOL> finally : <EOL> for postMethod in getattr ( klass , POST ( klass , name ) ) : <EOL> postMethod ( * args , ** kw ) <EOL> try : <EOL> newfunc . func_name = name <EOL> except TypeError : <EOL> pass <EOL> oldfunc = getattr ( klass , name ) . im_func <EOL> setattr ( klass , ORIG ( klass , name ) , oldfunc ) <EOL> setattr ( klass , PRE ( klass , name ) , [ ] ) <EOL> setattr ( klass , POST ( klass , name ) , [ ] ) <EOL> setattr ( klass , name , newfunc ) <EOL> def _dehook ( klass , name ) : <EOL> "<STR_LIT>" <EOL> if not hasattr ( klass , ORIG ( klass , name ) ) : <EOL> raise HookError ( "<STR_LIT>" ) <EOL> setattr ( klass , name , getattr ( klass , ORIG ( klass , name ) ) ) <EOL> delattr ( klass , PRE ( klass , name ) ) <EOL> delattr ( klass , POST ( klass , name ) ) <EOL> delattr ( klass , ORIG ( klass , name ) ) </s>
