def get_name_filter(filter_this, include_this): """Returns a function that evaluates if a file or folder name must be filtered out. The compared paths are first converted to unicode and decomposed. This is neccesary because the way PY2.* `os.walk` read unicode paths in different filesystems. For instance, in OSX, it returns a decomposed unicode string. In those systems, u'ñ' is read as `\u0303` instead of `\xf1`. """ filter_this = [normalize(to_unicode(f)) for f in filter_this] include_this = [normalize(to_unicode(f)) for f in include_this] def fullmatch(path, pattern): path = normalize(path) name = os.path.basename(path) return fnmatch(name, pattern) or fnmatch(path, pattern) def must_be_filtered(name): return reduce(lambda r, pattern: r or fullmatch(name, pattern), filter_this, False) def must_be_included(name): return reduce(lambda r, pattern: r or fullmatch(name, pattern), include_this, False) def must_filter(path): return must_be_filtered(path) and not must_be_included(path) return must_filter
def fullmatch(path, pattern): path = normalize(path) name = os.path.basename(path) return fnmatch(name, pattern) or fnmatch(path, pattern)