diff options
author | gcode@loowis.durge.org <gcode@loowis.durge.org@67cdc799-7952-0410-af00-57a81ceafa0f> | 2012-08-15 16:36:54 +0000 |
---|---|---|
committer | gcode@loowis.durge.org <gcode@loowis.durge.org@67cdc799-7952-0410-af00-57a81ceafa0f> | 2012-08-15 16:36:54 +0000 |
commit | 22da1f03fb22e585394d026df7c653c64f895d0d (patch) | |
tree | 537a917085b8036fd149bac589f50dd80f9add2e | |
parent | 721500ce14bc96977d8aa9d9ede22334b1743b5c (diff) | |
download | pyfilesystem-22da1f03fb22e585394d026df7c653c64f895d0d.tar.gz |
Refactored/simplified the compatibility.copy_file_to_fs function.
git-svn-id: http://pyfilesystem.googlecode.com/svn/trunk@809 67cdc799-7952-0410-af00-57a81ceafa0f
-rw-r--r-- | fs/base.py | 91 | ||||
-rw-r--r-- | fs/compatibility.py | 83 |
2 files changed, 42 insertions, 132 deletions
@@ -38,8 +38,7 @@ from fs.errors import * from fs.local_functools import wraps import compatibility -import six -from six import PY3, b +from six import b class DummyLock(object): """A dummy lock object that doesn't do anything. @@ -772,89 +771,15 @@ class FS(object): """ - if progress_callback is None: - progress_callback = lambda bytes_written:None - finished_event = threading.Event() def do_setcontents(): - if PY3: - try: - f = None - try: - progress_callback(0) - - if hasattr(data, "read"): - 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() + try: + compatibility.copy_file_to_fs(data, self, path, chunk_size=chunk_size, progress_callback=progress_callback, finished_callback=finished_callback) + except Exception, e: + if error_callback is not None: + error_callback(e) + finally: + finished_event.set() threading.Thread(target=do_setcontents).start() return finished_event diff --git a/fs/compatibility.py b/fs/compatibility.py index b69d6b0..abf8fe8 100644 --- a/fs/compatibility.py +++ b/fs/compatibility.py @@ -8,56 +8,41 @@ Not for general usage, the functionality in this file is exposed elsewhere import six from six import PY3 -if PY3: - 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""" - if hasattr(data, "read"): +def copy_file_to_fs(data, dst_fs, dst_path, chunk_size=64 * 1024, progress_callback=None, finished_callback=None): + """Copy data from a string or a file-like object to a given fs/path""" + if progress_callback is None: + progress_callback = lambda bytes_written:None + bytes_written = 0 + f = None + try: + progress_callback(bytes_written) + if hasattr(data, "read"): read = data.read chunk = read(chunk_size) - f = None - try: - if isinstance(chunk, six.text_type): - f = dst_fs.open(dst_path, 'w') - else: - f = dst_fs.open(dst_path, 'wb') - - write = f.write - while chunk: - 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 + if PY3 and isinstance(chunk, six.text_type): + f = dst_fs.open(dst_path, 'w') + else: + f = dst_fs.open(dst_path, 'wb') + write = f.write + while chunk: + write(chunk) + bytes_written += len(chunk) + progress_callback(bytes_written) chunk = read(chunk_size) - while chunk: - write(chunk) - chunk = read(chunk_size) - else: - f.write(data) - if hasattr(f, 'flush'): - f.flush() - finally: - if f is not None: - f.close() + else: + if PY3 and isinstance(data, six.text_type): + f = dst_fs.open(dst_path, 'w') + else: + f = dst_fs.open(dst_path, 'wb') + f.write(data) + bytes_written += len(data) + progress_callback(bytes_written) + + if hasattr(f, 'flush'): + f.flush() + if finished_callback is not None: + finished_callback() + finally: + if f is not None: + f.close() |