GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#14)
by
unknown
02:26
created

b2blaze.models.file_list   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Test Coverage

Coverage 57.66%

Importance

Changes 0
Metric Value
eloc 129
dl 0
loc 209
ccs 64
cts 111
cp 0.5766
rs 10
c 0
b 0
f 0
wmc 27

6 Methods

Rating   Name   Duplication   Size   Complexity  
A B2FileList.all() 0 6 1
A B2FileList.__init__() 0 10 1
B B2FileList.get() 0 36 6
B B2FileList._update_files_list() 0 32 6
C B2FileList.upload_large_file() 0 68 9
A B2FileList.upload() 0 30 4
1
"""
2
Copyright George Sibble 2018
3
"""
4
5 1
from ..b2_exceptions import B2InvalidBucketName, B2InvalidBucketConfiguration, B2BucketCreationError
0 ignored issues
show
Unused Code introduced by
Unused B2InvalidBucketName imported from b2_exceptions
Loading history...
Unused Code introduced by
Unused B2BucketCreationError imported from b2_exceptions
Loading history...
Unused Code introduced by
Unused B2InvalidBucketConfiguration imported from b2_exceptions
Loading history...
6 1
from .b2_file import B2File
7 1
from ..utilities import b2_url_encode, get_content_length, get_part_ranges, decode_error, RangeStream, StreamWithHashProgress
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (125/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Unused Code introduced by
Unused StreamWithHashProgress imported from utilities
Loading history...
8 1
from ..b2_exceptions import B2RequestError, B2FileNotFound
9 1
from multiprocessing.dummy import Pool as ThreadPool
0 ignored issues
show
introduced by
standard import "from multiprocessing.dummy import Pool as ThreadPool" should be placed before "from ..b2_exceptions import B2InvalidBucketName, B2InvalidBucketConfiguration, B2BucketCreationError"
Loading history...
10
11 1
class B2FileList(object):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
12
    """
13
14
    """
15 1
    def __init__(self, connector, bucket):
16
        """
17
18
        :param connector:
19
        :param bucket:
20
        """
21 1
        self.connector = connector
22 1
        self.bucket = bucket
23 1
        self._files_by_name = {}
24 1
        self._files_by_id = {}
25
26 1
    def all(self):
27
        """
28
29
        :return:
30
        """
31 1
        return self._update_files_list(retrieve=True)
32
33 1
    def _update_files_list(self, retrieve=False):
34
        """
35
36
        :param retrieve:
37
        :return:
38
        """
39 1
        path = '/b2_list_file_names'
40 1
        files = []
41 1
        new_files_to_retrieve = True
42 1
        params = {
43
            'bucketId': self.bucket.bucket_id,
44
            'maxFileCount': 10000
45
        }
46 1
        while new_files_to_retrieve:
47 1
            response = self.connector.make_request(path=path, method='post', params=params)
48 1
            if response.status_code == 200:
49 1
                files_json = response.json()
50 1
                self._files_by_name = {}
51 1
                self._files_by_id = {}
52 1
                for file_json in files_json['files']:
53 1
                    new_file = B2File(connector=self.connector, parent_list=self, **file_json)
54 1
                    files.append(new_file)
55 1
                    self._files_by_name[file_json['fileName']] = new_file
56 1
                    self._files_by_id[file_json['fileId']] = new_file
57 1
                if files_json['nextFileName'] is None:
58 1
                    new_files_to_retrieve = False
59
                else:
60
                    params['startFileName'] = files_json['nextFileName']
61
            else:
62
                raise B2RequestError(decode_error(response))
63 1
        if retrieve:
64 1
            return files
65
66 1
    def get(self, file_name=None, file_id=None):
67
        """
68
69
        :param file_name:
70
        :param file_id:
71
        :return:
72
        """
73 1
        if file_name is not None:
74 1
            path = '/b2_list_file_names'
75 1
            params = {
76
                'prefix': b2_url_encode(file_name),
77
                'bucketId': self.bucket.bucket_id
78
            }
79
80 1
            response = self.connector.make_request(path, method='post', params=params)
81 1
            if response.status_code == 200:
82 1
                file_json = response.json()
83 1
                if len(file_json['files']) > 0:
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) to determine if a sequence is empty
Loading history...
84 1
                    return B2File(connector=self.connector, parent_list=self, **file_json['files'][0])
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
85
                else:
86 1
                    raise B2FileNotFound('fileName - ' + file_name)
87
            else:
88
                raise B2RequestError(decode_error(response))
89 1
        elif file_id is not None:
90 1
            path = '/b2_get_file_info'
91 1
            params = {
92
                'fileId': file_id
93
            }
94 1
            response = self.connector.make_request(path, method='post', params=params)
95 1
            if response.status_code == 200:
96 1
                file_json = response.json()
97 1
                return B2File(connector=self.connector, parent_list=self, **file_json)
98
            else:
99 1
                raise B2RequestError(decode_error(response))
100
        else:
101
            raise ValueError('file_name or file_id must be passed')
102
103
        # self._update_files_list()
104
        # if file_name is not None:
105
        #     return self._files_by_name.get(file_name, None)
106
        # else:
107
        #     return self._files_by_id.get(file_id, None)
