diff options
author | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2013-03-31 21:30:34 +0000 |
---|---|---|
committer | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2013-03-31 21:30:34 +0000 |
commit | 3ea4efe11b1a7f31c355103608950379d52fdec5 (patch) | |
tree | 5547f10f29e28894ae70ff5b97e858456f94103a /fs/wrapfs | |
parent | c6391b6f5dcf2a30d4657b5808f9f668fbc02de3 (diff) | |
download | pyfilesystem-git-3ea4efe11b1a7f31c355103608950379d52fdec5.tar.gz |
Change of api (fs.open, fs.setcontent, fs.getcontents) to support io module in Py2.6+ and Py3
Diffstat (limited to 'fs/wrapfs')
-rw-r--r-- | fs/wrapfs/__init__.py | 8 | ||||
-rw-r--r-- | fs/wrapfs/limitsizefs.py | 22 | ||||
-rw-r--r-- | fs/wrapfs/readonlyfs.py | 28 | ||||
-rw-r--r-- | fs/wrapfs/subfs.py | 18 |
4 files changed, 45 insertions, 31 deletions
diff --git a/fs/wrapfs/__init__.py b/fs/wrapfs/__init__.py index e69d412..b8b9bea 100644 --- a/fs/wrapfs/__init__.py +++ b/fs/wrapfs/__init__.py @@ -150,21 +150,21 @@ class WrapFS(FS): return self.wrapped_fs.hassyspath(self._encode(path)) @rewrite_errors - def open(self, path, mode="r", **kwargs): + def open(self, path, mode='r', **kwargs): (mode, wmode) = self._adjust_mode(mode) f = self.wrapped_fs.open(self._encode(path), wmode, **kwargs) return self._file_wrap(f, mode) @rewrite_errors - def setcontents(self, path, data, chunk_size=64*1024): + def setcontents(self, path, data, encoding=None, errors=None, chunk_size=64*1024): # We can't pass setcontents() through to the wrapped FS if the # wrapper has defined a _file_wrap method, as it would bypass # the file contents wrapping. #if self._file_wrap.im_func is WrapFS._file_wrap.im_func: if getattr(self.__class__, '_file_wrap', None) is getattr(WrapFS, '_file_wrap', None): - return self.wrapped_fs.setcontents(self._encode(path), data, chunk_size=chunk_size) + return self.wrapped_fs.setcontents(self._encode(path), data, encoding=encoding, errors=errors, chunk_size=chunk_size) else: - return super(WrapFS,self).setcontents(path, data, chunk_size=chunk_size) + return super(WrapFS, self).setcontents(path, data, encoding=encoding, errors=errors, chunk_size=chunk_size) @rewrite_errors def createfile(self, path): diff --git a/fs/wrapfs/limitsizefs.py b/fs/wrapfs/limitsizefs.py index 45a5fcf..b3565dc 100644 --- a/fs/wrapfs/limitsizefs.py +++ b/fs/wrapfs/limitsizefs.py @@ -58,14 +58,20 @@ class LimitSizeFS(WrapFS): raise NoSysPathError(path) return None - def open(self, path, mode="r"): + def open(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs): path = relpath(normpath(path)) with self._size_lock: try: size = self.getsize(path) except ResourceNotFoundError: size = 0 - f = super(LimitSizeFS,self).open(path,mode) + f = super(LimitSizeFS,self).open(path, + mode=mode, + buffering=buffering, + errors=errors, + newline=newline, + line_buffering=line_buffering, + **kwargs) if "w" not in mode: self._set_file_size(path,None,1) else: @@ -92,12 +98,12 @@ class LimitSizeFS(WrapFS): else: self._file_sizes[path] = (size,count) - def setcontents(self, path, data, chunk_size=64*1024): + def setcontents(self, path, data, chunk_size=64*1024): f = None - try: + try: f = self.open(path, 'wb') if hasattr(data, 'read'): - chunk = data.read(chunk_size) + chunk = data.read(chunk_size) while chunk: f.write(chunk) chunk = data.read(chunk_size) @@ -106,7 +112,7 @@ class LimitSizeFS(WrapFS): finally: if f is not None: f.close() - + def _file_closed(self, path): self._set_file_size(path,None,-1) @@ -135,7 +141,7 @@ class LimitSizeFS(WrapFS): return cur_size # We force use of several base FS methods, - # since they will fall back to writing out each file + # since they will fall back to writing out each file # and thus will route through our size checking logic. def copy(self, src, dst, **kwds): FS.copy(self,src,dst,**kwds) @@ -233,7 +239,7 @@ class LimitSizeFile(FileWrapper): self.fs = fs self.path = path self._lock = fs._lock - + @synchronize def _write(self, data, flushing=False): pos = self.wrapped_file.tell() diff --git a/fs/wrapfs/readonlyfs.py b/fs/wrapfs/readonlyfs.py index 5714745..426e143 100644 --- a/fs/wrapfs/readonlyfs.py +++ b/fs/wrapfs/readonlyfs.py @@ -10,44 +10,52 @@ from fs.base import NoDefaultMeta from fs.wrapfs import WrapFS from fs.errors import UnsupportedError, NoSysPathError + class ReadOnlyFS(WrapFS): """ Makes a FS object read only. Any operation that could potentially modify the underlying file system will throw an UnsupportedError - + Note that this isn't a secure sandbox, untrusted code could work around the read-only restrictions by getting the base class. Its main purpose is to provide a degree of safety if you want to protect an FS object from accidental modification. - + """ - + def getmeta(self, meta_name, default=NoDefaultMeta): if meta_name == 'read_only': return True return self.wrapped_fs.getmeta(meta_name, default) - + def hasmeta(self, meta_name): if meta_name == 'read_only': return True return self.wrapped_fs.hasmeta(meta_name) - + def getsyspath(self, path, allow_none=False): """ Doesn't technically modify the filesystem but could be used to work around read-only restrictions. """ if allow_none: return None raise NoSysPathError(path) - - def open(self, path, mode='r', **kwargs): + + def open(self, path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, line_buffering=False, **kwargs): """ Only permit read access """ if 'w' in mode or 'a' in mode or '+' in mode: raise UnsupportedError('write') - return super(ReadOnlyFS, self).open(path, mode, **kwargs) - + return super(ReadOnlyFS, self).open(path, + mode=mode, + buffering=buffering, + encoding=encoding, + errors=errors, + newline=newline, + line_buffering=line_buffering, + **kwargs) + def _no_can_do(self, *args, **kwargs): """ Replacement method for methods that can modify the file system """ raise UnsupportedError('write') - + move = _no_can_do movedir = _no_can_do copy = _no_can_do diff --git a/fs/wrapfs/subfs.py b/fs/wrapfs/subfs.py index c83b015..81f0e26 100644 --- a/fs/wrapfs/subfs.py +++ b/fs/wrapfs/subfs.py @@ -21,7 +21,7 @@ class SubFS(WrapFS): def __init__(self, wrapped_fs, sub_dir): self.sub_dir = abspath(normpath(sub_dir)) - super(SubFS,self).__init__(wrapped_fs) + super(SubFS, self).__init__(wrapped_fs) def _encode(self, path): return pathjoin(self.sub_dir, relpath(normpath(path))) @@ -34,17 +34,17 @@ class SubFS(WrapFS): return '<SubFS: %s/%s>' % (self.wrapped_fs, self.sub_dir.lstrip('/')) def __unicode__(self): - return u'<SubFS: %s/%s>' % (self.wrapped_fs, self.sub_dir.lstrip('/')) + return u'<SubFS: %s/%s>' % (self.wrapped_fs, self.sub_dir.lstrip('/')) def __repr__(self): - return "SubFS(%r, %r)" % (self.wrapped_fs, self.sub_dir) + return "SubFS(%r, %r)" % (self.wrapped_fs, self.sub_dir) - def desc(self, path): + def desc(self, path): if path in ('', '/'): return self.wrapped_fs.desc(self.sub_dir) return '%s!%s' % (self.wrapped_fs.desc(self.sub_dir), path) - - def setcontents(self, path, data, chunk_size=64*1024): + + def setcontents(self, path, data, encoding=None, errors=None, chunk_size=64*1024): path = self._encode(path) return self.wrapped_fs.setcontents(path, data, chunk_size=chunk_size) @@ -62,14 +62,14 @@ class SubFS(WrapFS): path = normpath(path) if path in ('', '/'): raise RemoveRootError(path) - super(SubFS,self).removedir(path,force=force) + super(SubFS, self).removedir(path, force=force) if recursive: try: if dirname(path) not in ('', '/'): - self.removedir(dirname(path),recursive=True) + self.removedir(dirname(path), recursive=True) except DirectoryNotEmptyError: pass - + # if path in ("","/"): # if not force: # for path2 in self.listdir(path): |