Commit 42e2c078 by willmcgugan

HideFS

parent 98aa6c9e
...@@ -174,7 +174,7 @@ class FS(object): ...@@ -174,7 +174,7 @@ class FS(object):
return self.__str__() return self.__str__()
def __del__(self): def __del__(self):
if not getattr(self, 'closed', False): if not getattr(self, 'closed', True):
try: try:
self.close() self.close()
except: except:
......
...@@ -8,6 +8,7 @@ An FS wrapper class for hiding dot-files in directory listings. ...@@ -8,6 +8,7 @@ An FS wrapper class for hiding dot-files in directory listings.
from fs.wrapfs import WrapFS from fs.wrapfs import WrapFS
from fs.path import * from fs.path import *
from fnmatch import fnmatch
class HideDotFilesFS(WrapFS): class HideDotFilesFS(WrapFS):
...@@ -59,7 +60,7 @@ class HideDotFilesFS(WrapFS): ...@@ -59,7 +60,7 @@ class HideDotFilesFS(WrapFS):
path = pathjoin(current_path, filename) path = pathjoin(current_path, filename)
if self.isdir(path): if self.isdir(path):
if dir_wildcard is not None: if dir_wildcard is not None:
if fnmatch(path, dir_wilcard): if fnmatch(path, dir_wildcard):
dirs.append(path) dirs.append(path)
else: else:
dirs.append(path) dirs.append(path)
......
"""
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
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