<s> import abc <EOL> import copy <EOL> import jsonschema <EOL> import six <EOL> from rally . common import logging <EOL> from rally . common . plugin import plugin <EOL> from rally . common import utils <EOL> from rally import exceptions <EOL> from rally . task import functional <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def configure ( name , order , hidden = False ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( cls ) : <EOL> cls = plugin . configure ( name = name ) ( cls ) <EOL> cls . _meta_set ( "<STR_LIT>" , order ) <EOL> cls . _meta_set ( "<STR_LIT>" , hidden ) <EOL> return cls <EOL> return wrapper <EOL> @ configure ( name = "<STR_LIT>" , order = <NUM_LIT:0> , hidden = True ) <EOL> class Context ( plugin . Plugin , functional . FunctionalMixin , <EOL> utils . RandomNameGeneratorMixin ) : <EOL> """<STR_LIT>""" <EOL> RESOURCE_NAME_FORMAT = "<STR_LIT>" <EOL> CONFIG_SCHEMA = { } <EOL> def __init__ ( self , ctx ) : <EOL> self . config = ctx . get ( "<STR_LIT>" , { } ) . get ( self . get_name ( ) , { } ) <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> for key , value in self . DEFAULT_CONFIG . items ( ) : <EOL> self . config . setdefault ( key , value ) <EOL> self . context = ctx <EOL> self . task = self . context . get ( "<STR_LIT>" , { } ) <EOL> def __lt__ ( self , other ) : <EOL> return self . get_order ( ) < other . get_order ( ) <EOL> def __gt__ ( self , other ) : <EOL> return self . get_order ( ) > other . get_order ( ) <EOL> def __eq__ ( self , other ) : <EOL> return self . get_order ( ) == other . get_order ( ) <EOL> @ classmethod <EOL> def validate ( cls , config , non_hidden = False ) : <EOL> if non_hidden and cls . _meta_get ( "<STR_LIT>" ) : <EOL> raise exceptions . PluginNotFound ( name = cls . get_name ( ) , <EOL> namespace = "<STR_LIT>" ) <EOL> jsonschema . validate ( config , cls . CONFIG_SCHEMA ) <EOL> @ classmethod <EOL> def get_order ( cls ) : <EOL> return cls . _meta_get ( "<STR_LIT>" ) <EOL> @ abc . abstractmethod <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractmethod <EOL> def cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> def map_for_scenario ( self , context_obj ) : <EOL> """<STR_LIT>""" <EOL> return context_obj <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , exc_traceback ) : <EOL> self . cleanup ( ) <EOL> class ContextManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , context_obj ) : <EOL> self . _visited = [ ] <EOL> self . context_obj = context_obj <EOL> @ staticmethod <EOL> def validate ( ctx , non_hidden = False ) : <EOL> for name , config in six . iteritems ( ctx ) : <EOL> Context . get ( name ) . validate ( config , non_hidden = non_hidden ) <EOL> def _get_sorted_context_lst ( self ) : <EOL> ctxlst = map ( Context . get , self . context_obj [ "<STR_LIT>" ] ) <EOL> return sorted ( map ( lambda ctx : ctx ( self . context_obj ) , ctxlst ) ) <EOL> def setup ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _visited = [ ] <EOL> for ctx in self . _get_sorted_context_lst ( ) : <EOL> self . _visited . append ( ctx ) <EOL> ctx . setup ( ) <EOL> return self . context_obj <EOL> def cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> ctxlst = self . _visited or self . _get_sorted_context_lst ( ) <EOL> for ctx in ctxlst [ : : - <NUM_LIT:1> ] : <EOL> try : <EOL> ctx . cleanup ( ) <EOL> except Exception as e : <EOL> LOG . error ( "<STR_LIT>" % ctx . get_name ( ) ) <EOL> LOG . exception ( e ) <EOL> def map_for_scenario ( self ) : <EOL> """<STR_LIT>""" <EOL> context_obj = copy . deepcopy ( self . context_obj ) <EOL> for ctx in self . _get_sorted_context_lst ( ) : <EOL> context_obj = ctx . map_for_scenario ( context_obj ) <EOL> return context_obj <EOL> def __enter__ ( self ) : <EOL> try : <EOL> self . setup ( ) <EOL> except Exception : <EOL> self . cleanup ( ) <EOL> raise <EOL> def __exit__ ( self , exc_type , exc_value , exc_traceback ) : <EOL> self . cleanup ( ) </s>
