<s> from st2common import log as logging <EOL> from st2common . constants . rules import RULE_TYPE_BACKSTOP <EOL> from st2reactor . rules . filter import RuleFilter , SecondPassRuleFilter <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> class RulesMatcher ( object ) : <EOL> def __init__ ( self , trigger_instance , trigger , rules , extra_info = False ) : <EOL> self . trigger_instance = trigger_instance <EOL> self . trigger = trigger <EOL> self . rules = rules <EOL> self . extra_info = extra_info <EOL> def get_matching_rules ( self ) : <EOL> first_pass , second_pass = self . _split_rules_into_passes ( ) <EOL> rule_filters = [ RuleFilter ( trigger_instance = self . trigger_instance , <EOL> trigger = self . trigger , <EOL> rule = rule , <EOL> extra_info = self . extra_info ) <EOL> for rule in first_pass ] <EOL> matched_rules = [ rule_filter . rule for rule_filter in rule_filters if rule_filter . filter ( ) ] <EOL> LOG . debug ( '<STR_LIT>' , len ( matched_rules ) , <EOL> self . trigger [ '<STR_LIT:name>' ] ) <EOL> rule_filters = [ SecondPassRuleFilter ( self . trigger_instance , self . trigger , rule , <EOL> matched_rules ) <EOL> for rule in second_pass ] <EOL> matched_in_second_pass = [ rule_filter . rule for rule_filter in rule_filters <EOL> if rule_filter . filter ( ) ] <EOL> LOG . debug ( '<STR_LIT>' , len ( matched_in_second_pass ) , <EOL> self . trigger [ '<STR_LIT:name>' ] ) <EOL> matched_rules . extend ( matched_in_second_pass ) <EOL> LOG . info ( '<STR_LIT>' , len ( matched_rules ) , <EOL> self . trigger [ '<STR_LIT:name>' ] ) <EOL> return matched_rules <EOL> def _split_rules_into_passes ( self ) : <EOL> """<STR_LIT>""" <EOL> first_pass = [ ] <EOL> second_pass = [ ] <EOL> for rule in self . rules : <EOL> if self . _is_first_pass_rule ( rule ) : <EOL> first_pass . append ( rule ) <EOL> else : <EOL> second_pass . append ( rule ) <EOL> return first_pass , second_pass <EOL> def _is_first_pass_rule ( self , rule ) : <EOL> return rule . type [ '<STR_LIT>' ] != RULE_TYPE_BACKSTOP </s>
