From f274b325fac8e47df6d45260111d6517abddde7e Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sun, 28 Jul 2019 17:51:37 +0100 Subject: [PATCH] Add missing methods to WrapFS (#321) * fix for makedirs race condition * changelog * add missing methods to wrapfs * added changelog * fix removetree --- CHANGELOG.md | 1 + fs/wrapfs.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2be6711..30c9fe72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Restored fs.path import - Fixed potential race condition in makedirs. Fixes [#310](https://github.com/PyFilesystem/pyfilesystem2/issues/310) +- Added missing methods to WrapFS. Fixed [#294](https://github.com/PyFilesystem/pyfilesystem2/issues/294) ### Changed diff --git a/fs/wrapfs.py b/fs/wrapfs.py index 5ff7d3d2..34e83fae 100644 --- a/fs/wrapfs.py +++ b/fs/wrapfs.py @@ -10,9 +10,9 @@ from . import errors from .base import FS -from .copy import copy_file +from .copy import copy_file, copy_dir from .info import Info -from .move import move_file +from .move import move_file, move_dir from .path import abspath, normpath from .error_tools import unwrap_errors @@ -180,6 +180,17 @@ def move(self, src_path, dst_path, overwrite=False): raise errors.DestinationExists(_dst_path) move_file(src_fs, _src_path, dst_fs, _dst_path) + def copydir(self, src_path, dst_path, create=False): + # type: (Text, Text, bool) -> None + src_fs, _src_path = self.delegate_path(src_path) + dst_fs, _dst_path = self.delegate_path(dst_path) + with unwrap_errors({_src_path: src_path, _dst_path: dst_path}): + if not create and not dst_fs.exists(_dst_path): + raise errors.ResourceNotFound(dst_path) + if not src_fs.getinfo(_src_path).is_dir: + raise errors.DirectoryExpected(src_path) + move_dir(src_fs, _src_path, dst_fs, _dst_path) + def openbin(self, path, mode="r", buffering=-1, **options): # type: (Text, Text, int, **Any) -> BinaryIO self.check() @@ -205,6 +216,16 @@ def removedir(self, path): with unwrap_errors(path): _fs.removedir(_path) + def removetree(self, dir_path): + # type: (Text) -> None + self.check() + _path = abspath(normpath(dir_path)) + if _path == "/": + raise errors.RemoveRootError() + _fs, _path = self.delegate_path(dir_path) + with unwrap_errors(dir_path): + _fs.removetree(_path) + def scandir( self, path, # type: Text @@ -247,6 +268,17 @@ def copy(self, src_path, dst_path, overwrite=False): raise errors.DestinationExists(_dst_path) copy_file(src_fs, _src_path, dst_fs, _dst_path) + def copydir(self, src_path, dst_path, create=False): + # type: (Text, Text, bool) -> None + src_fs, _src_path = self.delegate_path(src_path) + dst_fs, _dst_path = self.delegate_path(dst_path) + with unwrap_errors({_src_path: src_path, _dst_path: dst_path}): + if not create and not dst_fs.exists(_dst_path): + raise errors.ResourceNotFound(dst_path) + if not src_fs.getinfo(_src_path).is_dir: + raise errors.DirectoryExpected(src_path) + copy_dir(src_fs, _src_path, dst_fs, _dst_path) + def create(self, path, wipe=False): # type: (Text, bool) -> bool self.check() @@ -262,6 +294,13 @@ def desc(self, path): desc = _fs.desc(_path) return desc + def download(self, path, file, chunk_size=None, **options): + # type: (Text, BinaryIO, Optional[int], **Any) -> None + self.check() + _fs, _path = self.delegate_path(path) + with unwrap_errors(path): + _fs.download(_path, file, chunk_size=chunk_size, **options) + def exists(self, path): # type: (Text) -> bool self.check()