Skip to content

Commit

Permalink
Merge pull request #24 from ncoghlan/pep8-walkdir.py
Browse files Browse the repository at this point in the history
format by PEP8 walkdir.py
  • Loading branch information
palaviv committed Oct 8, 2018
2 parents 1823192 + 7267756 commit 9b27808
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions walkdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,38 @@
except NameError:
_str_base = str


# Filtering for inclusion


def _make_include_filter(patterns):
"""Create a filtering function from a collection of inclusion patterns"""
# Trivial case: exclude everything
if not patterns:
def _filter(names):
return names[0:0]

return _filter
# Use fnmatch.filter if it's applicable
if len(patterns) == 1:
def _filter(names):
return fnmatch.filter(names, patterns[0])

return _filter

# Handle the general case for inclusion
def _should_include(name):
return any(fnmatch.fnmatch(name, pattern)
for pattern in patterns)
for pattern in patterns)

def _filter(names):
for name in names:
if _should_include(name):
yield name

return _filter



def include_dirs(walk_iter, *include_filters):
"""Use :func:`fnmatch.fnmatch` patterns to select directories of interest
Expand All @@ -50,6 +58,7 @@ def include_dirs(walk_iter, *include_filters):
subdirs[:] = filter_subdirs(subdirs)
yield dir_entry


def include_files(walk_iter, *include_filters):
"""Use :func:`fnmatch.fnmatch` patterns to select files of interest
Expand All @@ -65,6 +74,7 @@ def include_files(walk_iter, *include_filters):
files[:] = filter_files(files)
yield dir_entry


# Filtering for exclusion

def _make_exclude_filter(patterns):
Expand All @@ -73,17 +83,22 @@ def _make_exclude_filter(patterns):
if not patterns:
def _filter(names):
return names

return _filter

# Handle the general case for exclusion
def _should_exclude(name):
return any(fnmatch.fnmatch(name, pattern)
for pattern in patterns)
for pattern in patterns)

def _filter(names):
for name in names:
if not _should_exclude(name):
yield name

return _filter


def exclude_dirs(walk_iter, *exclude_filters):
"""Use :func:`fnmatch.fnmatch` patterns to skip irrelevant directories
Expand All @@ -99,6 +114,7 @@ def exclude_dirs(walk_iter, *exclude_filters):
subdirs[:] = filter_subdirs(subdirs)
yield dir_entry


def exclude_files(walk_iter, *exclude_filters):
"""Use :func:`fnmatch.fnmatch` patterns to skip irrelevant files
Expand Down Expand Up @@ -134,7 +150,7 @@ def limit_depth(walk_iter, depth):
if depth < 0:
msg = "Depth limit less than 0 ({0!r} provided)"
raise ValueError(msg.format(depth))
sep=os.sep
sep = os.sep
for dir_entry in walk_iter:
yield dir_entry
top = dir_entry[0]
Expand All @@ -151,6 +167,7 @@ def limit_depth(walk_iter, depth):
if current_depth >= depth:
subdirs[:] = []


def min_depth(walk_iter, depth):
"""Only process subdirectories beyond a minimum depth
Expand Down Expand Up @@ -192,7 +209,7 @@ def min_depth(walk_iter, depth):
if depth < 1:
msg = "Minimum depth less than 1 ({0!r} provided)"
raise ValueError(msg.format(depth))
sep=os.sep
sep = os.sep
for dir_entry in walk_iter:
initial_depth = dir_entry[0].count(sep)
break
Expand All @@ -202,8 +219,8 @@ def min_depth(walk_iter, depth):
if current_depth >= depth:
yield dir_entry

# Symlink loop handling

# Symlink loop handling
def handle_symlink_loops(walk_iter, onloop=None):
"""Handle symlink loops when following symlinks during a walk
Expand All @@ -224,7 +241,7 @@ def onloop(dirpath):
msg = "Symlink {0!r} refers to a parent directory, skipping\n"
sys.stderr.write(msg.format(dirpath))
sys.stderr.flush()
sep=os.sep
sep = os.sep
for dir_entry in walk_iter:
yield dir_entry
top = dir_entry[0]
Expand All @@ -250,11 +267,12 @@ def onloop(dirpath):
continue
yield dir_entry


# Convenience function that puts together an iterator pipeline

def filtered_walk(top, included_files=None, included_dirs=None,
excluded_files=None, excluded_dirs=None,
depth=None, followlinks=False, min_depth=None):
excluded_files=None, excluded_dirs=None,
depth=None, followlinks=False, min_depth=None):
"""This is a wrapper around ``os.walk()`` and other filesystem traversal
iterators, with these additional features:
Expand Down Expand Up @@ -325,6 +343,7 @@ def dir_paths(walk_iter):
for dir_entry in walk_iter:
yield dir_entry[0]


def all_dir_paths(walk_iter):
"""Iterate over all directories reachable through the underlying walk
Expand Down Expand Up @@ -359,6 +378,7 @@ def all_dir_paths(walk_iter):
yield os.path.join(dirpath, subdir)
dir_entry = next(walk_iter, None)


def file_paths(walk_iter):
"""Iterate over the files in directories visited by the underlying walk
Expand All @@ -369,6 +389,7 @@ def file_paths(walk_iter):
for fname in dir_entry[2]:
yield os.path.join(dir_entry[0], fname)


def all_paths(walk_iter):
"""Iterate over all paths reachable through the underlying walk
Expand Down Expand Up @@ -399,6 +420,7 @@ def all_paths(walk_iter):
yield os.path.join(dirpath, subdir)
dir_entry = next(walk_iter, None)


# Legacy API
iter_dir_paths = dir_paths
iter_file_paths = file_paths
Expand Down

0 comments on commit 9b27808

Please sign in to comment.