summaryrefslogtreecommitdiff
path: root/fs/wrapfs
diff options
context:
space:
mode:
authorrfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f>2011-01-26 00:40:17 +0000
committerrfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f>2011-01-26 00:40:17 +0000
commit829536a6e612b221c259c290dea14881563b6475 (patch)
tree2382f60c9411829e6b386a6a9c478f505a4e11e3 /fs/wrapfs
parentbeb45a017fe1267d3fe5f01302f9568fcae0f1ff (diff)
downloadpyfilesystem-829536a6e612b221c259c290dea14881563b6475.tar.gz
Implement walk/walkfiles/walkdirs in WrapFS.
This allows FS subclasses to provide more efficient implementations, and have them transparently used through various wrapper classes. git-svn-id: http://pyfilesystem.googlecode.com/svn/trunk@616 67cdc799-7952-0410-af00-57a81ceafa0f
Diffstat (limited to 'fs/wrapfs')
-rw-r--r--fs/wrapfs/__init__.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/fs/wrapfs/__init__.py b/fs/wrapfs/__init__.py
index 46d1c48..2fafb08 100644
--- a/fs/wrapfs/__init__.py
+++ b/fs/wrapfs/__init__.py
@@ -278,6 +278,64 @@ class WrapFS(FS):
yield (nm,info)
@rewrite_errors
+ def walk(self,path="/",wildcard=None,dir_wildcard=None,search="breadth",ignore_errors=False):
+ if dir_wildcard is not None:
+ # If there is a dir_wildcard, fall back to the default impl
+ # that uses listdir(). Otherwise we run the risk of enumerating
+ # lots of directories that will just be thrown away.
+ for item in super(WrapFS,self).walk(path,wildcard,dir_wildcard,search,ignore_errors):
+ yield item
+ # Otherwise, the wrapped FS may provide a more efficient impl
+ # which we can use directly.
+ else:
+ if wildcard is not None and not callable(wildcard):
+ wildcard_re = re.compile(fnmatch.translate(wildcard))
+ wildcard = lambda fn:bool (wildcard_re.match(fn))
+ for (dirpath,filepaths) in self.wrapped_fs.walk(self._encode(path),search=search,ignore_errors=ignore_errors):
+ filepaths = [basename(self._decode(pathjoin(dirpath,p)))
+ for p in filepaths]
+ dirpath = abspath(self._decode(dirpath))
+ if wildcard is not None:
+ filepaths = [p for p in filepaths if wildcard(p)]
+ yield (dirpath,filepaths)
+
+ @rewrite_errors
+ def walkfiles(self,path="/",wildcard=None,dir_wildcard=None,search="breadth",ignore_errors=False):
+ if dir_wildcard is not None:
+ # If there is a dir_wildcard, fall back to the default impl
+ # that uses listdir(). Otherwise we run the risk of enumerating
+ # lots of directories that will just be thrown away.
+ for item in super(WrapFS,self).walkfiles(path,wildcard,dir_wildcard,search,ignore_errors):
+ yield item
+ # Otherwise, the wrapped FS may provide a more efficient impl
+ # which we can use directly.
+ else:
+ if wildcard is not None and not callable(wildcard):
+ wildcard_re = re.compile(fnmatch.translate(wildcard))
+ wildcard = lambda fn:bool (wildcard_re.match(fn))
+ for filepath in self.wrapped_fs.walkfiles(self._encode(path),search=search,ignore_errors=ignore_errors):
+ filepath = abspath(self._decode(filepath))
+ if wildcard is not None:
+ if not wildcard(basename(filepath)):
+ continue
+ yield filepath
+
+ @rewrite_errors
+ def walkdirs(self,path="/",wildcard=None,search="breadth",ignore_errors=False):
+ if wildcard is not None:
+ # If there is a wildcard, fall back to the default impl
+ # that uses listdir(). Otherwise we run the risk of enumerating
+ # lots of directories that will just be thrown away.
+ for item in super(WrapFS,self).walkdirs(path,wildcard,search,ignore_errors):
+ yield item
+ # Otherwise, the wrapped FS may provide a more efficient impl
+ # which we can use directly.
+ else:
+ for dirpath in self.wrapped_fs.walkdirs(self._encode(path),search=search,ignore_errors=ignore_errors):
+ yield abspath(self._decode(dirpath))
+
+
+ @rewrite_errors
def makedir(self, path, *args, **kwds):
return self.wrapped_fs.makedir(self._encode(path),*args,**kwds)