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

Replace BinaryIO with IO[bytes] #644

Merged
merged 7 commits into from
May 20, 2024
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
10 changes: 5 additions & 5 deletions aiodocker/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import (
Any,
AsyncIterator,
BinaryIO,
Dict,
List,
Mapping,
Expand All @@ -17,6 +16,7 @@
from typing_extensions import Literal

from .jsonstream import json_stream_list, json_stream_stream
from .types import SupportsRead
from .utils import clean_map, compose_auth_header


Expand Down Expand Up @@ -226,7 +226,7 @@ async def delete(
return await self.docker._query_json(f"images/{name}", "DELETE", params=params)

@staticmethod
async def _stream(fileobj: BinaryIO) -> AsyncIterator[bytes]:
async def _stream(fileobj: SupportsRead[bytes]) -> AsyncIterator[bytes]:
chunk = fileobj.read(io.DEFAULT_BUFFER_SIZE)
while chunk:
yield chunk
Expand All @@ -237,7 +237,7 @@ async def build(
self,
*,
remote: Optional[str] = None,
fileobj: Optional[BinaryIO] = None,
fileobj: Optional[SupportsRead[bytes]] = None,
path_dockerfile: Optional[str] = None,
tag: Optional[str] = None,
quiet: bool = False,
Expand All @@ -257,7 +257,7 @@ def build(
self,
*,
remote: Optional[str] = None,
fileobj: Optional[BinaryIO] = None,
fileobj: Optional[SupportsRead[bytes]] = None,
path_dockerfile: Optional[str] = None,
tag: Optional[str] = None,
quiet: bool = False,
Expand All @@ -276,7 +276,7 @@ def build(
self,
*,
remote: Optional[str] = None,
fileobj: Optional[BinaryIO] = None,
fileobj: Optional[SupportsRead[bytes]] = None,
path_dockerfile: Optional[str] = None,
tag: Optional[str] = None,
quiet: bool = False,
Expand Down
11 changes: 11 additions & 0 deletions aiodocker/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import (
Protocol,
TypeVar,
)


_T_co = TypeVar("_T_co", covariant=True)


class SupportsRead(Protocol[_T_co]):
def read(self, length: int = ..., /) -> _T_co: ...
13 changes: 6 additions & 7 deletions aiodocker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import (
IO,
Any,
BinaryIO,
Iterable,
Mapping,
MutableMapping,
Expand Down Expand Up @@ -220,7 +219,7 @@ def clean_filters(filters: Optional[Mapping] = None) -> str:
return json.dumps(filters)


def mktar_from_dockerfile(fileobject: BinaryIO) -> IO:
def mktar_from_dockerfile(fileobj: Union[BytesIO, IO[bytes]]) -> IO[bytes]:
"""
Create a zipped tar archive from a Dockerfile
**Remember to close the file object**
Expand All @@ -233,14 +232,14 @@ def mktar_from_dockerfile(fileobject: BinaryIO) -> IO:
f = tempfile.NamedTemporaryFile()
t = tarfile.open(mode="w:gz", fileobj=f)

if isinstance(fileobject, BytesIO):
if isinstance(fileobj, BytesIO):
dfinfo = tarfile.TarInfo("Dockerfile")
dfinfo.size = len(fileobject.getvalue())
fileobject.seek(0)
dfinfo.size = len(fileobj.getvalue())
fileobj.seek(0)
else:
dfinfo = t.gettarinfo(fileobj=fileobject, arcname="Dockerfile")
dfinfo = t.gettarinfo(fileobj=fileobj, arcname="Dockerfile")

t.addfile(dfinfo, fileobject)
t.addfile(dfinfo, fileobj)
t.close()
f.seek(0)
return f
Expand Down
Loading