summaryrefslogtreecommitdiff
path: root/fs/compatibility.py
blob: 6e4baf158cca2cb08dbf32ac2154873505755fc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Some functions for Python3 compatibility.

Not for general usage, the functionality in this file is exposed elsewhere

"""

import six
from six import PY3


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)
            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)
                bytes_written += len(chunk)
                progress_callback(bytes_written)
                chunk = read(chunk_size)
        else:
            if 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()