"""Filename matching with shell patterns. fnmatch(FILENAME, PATTERN) matches according to the local convention. fnmatchcase(FILENAME, PATTERN) always takes case in account. The functions operate by translating the pattern into a regular expression. They cache the compiled regular expressions for speed. The function translate(PATTERN) returns a regular expression corresponding to PATTERN. (It does not compile it.) Based on code from fnmatch.py file distributed with Python 2.6. Licensed under PSF License (see LICENSE.PSF file). Changes to original fnmatch module: - translate function supports ``*`` and ``**`` similarly to fnmatch C library """ import os import re __all__ = ["fnmatch", "fnmatchcase", "translate"] _cache = {} LEFT_BRACE = re.compile( r""" (? 0 and not is_escaped: result += '|' else: result += '\\,' elif current_char == '}': if brace_level > 0 and not is_escaped: result += ')' brace_level -= 1 else: result += '\\}' elif current_char == '/': if pat[index:(index + 3)] == "**/": result += "(?:/|/.*/)" index += 3 else: result += '/' elif current_char != '\\': result += re.escape(current_char) if current_char == '\\': if is_escaped: result += re.escape(current_char) is_escaped = not is_escaped else: is_escaped = False if not nested: result = r'(?s)%s\Z' % result return result, numeric_groups