Commit 3af4d595 by willmcgugan

Added an ignore_errors keyword parameter to listdir

parent 29cbc8af
...@@ -496,7 +496,12 @@ class FS(object): ...@@ -496,7 +496,12 @@ class FS(object):
return sub_fs return sub_fs
def walk(self, path="/", wildcard=None, dir_wildcard=None, search="breadth"): def walk(self,
path="/",
wildcard=None,
dir_wildcard=None,
search="breadth",
ignore_errors=False):
"""Walks a directory tree and yields the root path and contents. """Walks a directory tree and yields the root path and contents.
Yields a tuple of the path of each directory and a list of its file Yields a tuple of the path of each directory and a list of its file
contents. contents.
...@@ -507,15 +512,26 @@ class FS(object): ...@@ -507,15 +512,26 @@ class FS(object):
:param search: -- A string dentifying the method used to walk the directories. There are two such methods: :param search: -- A string dentifying the method used to walk the directories. There are two such methods:
* 'breadth' Yields paths in the top directories first * 'breadth' Yields paths in the top directories first
* 'depth' Yields the deepest paths first * 'depth' Yields the deepest paths first
:param ignore_errors: Ignore any errors reading the directory
""" """
def listdir(path, *args, **kwargs):
if ignore_errors:
try:
return self.listdir(path, *args, **kwargs)
except:
return []
else:
return self.listdir(path, *args, **kwargs)
if search == "breadth": if search == "breadth":
dirs = [path] dirs = [path]
while dirs: while dirs:
current_path = dirs.pop() current_path = dirs.pop()
paths = [] paths = []
for filename in self.listdir(current_path): for filename in listdir(current_path):
path = pathjoin(current_path, filename) path = pathjoin(current_path, filename)
if self.isdir(path): if self.isdir(path):
...@@ -535,7 +551,7 @@ class FS(object): ...@@ -535,7 +551,7 @@ class FS(object):
elif search == "depth": elif search == "depth":
def recurse(recurse_path): def recurse(recurse_path):
for path in self.listdir(recurse_path, wildcard=dir_wildcard, full=True, dirs_only=True): for path in listdir(recurse_path, wildcard=dir_wildcard, full=True, dirs_only=True):
for p in recurse(path): for p in recurse(path):
yield p yield p
yield (recurse_path, self.listdir(recurse_path, wildcard=wildcard, files_only=True)) yield (recurse_path, self.listdir(recurse_path, wildcard=wildcard, files_only=True))
...@@ -546,27 +562,38 @@ class FS(object): ...@@ -546,27 +562,38 @@ class FS(object):
raise ValueError("Search should be 'breadth' or 'depth'") raise ValueError("Search should be 'breadth' or 'depth'")
def walkfiles(self, path="/", wildcard=None, dir_wildcard=None, search="breadth" ): def walkfiles(self,
path="/",
wildcard=None,
dir_wildcard=None,
search="breadth",
ignore_errors=False ):
"""Like the 'walk' method, but just yields files. """Like the 'walk' method, but just yields files.
:param path: Root path to start walking :param path: Root path to start walking
:param wildcard: If given, only return files that match this wildcard :param wildcard: If given, only return files that match this wildcard
:param dir_wildcard: If given, only walk directories that match the wildcard :param dir_wildcard: If given, only walk directories that match the wildcard
:param search: Same as walk method :param search: Same as walk method
:param ignore_errors: Ignore any errors reading the directory
""" """
for path, files in self.walk(path, wildcard=wildcard, dir_wildcard=dir_wildcard, search=search): for path, files in self.walk(path, wildcard=wildcard, dir_wildcard=dir_wildcard, search=search, ignore_errors=ignore_errors):
for f in files: for f in files:
yield pathjoin(path, f) yield pathjoin(path, f)
def walkdirs(self, path="/", wildcard=None, search="breadth"): def walkdirs(self,
path="/",
wildcard=None,
search="breadth",
ignore_errors=False):
""" Like the 'walk' method but yields directories. """ Like the 'walk' method but yields directories.
:param path: -- Root path to start walking :param path: -- Root path to start walking
:param wildcard: -- If given, only return dictories that match this wildcard :param wildcard: -- If given, only return dictories that match this wildcard
:param search: -- Same as the walk method :param search: -- Same as the walk method
:param ignore_errors: Ignore any errors reading the directory
""" """
for p, files in self.walk(path, wildcard=wildcard, search=search): for p, files in self.walk(path, wildcard=wildcard, search=search, ignore_errors=ignore_errors):
yield p yield p
...@@ -825,9 +852,9 @@ class SubFS(FS): ...@@ -825,9 +852,9 @@ class SubFS(FS):
def desc(self, path): def desc(self, path):
if self.isdir(path): if self.isdir(path):
return "Sub dir of %s"%str(self.parent) return "Sub dir of %s" % str(self.parent)
else: else:
return "File in sub dir of %s"%str(self.parent) return "File in sub dir of %s" % str(self.parent)
def _delegate(self, path): def _delegate(self, path):
return pathjoin(self.sub_dir, relpath(normpath(path))) return pathjoin(self.sub_dir, relpath(normpath(path)))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment