Commit 4b2f57cb by willmcgugan

Work in progress with the memoryfs

parent 1cde4462
......@@ -10,7 +10,7 @@ import datetime
error_msgs = {
"UNKNOWN_ERROR" : "No information on error: %(path)s",
"UNSUPPORTED" : "This filesystem does not support this action.",
"UNSUPPORTED" : "Action is unsupported by this filesystem.",
"INVALID_PATH" : "Path is invalid: %(path)s",
"NO_DIR" : "Directory does not exist: %(path)s",
"NO_FILE" : "No such file: %(path)s",
......@@ -196,12 +196,22 @@ class FS(object):
def getsyspath(self, path):
raise FSError("NO_SYS_PATH", path)
def safeopen(self, *args, **kwargs):
try:
f = self.open(*args, **kwargs)
except FSError, e:
if e.code == "NO_FILE":
return NullFile()
raise
def open(self, path, mode="r", buffering=-1, **kwargs):
pass
def open_dir(self, path):
def opendir(self, path):
if not self.exists(path):
raise FSError("NO_DIR", path)
......@@ -487,7 +497,7 @@ if __name__ == "__main__":
osfs = OSFS("~/projects")
print osfs
for filename in osfs.walk_files("/prettycharts", "*.pov"):
for filename in osfs.walk_files("/", "*.pov"):
print filename
print osfs.getinfo(filename)
......
......@@ -192,7 +192,7 @@ class MemoryFS(FS):
def exists(self, path):
return self._getdir(path) is not None
return self._get_dir_entry(path) is not None
def mkdir(self, dirname, mode=0777, recursive=False, allow_recreate=False):
......@@ -339,6 +339,8 @@ class MemoryFS(FS):
def listdir(self, path="/", wildcard=None, full=False, absolute=False, hidden=False, dirs_only=False, files_only=False):
dir_entry = self._get_dir_entry(path)
if dir_entry is None:
raise FSError("NO_DIR", path)
paths = dir_entry.contents.keys()
return self._listdir_helper(path, paths, wildcard, full, absolute, hidden, dirs_only, files_only)
......
......@@ -15,7 +15,7 @@ class MultiFS(FS):
return "<MultiFS: %s>" % ", ".join(str(fs) for fs in self.fs_sequence)
def add_fs(self, name, fs):
def addfs(self, name, fs):
if name in self.fs_lookup:
raise ValueError("Name already exists.")
......@@ -24,7 +24,7 @@ class MultiFS(FS):
self.fs_lookup[name] = fs
def remove_fs(self, name):
def removefs(self, name):
fs = self.fs_lookup[name]
self.fs_sequence.remove(fs)
......@@ -42,7 +42,7 @@ class MultiFS(FS):
def _delegate_search(self, path):
for fs in self:
if self.exists(path):
if fs.exists(path):
return fs
return None
......@@ -71,21 +71,21 @@ class MultiFS(FS):
fs = self._delegate_search(path)
if fs is not None:
return fs.isdir()
return fs.isdir(path)
return False
def isfile(self, path):
fs = self._delegate_search(path)
if fs is not None:
return fs.isfile()
return fs.isfile(path)
return False
def ishidden(self, path):
fs = self._delegate_search(path)
if fs is not None:
return fs.isfile()
return fs.isfile(path)
return False
def listdir(self, path="./", *args, **kwargs):
......@@ -102,7 +102,7 @@ class MultiFS(FS):
def remove(self, path):
for fs in self:
if fs.exist(path):
if fs.exists(path):
fs.remove(path)
return
raise FSError("NO_FILE", path)
......@@ -119,7 +119,31 @@ class MultiFS(FS):
def getinfo(self, path):
for fs in self:
if fs.exist(path):
if fs.exists(path):
return fs.getinfo(path)
raise FSError("NO_FILE", path)
if __name__ == "__main__":
import fs
osfs = fs.OSFS('~/')
import memoryfs
mem_fs = memoryfs.MemoryFS()
mem_fs.mkdir('projects/test2', recursive=True)
mem_fs.mkdir('projects/A', recursive=True)
mem_fs.mkdir('projects/A/B', recursive=True)
mem_fs.open("projects/readme.txt", 'w').write("Hello, World!")
mem_fs.open("projects/readme.txt", 'wa').write("\nSecond Line")
multifs = MultiFS()
multifs.addfs("osfs", osfs)
multifs.addfs("mem_fs", mem_fs)
import browsewin
browsewin.browse(multifs)
\ 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