我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用fnmatch.translate()。
def _glob_to_re(self, pattern): """Translate a shell-like glob pattern to a regular expression. Return a string containing the regex. Differs from 'fnmatch.translate()' in that '*' does not match "special characters" (which are platform-specific). """ pattern_re = fnmatch.translate(pattern) # '?' and '*' in the glob pattern become '.' and '.*' in the RE, which # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix, # and by extension they shouldn't match such "special characters" under # any OS. So change all non-escaped dots in the RE to match any # character except the special characters (currently: just os.sep). sep = os.sep if os.sep == '\\': # we're using a regex to manipulate a regex, so we need # to escape the backslash twice sep = r'\\\\' escaped = r'\1[^%s]' % sep pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', escaped, pattern_re) return pattern_re
def __init__( self, index_url="https://pypi.python.org/simple", hosts=('*',), ca_bundle=None, verify_ssl=True, *args, **kw ): Environment.__init__(self, *args, **kw) self.index_url = index_url + "/" [:not index_url.endswith('/')] self.scanned_urls = {} self.fetched_urls = {} self.package_pages = {} self.allows = re.compile('|'.join(map(translate, hosts))).match self.to_scan = [] use_ssl = ( verify_ssl and ssl_support.is_available and (ca_bundle or ssl_support.find_ca_bundle()) ) if use_ssl: self.opener = ssl_support.opener_for(ca_bundle) else: self.opener = urllib.request.urlopen
def glob_to_re(pattern): """Translate a shell-like glob pattern to a regular expression. Return a string containing the regex. Differs from 'fnmatch.translate()' in that '*' does not match "special characters" (which are platform-specific). """ pattern_re = fnmatch.translate(pattern) # '?' and '*' in the glob pattern become '.' and '.*' in the RE, which # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix, # and by extension they shouldn't match such "special characters" under # any OS. So change all non-escaped dots in the RE to match any # character except the special characters. # XXX currently the "special characters" are just slash -- i.e. this is # Unix-only. pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', r'\1[^/]', pattern_re) return pattern_re
def glob_to_re(pattern): """Translate a shell-like glob pattern to a regular expression. Return a string containing the regex. Differs from 'fnmatch.translate()' in that '*' does not match "special characters" (which are platform-specific). """ pattern_re = fnmatch.translate(pattern) # '?' and '*' in the glob pattern become '.' and '.*' in the RE, which # IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix, # and by extension they shouldn't match such "special characters" under # any OS. So change all non-escaped dots in the RE to match any # character except the special characters (currently: just os.sep). sep = os.sep if os.sep == '\\': # we're using a regex to manipulate a regex, so we need # to escape the backslash twice sep = r'\\\\' escaped = r'\1[^%s]' % sep pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', escaped, pattern_re) return pattern_re
def fnmatch_to_re(self, clean_fn_match): # pylint: disable=no-self-use """Method converts Apache's basic fnmatch to regular expression. Assumption - Configs are assumed to be well-formed and only writable by privileged users. https://apr.apache.org/docs/apr/2.0/apr__fnmatch_8h_source.html http://apache2.sourcearchive.com/documentation/2.2.16-6/apr__fnmatch_8h_source.html :param str clean_fn_match: Apache style filename match, like globs :returns: regex suitable for augeas :rtype: str """ if sys.version_info < (3, 6): # This strips off final /Z(?ms) return fnmatch.translate(clean_fn_match)[:-7] else: # pragma: no cover # Since Python 3.6, it returns a different pattern like (?s:.*\.load)\Z return fnmatch.translate(clean_fn_match)[4:-3]
def exclusion_filter(exclude: str): """Converts a filter string "*.abc;*.def" to a function that can be passed to pack(). If 'exclude' is None or an empty string, returns None (which means "no filtering"). """ if not exclude: return None import re import fnmatch # convert string into regex callback that operates on bytes # "*.txt;*.png;*.rst" --> rb".*\.txt$|.*\.png$|.*\.rst$" pattern = b'|'.join(fnmatch.translate(f).encode('utf-8') for f in exclude.split(';') if f) compiled_pattern = re.compile(pattern, re.IGNORECASE) def filename_filter(fname: bytes): return not compiled_pattern.match(fname) return filename_filter
def check_newline_eof(): includes = r'|'.join([fnmatch.translate(x) for x in NEWLINE_EOF_INCLUDE_PATTERNS]) excludes = r'|'.join([fnmatch.translate(x) for x in NEWLINE_EOF_EXCLUDE_PATTERNS]) return_code = 0 def has_newline_eof(path): with open(path, 'r') as f: data = f.read() if data and data[-1] != '\n': LOG.error('%s file error: no newline at end of file', path) return False return True for root, dirs, files in os.walk(PROJECT_ROOT): dirs[:] = [d for d in dirs if not re.match(excludes, d)] for f in files: if not re.match(excludes, f) and re.match(includes, f): if not has_newline_eof(os.path.join(root, f)): return_code = 1 return return_code
def keys(self, pattern='*'): """Emulate keys.""" import fnmatch import re # making sure the pattern is unicode/str. try: pattern = pattern.decode('utf-8') # This throws an AttributeError in python 3, or an # UnicodeEncodeError in python 2 except (AttributeError, UnicodeEncodeError): pass # Make regex out of glob styled pattern. regex = fnmatch.translate(pattern) regex = re.compile(regex) # Find every key that matches the pattern return [key for key in self.redis.keys() if regex.match(key.decode('utf-8'))]
def get_all_fastq_files(data_dir): """ recursively go down data_dir and get all fastq files arguments: data_dir -- the directory that has SampleSheet.csv in it return list containing path for fastq files """ pattern = fn_translate("*.fastq.*") fastq_files_path = path.join(data_dir, "Data", "Intensities", "BaseCalls") try: file_list = listdir(fastq_files_path) fastq_file_list = [path.join(fastq_files_path, file) for file in file_list if re.match(pattern, file)] fastq_file_list.sort() except OSError: msg = "Invalid directory " + fastq_files_path raise OSError(msg) return fastq_file_list