<s> from contextlib import contextmanager <EOL> from robot . errors import DataError <EOL> from robot . utils import unic <EOL> class ExecutionContexts ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _contexts = [ ] <EOL> @ property <EOL> def current ( self ) : <EOL> return self . _contexts [ - <NUM_LIT:1> ] if self . _contexts else None <EOL> @ property <EOL> def top ( self ) : <EOL> return self . _contexts [ <NUM_LIT:0> ] if self . _contexts else None <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _contexts ) <EOL> @ property <EOL> def namespaces ( self ) : <EOL> return ( context . namespace for context in self ) <EOL> def start_suite ( self , suite , namespace , output , dry_run = False ) : <EOL> ctx = _ExecutionContext ( suite , namespace , output , dry_run ) <EOL> self . _contexts . append ( ctx ) <EOL> return ctx <EOL> def end_suite ( self ) : <EOL> self . _contexts . pop ( ) <EOL> EXECUTION_CONTEXTS = ExecutionContexts ( ) <EOL> class _ExecutionContext ( object ) : <EOL> _started_keywords_threshold = <NUM_LIT> <EOL> def __init__ ( self , suite , namespace , output , dry_run = False ) : <EOL> self . suite = suite <EOL> self . test = None <EOL> self . timeouts = set ( ) <EOL> self . namespace = namespace <EOL> self . output = output <EOL> self . dry_run = dry_run <EOL> self . in_suite_teardown = False <EOL> self . in_test_teardown = False <EOL> self . in_keyword_teardown = <NUM_LIT:0> <EOL> self . _started_keywords = <NUM_LIT:0> <EOL> self . timeout_occurred = False <EOL> @ contextmanager <EOL> def suite_teardown ( self ) : <EOL> self . in_suite_teardown = True <EOL> try : <EOL> yield <EOL> finally : <EOL> self . in_suite_teardown = False <EOL> @ contextmanager <EOL> def test_teardown ( self , test ) : <EOL> self . variables . set_test ( '<STR_LIT>' , test . status ) <EOL> self . variables . set_test ( '<STR_LIT>' , test . message ) <EOL> self . in_test_teardown = True <EOL> self . _remove_timeout ( test . timeout ) <EOL> try : <EOL> yield <EOL> finally : <EOL> self . in_test_teardown = False <EOL> @ contextmanager <EOL> def keyword_teardown ( self , error ) : <EOL> self . variables . set_keyword ( '<STR_LIT>' , '<STR_LIT>' if error else '<STR_LIT>' ) <EOL> self . variables . set_keyword ( '<STR_LIT>' , unic ( error or '<STR_LIT>' ) ) <EOL> self . in_keyword_teardown += <NUM_LIT:1> <EOL> try : <EOL> yield <EOL> finally : <EOL> self . in_keyword_teardown -= <NUM_LIT:1> <EOL> @ property <EOL> @ contextmanager <EOL> def user_keyword ( self ) : <EOL> self . namespace . start_user_keyword ( ) <EOL> try : <EOL> yield <EOL> finally : <EOL> self . namespace . end_user_keyword ( ) <EOL> @ contextmanager <EOL> def timeout ( self , timeout ) : <EOL> self . _add_timeout ( timeout ) <EOL> try : <EOL> yield <EOL> finally : <EOL> self . _remove_timeout ( timeout ) <EOL> @ property <EOL> def in_teardown ( self ) : <EOL> return bool ( self . in_suite_teardown or <EOL> self . in_test_teardown or <EOL> self . in_keyword_teardown ) <EOL> @ property <EOL> def variables ( self ) : <EOL> return self . namespace . variables <EOL> def end_suite ( self , suite ) : <EOL> for name in [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] : <EOL> self . variables . set_global ( name , self . variables [ name ] ) <EOL> self . output . end_suite ( suite ) <EOL> self . namespace . end_suite ( ) <EOL> EXECUTION_CONTEXTS . end_suite ( ) <EOL> def set_suite_variables ( self , suite ) : <EOL> self . variables [ '<STR_LIT>' ] = suite . longname <EOL> self . variables [ '<STR_LIT>' ] = suite . source or '<STR_LIT>' <EOL> self . variables [ '<STR_LIT>' ] = suite . doc <EOL> self . variables [ '<STR_LIT>' ] = suite . metadata . copy ( ) <EOL> def report_suite_status ( self , status , message ) : <EOL> self . variables [ '<STR_LIT>' ] = status <EOL> self . variables [ '<STR_LIT>' ] = message <EOL> def start_test ( self , test ) : <EOL> self . test = test <EOL> self . _add_timeout ( test . timeout ) <EOL> self . namespace . start_test ( ) <EOL> self . variables . set_test ( '<STR_LIT>' , test . name ) <EOL> self . variables . set_test ( '<STR_LIT>' , test . doc ) <EOL> self . variables . set_test ( '<STR_LIT>' , list ( test . tags ) ) <EOL> def _add_timeout ( self , timeout ) : <EOL> if timeout : <EOL> timeout . start ( ) <EOL> self . timeouts . add ( timeout ) <EOL> def _remove_timeout ( self , timeout ) : <EOL> if timeout in self . timeouts : <EOL> self . timeouts . remove ( timeout ) <EOL> def end_test ( self , test ) : <EOL> self . test = None <EOL> self . _remove_timeout ( test . timeout ) <EOL> self . namespace . end_test ( ) <EOL> self . variables . set_suite ( '<STR_LIT>' , test . name ) <EOL> self . variables . set_suite ( '<STR_LIT>' , test . status ) <EOL> self . variables . set_suite ( '<STR_LIT>' , test . message ) <EOL> self . timeout_occurred = False <EOL> def start_keyword ( self , keyword ) : <EOL> self . _started_keywords += <NUM_LIT:1> <EOL> if self . _started_keywords > self . _started_keywords_threshold : <EOL> raise DataError ( '<STR_LIT>' ) <EOL> self . output . start_keyword ( keyword ) <EOL> def end_keyword ( self , keyword ) : <EOL> self . output . end_keyword ( keyword ) <EOL> self . _started_keywords -= <NUM_LIT:1> <EOL> def get_runner ( self , name ) : <EOL> return self . namespace . get_runner ( name ) <EOL> def trace ( self , message ) : <EOL> self . output . trace ( message ) <EOL> def debug ( self , message ) : <EOL> self . output . debug ( message ) <EOL> def info ( self , message ) : <EOL> self . output . info ( message ) <EOL> def warn ( self , message ) : <EOL> self . output . warn ( message ) <EOL> def fail ( self , message ) : <EOL> self . output . fail ( message ) </s>
