Skip to content

Commit 18cd37b

Browse files
formated
1 parent 2e1d1cd commit 18cd37b

File tree

5 files changed

+45
-42
lines changed

5 files changed

+45
-42
lines changed

databricks/sdk/config.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,19 @@ class Config:
147147
# For AWS, minimum is 5Mb: https://docs.aws.amazon.com/AmazonS3/latest/userguide/qfacts.html
148148
# For GCP, minimum is 256 KiB (and also recommended multiple is 256 KiB)
149149
# boto uses 8Mb: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/customizations/s3.html#boto3.s3.transfer.TransferConfig
150-
files_ext_multipart_upload_default_part_size: int = 10 * 1024 * 1024 # 10 MiB
150+
files_ext_multipart_upload_default_part_size: int = 10 * 1024 * 1024 # 10 MiB
151151

152152
# List of multipart upload part sizes that can be automatically selected
153153
files_ext_multipart_upload_part_size_options: list[int] = [
154-
10 * 1024 * 1024, # 10 MiB
155-
20 * 1024 * 1024, # 20 MiB
156-
50 * 1024 * 1024, # 50 MiB
157-
100 * 1024 * 1024, # 100 MiB
158-
200 * 1024 * 1024, # 200 MiB
159-
500 * 1024 * 1024, # 500 MiB
160-
1 * 1024 * 1024 * 1024, # 1 GiB
161-
2 * 1024 * 1024 * 1024, # 2 GiB
162-
4 * 1024 * 1024 * 1024, # 4 GiB
154+
10 * 1024 * 1024, # 10 MiB
155+
20 * 1024 * 1024, # 20 MiB
156+
50 * 1024 * 1024, # 50 MiB
157+
100 * 1024 * 1024, # 100 MiB
158+
200 * 1024 * 1024, # 200 MiB
159+
500 * 1024 * 1024, # 500 MiB
160+
1 * 1024 * 1024 * 1024, # 1 GiB
161+
2 * 1024 * 1024 * 1024, # 2 GiB
162+
4 * 1024 * 1024 * 1024, # 4 GiB
163163
]
164164

165165
# Maximum size of a single part in multipart upload.

databricks/sdk/mixins/files.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
from dataclasses import dataclass
1919
from datetime import timedelta
2020
from io import BytesIO
21-
from queue import Queue, Empty, Full
22-
from tempfile import NamedTemporaryFile, mkstemp
23-
from threading import Thread, Event
21+
from queue import Empty, Full, Queue
22+
from tempfile import mkstemp
23+
from threading import Event, Thread
2424
from types import TracebackType
25-
from typing import TYPE_CHECKING, AnyStr, BinaryIO, Callable, Generator, Iterable, Optional, Type, Union
25+
from typing import (TYPE_CHECKING, AnyStr, BinaryIO, Callable, Generator,
26+
Iterable, Optional, Type, Union)
2627
from urllib import parse
2728

2829
import requests
@@ -38,8 +39,8 @@
3839
from ..service import files
3940
from ..service._internal import _escape_multi_segment_path_parameter
4041
from ..service.files import DownloadResponse
41-
42-
from .files_utils import _ConcatenatedInputStream, _PresignedUrlDistributor, CreateDownloadUrlResponse
42+
from .files_utils import (CreateDownloadUrlResponse, _ConcatenatedInputStream,
43+
_PresignedUrlDistributor)
4344

4445
if TYPE_CHECKING:
4546
from _typeshed import Self
@@ -736,21 +737,18 @@ def __init__(self, message):
736737
class UploadStreamResult:
737738
"""Result of an upload from stream operation. Currently empty, but can be extended in the future."""
738739

739-
pass
740740

741741

742742
@dataclass
743743
class UploadFileResult:
744744
"""Result of an upload from file operation. Currently empty, but can be extended in the future."""
745745

746-
pass
747746

748747

749748
@dataclass
750749
class DownloadFileResult:
751750
"""Result of a download to file operation. Currently empty, but can be extended in the future."""
752751

753-
pass
754752

755753

756754
class FilesExt(files.FilesAPI):

databricks/sdk/mixins/files_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
22

3-
from dataclasses import dataclass
4-
from typing import BinaryIO, Optional, Any, Iterable, Callable
53
import os
64
import threading
5+
from dataclasses import dataclass
6+
from typing import Any, BinaryIO, Callable, Iterable, Optional
7+
78

89
@dataclass
910
class CreateDownloadUrlResponse:
@@ -23,6 +24,7 @@ def from_dict(cls, data: dict[str, Any]) -> CreateDownloadUrlResponse:
2324
parsed_headers = {x["name"]: x["value"] for x in headers}
2425
return cls(url=data["url"], headers=parsed_headers)
2526

27+
2628
class _ConcatenatedInputStream(BinaryIO):
2729
"""This class joins two input streams into one."""
2830

@@ -244,6 +246,7 @@ def __exit__(self, __type, __value, __traceback) -> None:
244246
def __str__(self) -> str:
245247
return f"Concat: {self._head_stream}, {self._tail_stream}]"
246248

249+
247250
class _PresignedUrlDistributor:
248251
"""
249252
Distributes and manages presigned URLs for downloading files.
@@ -253,7 +256,6 @@ class _PresignedUrlDistributor:
253256
"""
254257

