Skip to content

Commit c6b4698

Browse files
authored
Merge pull request #528 from reef-technologies/ruff
Replace yapf with ruff
2 parents 1416ffe + 7e9e862 commit c6b4698

File tree

172 files changed

+3362
-3006
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+3362
-3006
lines changed

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In order to make it easier to contribute, core developers of this project:
88
* maintain a set of unit tests
99
* maintain a set of integration tests (run with a production cloud)
1010
* maintain development automation tools using [nox](https://github.com/theacodes/nox) that can easily:
11-
* format the code using [yapf](https://github.com/google/yapf) and [ruff](https://github.com/astral-sh/ruff)
11+
* format the code using [ruff](https://github.com/astral-sh/ruff)
1212
* runs linters to find subtle/potential issues with maintainability
1313
* run the test suite on multiple Python versions using [pytest](https://github.com/pytest-dev/pytest)
1414
* maintain Continuous Integration (by using GitHub Actions) that:

b2sdk/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
from __future__ import annotations
1111

1212
import b2sdk.version # noqa: E402
13+
1314
__version__ = b2sdk.version.VERSION
1415
assert __version__ # PEP-0396

b2sdk/_internal/account_info/abstract.py

+31-10
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ def is_master_key(self) -> bool:
142142
new_style_master_key_suffix = '0000000000'
143143
if account_id == application_key_id:
144144
return True # old style
145-
if len(application_key_id
146-
) == (3 + len(account_id) + len(new_style_master_key_suffix)): # 3 for cluster id
145+
if len(application_key_id) == (
146+
3 + len(account_id) + len(new_style_master_key_suffix)
147+
): # 3 for cluster id
147148
# new style
148149
if application_key_id.endswith(account_id + new_style_master_key_suffix):
149150
return True
@@ -320,9 +321,17 @@ def set_auth_data(
320321
assert self.allowed_is_valid(allowed)
321322

322323
self._set_auth_data(
323-
account_id, auth_token, api_url, download_url, recommended_part_size,
324-
absolute_minimum_part_size, application_key, realm, s3_api_url, allowed,
325-
application_key_id
324+
account_id,
325+
auth_token,
326+
api_url,
327+
download_url,
328+
recommended_part_size,
329+
absolute_minimum_part_size,
330+
application_key,
331+
realm,
332+
s3_api_url,
333+
allowed,
334+
application_key_id,
326335
)
327336

328337
@classmethod
@@ -338,15 +347,27 @@ def allowed_is_valid(cls, allowed):
338347
:rtype: bool
339348
"""
340349
return (
341-
('bucketId' in allowed) and ('bucketName' in allowed) and
342-
((allowed['bucketId'] is not None) or (allowed['bucketName'] is None)) and
343-
('capabilities' in allowed) and ('namePrefix' in allowed)
350+
('bucketId' in allowed)
351+
and ('bucketName' in allowed)
352+
and ((allowed['bucketId'] is not None) or (allowed['bucketName'] is None))
353+
and ('capabilities' in allowed)
354+
and ('namePrefix' in allowed)
344355
)
345356

346357
@abstractmethod
347358
def _set_auth_data(
348-
self, account_id, auth_token, api_url, download_url, recommended_part_size,
349-
absolute_minimum_part_size, application_key, realm, s3_api_url, allowed, application_key_id
359+
self,
360+
account_id,
361+
auth_token,
362+
api_url,
363+
download_url,
364+
recommended_part_size,
365+
absolute_minimum_part_size,
366+
application_key,
367+
realm,
368+
s3_api_url,
369+
allowed,
370+
application_key_id,
350371
):
351372
"""
352373
Actually store the auth data. Can assume that 'allowed' is present and valid.

b2sdk/_internal/account_info/exception.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class AccountInfoError(B2Error, metaclass=ABCMeta):
1818
"""
1919
Base class for all account info errors.
2020
"""
21+
2122
pass
2223

2324

@@ -35,8 +36,10 @@ def __init__(self, file_name):
3536
self.file_name = file_name
3637

3738
def __str__(self):
38-
return f'Account info file ({self.file_name}) appears corrupted. ' \
39-
f'Try removing and then re-authorizing the account.'
39+
return (
40+
f'Account info file ({self.file_name}) appears corrupted. '
41+
f'Try removing and then re-authorizing the account.'
42+
)
4043

4144

4245
class MissingAccountData(AccountInfoError):

b2sdk/_internal/account_info/in_memory.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,18 @@ def _clear_in_memory_account_fields(self):
6060
self._s3_api_url = None
6161

6262
def _set_auth_data(
63-
self, account_id, auth_token, api_url, download_url, recommended_part_size,
64-
absolute_minimum_part_size, application_key, realm, s3_api_url, allowed, application_key_id
63+
self,
64+
account_id,
65+
auth_token,
66+
api_url,
67+
download_url,
68+
recommended_part_size,
69+
absolute_minimum_part_size,
70+
application_key,
71+
realm,
72+
s3_api_url,
73+
allowed,
74+
application_key_id,
6575
):
6676
self._account_id = account_id
6777
self._application_key_id = application_key_id

b2sdk/_internal/account_info/sqlite_account_info.py

+28-18
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
B2_ACCOUNT_INFO_PROFILE_NAME_REGEXP = re.compile(r'[a-zA-Z0-9_\-]{1,64}')
3030
XDG_CONFIG_HOME_ENV_VAR = 'XDG_CONFIG_HOME'
3131

32-
DEFAULT_ABSOLUTE_MINIMUM_PART_SIZE = 5000000 # this value is used ONLY in migrating db, and in v1 wrapper, it is not
32+
DEFAULT_ABSOLUTE_MINIMUM_PART_SIZE = (
33+
5000000 # this value is used ONLY in migrating db, and in v1 wrapper, it is not
34+
)
3335
# meant to be a default for other applications
3436

3537

@@ -162,8 +164,13 @@ def _validate_database(self, last_upgrade_to_run=None):
162164
with open(self.filename, 'rb') as f:
163165
data = json.loads(f.read().decode('utf-8'))
164166
keys = [
165-
'account_id', 'application_key', 'account_auth_token', 'api_url',
166-
'download_url', 'minimum_part_size', 'realm'
167+
'account_id',
168+
'application_key',
169+
'account_auth_token',
170+
'api_url',
171+
'download_url',
172+
'minimum_part_size',
173+
'realm',
167174
]
168175
if all(k in data for k in keys):
169176
# remove the json file
@@ -184,7 +191,7 @@ def _validate_database(self, last_upgrade_to_run=None):
184191
# new column absolute_minimum_part_size = DEFAULT_ABSOLUTE_MINIMUM_PART_SIZE
185192
conn.execute(
186193
insert_statement,
187-
(*(data[k] for k in keys), DEFAULT_ABSOLUTE_MINIMUM_PART_SIZE)
194+
(*(data[k] for k in keys), DEFAULT_ABSOLUTE_MINIMUM_PART_SIZE),
188195
)
189196
# all is happy now
190197
return
@@ -287,23 +294,24 @@ def _create_tables(self, conn, last_upgrade_to_run):
287294
self._ensure_update(3, ['ALTER TABLE account ADD COLUMN s3_api_url TEXT;'])
288295
if 4 <= last_upgrade_to_run:
289296
self._ensure_update(
290-
4, [
291-
"""
297+
4,
298+
[
299+
f"""
292300
CREATE TABLE
293301
tmp_account (
294302
account_id TEXT NOT NULL,
295303
application_key TEXT NOT NULL,
296304
account_auth_token TEXT NOT NULL,
297305
api_url TEXT NOT NULL,
298306
download_url TEXT NOT NULL,
299-
absolute_minimum_part_size INT NOT NULL DEFAULT {},
307+
absolute_minimum_part_size INT NOT NULL DEFAULT {DEFAULT_ABSOLUTE_MINIMUM_PART_SIZE},
300308
recommended_part_size INT NOT NULL,
301309
realm TEXT NOT NULL,
302310
allowed TEXT,
303311
account_id_or_app_key_id TEXT,
304312
s3_api_url TEXT
305313
);
306-
""".format(DEFAULT_ABSOLUTE_MINIMUM_PART_SIZE),
314+
""",
307315
"""INSERT INTO tmp_account(
308316
account_id,
309317
application_key,
@@ -373,7 +381,7 @@ def _create_tables(self, conn, last_upgrade_to_run):
373381
FROM tmp_account;
374382
""",
375383
'DROP TABLE tmp_account;',
376-
]
384+
],
377385
)
378386

379387
def _ensure_update(self, update_number, update_commands: list[str]):
@@ -387,7 +395,7 @@ def _ensure_update(self, update_number, update_commands: list[str]):
387395
conn.execute('BEGIN')
388396
cursor = conn.execute(
389397
'SELECT COUNT(*) AS count FROM update_done WHERE update_number = ?;',
390-
(update_number,)
398+
(update_number,),
391399
)
392400
update_count = cursor.fetchone()[0]
393401
if update_count == 0:
@@ -433,7 +441,8 @@ def _set_auth_data(
433441
"""
434442

435443
conn.execute(
436-
insert_statement, (
444+
insert_statement,
445+
(
437446
account_id,
438447
application_key_id,
439448
application_key,
@@ -445,7 +454,7 @@ def _set_auth_data(
445454
realm,
446455
json.dumps(allowed),
447456
s3_api_url,
448-
)
457+
),
449458
)
450459

451460
def set_auth_data_with_schema_0_for_test(
@@ -480,15 +489,16 @@ def set_auth_data_with_schema_0_for_test(
480489
"""
481490

482491
conn.execute(
483-
insert_statement, (
492+
insert_statement,
493+
(
484494
account_id,
485495
application_key,
486496
auth_token,
487497
api_url,
488498
download_url,
489499
minimum_part_size,
490500
realm,
491-
)
501+
),
492502
)
493503

494504
def get_application_key(self):
@@ -576,25 +586,25 @@ def _get_account_info_or_raise(self, column_name):
576586
except Exception as e:
577587
logger.exception(
578588
'_get_account_info_or_raise encountered a problem while trying to retrieve "%s"',
579-
column_name
589+
column_name,
580590
)
581591
raise MissingAccountData(str(e))
582592

583593
def refresh_entire_bucket_name_cache(self, name_id_iterable):
584594
with self._get_connection() as conn:
585595
conn.execute('DELETE FROM bucket;')
586-
for (bucket_name, bucket_id) in name_id_iterable:
596+
for bucket_name, bucket_id in name_id_iterable:
587597
conn.execute(
588598
'INSERT INTO bucket (bucket_name, bucket_id) VALUES (?, ?);',
589-
(bucket_name, bucket_id)
599+
(bucket_name, bucket_id),
590600
)
591601

592602
def save_bucket(self, bucket):
593603
with self._get_connection() as conn:
594604
conn.execute('DELETE FROM bucket WHERE bucket_id = ?;', (bucket.id_,))
595605
conn.execute(
596606
'INSERT INTO bucket (bucket_id, bucket_name) VALUES (?, ?);',
597-
(bucket.id_, bucket.name)
607+
(bucket.id_, bucket.name),
598608
)
599609

600610
def remove_bucket_name(self, bucket_name):

b2sdk/_internal/account_info/upload_url_pool.py

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class UrlPoolAccountInfo(AbstractAccountInfo):
7171
Implement part of :py:class:`AbstractAccountInfo` for upload URL pool management
7272
with a simple, key-value storage, such as :py:class:`b2sdk.v2.UploadUrlPool`.
7373
"""
74+
7475
# staticmethod is necessary here to avoid the first argument binding to the first argument (like ``partial(fun, arg)``)
7576
BUCKET_UPLOAD_POOL_CLASS = staticmethod(
7677
UploadUrlPool

b2sdk/_internal/api.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def url_for_api(info, api_name):
6868

6969

7070
class Services:
71-
""" Gathers objects that provide high level logic over raw api usage. """
71+
"""Gathers objects that provide high level logic over raw api usage."""
72+
7273
UPLOAD_MANAGER_CLASS = staticmethod(UploadManager)
7374
COPY_MANAGER_CLASS = staticmethod(CopyManager)
7475
DOWNLOAD_MANAGER_CLASS = staticmethod(DownloadManager)
@@ -133,6 +134,7 @@ class handles several things that simplify the task of uploading
133134
The class also keeps a cache of information needed to access the
134135
service, such as auth tokens and upload URLs.
135136
"""
137+
136138
BUCKET_FACTORY_CLASS = staticmethod(BucketFactory)
137139
BUCKET_CLASS = staticmethod(Bucket)
138140
SESSION_CLASS = staticmethod(B2Session)
@@ -272,8 +274,12 @@ def create_bucket(
272274
replication=replication,
273275
)
274276
bucket = self.BUCKET_FACTORY_CLASS.from_api_bucket_dict(self, response)
275-
assert name == bucket.name, f'API created a bucket with different name than requested: {name} != {name}'
276-
assert bucket_type == bucket.type_, f'API created a bucket with different type than requested: {bucket_type} != {bucket.type_}'
277+
assert (
278+
name == bucket.name
279+
), f'API created a bucket with different name than requested: {name} != {name}'
280+
assert (
281+
bucket_type == bucket.type_
282+
), f'API created a bucket with different type than requested: {bucket_type} != {bucket.type_}'
277283
self.cache.save_bucket(bucket)
278284
return bucket
279285

@@ -389,8 +395,9 @@ def delete_bucket(self, bucket):
389395
account_id = self.account_info.get_account_id()
390396
self.session.delete_bucket(account_id, bucket.id_)
391397

392-
def list_buckets(self, bucket_name=None, bucket_id=None, *,
393-
use_cache: bool = False) -> Sequence[Bucket]:
398+
def list_buckets(
399+
self, bucket_name=None, bucket_id=None, *, use_cache: bool = False
400+
) -> Sequence[Bucket]:
394401
"""
395402
Call ``b2_list_buckets`` and return a list of buckets.
396403
@@ -418,13 +425,14 @@ def list_buckets(self, bucket_name=None, bucket_id=None, *,
418425
cached_list = self.cache.list_bucket_names_ids()
419426
buckets = [
420427
self.BUCKET_CLASS(self, cache_b_id, name=cached_b_name)
421-
for cached_b_name, cache_b_id in cached_list if (
422-
(bucket_name is None or bucket_name == cached_b_name) and
423-
(bucket_id is None or bucket_id == cache_b_id)
428+
for cached_b_name, cache_b_id in cached_list
429+
if (
430+
(bucket_name is None or bucket_name == cached_b_name)
431+
and (bucket_id is None or bucket_id == cache_b_id)
424432
)
425433
]
426434
if buckets:
427-
logger.debug("Using cached bucket list as it is not empty")
435+
logger.debug('Using cached bucket list as it is not empty')
428436
return buckets
429437

430438
account_id = self.account_info.get_account_id()
@@ -494,8 +502,8 @@ def get_download_url_for_file_name(self, bucket_name, file_name):
494502
:param str file_name: a file name
495503
"""
496504
self.check_bucket_name_restrictions(bucket_name)
497-
return '{}/file/{}/{}'.format(
498-
self.account_info.get_download_url(), bucket_name, b2_url_encode(file_name)
505+
return (
506+
f'{self.account_info.get_download_url()}/file/{bucket_name}/{b2_url_encode(file_name)}'
499507
)
500508

501509
# keys
@@ -524,7 +532,7 @@ def create_key(
524532
key_name=key_name,
525533
valid_duration_seconds=valid_duration_seconds,
526534
bucket_id=bucket_id,
527-
name_prefix=name_prefix
535+
name_prefix=name_prefix,
528536
)
529537

530538
assert set(response['capabilities']) == set(capabilities)
@@ -551,8 +559,9 @@ def delete_key_by_id(self, application_key_id: str) -> ApplicationKey:
551559
response = self.session.delete_key(application_key_id=application_key_id)
552560
return ApplicationKey.from_api_response(response)
553561

554-
def list_keys(self, start_application_key_id: str | None = None
555-
) -> Generator[ApplicationKey, None, None]:
562+
def list_keys(
563+
self, start_application_key_id: str | None = None
564+
) -> Generator[ApplicationKey, None, None]:
556565
"""
557566
List application keys. Lazily perform requests to B2 cloud and return all keys.
558567
@@ -603,7 +612,7 @@ def get_file_info(self, file_id: str) -> FileVersion:
603612

604613
def get_file_info_by_name(self, bucket_name: str, file_name: str) -> DownloadVersion:
605614
"""
606-
Gets info about a file version. Similar to `get_file_info` but
615+
Gets info about a file version. Similar to `get_file_info` but
607616
takes the bucket name and file name instead of file id.
608617
609618
:param str bucket_name: The name of the bucket where the file resides.

0 commit comments

Comments
 (0)