Skip to content

Commit

Permalink
refactor: async_file_obj support async iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Guibod committed Feb 25, 2023
1 parent 37d68ca commit f5ed7dc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/mightstone/ass/compressor/async_file_obj.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, AsyncIterable

from mightstone.ass.compressor.codecs import error_import_usage

Expand Down Expand Up @@ -54,6 +54,22 @@ async def read(self, n: Optional[int] = None):
return b""

buffer_size = n if n else 1024 * 1024
if isinstance(self._afd, AsyncIterable):
async for data in self._afd:
self._buffer += self._decompressor.decompress(data)
if len(self._buffer) >= buffer_size and n is not None:
result = self._buffer[:buffer_size]
self._buffer = self._buffer[buffer_size:]
return result

if hasattr(self._decompressor, "flush"):
data = self._decompressor.flush()
if data:
self._buffer += data
result = self._buffer
self._buffer = b""
return result

while True:
if self._eof:
result = self._buffer
Expand Down
33 changes: 33 additions & 0 deletions src/mightstone/containers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import httpx_cache
import logging.config
from dependency_injector import containers, providers

from .config import Settings
from .services.scryfall import Scryfall


class Container(containers.DeclarativeContainer):
config = providers.Configuration(pydantic_settings=[Settings()])
logging = providers.Resource(
logging.config.dictConfig,
fname="logging.ini",
)

# Gateways
httpx_client = providers.Singleton(
httpx_cache.AsyncClient,
config.database.dsn,
)

# s3_client = providers.Singleton(
# boto3.client,
# service_name="s3",
# aws_access_key_id=config.aws.access_key_id,
# aws_secret_access_key=config.aws.secret_access_key,
# )

# Services
scryfall = providers.Factory(
Scryfall,
client=httpx_client,
)

0 comments on commit f5ed7dc

Please sign in to comment.