<s> import re <EOL> from functools import partial <EOL> from . compat import py2to3 <EOL> from . normalizing import normalize <EOL> from . robottypes import is_string <EOL> def eq ( str1 , str2 , ignore = ( ) , caseless = True , spaceless = True ) : <EOL> str1 = normalize ( str1 , ignore , caseless , spaceless ) <EOL> str2 = normalize ( str2 , ignore , caseless , spaceless ) <EOL> return str1 == str2 <EOL> @ py2to3 <EOL> class Matcher ( object ) : <EOL> _pattern_tokenizer = re . compile ( '<STR_LIT>' ) <EOL> _wildcards = { '<STR_LIT:*>' : '<STR_LIT>' , '<STR_LIT:?>' : '<STR_LIT:.>' } <EOL> def __init__ ( self , pattern , ignore = ( ) , caseless = True , spaceless = True , <EOL> regexp = False ) : <EOL> self . pattern = pattern <EOL> self . _normalize = partial ( normalize , ignore = ignore , caseless = caseless , <EOL> spaceless = spaceless ) <EOL> self . _regexp = self . _get_and_compile_regexp ( self . _normalize ( pattern ) , <EOL> regexp = regexp ) <EOL> def _get_and_compile_regexp ( self , pattern , regexp = False ) : <EOL> if not regexp : <EOL> pattern = '<STR_LIT>' % '<STR_LIT>' . join ( self . _glob_pattern_to_regexp ( pattern ) ) <EOL> return re . compile ( pattern , re . DOTALL ) <EOL> def _glob_pattern_to_regexp ( self , pattern ) : <EOL> for token in self . _pattern_tokenizer . split ( pattern ) : <EOL> if token in self . _wildcards : <EOL> yield self . _wildcards [ token ] <EOL> else : <EOL> yield re . escape ( token ) <EOL> def match ( self , string ) : <EOL> return self . _regexp . match ( self . _normalize ( string ) ) is not None <EOL> def match_any ( self , strings ) : <EOL> return any ( self . match ( s ) for s in strings ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . _normalize ( self . pattern ) ) <EOL> class MultiMatcher ( object ) : <EOL> def __init__ ( self , patterns = None , ignore = ( ) , caseless = True , spaceless = True , <EOL> match_if_no_patterns = False , regexp = False ) : <EOL> self . _matchers = [ Matcher ( pattern , ignore , caseless , spaceless , regexp ) <EOL> for pattern in self . _ensure_list ( patterns ) ] <EOL> self . _match_if_no_patterns = match_if_no_patterns <EOL> def _ensure_list ( self , patterns ) : <EOL> if patterns is None : <EOL> return [ ] <EOL> if is_string ( patterns ) : <EOL> return [ patterns ] <EOL> return patterns <EOL> def match ( self , string ) : <EOL> if self . _matchers : <EOL> return any ( m . match ( string ) for m in self . _matchers ) <EOL> return self . _match_if_no_patterns <EOL> def match_any ( self , strings ) : <EOL> return any ( self . match ( s ) for s in strings ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _matchers ) <EOL> def __iter__ ( self ) : <EOL> for matcher in self . _matchers : <EOL> yield matcher . pattern </s>
