<s> from robot . utils import get_error_details , py2to3 <EOL> from . listenerarguments import ListenerArguments <EOL> from . logger import LOGGER <EOL> @ py2to3 <EOL> class ListenerMethods ( object ) : <EOL> def __init__ ( self , method_name , listeners ) : <EOL> self . _methods = [ ] <EOL> self . _method_name = method_name <EOL> if listeners : <EOL> self . _register_methods ( method_name , listeners ) <EOL> def _register_methods ( self , method_name , listeners ) : <EOL> for listener in listeners : <EOL> method = getattr ( listener , method_name ) <EOL> if method : <EOL> self . _methods . append ( ListenerMethod ( method , listener ) ) <EOL> def __call__ ( self , * args ) : <EOL> if self . _methods : <EOL> args = ListenerArguments . by_method_name ( self . _method_name , args ) <EOL> for method in self . _methods : <EOL> method ( args . get_arguments ( method . version ) ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . _methods ) <EOL> class LibraryListenerMethods ( object ) : <EOL> def __init__ ( self , method_name ) : <EOL> self . _method_stack = [ ] <EOL> self . _method_name = method_name <EOL> def new_suite_scope ( self ) : <EOL> self . _method_stack . append ( [ ] ) <EOL> def discard_suite_scope ( self ) : <EOL> self . _method_stack . pop ( ) <EOL> def register ( self , listeners , library ) : <EOL> methods = self . _method_stack [ - <NUM_LIT:1> ] <EOL> for listener in listeners : <EOL> method = getattr ( listener , self . _method_name ) <EOL> if method : <EOL> info = ListenerMethod ( method , listener , library ) <EOL> methods . append ( info ) <EOL> def unregister ( self , library ) : <EOL> methods = [ m for m in self . _method_stack [ - <NUM_LIT:1> ] if m . library is not library ] <EOL> self . _method_stack [ - <NUM_LIT:1> ] = methods <EOL> def __call__ ( self , * args , ** conf ) : <EOL> methods = self . _get_methods ( ** conf ) <EOL> if methods : <EOL> args = ListenerArguments . by_method_name ( self . _method_name , args ) <EOL> for method in methods : <EOL> method ( args . get_arguments ( method . version ) ) <EOL> def _get_methods ( self , library = None ) : <EOL> if not ( self . _method_stack and self . _method_stack [ - <NUM_LIT:1> ] ) : <EOL> return [ ] <EOL> methods = self . _method_stack [ - <NUM_LIT:1> ] <EOL> if library : <EOL> return [ m for m in methods if m . library is library ] <EOL> return methods <EOL> class ListenerMethod ( object ) : <EOL> called = False <EOL> def __init__ ( self , method , listener , library = None ) : <EOL> self . method = method <EOL> self . listener_name = listener . name <EOL> self . version = listener . version <EOL> self . library = library <EOL> def __call__ ( self , args ) : <EOL> if self . called : <EOL> return <EOL> try : <EOL> ListenerMethod . called = True <EOL> self . method ( * args ) <EOL> except : <EOL> message , details = get_error_details ( ) <EOL> LOGGER . error ( "<STR_LIT>" <EOL> % ( self . method . __name__ , self . listener_name , message ) ) <EOL> LOGGER . info ( "<STR_LIT>" % details ) <EOL> finally : <EOL> ListenerMethod . called = False </s>
