Skip to content

Commit

Permalink
Add size option to FilelikeObjectAdapter
Browse files Browse the repository at this point in the history
fix: #59

Because in resumable uploading we need to upload chunks of the file stream.
size option is need for FilelikeObjectAdapter

1. Add `size` option for FilelikeObjectAdapter
2. Add a unit test for FilelikeObjectAdapter's reading.
3. modify flake8 address from gitlab to github
  • Loading branch information
karajan1001 committed Dec 4, 2022
1 parent 03bc698 commit 42d5a4c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
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"

0 comments on commit 42d5a4c

Please sign in to comment.