255258
def __init__(self, get_new_url_func: Callable[[], CreateDownloadUrlResponse]):
256-
257259
"""
258260
Initialize the distributor.
259261

tests/test_files.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,28 @@
1010
from dataclasses import dataclass
1111
from datetime import datetime, timedelta, timezone
1212
from enum import Enum
13-
from tempfile import mkstemp, NamedTemporaryFile
14-
from typing import Callable, List, Optional, Type, Union, Any
13+
from tempfile import NamedTemporaryFile, mkstemp
14+
from threading import Lock
15+
from typing import Any, Callable, List, Optional, Type, Union
1516
from urllib.parse import parse_qs, urlparse
1617

1718
import pytest
1819
import requests
1920
import requests_mock
2021
from requests import RequestException
21-
from threading import Lock
2222

23-
from .test_files_utils import Utils
2423
from databricks.sdk import WorkspaceClient
25-
from databricks.sdk.environments import DatabricksEnvironment, Cloud
2624
from databricks.sdk.core import Config
27-
from databricks.sdk.errors.platform import (
28-
AlreadyExists,
29-
BadRequest,
30-
InternalError,
31-
PermissionDenied,
32-
TooManyRequests,
33-
NotImplemented,
34-
)
35-
from databricks.sdk.service.files import DownloadResponse
25+
from databricks.sdk.environments import Cloud, DatabricksEnvironment
26+
from databricks.sdk.errors.platform import (AlreadyExists, BadRequest,
27+
InternalError, NotImplemented,
28+
PermissionDenied, TooManyRequests)
3629
from databricks.sdk.mixins.files import FallbackToDownloadUsingFilesApi
3730
from databricks.sdk.mixins.files_utils import CreateDownloadUrlResponse
3831
from tests.clock import FakeClock
3932

33+
from .test_files_utils import Utils
34+
4035
logger = logging.getLogger(__name__)
4136

4237

@@ -1275,7 +1270,6 @@ def clear_state(self) -> None:
12751270
os.remove(file_path)
12761271
except OSError:
12771272
logger.warning("Failed to remove temp file: %s", file_path)
1278-
pass
12791273
self.created_temp_files = []
12801274

12811275
def get_upload_file(self, content: bytes, source_type: "UploadSourceType") -> Union[str, io.BytesIO]:

tests/test_files_utils.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import logging
22
import os
3+
from abc import ABC, abstractmethod
34
from io import BytesIO, RawIOBase, UnsupportedOperation
45
from typing import BinaryIO, Callable, Optional
6+
57
import pytest
6-
from abc import ABC, abstractmethod
78

8-
from databricks.sdk.mixins.files_utils import _ConcatenatedInputStream, _PresignedUrlDistributor
9+
from databricks.sdk.mixins.files_utils import (_ConcatenatedInputStream,
10+
_PresignedUrlDistributor)
911

1012
logger = logging.getLogger(__name__)
1113

14+
1215
class Utils:
1316
@staticmethod
14-
def parse_range_header(range_header: str, content_length: Optional[int]=None) -> tuple[int, int]:
17+
def parse_range_header(range_header: str, content_length: Optional[int] = None) -> tuple[int, int]:
1518
"""
1619
Parses a Range header string and returns the start and end byte positions.
1720
Example input: "bytes=0-499"
1821
Example output: (0, 499)
1922
"""
2023
if not range_header.startswith("bytes="):
2124
raise ValueError("Invalid Range header format")
22-
byte_range = range_header[len("bytes="):]
25+
byte_range = range_header[len("bytes=") :]
2326
start_str, end_str = byte_range.split("-")
2427
start = int(start_str) if start_str else 0
2528
end = int(end_str) if end_str else None
@@ -34,6 +37,7 @@ def parse_range_header(range_header: str, content_length: Optional[int]=None) ->
3437

3538
return start, end
3639

40+
3741
class NonSeekableBuffer(RawIOBase, BinaryIO):
3842
"""
3943
A non-seekable buffer that wraps a bytes object. Used for unit tests only.
@@ -268,17 +272,20 @@ def safe_call(buf: BinaryIO, call: Callable[[BinaryIO], any]) -> (any, bool):
268272
assert buffer.tell() == native_buffer.tell()
269273
assert read_and_restore(buffer) == read_and_restore(native_buffer)
270274

275+
271276
class DummyResponse:
272277
def __init__(self, value):
273278
self.value = value
274279

280+
275281
def test_get_url_returns_url_and_version():
276282
distributor = _PresignedUrlDistributor(lambda: DummyResponse("url1"))
277283
url, version = distributor.get_url()
278284
assert isinstance(url, DummyResponse)
279285
assert url.value == "url1"
280286
assert version == 0
281287

288+
282289
def test_get_url_caches_url():
283290
calls = []
284291
distributor = _PresignedUrlDistributor(lambda: calls.append(1) or DummyResponse("url2"))
@@ -288,6 +295,7 @@ def test_get_url_caches_url():
288295
assert version1 == version2
289296
assert calls.count(1) == 1 # Only called once
290297

298+
291299
def test_invalidate_url_changes_url_and_version():
292300
responses = [DummyResponse("urlA"), DummyResponse("urlB")]
293301
distributor = _PresignedUrlDistributor(lambda: responses.pop(0))
@@ -298,10 +306,11 @@ def test_invalidate_url_changes_url_and_version():
298306
assert url2.value == "urlB"
299307
assert version2 == version1 + 1
300308

309+
301310
def test_invalidate_url_wrong_version_does_not_invalidate():
302311
distributor = _PresignedUrlDistributor(lambda: DummyResponse("urlX"))
303312
url1, version1 = distributor.get_url()
304313
distributor.invalidate_url(version1 + 1) # Wrong version
305314
url2, version2 = distributor.get_url()
306315
assert url1 is url2
307-
assert version2 == version1
316+
assert version2 == version1

0 commit comments

Comments
 (0)