diff options
author | willmcgugan <willmcgugan@67cdc799-7952-0410-af00-57a81ceafa0f> | 2012-05-29 12:22:47 +0000 |
---|---|---|
committer | willmcgugan <willmcgugan@67cdc799-7952-0410-af00-57a81ceafa0f> | 2012-05-29 12:22:47 +0000 |
commit | fe4a59e2afcc2703d305489ead0bacc49eee1f73 (patch) | |
tree | 255213e0ecbd2ea26bc3de567f42678df576b08c /fs | |
parent | 9b60983c4b2f1369bdfdb60f373ff9076c979409 (diff) | |
download | pyfilesystem-fe4a59e2afcc2703d305489ead0bacc49eee1f73.tar.gz |
HideFS
git-svn-id: http://pyfilesystem.googlecode.com/svn/trunk@788 67cdc799-7952-0410-af00-57a81ceafa0f
Diffstat (limited to 'fs')
-rw-r--r-- | fs/base.py | 2 | ||||
-rw-r--r-- | fs/wrapfs/hidedotfilesfs.py | 3 | ||||
-rw-r--r-- | fs/wrapfs/hidefs.py | 45 |
3 files changed, 48 insertions, 2 deletions
@@ -174,7 +174,7 @@ class FS(object): return self.__str__() def __del__(self): - if not getattr(self, 'closed', False): + if not getattr(self, 'closed', True): try: self.close() except: diff --git a/fs/wrapfs/hidedotfilesfs.py b/fs/wrapfs/hidedotfilesfs.py index 46b2b46..0fa0731 100644 --- a/fs/wrapfs/hidedotfilesfs.py +++ b/fs/wrapfs/hidedotfilesfs.py @@ -8,6 +8,7 @@ An FS wrapper class for hiding dot-files in directory listings. from fs.wrapfs import WrapFS from fs.path import * +from fnmatch import fnmatch class HideDotFilesFS(WrapFS): @@ -59,7 +60,7 @@ class HideDotFilesFS(WrapFS): path = pathjoin(current_path, filename) if self.isdir(path): if dir_wildcard is not None: - if fnmatch(path, dir_wilcard): + if fnmatch(path, dir_wildcard): dirs.append(path) else: dirs.append(path) diff --git a/fs/wrapfs/hidefs.py b/fs/wrapfs/hidefs.py new file mode 100644 index 0000000..486c9a0 --- /dev/null +++ b/fs/wrapfs/hidefs.py @@ -0,0 +1,45 @@ +""" +fs.wrapfs.hidefs +================ + +Removes resources from a directory listing if they match a given set of wildcards + +""" + +from fs.wrapfs import WrapFS +from fs.path import basename +import re +import fnmatch + +class HideFS(WrapFS): + """FS wrapper that hides resources if they match a wildcard(s). + + For example, to hide all pyc file and subversion directories from a filesystem:: + + HideFS(my_fs, "*.pyc", ".svn") + + """ + + def __init__(self, wrapped_fs, *hide_wildcards): + self._hide_wildcards = [re.compile(fnmatch.translate(wildcard)) for wildcard in hide_wildcards] + super(HideFS, self).__init__(wrapped_fs) + + def _should_hide(self, name): + name = basename(name) + return any(wildcard.match(name) for wildcard in self._hide_wildcards) + + def _encode(self, path): + return path + + def _decode(self, path): + return path + + def listdir(self, path="", *args, **kwargs): + entries = super(HideFS, self).listdir(path, *args, **kwargs) + entries = [entry for entry in entries if not self._should_hide(entry)] + return entries + +if __name__ == "__main__": + from fs.osfs import OSFS + hfs = HideFS(OSFS('~/projects/pyfilesystem'), "*.pyc", ".svn") + hfs.tree()
\ No newline at end of file |