Passed
Push — master ( 9e42ed...b2d36d )
by Jaisen
02:09
created

elodie/compatability.py (4 issues)

1
import os
0 ignored issues
show
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
import shutil
3
import sys
4
5
from elodie import constants
6
7
8
def _decode(string, encoding=sys.getfilesystemencoding()):
9
    """Return a utf8 encoded unicode string.
10
11
    Python2 and Python3 differ in how they handle strings.
12
    So we do a few checks to see if the string is ascii or unicode.
13
    Then we decode it if needed.
14
    """
15
    if hasattr(string, 'decode'):
16
        # If the string is already unicode we return it.
17
        try:
18
            if isinstance(string, unicode):
19
                return string
20
        except NameError:
21
            pass
22
23
        return string.decode(encoding)
24
25
    return string
26
27
28
def _copyfile(src, dst):
29
    # shutil.copy seems slow, changing to streaming according to
30
    # http://stackoverflow.com/questions/22078621/python-how-to-copy-files-fast  # noqa
31
    # Python 3 hangs using open/write method so we proceed with shutil.copy
32
    #  and only perform the optimized write for Python 2.
33
    if (constants.python_version == 3):
0 ignored issues
show
Unused Code Coding Style introduced by Jaisen Mathai
There is an unnecessary parenthesis after if.
Loading history...
34
        # Do not use copy2(), it will have an issue when copying to a
35
        #  network/mounted drive.
36
        # Using copy and manual set_date_from_filename gets the job done.
37
        # The calling function is responsible for setting the time.
38
        shutil.copy(src, dst)
39
        return
40
41
    try:
42
        O_BINARY = os.O_BINARY
43
    except:
44
        O_BINARY = 0
45
46
    READ_FLAGS = os.O_RDONLY | O_BINARY
47
    WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY
48
    TEN_MEGABYTES = 10485760
49
    BUFFER_SIZE = min(TEN_MEGABYTES, os.path.getsize(src))
50
51
    try:
52
        fin = os.open(src, READ_FLAGS)
53
        stat = os.fstat(fin)
54
        fout = os.open(dst, WRITE_FLAGS, stat.st_mode)
55
        for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""):
56
            os.write(fout, x)
57
    finally:
58
        try:
59
            os.close(fin)
60
        except:
61
            pass
62
        try:
63
            os.close(fout)
64
        except:
65
            pass
66
67
68
# If you want cross-platform overwriting of the destination, 
0 ignored issues
show
Trailing whitespace
Loading history...
69
# use os.replace() instead of rename().
70
# https://docs.python.org/3/library/os.html#os.rename
71
def _rename(src, dst):
72
    if (constants.python_version == 3):
0 ignored issues
show
Unused Code Coding Style introduced by Jaisen Mathai
There is an unnecessary parenthesis after if.
Loading history...
73
        return os.replace(src, dst)
74
    else:
75
        return os.rename(src, dst)
76