<s> import os <EOL> import six <EOL> from jinja2 . exceptions import UndefinedError <EOL> from st2common import log as logging <EOL> from st2common . content . loader import MetaLoader <EOL> from st2common . models . db . rule import RuleDB <EOL> from st2common . models . db . trigger import TriggerDB <EOL> from st2common . models . db . trigger import TriggerInstanceDB <EOL> from st2common . models . system . common import ResourceReference <EOL> from st2common . persistence . reactor import Rule , TriggerInstance , Trigger <EOL> from st2reactor . rules . enforcer import RuleEnforcer <EOL> from st2reactor . rules . matcher import RulesMatcher <EOL> __all__ = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class RuleTester ( object ) : <EOL> def __init__ ( self , rule_file_path = None , rule_ref = None , trigger_instance_file_path = None , <EOL> trigger_instance_id = None ) : <EOL> """<STR_LIT>""" <EOL> self . _rule_file_path = rule_file_path <EOL> self . _rule_ref = rule_ref <EOL> self . _trigger_instance_file_path = trigger_instance_file_path <EOL> self . _trigger_instance_id = trigger_instance_id <EOL> self . _meta_loader = MetaLoader ( ) <EOL> def evaluate ( self ) : <EOL> """<STR_LIT>""" <EOL> rule_db = self . _get_rule_db ( ) <EOL> trigger_instance_db , trigger_db = self . _get_trigger_instance_db ( ) <EOL> if rule_db . trigger != trigger_db . ref : <EOL> LOG . info ( '<STR_LIT>' , <EOL> rule_db . trigger , trigger_db . ref ) <EOL> return False <EOL> matcher = RulesMatcher ( trigger_instance = trigger_instance_db , trigger = trigger_db , <EOL> rules = [ rule_db ] , extra_info = True ) <EOL> matching_rules = matcher . get_matching_rules ( ) <EOL> if len ( matching_rules ) < <NUM_LIT:1> : <EOL> return False <EOL> try : <EOL> enforcer = RuleEnforcer ( trigger_instance = trigger_instance_db , rule = rule_db ) <EOL> params = enforcer . get_resolved_parameters ( ) <EOL> LOG . info ( '<STR_LIT>' ) <EOL> for param in six . iteritems ( params ) : <EOL> LOG . info ( '<STR_LIT>' , param [ <NUM_LIT:0> ] , param [ <NUM_LIT:1> ] ) <EOL> return True <EOL> except ( UndefinedError , ValueError ) as e : <EOL> LOG . error ( '<STR_LIT>' , str ( e ) ) <EOL> return False <EOL> except : <EOL> LOG . exception ( '<STR_LIT>' ) <EOL> return False <EOL> def _get_rule_db ( self ) : <EOL> if self . _rule_file_path : <EOL> return self . _get_rule_db_from_file ( <EOL> file_path = os . path . realpath ( self . _rule_file_path ) ) <EOL> elif self . _rule_ref : <EOL> return Rule . get_by_ref ( self . _rule_ref ) <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def _get_trigger_instance_db ( self ) : <EOL> if self . _trigger_instance_file_path : <EOL> return self . _get_trigger_instance_db_from_file ( <EOL> file_path = os . path . realpath ( self . _trigger_instance_file_path ) ) <EOL> elif self . _trigger_instance_id : <EOL> trigger_instance_db = TriggerInstance . get_by_id ( self . _trigger_instance_id ) <EOL> trigger_db = Trigger . get_by_ref ( trigger_instance_db . trigger ) <EOL> return trigger_instance_db , trigger_db <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _get_rule_db_from_file ( self , file_path ) : <EOL> data = self . _meta_loader . load ( file_path = file_path ) <EOL> pack = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> name = data . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> trigger = data [ '<STR_LIT>' ] [ '<STR_LIT:type>' ] <EOL> criteria = data . get ( '<STR_LIT>' , None ) <EOL> action = data . get ( '<STR_LIT:action>' , { } ) <EOL> rule_db = RuleDB ( pack = pack , name = name , trigger = trigger , criteria = criteria , action = action , <EOL> enabled = True ) <EOL> return rule_db <EOL> def _get_trigger_instance_db_from_file ( self , file_path ) : <EOL> data = self . _meta_loader . load ( file_path = file_path ) <EOL> instance = TriggerInstanceDB ( ** data ) <EOL> trigger_ref = ResourceReference . from_string_reference ( instance [ '<STR_LIT>' ] ) <EOL> trigger_db = TriggerDB ( pack = trigger_ref . pack , name = trigger_ref . name , type = trigger_ref . ref ) <EOL> return instance , trigger_db </s>
