Commit aece1874 by willmcgugan

Refinements to the memoryfs class, and added a 'walk' generator to the base class

parent 36b626d8
...@@ -9,7 +9,7 @@ import fs ...@@ -9,7 +9,7 @@ import fs
class BrowseFrame(wx.Frame): class BrowseFrame(wx.Frame):
def __init__(self, fs): def __init__(self, fs):
wx.Frame.__init__(self, None) wx.Frame.__init__(self, None)
self.fs = fs self.fs = fs
...@@ -44,7 +44,7 @@ class BrowseFrame(wx.Frame): ...@@ -44,7 +44,7 @@ class BrowseFrame(wx.Frame):
if not self.fs.isdir(path): if not self.fs.isdir(path):
return return
if item_data['expanded']: if item_data['expanded']:
return return
paths = [(self.fs.isdir(p), p) for p in self.fs.listdir(path, absolute=True)] paths = [(self.fs.isdir(p), p) for p in self.fs.listdir(path, absolute=True)]
...@@ -55,16 +55,16 @@ class BrowseFrame(wx.Frame): ...@@ -55,16 +55,16 @@ class BrowseFrame(wx.Frame):
return return
paths.sort(key=lambda p:(not p[0], p[1].lower())) paths.sort(key=lambda p:(not p[0], p[1].lower()))
for is_dir, new_path in paths: for is_dir, new_path in paths:
name = fs.pathsplit(new_path)[-1] name = fs.pathsplit(new_path)[-1]
if not is_dir and name.endswith('.txt'): if not is_dir and name.endswith('.txt'):
txt = self.fs.open(new_path).read(50) txt = self.fs.open(new_path).readline()[:50].rstrip()
name += " - "+txt name += " - "+txt
new_item = self.tree.AppendItem(item_id, name, data=wx.TreeItemData({'path':new_path, 'expanded':False})) new_item = self.tree.AppendItem(item_id, name, data=wx.TreeItemData({'path':new_path, 'expanded':False}))
if is_dir: if is_dir:
......
...@@ -13,7 +13,8 @@ error_msgs = { ...@@ -13,7 +13,8 @@ error_msgs = {
"LISTDIR_FAILED" : "Unable to get directory listing: %(path)s", "LISTDIR_FAILED" : "Unable to get directory listing: %(path)s",
"NO_SYS_PATH" : "No mapping to OS filesytem: %(path)s,", "NO_SYS_PATH" : "No mapping to OS filesytem: %(path)s,",
"DIR_EXISTS" : "Directory exists (try allow_recreate=True): %(path)s", "DIR_EXISTS" : "Directory exists (try allow_recreate=True): %(path)s",
"OPEN_FAILED" : "Unable to open file: %(path)s" "OPEN_FAILED" : "Unable to open file: %(path)s",
"FILE_LOCKED" : "File is locked: %(path)s",
} }
error_codes = error_msgs.keys() error_codes = error_msgs.keys()
...@@ -29,9 +30,9 @@ class FSError(Exception): ...@@ -29,9 +30,9 @@ class FSError(Exception):
def __str__(self): def __str__(self):
msg = self.msg % dict((k, str(v)) for k,v in self.__dict__.iteritems()) msg = self.msg % dict((k, str(v)) for k, v in self.__dict__.iteritems())
return '%s %s' % (self.code, msg) return '%s. %s' % (self.code, msg)
class NullFile: class NullFile:
...@@ -224,17 +225,40 @@ class FS(object): ...@@ -224,17 +225,40 @@ class FS(object):
elif absolute: elif absolute:
paths = [self.abspath(pathjoin(path, p)) for p in paths] paths = [self.abspath(pathjoin(path, p)) for p in paths]
return paths return paths
def walk_files(self, path="/", wildcard=None, dir_wildcard=None):
dirs = [path]
files = []
while dirs:
path = dirs.pop()
for path in self.listdir(path, full=True):
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):
yield path
else:
yield path
class SubFS(FS): class SubFS(FS):
def __init__(self, parent, sub_dir): def __init__(self, parent, sub_dir):
self.parent = parent self.parent = parent
self.sub_dir = parent.abspath(sub_dir) self.sub_dir = parent.abspath(sub_dir)
#print "sub_dir", self.sub_dir
def __str__(self): def __str__(self):
return "<SubFS \"%s\" of %s>" % (self.sub_dir, self.parent) return "<SubFS \"%s\" of %s>" % (self.sub_dir, self.parent)
...@@ -242,7 +266,6 @@ class SubFS(FS): ...@@ -242,7 +266,6 @@ class SubFS(FS):
def _delegate(self, dirname): def _delegate(self, dirname):
delegate_path = pathjoin(self.sub_dir, resolvepath(makerelative(dirname))) delegate_path = pathjoin(self.sub_dir, resolvepath(makerelative(dirname)))
#print "delegate path", delegate_path
return delegate_path return delegate_path
def getsyspath(self, pathname): def getsyspath(self, pathname):
...@@ -274,7 +297,6 @@ class OSFS(FS): ...@@ -274,7 +297,6 @@ class OSFS(FS):
raise FSError("NO_DIR", expanded_path, msg="Root path is not a directory: %(path)s") raise FSError("NO_DIR", expanded_path, msg="Root path is not a directory: %(path)s")
self.root_path = normpath(os.path.abspath(expanded_path)) self.root_path = normpath(os.path.abspath(expanded_path))
#print "Root path", self.root_path
def __str__(self): def __str__(self):
return "<OSFS \"%s\">" % self.root_path return "<OSFS \"%s\">" % self.root_path
...@@ -320,7 +342,7 @@ class OSFS(FS): ...@@ -320,7 +342,7 @@ class OSFS(FS):
try: try:
paths = os.listdir(self.getsyspath(path)) paths = os.listdir(self.getsyspath(path))
except IOError, e: except (OSError, IOError), e:
raise FSError("LISTDIR_FAILED", path, details=e, msg="Unable to get directory listing: %(path)s - (%(details)s)") raise FSError("LISTDIR_FAILED", path, details=e, msg="Unable to get directory listing: %(path)s - (%(details)s)")
return self._listdir_helper(path, paths, wildcard, full, absolute, hidden, dirs_only, files_only) return self._listdir_helper(path, paths, wildcard, full, absolute, hidden, dirs_only, files_only)
...@@ -470,9 +492,11 @@ class MountFS(FS): ...@@ -470,9 +492,11 @@ class MountFS(FS):
if __name__ == "__main__": if __name__ == "__main__":
osfs = OSFS("~/") osfs = OSFS("~/projects")
print osfs print osfs
#print osfs
for filename in osfs.walk_files("/prettycharts", "*.pov"):
print filename
import browsewin import browsewin
browsewin.browse(osfs) browsewin.browse(osfs)
......
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