Skip to content

Commit

Permalink
Merge 6dbda8b into 84c0f7d
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Dec 28, 2017
2 parents 84c0f7d + 6dbda8b commit 5cbac6c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
28 changes: 27 additions & 1 deletion fs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FS(object):

# This is the "standard" meta namespace.
_meta = {}

# most FS will use default walking algorithms
walker_class=Walker

Expand Down Expand Up @@ -404,6 +404,32 @@ def exists(self, path):
else:
return True

def extract(self, path, file, **options):
"""Copies a file from the filesystem to an binary file-like
object.
This may be more efficient that opening and copying files
manually if the filesystem supplies an optimized method.
Arguments:
path (str): Path to a resource
file (file-link): A file-like object open for writing in
binary mode.
options: Implementation specific options required to open
the source file.
Note that the file object ``file`` will *not* be closed by this
method. Take care to close it after this method completes
(ideally with a context manager).
Example:
>>> with open('starwars.mov', 'wb') as write_file:
... my_fs.extract('starwars.mov', write_file)
"""
with self.openbin(path, **options) as read_file:
tools.copy_file_data(read_file, file)

def filterdir(self,
path,
files=None,
Expand Down
10 changes: 10 additions & 0 deletions fs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,16 @@ def test_exists(self):
self.assertTrue(self.fs.exists('/'))
self.assertTrue(self.fs.exists(''))

def test_extract(self):
test_bytes = b'Hello, World'
self.fs.setbytes('hello.bin', test_bytes)
write_file = io.BytesIO()
self.fs.extract('hello.bin', write_file)
self.assertEqual(write_file.getvalue(), test_bytes)

with self.assertRaises(errors.ResourceNotFound):
self.fs.extract('foo.bin', write_file)

def test_listdir(self):
# Check listing directory that doesn't exist
with self.assertRaises(errors.ResourceNotFound):
Expand Down
1 change: 0 additions & 1 deletion tests/test_ftpfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def test_manager(self):
with ftp_errors(mem_fs):
raise error_perm('999 foo')


class TestFTPFS(FSTestCases, unittest.TestCase):

user = 'user'
Expand Down

0 comments on commit 5cbac6c

Please sign in to comment.