Refactored/simplified the compatibility.copy_file_to_fs function.

parent ef89627b
...@@ -38,8 +38,7 @@ from fs.errors import * ...@@ -38,8 +38,7 @@ from fs.errors import *
from fs.local_functools import wraps from fs.local_functools import wraps
import compatibility import compatibility
import six from six import b
from six import PY3, b
class DummyLock(object): class DummyLock(object):
"""A dummy lock object that doesn't do anything. """A dummy lock object that doesn't do anything.
...@@ -772,89 +771,15 @@ class FS(object): ...@@ -772,89 +771,15 @@ class FS(object):
""" """
if progress_callback is None:
progress_callback = lambda bytes_written:None
finished_event = threading.Event() finished_event = threading.Event()
def do_setcontents(): def do_setcontents():
if PY3: try:
try: compatibility.copy_file_to_fs(data, self, path, chunk_size=chunk_size, progress_callback=progress_callback, finished_callback=finished_callback)
f = None except Exception, e:
try: if error_callback is not None:
progress_callback(0) error_callback(e)
finally:
if hasattr(data, "read"): finished_event.set()
bytes_written = 0
read = data.read
chunk = read(chunk_size)
if isinstance(chunk, six.text_type):
f = self.open(path, 'w')
else:
f = self.open(path, 'wb')
write = f.write
while chunk:
write(chunk)
bytes_written += len(chunk)
progress_callback(bytes_written)
chunk = read(chunk_size)
else:
if isinstance(data, six.text_type):
f = self.open(path, 'w')
else:
f = self.open(path, 'wb')
f.write(data)
progress_callback(len(data))
if finished_callback is not None:
finished_callback()
finally:
if f is not None:
f.close()
except Exception, e:
if error_callback is not None:
error_callback(e)
raise
finally:
finished_event.set()
else:
try:
f = None
try:
f = self.open(path, 'wb')
progress_callback(0)
if hasattr(data, "read"):
bytes_written = 0
read = data.read
write = f.write
chunk = read(chunk_size)
while chunk:
write(chunk)
bytes_written += len(chunk)
progress_callback(bytes_written)
chunk = read(chunk_size)
else:
f.write(data)
progress_callback(len(data))
if finished_callback is not None:
finished_callback()
finally:
if f is not None:
f.close()
except Exception, e:
if error_callback is not None:
error_callback(e)
finally:
finished_event.set()
threading.Thread(target=do_setcontents).start() threading.Thread(target=do_setcontents).start()
return finished_event return finished_event
......
...@@ -8,56 +8,41 @@ Not for general usage, the functionality in this file is exposed elsewhere ...@@ -8,56 +8,41 @@ Not for general usage, the functionality in this file is exposed elsewhere
import six import six
from six import PY3 from six import PY3
if PY3: def copy_file_to_fs(data, dst_fs, dst_path, chunk_size=64 * 1024, progress_callback=None, finished_callback=None):
def copy_file_to_fs(data, dst_fs, dst_path, chunk_size=64 * 1024): """Copy data from a string or a file-like object to a given fs/path"""
"""Copy data from a string or a file-like object to a given fs/path""" if progress_callback is None:
if hasattr(data, "read"): progress_callback = lambda bytes_written:None
bytes_written = 0
f = None
try:
progress_callback(bytes_written)
if hasattr(data, "read"):
read = data.read read = data.read
chunk = read(chunk_size) chunk = read(chunk_size)
f = None if PY3 and isinstance(chunk, six.text_type):
try: f = dst_fs.open(dst_path, 'w')
if isinstance(chunk, six.text_type): else:
f = dst_fs.open(dst_path, 'w') f = dst_fs.open(dst_path, 'wb')
else: write = f.write
f = dst_fs.open(dst_path, 'wb') while chunk:
write(chunk)
write = f.write bytes_written += len(chunk)
while chunk: progress_callback(bytes_written)
write(chunk)
chunk = read(chunk_size)
finally:
if f is not None:
f.close()
else:
f = None
try:
if isinstance(data, six.text_type):
f = dst_fs.open(dst_path, 'w')
else:
f = dst_fs.open(dst_path, 'wb')
f.write(data)
finally:
if f is not None:
f.close()
else:
def copy_file_to_fs(data, dst_fs, dst_path, chunk_size=64 * 1024):
"""Copy data from a string or a file-like object to a given fs/path"""
f = None
try:
f = dst_fs.open(dst_path, 'wb')
if hasattr(data, "read"):
read = data.read
write = f.write
chunk = read(chunk_size) chunk = read(chunk_size)
while chunk: else:
write(chunk) if PY3 and isinstance(data, six.text_type):
chunk = read(chunk_size) f = dst_fs.open(dst_path, 'w')
else: else:
f.write(data) f = dst_fs.open(dst_path, 'wb')
if hasattr(f, 'flush'): f.write(data)
f.flush() bytes_written += len(data)
finally: progress_callback(bytes_written)
if f is not None:
f.close() if hasattr(f, 'flush'):
f.flush()
if finished_callback is not None:
finished_callback()
finally:
if f is not None:
f.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment