(file_pattern, ignore_case, fuzzy)
return simple_compare
Return the adequate comparison (regex or simple)
Return the adequate comparison (regex or simple)
Return the adequate comparison ( regex or simple )
def create_comparison(file_pattern, ignore_case, fuzzy):
    ''' Return the adequate comparison (regex or simple) '''
    if fuzzy:
        # Generate a pattern to fuzzy match
        file_pattern = '.*?'.join(f for f in file_pattern)

    try:
        if ignore_case:
            pattern = re.compile(file_pattern, re.IGNORECASE)
        else:
            pattern = re.compile(file_pattern)
    except re.error:
        msg = (
        '{red}Sorry, the expression {pattern} is incorrect.{no_color}\n'
        'Remember that this should be a regular expression.\n'
        '(https://docs.python.org/howto/regex.html)\n'
        'If you are trying something "*py" for "Everyfile that ends with py"\n'
        'you can use just "py"(if you can tolerate .pyc) or "py$"\n'
        )
        raise WrongPattern(msg.format(pattern=file_pattern,
                                      red=RED_CHARACTER,
                                      no_color=NO_COLOR))

    def regex_compare(to_match):
        match = re.search(pattern, to_match)
        if match:
            smatch = [to_match[:match.start()],
                      to_match[match.start(): match.end()],
                      to_match[match.end():]]
            return smatch

    # Check if is a proper regex (contains a char different from [a-zA-Z0-9])
    if fuzzy or re.search(r'[^a-zA-Z0-9]', file_pattern):
        return regex_compare

    # We can go with a simplified comparison
    if ignore_case:
        file_pattern = file_pattern.lower()

        def simple_compare_case_insensitive(to_match):
            if file_pattern in to_match.lower():
                return regex_compare(to_match)
        return simple_compare_case_insensitive

    def simple_compare(to_match):
        if file_pattern in to_match:
            return regex_compare(to_match)

    return simple_compare
def create_comparison ( file_pattern , ignore_case , fuzzy ) : if fuzzy : # Generate a pattern to fuzzy match file_pattern = '.*?' . join ( f for f in file_pattern ) try : if ignore_case : pattern = re . compile ( file_pattern , re . IGNORECASE ) else : pattern = re . compile ( file_pattern ) except re . error : msg = ( '{red}Sorry, the expression {pattern} is incorrect.{no_color}\n' 'Remember that this should be a regular expression.\n' '(https://docs.python.org/howto/regex.html)\n' 'If you are trying something "*py" for "Everyfile that ends with py"\n' 'you can use just "py"(if you can tolerate .pyc) or "py$"\n' ) raise WrongPattern ( msg . format ( pattern = file_pattern , red = RED_CHARACTER , no_color = NO_COLOR ) ) def regex_compare ( to_match ) : match = re . search ( pattern , to_match ) if match : smatch = [ to_match [ : match . start ( ) ] , to_match [ match . start ( ) : match . end ( ) ] , to_match [ match . end ( ) : ] ] return smatch # Check if is a proper regex (contains a char different from [a-zA-Z0-9]) if fuzzy or re . search ( r'[^a-zA-Z0-9]' , file_pattern ) : return regex_compare # We can go with a simplified comparison if ignore_case : file_pattern = file_pattern . lower ( ) def simple_compare_case_insensitive ( to_match ) : if file_pattern in to_match . lower ( ) : return regex_compare ( to_match ) return simple_compare_case_insensitive def simple_compare ( to_match ) : if file_pattern in to_match : return regex_compare ( to_match ) return simple_compare