Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add size option to FilelikeObjectAdapter #60

Merged
merged 1 commit into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
- flake8-comprehensions
- flake8-debugger
- flake8-string-format
repo: https://gitlab.com/pycqa/flake8
repo: https://github.com/PyCQA/flake8
rev: 5.0.4
- repo: local
hooks:
Expand Down
16 changes: 13 additions & 3 deletions aiooss2/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,26 @@ def __init__(self, stream, **kwargs):
except AttributeError:
pass

def _length_to_read(self, amt: int) -> int:
if self.size is None:
return amt
length_to_read = self.size - self.offset
if amt > 0:
length_to_read = min(amt, length_to_read)
return length_to_read

async def read(self, amt: int = -1) -> Awaitable[bytes]:
if self._read_all:
if self._read_all or (self.size and self.offset >= self.size):
return b""
if self.offset < self.discard and amt:
amt += self.discard - self.offset

length_to_read = self._length_to_read(amt)

if inspect.iscoroutinefunction(self.stream.read):
content = await self.stream.read(amt)
content = await self.stream.read(length_to_read)
else:
content = self.stream.read(amt)
content = self.stream.read(length_to_read)
if not content:
self._read_all = True
return self._invoke_callbacks(content)
Expand Down
6 changes: 3 additions & 3 deletions aiooss2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import copy
import logging
from typing import TYPE_CHECKING, Awaitable, Optional
from typing import TYPE_CHECKING, Awaitable

from oss2.exceptions import ClientError
from oss2.headers import (
Expand Down Expand Up @@ -166,11 +166,11 @@ def client_crc(self):
return self.stream.crc
return None

async def read(self, amt: Optional[int] = None) -> Awaitable[bytes]:
async def read(self, amt: int = -1) -> Awaitable[bytes]:
"""async read data from stream

Args:
amt (int, optional): batch size of the data to read
amt int: batch size of the data to read

Returns:
Awaitable[bytes]:
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

if TYPE_CHECKING:
from oss2 import Bucket
from py.path import local

from aiooss2.api import AioBucket

Expand Down Expand Up @@ -140,3 +141,17 @@ def test_make_adapter_error():
assert make_adapter(
["data1", "data2", "data3"], discard=1, enable_crc=True
)


def test_adapter_read(
tmpdir: "local",
):
data = b"123456789"
file = tmpdir / "file"
file.write(data)

with open(str(file), "rb") as f_r:
f_r.seek(3, os.SEEK_SET)
adaptor = FilelikeObjectAdapter(f_r, size=5)
result = asyncio.run(adaptor.read())
assert result == b"45678"