summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f>2009-05-05 06:07:06 +0000
committerrfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f>2009-05-05 06:07:06 +0000
commitbe7a1a6deec2de7b73b10c6c29a3ca2163cf7201 (patch)
tree84619fb53a5dfaf0c677a3eafc66a560c2e84041
parentfacbf53c6b075a7e959cc3bd32d7183bd576b3a2 (diff)
downloadpyfilesystem-be7a1a6deec2de7b73b10c6c29a3ca2163cf7201.tar.gz
Update OSFS touse new exceptions
git-svn-id: http://pyfilesystem.googlecode.com/svn/branches/rfk-ideas@130 67cdc799-7952-0410-af00-57a81ceafa0f
-rw-r--r--fs/osfs.py282
1 files changed, 108 insertions, 174 deletions
diff --git a/fs/osfs.py b/fs/osfs.py
index 9fc7342..613b50e 100644
--- a/fs/osfs.py
+++ b/fs/osfs.py
@@ -1,5 +1,7 @@
#!/usr/bin/env python
+import os
+
from base import *
from helpers import *
@@ -9,31 +11,32 @@ except ImportError:
xattr = None
class OSFS(FS):
+ """Expose the underlying operating-system filesystem as an FS object.
- """The most basic of filesystems. Simply shadows the underlaying filesytem
- of the Operating System.
+ This is the most basic of filesystems, which simply shadows the underlaying
+ filesytem of the OS. Most of its methods simply defer to the corresponding
+ methods in the os and os.path modules.
"""
- def __init__(self, root_path, thread_syncronize=True):
- FS.__init__(self, thread_syncronize=thread_syncronize)
+ def __init__(self, root_path, dir_mode=0700, thread_synchronize=True):
+ FS.__init__(self, thread_synchronize=thread_synchronize)
expanded_path = normpath(os.path.expanduser(os.path.expandvars(root_path)))
if not os.path.exists(expanded_path):
- raise ResourceNotFoundError("NO_DIR", expanded_path, msg="Root directory does not exist: %(path)s")
+ raise DirectoryNotFoundError(expanded_path, msg="Root directory does not exist: %(path)s")
if not os.path.isdir(expanded_path):
- raise ResourceNotFoundError("NO_DIR", expanded_path, msg="Root path is not a directory: %(path)s")
+ raise InvalidResourceError(expanded_path, msg="Root path is not a directory: %(path)s")
self.root_path = normpath(os.path.abspath(expanded_path))
+ self.dir_mode = dir_mode
def __str__(self):
return "<OSFS: %s>" % self.root_path
- __repr__ = __str__
-
def getsyspath(self, path, allow_none=False):
- sys_path = os.path.join(self.root_path, makerelative(self._resolve(path))).replace('/', os.sep)
+ sys_path = os.path.join(self.root_path, makerelative(normpath(path))).replace('/', os.sep)
return sys_path
def open(self, path, mode="r", **kwargs):
@@ -41,8 +44,8 @@ class OSFS(FS):
f = open(self.getsyspath(path), mode, kwargs.get("buffering", -1))
except IOError, e:
if e.errno == 2:
- raise ResourceNotFoundError("NO_FILE", path)
- raise OperationFailedError("OPEN_FAILED", path, details=e, msg=str(e))
+ raise FileNotFoundError(path)
+ raise OperationFailedError("open file", details=e, msg=str(e))
return f
@@ -58,53 +61,45 @@ class OSFS(FS):
path = self.getsyspath(path)
return os.path.isfile(path)
- def ishidden(self, path):
- return path.startswith('.')
-
- def listdir(self, path="./", wildcard=None, full=False, absolute=False, hidden=True, dirs_only=False, files_only=False):
+ def listdir(self, path="./", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False):
try:
paths = os.listdir(self.getsyspath(path))
except (OSError, IOError), e:
- raise OperationFailedError("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)
+ if e.errno == 2:
+ raise ResourceNotFoundError(path)
+ if e.errno == 20:
+ raise ResourceInvalidError(path,msg="Can't list directory contents of a file: %(path)s")
+ raise OperationFailedError("list directory", path=path, details=e, msg="Unable to get directory listing: %(path)s - (%(details)s)")
+ return self._listdir_helper(path, paths, wildcard, full, absolute, dirs_only, files_only)
- def makedir(self, path, mode=0777, recursive=False, allow_recreate=False):
+ def makedir(self, path, recursive=False, allow_recreate=False):
sys_path = self.getsyspath(path)
-
try:
if recursive:
- os.makedirs(sys_path, mode)
+ os.makedirs(sys_path, self.dir_mode)
else:
- if not allow_recreate and self.exists(path):
- raise OperationFailedError("MAKEDIR_FAILED", dirname, msg="Can not create a directory that already exists (try allow_recreate=True): %(path)s")
- try:
- os.mkdir(sys_path, mode)
- except OSError, e:
- if allow_recreate:
- if e.errno != 17:
- raise OperationFailedError("MAKEDIR_FAILED", path)
- else:
- raise OperationFailedError("MAKEDIR_FAILED", path)
- except WindowsError, e:
- if allow_recreate:
- if e.errno != 183:
- raise OperationFailedError("MAKEDIR_FAILED", path)
- else:
- raise OperationFailedError("MAKEDIR_FAILED", path)
-
+ os.mkdir(sys_path, self.dir_mode)
except OSError, e:
- if e.errno == 17:
- return
+ if e.errno == 17 or e.errno == 183:
+ if self.isfile(path):
+ raise ResourceInvalidError(path,msg="Cannot create directory, there's already a file of that name: %(path)s")
+ if not allow_recreate:
+ raise DestinationExistsError(path,msg="Can not create a directory that already exists (try allow_recreate=True): %(path)s")
+ elif e.errno == 2:
+ raise ParentDirectoryMissingError(path)
else:
- raise OperationFailedError("MAKEDIR_FAILED", path, details=e)
-
+ raise OperationFailedError("make directory",path=path,details=e)
+
def remove(self, path):
sys_path = self.getsyspath(path)
try:
os.remove(sys_path)
except OSError, e:
- raise OperationFailedError("REMOVE_FAILED", path, details=e)
+ if not self.exists(path):
+ raise ResourceNotFoundError(path)
+ if self.isdir(path):
+ raise ResourceInvalidError(path,msg="Cannot use remove() on a directory: %(path)s")
+ raise OperationFailedError("remove file", path=path, details=e)
def removedir(self, path, recursive=False,force=False):
sys_path = self.getsyspath(path)
@@ -119,7 +114,11 @@ class OSFS(FS):
try:
os.rmdir(sys_path)
except OSError, e:
- raise OperationFailedError("REMOVEDIR_FAILED", path, details=e)
+ if self.isfile(path):
+ raise ResourceInvalidError(path,msg="Can't use removedir() on a file: %(path)s")
+ if self.listdir(path):
+ raise DirectoryNotEmptyError(path)
+ raise OperationFailedError("remove directory", path=path, details=e)
# Using os.removedirs() for this can result in dirs being
# removed outside the root of this FS, so we recurse manually.
if recursive:
@@ -133,36 +132,28 @@ class OSFS(FS):
raise ValueError("Destination path must the same directory (user the move method for moving to a different directory)")
path_src = self.getsyspath(src)
path_dst = self.getsyspath(dst)
-
try:
os.rename(path_src, path_dst)
except OSError, e:
- raise OperationFailedError("RENAME_FAILED", src)
+ raise OperationFailedError("rename resource", path=src, details=e)
def getinfo(self, path):
sys_path = self.getsyspath(path)
-
try:
stats = os.stat(sys_path)
except OSError, e:
- raise FSError("UNKNOWN_ERROR", path, details=e)
-
+ raise ResourceError(path, details=e)
info = dict((k, getattr(stats, k)) for k in dir(stats) if not k.startswith('__') )
-
info['size'] = info['st_size']
-
ct = info.get('st_ctime', None)
if ct is not None:
info['created_time'] = datetime.datetime.fromtimestamp(ct)
-
at = info.get('st_atime', None)
if at is not None:
info['accessed_time'] = datetime.datetime.fromtimestamp(at)
-
mt = info.get('st_mtime', None)
if mt is not None:
info['modified_time'] = datetime.datetime.fromtimestamp(at)
-
return info
@@ -171,127 +162,70 @@ class OSFS(FS):
try:
stats = os.stat(sys_path)
except OSError, e:
- raise FSError("UNKNOWN_ERROR", path, details=e)
-
+ raise ResourceError(path, details=e)
return stats.st_size
- def setxattr(self, path, key, value):
- self._lock.acquire()
- try:
- if xattr is None:
- return FS.setxattr(self, path, key, value)
- try:
- xattr.xattr(self.getsyspath(path))[key]=value
- except IOError, e:
- if e.errno == 95:
- return FS.setxattr(self, path, key, value)
- else:
- raise OperationFailedError('XATTR_FAILED', path, details=e)
- finally:
- self._lock.release()
-
- def getxattr(self, path, key, default=None):
- self._lock.acquire()
- try:
- if xattr is None:
- return FS.getxattr(self, path, key, default)
- try:
- return xattr.xattr(self.getsyspath(path)).get(key)
- except IOError, e:
- if e.errno == 95:
- return FS.getxattr(self, path, key, default)
- else:
- raise OperationFailedError('XATTR_FAILED', path, details=e)
- finally:
- self._lock.release()
-
- def removexattr(self, path, key):
- self._lock.acquire()
- try:
- if xattr is None:
- return FS.removexattr(self, path, key)
- try:
- del xattr.xattr(self.getsyspath(path))[key]
- except KeyError:
- pass
- except IOError, e:
- if e.errono == 95:
- return FS.removexattr(self, path, key)
- else:
- raise OperationFailedError('XATTR_FAILED', path, details=e)
- finally:
- self._lock.release()
-
- def listxattrs(self, path):
- self._lock.acquire()
- try:
- if xattr is None:
- return FS.listxattrs(self, path)
- try:
- return xattr.xattr(self.getsyspath(path)).keys()
- except IOError, e:
- if errono == 95:
- return FS.listxattrs(self, path)
- else:
- raise OperationFailedError('XATTR_FAILED', path, details=e)
- finally:
- self._lock.release()
-
-
-
-if __name__ == "__main__":
-
-
- osfs = OSFS('testfs')
-
-
-
-
- #a = xattr.xattr('/home/will/projects/pyfilesystem/fs/testfs/test.txt')
- #a['user.comment'] = 'world'
-
- #print xattr.xattr('/home/will/projects/pyfilesystem/fs/testfs/test.txt').keys()
-
- print osfs.listxattrs('test.txt')
- osfs.removexattr('test.txt', 'user.foo')
- #print osfs.listxattrs('test.txt')
- osfs.setxattr('test.txt', 'user.foo', 'bar')
- print osfs.getxattr('test.txt', 'user.foo')
- print osfs.listxattrs('test.txt')
- print osfs.getxattrs('test.txt')
+# def setxattr(self, path, key, value):
+# self._lock.acquire()
+# try:
+# if xattr is None:
+# return FS.setxattr(self, path, key, value)
+# try:
+# xattr.xattr(self.getsyspath(path))[key]=value
+# except IOError, e:
+# if e.errno == 95:
+# return FS.setxattr(self, path, key, value)
+# else:
+# raise OperationFailedError('XATTR_FAILED', path, details=e)
+# finally:
+# self._lock.release()
+#
+# def getxattr(self, path, key, default=None):
+# self._lock.acquire()
+# try:
+# if xattr is None:
+# return FS.getxattr(self, path, key, default)
+# try:
+# return xattr.xattr(self.getsyspath(path)).get(key)
+# except IOError, e:
+# if e.errno == 95:
+# return FS.getxattr(self, path, key, default)
+# else:
+# raise OperationFailedError('XATTR_FAILED', path, details=e)
+# finally:
+# self._lock.release()
+#
+# def removexattr(self, path, key):
+# self._lock.acquire()
+# try:
+# if xattr is None:
+# return FS.removexattr(self, path, key)
+# try:
+# del xattr.xattr(self.getsyspath(path))[key]
+# except KeyError:
+# pass
+# except IOError, e:
+# if e.errono == 95:
+# return FS.removexattr(self, path, key)
+# else:
+# raise OperationFailedError('XATTR_FAILED', path, details=e)
+# finally:
+# self._lock.release()
+#
+# def listxattrs(self, path):
+# self._lock.acquire()
+# try:
+# if xattr is None:
+# return FS.listxattrs(self, path)
+# try:
+# return xattr.xattr(self.getsyspath(path)).keys()
+# except IOError, e:
+# if errono == 95:
+# return FS.listxattrs(self, path)
+# else:
+# raise OperationFailedError('XATTR_FAILED', path, details=e)
+# finally:
+# self._lock.release()
+#
- #
- #osfs = OSFS("~/projects")
- #
- #
- ##for p in osfs.walk("tagging-trunk", search='depth'):
- ## print p
- #
- #import browsewin
- #browsewin.browse(osfs)
- #
- #print_fs(osfs)
- #
- ##print osfs.listdir("/projects/fs")
- #
- ##sub_fs = osfs.open_dir("projects/")
- #
- ##print sub_fs
- #
- ##sub_fs.open('test.txt')
- #
- ##print sub_fs.listdir(dirs_only=True)
- ##print sub_fs.listdir()
- ##print_fs(sub_fs, max_levels=2)
- #
- ##for f in osfs.listdir():
- ## print f
- #
- ##print osfs.listdir('projects', dirs_only=True, wildcard="d*")
- #
- ##print_fs(osfs, 'projects/')
- #
- #print pathjoin('/', 'a')
- #
- #print pathjoin('a/b/c', '../../e/f')