Passed
Push — master ( b2d36d...e1a621 )
by Jaisen
02:25
created

elodie.compatability._bytes()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
import os
0 ignored issues
show
Coding Style introduced by Jaisen Mathai
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):
0 ignored issues
show
Comprehensibility Best Practice introduced by Jaisen Mathai
The variable unicode does not seem to be defined.
Loading history...
19
                return string
20
        except NameError:
21
            pass
22
23
        return string.decode(encoding)
24
25
    return string
26
27
def _bytes(string):
28
    if constants.python_version == 3:
29
        return bytes(string, 'utf8')
30
    else:
31
        return bytes(string)
32
33
def _copyfile(src, dst):
34
    # shutil.copy seems slow, changing to streaming according to
35
    # http://stackoverflow.com/questions/22078621/python-how-to-copy-files-fast  # noqa
36
    # Python 3 hangs using open/write method so we proceed with shutil.copy
37
    #  and only perform the optimized write for Python 2.
38
    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...
39
        # Do not use copy2(), it will have an issue when copying to a
40
        #  network/mounted drive.
41
        # Using copy and manual set_date_from_filename gets the job done.
42
        # The calling function is responsible for setting the time.
43
        shutil.copy(src, dst)
44
        return
45
46
    try:
47
        O_BINARY = os.O_BINARY
48
    except:
49
        O_BINARY = 0
50
51
    READ_FLAGS = os.O_RDONLY | O_BINARY
52
    WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY
53
    TEN_MEGABYTES = 10485760
54
    BUFFER_SIZE = min(TEN_MEGABYTES, os.path.getsize(src))
55
56
    try:
57
        fin = os.open(src, READ_FLAGS)
58
        stat = os.fstat(fin)
59
        fout = os.open(dst, WRITE_FLAGS, stat.st_mode)
60
        for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""):
0 ignored issues
show
introduced by Jaisen Mathai
The variable BUFFER_SIZE does not seem to be defined for all execution paths.
Loading history...
introduced by Jaisen Mathai
The variable fin does not seem to be defined for all execution paths.
Loading history...
61
            os.write(fout, x)
62
    finally:
63
        try:
64
            os.close(fin)
65
        except:
66
            pass
67
        try:
68
            os.close(fout)
69
        except:
70
            pass
71
72
73
# If you want cross-platform overwriting of the destination, 
0 ignored issues
show
Coding Style introduced by Jaisen Mathai
Trailing whitespace
Loading history...
74
# use os.replace() instead of rename().
75
# https://docs.python.org/3/library/os.html#os.rename
76
def _rename(src, dst):
77
    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...
78
        return os.replace(src, dst)
79
    else:
80
        return os.rename(src, dst)
81