Skip to content

Commit

Permalink
Merge pull request #506 from reef-technologies/truncation_fix
Browse files Browse the repository at this point in the history
TruncatedOutput fix
  • Loading branch information
mjurbanski-reef authored Jul 30, 2024
2 parents 42b1431 + 3d8b680 commit 7535c7a
Show file tree
Hide file tree
Showing 23 changed files with 624 additions and 156 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ env:

jobs:
lint:
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -35,6 +36,7 @@ jobs:
- name: Changelog validation
run: nox -vs towncrier_check
build:
timeout-minutes: 30
needs: lint
runs-on: ubuntu-latest
steps:
Expand All @@ -51,6 +53,7 @@ jobs:
- name: Build the distribution
run: nox -vs build
cleanup_buckets:
timeout-minutes: 30
needs: lint
env:
B2_TEST_APPLICATION_KEY: ${{ secrets.B2_TEST_APPLICATION_KEY }}
Expand All @@ -74,6 +77,7 @@ jobs:
if: ${{ env.B2_TEST_APPLICATION_KEY != '' && env.B2_TEST_APPLICATION_KEY_ID != '' }} # TODO: skip this whole job instead
run: nox -vs cleanup_old_buckets
test:
timeout-minutes: 90
needs: cleanup_buckets
env:
B2_TEST_APPLICATION_KEY: ${{ secrets.B2_TEST_APPLICATION_KEY }}
Expand Down Expand Up @@ -126,6 +130,7 @@ jobs:
if: ${{ env.B2_TEST_APPLICATION_KEY != '' && env.B2_TEST_APPLICATION_KEY_ID != '' }}
run: nox -vs integration -- --dont-cleanup-old-buckets -v
doc:
timeout-minutes: 30
needs: build
runs-on: ubuntu-latest
steps:
Expand Down
10 changes: 5 additions & 5 deletions b2sdk/_internal/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def upload_bytes(
self,
data_bytes,
file_name,
content_type=None,
content_type: str | None = None,
file_info: dict | None = None,
progress_listener=None,
encryption: EncryptionSetting | None = None,
Expand All @@ -568,7 +568,7 @@ def upload_bytes(
content_disposition: str | None = None,
content_encoding: str | None = None,
content_language: str | None = None,
):
) -> FileVersion:
"""
Upload bytes in memory to a B2 file.
Expand Down Expand Up @@ -867,7 +867,7 @@ def upload(
self,
upload_source,
file_name,
content_type=None,
content_type: str | None = None,
file_info=None,
min_part_size: int | None = None,
progress_listener=None,
Expand Down Expand Up @@ -945,7 +945,7 @@ def create_file(
self,
write_intents,
file_name,
content_type=None,
content_type: str | None = None,
file_info=None,
progress_listener=None,
recommended_upload_part_size=None,
Expand Down Expand Up @@ -1124,7 +1124,7 @@ def _create_file(
emerger_method,
write_intents_iterable,
file_name,
content_type=None,
content_type: str | None = None,
file_info=None,
progress_listener=None,
recommended_upload_part_size=None,
Expand Down
21 changes: 10 additions & 11 deletions b2sdk/_internal/file_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .replication.types import ReplicationStatus
from .utils import Sha1HexDigest, b2_url_decode
from .utils.http_date import parse_http_date
from .utils.range_ import Range
from .utils.range_ import EMPTY_RANGE, Range

if TYPE_CHECKING:
from .api import B2Api
Expand Down Expand Up @@ -423,8 +423,8 @@ def __init__(
content_disposition: str | None,
content_length: int,
content_language: str | None,
expires,
cache_control,
expires: str | None,
cache_control: str | None,
content_encoding: str | None,
file_retention: FileRetentionSetting = NO_RETENTION_FILE_SETTING,
legal_hold: LegalHold = LegalHold.UNSET,
Expand Down Expand Up @@ -589,11 +589,9 @@ def __init__(self, api: B2Api):
self.api = api

@classmethod
def range_and_size_from_header(cls, header):
raw_range, raw_size = header.split('/')
range_ = Range.from_header(raw_range)
size = int(raw_size)

def range_and_size_from_header(cls, header: str) -> tuple[Range, int]:
range_, size = Range.from_header_with_size(header)
assert size is not None, 'Total length was expected in Content-Range header'
return range_, size

@classmethod
Expand All @@ -609,12 +607,13 @@ def file_info_from_headers(cls, headers: dict) -> dict:
def from_response_headers(self, headers):
file_info = self.file_info_from_headers(headers)

if 'Content-Range' in headers:
range_, size = self.range_and_size_from_header(headers['Content-Range'])
content_range_header_value = headers.get('Content-Range')
if content_range_header_value:
range_, size = self.range_and_size_from_header(content_range_header_value)
content_length = int(headers['Content-Length'])
else:
size = content_length = int(headers['Content-Length'])
range_ = Range(0, max(size - 1, 0))
range_ = Range(0, size - 1) if size else EMPTY_RANGE

return DownloadVersion(
api=self.api,
Expand Down
18 changes: 9 additions & 9 deletions b2sdk/_internal/raw_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ def create_key(
@abstractmethod
def download_file_from_url(
self,
account_auth_token_or_none,
url,
range_=None,
account_auth_token_or_none: str | None,
url: str,
range_: tuple[int, int] | None = None,
encryption: EncryptionSetting | None = None,
):
pass
Expand Down Expand Up @@ -645,17 +645,17 @@ def delete_key(self, api_url, account_auth_token, application_key_id):

def download_file_from_url(
self,
account_auth_token_or_none,
url,
range_=None,
account_auth_token_or_none: str | None,
url: str,
range_: tuple[int, int] | None = None,
encryption: EncryptionSetting | None = None,
):
"""
Issue a streaming request for download of a file, potentially authorized.
:param str account_auth_token_or_none: an optional account auth token to pass in
:param str url: the full URL to download from
:param tuple range: two-element tuple for http Range header
:param account_auth_token_or_none: an optional account auth token to pass in
:param url: the full URL to download from
:param range_: two-element tuple for http Range header
:param b2sdk.v2.EncryptionSetting encryption: encryption settings for downloading
:return: b2_http response
"""
Expand Down
22 changes: 15 additions & 7 deletions b2sdk/_internal/raw_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ def sort_key(self):
"""
return (self.name, self.file_id)