108
        # pass
109
110 1
    def upload(self, contents, file_name, mime_content_type=None, content_length=None, progress_listener=None):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (111/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
best-practice introduced by
Too many arguments (6/5)
Loading history...
Unused Code introduced by
The argument mime_content_type seems to be unused.
Loading history...
111
        """
112
113
        :param contents:
114
        :param file_name:
115
        :param mime_content_type:
116
        :param content_length:
117
        :param progress_listener:
118
        :return:
119
        """
120 1
        if file_name[0] == '/':
121
            file_name = file_name[1:]
122 1
        get_upload_url_path = '/b2_get_upload_url'
123 1
        params = {
124
            'bucketId': self.bucket.bucket_id
125
        }
126 1
        upload_url_response = self.connector.make_request(path=get_upload_url_path, method='post', params=params)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (113/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
127 1
        if upload_url_response.status_code == 200:
128 1
            upload_url = upload_url_response.json().get('uploadUrl', None)
129 1
            auth_token = upload_url_response.json().get('authorizationToken', None)
130 1
            upload_response = self.connector.upload_file(file_contents=contents, file_name=file_name,
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
131
                                                         upload_url=upload_url, auth_token=auth_token,
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
132
                                                         content_length=content_length, progress_listener=progress_listener)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (124/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
133 1
            if upload_response.status_code == 200:
134 1
                new_file = B2File(connector=self.connector, parent_list=self, **upload_response.json())
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
135 1
                return new_file
136
            else:
137
                raise B2RequestError(decode_error(upload_response))
138
        else:
139
            raise B2RequestError(decode_error(upload_url_response))
140
141 1
    def upload_large_file(self, contents, file_name, part_size=None, num_threads=4,
0 ignored issues
show
best-practice introduced by
Too many arguments (8/5)
Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (19/15).
Loading history...
142
                          mime_content_type=None, content_length=None, progress_listener=None):
143
        """
144
145
        :param contents:
146
        :param file_name:
147
        :param part_size:
148
        :param num_threads:
149
        :param mime_content_type:
150
        :param content_length:
151
        :param progress_listener:
152
        :return:
153
        """
154
        if file_name[0] == '/':
155
            file_name = file_name[1:]
156
        if part_size == None:
0 ignored issues
show
introduced by
Comparison to None should be 'expr is None'
Loading history...
157
            part_size = self.connector.recommended_part_size
158
        if content_length == None:
0 ignored issues
show
introduced by
Comparison to None should be 'expr is None'
Loading history...
159
            content_length = get_content_length(contents)
160
        start_large_file_path = '/b2_start_large_file'
161
        params = {
162
            'bucketId': self.bucket.bucket_id,
163
            'fileName': b2_url_encode(file_name),
164
            'contentType': mime_content_type or 'b2/x-auto'
165
        }
166
        large_file_response = self.connector.make_request(path=start_large_file_path, method='post', params=params)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (115/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
167
        if large_file_response.status_code == 200:
168
            file_id = large_file_response.json().get('fileId', None)
169
            get_upload_part_url_path = '/b2_get_upload_part_url'
170
            params = {
171
                'fileId': file_id
172
            }
173
            pool = ThreadPool(num_threads)
174
            def upload_part_worker(args):
0 ignored issues
show
Coding Style introduced by
This function 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...
175
                part_number, part_range = args
176
                offset, content_length = part_range
177
                with open(contents.name, 'rb') as file:
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in file.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
178
                    file.seek(offset)
179
                    stream = RangeStream(file, offset, content_length)
180
                    upload_part_url_response = self.connector.make_request(path=get_upload_part_url_path, method='post', params=params)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (135/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
181
                    if upload_part_url_response.status_code == 200:
182
                        upload_url = upload_part_url_response.json().get('uploadUrl')
183
                        auth_token = upload_part_url_response.json().get('authorizationToken')
184
                        upload_part_response = self.connector.upload_part(file_contents=stream, content_length=content_length,
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (126/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
185
                                                                          part_number=part_number, upload_url=upload_url,
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (121/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
186
                                                                          auth_token=auth_token, progress_listener=progress_listener)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (133/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
187
                        if upload_part_response.status_code == 200:
188
                            return upload_part_response.json().get('contentSha1', None)
189
                        else:
190
                            raise B2RequestError(decode_error(upload_part_response))
191
                    else:
192
                        raise B2RequestError(decode_error(upload_part_url_response))
193
            sha_list = pool.map(upload_part_worker, enumerate(get_part_ranges(content_length, part_size), 1))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (109/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
194
            pool.close()
195
            pool.join()
196
            finish_large_file_path = '/b2_finish_large_file'
197
            params = {
198
                'fileId': file_id,
199
                'partSha1Array': sha_list
200
            }
201
            finish_large_file_response = self.connector.make_request(path=finish_large_file_path, method='post', params=params)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (127/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
202
            if finish_large_file_response.status_code == 200:
203
                new_file = B2File(connector=self.connector, parent_list=self, **finish_large_file_response.json())
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (114/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
204
                return new_file
205
            else:
206
                raise B2RequestError(decode_error(finish_large_file_response))
207
        else:
208
            raise B2RequestError(decode_error(large_file_response))
209