summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f>2009-05-06 06:01:39 +0000
committerrfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f>2009-05-06 06:01:39 +0000
commit27ba38114e1b4f834ea24e4f39be3cd8f5d9d1ae (patch)
tree0bd462880699633e7c7650e5994bbba047eab177
parenta572d441c8f3de3db5bf01877807e88bef44fecb (diff)
downloadpyfilesystem-27ba38114e1b4f834ea24e4f39be3cd8f5d9d1ae.tar.gz
added "HideDotFiles" wrapper
git-svn-id: http://pyfilesystem.googlecode.com/svn/branches/rfk-ideas@140 67cdc799-7952-0410-af00-57a81ceafa0f
-rw-r--r--fs/wrappers/hidedotfiles.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/fs/wrappers/hidedotfiles.py b/fs/wrappers/hidedotfiles.py
new file mode 100644
index 0000000..6203410
--- /dev/null
+++ b/fs/wrappers/hidedotfiles.py
@@ -0,0 +1,78 @@
+"""
+
+ fs.wrappers.hidedotfiles: FS wrapper to hide dot-files in dir listings
+
+"""
+
+from fs.helpers import *
+from fs.errors import *
+from fs.wrappers import FSWrapper
+
+
+class HideDotFiles(FSWrapper):
+ """FS wrapper class that hides dot-files in directory listings.
+
+ The listdir() function takes an extra keyword argument 'hidden'
+ indicating whether hidden dotfiles shoud be included in the output.
+ It is False by default.
+ """
+
+ def is_hidden(self,path):
+ """Check whether the given path should be hidden."""
+ return path and resourcename(path)[0] == "."
+
+ def _encode(self,path):
+ return path
+
+ def _decode(self,path):
+ return path
+
+ def listdir(self,path="",**kwds):
+ hidden = kwds.pop("hidden",True)
+ entries = self.wrapped_fs.listdir(path,**kwds)
+ if not hidden:
+ entries = [e for e in entries if not self.is_hidden(e)]
+ return entries
+
+ def walk(self, path="/", wildcard=None, dir_wildcard=None, search="breadth",hidden=False):
+ if search == "breadth":
+ dirs = [path]
+ while dirs:
+ current_path = dirs.pop()
+ paths = []
+ for filename in self.listdir(current_path,hidden=hidden):
+ path = pathjoin(current_path, filename)
+ if self.isdir(path):
+ if dir_wildcard is not None:
+ if fnmatch.fnmatch(path, dir_wilcard):
+ dirs.append(path)
+ else:
+ dirs.append(path)
+ else:
+ if wildcard is not None:
+ if fnmatch.fnmatch(path, wildcard):
+ paths.append(filename)
+ else:
+ paths.append(filename)
+ yield (current_path, paths)
+ elif search == "depth":
+ def recurse(recurse_path):
+ for path in self.listdir(recurse_path, wildcard=dir_wildcard, full=True, dirs_only=True,hidden=hidden):
+ for p in recurse(path):
+ yield p
+ yield (recurse_path, self.listdir(recurse_path, wildcard=wildcard, files_only=True,hidden=hidden))
+ for p in recurse(path):
+ yield p
+ else:
+ raise ValueError("Search should be 'breadth' or 'depth'")
+
+
+ def isdirempty(self, path):
+ path = normpath(path)
+ iter_dir = iter(self.listdir(path,hidden=True))
+ try:
+ iter_dir.next()
+ except StopIteration:
+ return True
+ return False
+