def as_download_headers(self, account_auth_token_or_none, range_=None):
def as_download_headers(
self, account_auth_token_or_none: str | None = None, range_: tuple[int, int] | None = None
) -> dict[str, str]:
if self.data_bytes is None:
content_length = 0
elif range_ is not None:
Expand All @@ -241,10 +243,10 @@ def as_download_headers(self, account_auth_token_or_none, range_=None):
content_length = len(self.data_bytes)
headers = CaseInsensitiveDict(
{
'content-length': content_length,
'content-length': str(content_length),
'content-type': self.content_type,
'x-bz-content-sha1': self.content_sha1,
'x-bz-upload-timestamp': self.upload_timestamp,
'x-bz-upload-timestamp': str(self.upload_timestamp),
'x-bz-file-id': self.file_id,
'x-bz-file-name': b2_url_encode(self.name),
}
Expand Down Expand Up @@ -514,6 +516,12 @@ def request(self):
def close(self):
pass

def __enter__(self):
return self

def __exit__(self, *args):
self.close()


class BucketSimulator:

Expand Down Expand Up @@ -1494,10 +1502,10 @@ def delete_bucket(self, api_url, account_auth_token, account_id, bucket_id):

def download_file_from_url(
self,
account_auth_token_or_none,
url,
range_=None,
encryption: EncryptionSetting | None = None
account_auth_token_or_none: str | None,
url: str,
range_: tuple[int, int] | None = None,
encryption: EncryptionSetting | None = None,
):
# TODO: check auth token if bucket is not public
matcher = self.DOWNLOAD_URL_MATCHER.match(url)
Expand Down
7 changes: 6 additions & 1 deletion b2sdk/_internal/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ def delete_file_version(self, file_id, file_name, bypass_governance: bool = Fals
self.raw_api.delete_file_version, file_id, file_name, bypass_governance
)

def download_file_from_url(self, url, range_=None, encryption: EncryptionSetting | None = None):
def download_file_from_url(
self,
url: str,
range_: tuple[int, int] | None = None,
encryption: EncryptionSetting | None = None,
):
return self._wrap_token(
self.raw_api.download_file_from_url,
TokenType.API_TOKEN_ONLY,
Expand Down
10 changes: 6 additions & 4 deletions b2sdk/_internal/transfer/inbound/download_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from b2sdk._internal.exception import (
InvalidRange,
)
from b2sdk._internal.progress import DoNothingProgressListener
from b2sdk._internal.progress import AbstractProgressListener, DoNothingProgressListener
from b2sdk._internal.utils import B2TraceMetaAbstract

from ...utils.thread_pool import ThreadPoolMixin
Expand Down Expand Up @@ -78,12 +78,14 @@ def __init__(

def download_file_from_url(
self,
url,
progress_listener=None,
range_=None,
url: str,
progress_listener: AbstractProgressListener | None = None,
range_: tuple[int, int] | None = None,
encryption: EncryptionSetting | None = None,
) -> DownloadedFile:
"""
Download file by URL.
:param url: url from which the file should be downloaded
:param progress_listener: where to notify about downloading progress
:param range_: 2-element tuple containing data of http Range header
Expand Down
Loading

0 comments on commit 7535c7a

Please sign in to comment.