diff options
author | rfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f> | 2009-06-15 13:39:13 +0000 |
---|---|---|
committer | rfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f> | 2009-06-15 13:39:13 +0000 |
commit | f68f98631e5ed183e200da7e5ab42e2886e387b0 (patch) | |
tree | f38ca1b4afcfa6e3aa3229da35858522296feadd /fs/zipfs.py | |
parent | 29a1ca1c9f8dc8ef536cfb6d0ca25750267de093 (diff) | |
download | pyfilesystem-git-f68f98631e5ed183e200da7e5ab42e2886e387b0.tar.gz |
merge branch "rfk-ideas" into trunk
Diffstat (limited to 'fs/zipfs.py')
-rw-r--r-- | fs/zipfs.py | 73 |
1 files changed, 15 insertions, 58 deletions
diff --git a/fs/zipfs.py b/fs/zipfs.py index 90f84d4..2d1bdec 100644 --- a/fs/zipfs.py +++ b/fs/zipfs.py @@ -1,7 +1,6 @@ #!/usr/bin/env python -from base import * -from helpers import * +from fs.base import * from zipfile import ZipFile, ZIP_DEFLATED, ZIP_STORED from memoryfs import MemoryFS @@ -50,17 +49,17 @@ class ZipFS(FS): """A FileSystem that represents a zip file.""" - def __init__(self, zip_file, mode="r", compression="deflated", allowZip64=False, thread_syncronize=True): + def __init__(self, zip_file, mode="r", compression="deflated", allowZip64=False, thread_synchronize=True): """Create a FS that maps on to a zip file. zip_file -- A (system) path, or a file-like object mode -- Mode to open zip file: 'r' for reading, 'w' for writing or 'a' for appending compression -- Can be 'deflated' (default) to compress data or 'stored' to just store date allowZip64 -- Set to True to use zip files greater than 2 MB, default is False - thread_syncronize -- Set to True (default) to enable thread-safety + thread_synchronize -- Set to True (default) to enable thread-safety """ - FS.__init__(self, thread_syncronize=thread_syncronize) + FS.__init__(self, thread_synchronize=thread_synchronize) if compression == "deflated": compression_type = ZIP_DEFLATED elif compression == "stored": @@ -75,7 +74,7 @@ class ZipFS(FS): try: self.zf = ZipFile(zip_file, mode, compression_type, allowZip64) except IOError: - raise ResourceNotFoundError("NO_FILE", str(zip_file), msg="Zip file does not exist: %(path)s") + raise ResourceNotFoundError(str(zip_file), msg="Zip file does not exist: %(path)s") self.zip_path = str(zip_file) self.temp_fs = None @@ -129,11 +128,11 @@ class ZipFS(FS): if 'r' in mode: if self.zip_mode not in 'ra': - raise OperationFailedError("OPEN_FAILED", path, msg="Zip file must be opened for reading ('r') or appending ('a')") + raise OperationFailedError("open file", path=path, msg="Zip file must be opened for reading ('r') or appending ('a')") try: contents = self.zf.read(path) except KeyError: - raise ResourceNotFoundError("NO_FILE", path) + raise ResourceNotFoundError(path) return StringIO(contents) if 'w' in mode: @@ -154,14 +153,14 @@ class ZipFS(FS): self._lock.acquire() try: if not self.exists(path): - raise ResourceNotFoundError("NO_FILE", path) + raise ResourceNotFoundError(path) path = normpath(path) try: contents = self.zf.read(path) except KeyError: - raise ResourceNotFoundError("NO_FILE", path) + raise ResourceNotFoundError(path) except RuntimeError: - raise OperationFailedError("READ_FAILED", path, "Zip file must be oppened with 'r' or 'a' to read") + raise OperationFailedError("read file", path=path, msg="Zip file must be oppened with 'r' or 'a' to read") return contents finally: self._lock.release() @@ -194,23 +193,23 @@ class ZipFS(FS): try: dirname = normpath(dirname) if self.zip_mode not in "wa": - raise OperationFailedError("MAKEDIR_FAILED", dirname, "Zip file must be opened for writing ('w') or appending ('a')") + raise OperationFailedError("create directory", path=dirname, msg="Zip file must be opened for writing ('w') or appending ('a')") if not dirname.endswith('/'): dirname += '/' self._add_resource(dirname) finally: self._lock.release() - 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): - return self._path_fs.listdir(path, wildcard, full, absolute, hidden, dirs_only, files_only) + return self._path_fs.listdir(path, wildcard, full, absolute, dirs_only, files_only) def getinfo(self, path): self._lock.acquire() try: if not self.exists(path): - return ResourceNotFoundError("NO_RESOURCE", path) + return ResourceNotFoundError(path) path = normpath(path).lstrip('/') try: zi = self.zf.getinfo(path) @@ -225,46 +224,4 @@ class ZipFS(FS): finally: self._lock.release() -if __name__ == "__main__": - def test(): - zfs = ZipFS("t.zip", "w") - zfs.createfile("t.txt", "Hello, World!") - zfs.close() - rfs = ZipFS("t.zip", 'r') - print rfs.getcontents("t.txt") - print rfs.getcontents("w.txt") - - def test2(): - zfs = ZipFS("t2.zip", "r") - print zfs.listdir("/tagging-trunk") - print zfs.listdir("/") - import browsewin - browsewin.browse(zfs) - zfs.close() - #zfs.open("t.txt") - #print zfs.listdir("/") - - test2() - - zfs = ZipFS("t3.zip", "w") - zfs.createfile("t.txt", "Hello, World!") - zfs.createfile("foo/bar/baz/t.txt", "Hello, World!") - - print zfs.getcontents('t.txt') - #print zfs.isdir("t.txt") - #print zfs.isfile("t.txt") - #print zfs.isfile("foo/bar") - zfs.close() - zfs = ZipFS("t3.zip", "r") - print "--" - print zfs.listdir("foo") - print zfs.isdir("foo/bar") - print zfs.listdir("foo/bar") - print zfs.listdir("foo/bar/baz") - print_fs(zfs) - - - #zfs = ZipFS("t3.zip", "r") - #print zfs.zf.getinfo("asd.txt") - - #zfs.close() + |