Skip to content

Commit

Permalink
Use aiofiles.open() instead of open()
Browse files Browse the repository at this point in the history
  • Loading branch information
ivknv committed Jan 30, 2023
1 parent 5d9f78c commit 33b0569
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
aiohttp
aiofiles
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
author_email="[email protected]",
license="LGPLv3",
python_requires=">=3.8",
install_requires=["aiohttp"],
tests_require=["aiofiles"],
install_requires=["aiohttp", "aiofiles"],
url="https://github.com/ivknv/yadisk-async",
project_urls={"Source code": "https://github.com/ivknv/yadisk-async",
"Documentation (EN)": "https://yadisk-async.readthedocs.io/en/latest",
Expand Down
15 changes: 9 additions & 6 deletions yadisk_async/yadisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
from . import settings
from .session import SessionWithHeaders
from .api import *
from .exceptions import InvalidResponseError, UnauthorizedError, OperationNotFoundError
from .exceptions import PathNotFoundError, WrongResourceTypeError
from .exceptions import (
InvalidResponseError, UnauthorizedError, OperationNotFoundError,
PathNotFoundError, WrongResourceTypeError)
from .utils import get_exception, auto_retry
from .objects import ResourceLinkObject, PublicResourceLinkObject

from typing import Any, Optional, Union, IO, TYPE_CHECKING
from .compat import Callable, AsyncGenerator, List, Awaitable, Dict

import aiofiles

if TYPE_CHECKING:
from .objects import (
TokenObject, TokenRevokeStatusObject, DiskInfoObject,
Expand Down Expand Up @@ -683,7 +686,7 @@ async def _upload(self,
try:
if isinstance(file_or_path, (str, bytes)):
close_file = True
file = open(file_or_path, "rb")
file = await aiofiles.open(file_or_path, "rb")
elif inspect.isasyncgenfunction(file_or_path):
generator_factory = file_or_path
else:
Expand Down Expand Up @@ -737,7 +740,7 @@ async def attempt():
await auto_retry(attempt, n_retries, retry_interval)
finally:
if close_file and file is not None:
file.close()
await file.close()

async def upload(self,
path_or_file: FileOrPath,
Expand Down Expand Up @@ -849,7 +852,7 @@ async def _download(self,
try:
if isinstance(file_or_path, (str, bytes)):
close_file = True
file = open(file_or_path, "wb")
file = await aiofiles.open(file_or_path, "wb")
else:
close_file = False
file = file_or_path
Expand Down Expand Up @@ -893,7 +896,7 @@ async def attempt() -> None:
return await auto_retry(attempt, n_retries, retry_interval)
finally:
if close_file and file is not None:
file.close()
await file.close()

async def download(self,
src_path: str,
Expand Down

0 comments on commit 33b0569

Please sign in to comment.