Skip to content

Commit

Permalink
added appendtext and appendbytes
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Nov 20, 2016
1 parent 5c53e02 commit dca0697
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
40 changes: 37 additions & 3 deletions fs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,42 @@ def setinfo(self, path, info):
# Filesystems *may* implement these methods. #
# ---------------------------------------------------------------- #

def appendbytes(self, path, data):
"""
Append bytes to the end of a file. Creating the file if it
doesn't already exists.
:param str path: Path to a file.
:param bytes data: Bytes to append.
:raises: :class:`ValueError` if ``data`` is not bytes.
:raises: :class:`fs.errors.ResourceNotFound` if a parent
directory of ``path`` does not exist.
"""
if not isinstance(data, bytes):
raise ValueError('must be bytes')
with self._lock:
with self.open(path, 'ab') as append_file:
append_file.write(data)

def appendtext(self, path, text):
"""
Append text to a file. Creating the file if it doesn't already
exists.
:param str path: Path to a file.
:param str text: Text to append.
:raises: :class:`ValueError` if ``text`` is not bytes.
:raises: :class:`fs.errors.ResourceNotFound` if a parent
directory of ``path`` does not exist.
"""
if not isinstance(text, six.text_type):
raise ValueError('must be unicode str')
with self._lock:
with self.open(path, 'at') as append_file:
append_file.write(text)

def close(self):
"""
Close the filesystem and release any resources.
Expand Down Expand Up @@ -641,9 +677,7 @@ def isempty(self, path):
:rtype: bool
"""
for info in self.scandir(path):
return False
return True
return next(iter(self.scandir(path)), None) is None

def isfile(self, path):
"""Check a path exists and is a file."""
Expand Down
2 changes: 1 addition & 1 deletion fs/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def validate_bin(self):
@property
def create(self):
"""Check if the mode would create a file."""
return 'w' in self or 'x' in self
return 'a' in self or 'w' in self or 'x' in self

@property
def reading(self):
Expand Down
14 changes: 7 additions & 7 deletions fs/wrapfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ def filterdir(self,
self.check()
_fs, _path = self.delegate_path(path)
iter_files = iter(_fs.filterdir(
_path,
exclude_dirs=exclude_dirs,
exclude_files=exclude_files,
files=files,
dirs=dirs,
namespaces=namespaces,
page=page
_path,
exclude_dirs=exclude_dirs,
exclude_files=exclude_files,
files=files,
dirs=dirs,
namespaces=namespaces,
page=page
))
with unwrap_errors(path):
for info in iter_files:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,19 @@ def assert_text(self, path, contents):
self.assertEqual(data, contents)
self.assertIsInstance(data, text_type)


def test_appendbytes(self):
with self.assertRaises(ValueError):
self.fs.appendbytes('foo', 'bar')
self.fs.appendbytes('foo', b'bar')
self.assert_bytes('foo', b'bar')

def test_appendtext(self):
with self.assertRaises(ValueError):
self.fs.appendtext('foo', b'bar')
self.fs.appendtext('foo', 'bar')
self.assert_text('foo', 'bar')

def test_basic(self):
# Check str and repr don't break
repr(self.fs)
Expand Down

0 comments on commit dca0697

Please sign in to comment.