From 69cbc95f011e29418cd2c0904245db16b7b0fb64 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Sun, 12 Mar 2023 11:08:27 +0100 Subject: [PATCH] Use anyio instead of aiofiles (#72) --- pyproject.toml | 4 ++-- ypy_websocket/ystore.py | 25 ++++++++++++------------- ypy_websocket/yutils.py | 4 ++-- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c6bc62f..3f00458 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,8 +28,8 @@ classifiers = [ "Programming Language :: Python :: 3.11", ] dependencies = [ - "aiofiles >=22.1.0,<23", - "aiosqlite >=0.17.0,<1", + "anyio >=3.6.2,<4", + "aiosqlite >=0.18.0,<1", "y-py >=0.6.0,<0.7.0", ] diff --git a/ypy_websocket/ystore.py b/ypy_websocket/ystore.py index 9acd01a..e01e94f 100644 --- a/ypy_websocket/ystore.py +++ b/ypy_websocket/ystore.py @@ -7,9 +7,8 @@ from pathlib import Path from typing import AsyncIterator, Callable, Optional, Tuple -import aiofiles # type: ignore -import aiofiles.os # type: ignore -import aiosqlite # type: ignore +import aiosqlite +import anyio import y_py as Y from .yutils import Decoder, get_new_path, write_var_uint @@ -63,12 +62,12 @@ def __init__(self, path: str, metadata_callback: Optional[Callable] = None, log= self.lock = asyncio.Lock() async def check_version(self) -> int: - if not await aiofiles.os.path.exists(self.path): + if not await anyio.Path(self.path).exists(): version_mismatch = True else: version_mismatch = False move_file = False - async with aiofiles.open(self.path, "rb") as f: + async with await anyio.open_file(self.path, "rb") as f: header = await f.read(8) if header == b"VERSION:": version = int(await f.readline()) @@ -83,9 +82,9 @@ async def check_version(self) -> int: if move_file: new_path = await get_new_path(self.path) self.log.warning(f"YStore version mismatch, moving {self.path} to {new_path}") - await aiofiles.os.rename(self.path, new_path) + await anyio.Path(self.path).rename(new_path) if version_mismatch: - async with aiofiles.open(self.path, "wb") as f: + async with await anyio.open_file(self.path, "wb") as f: version_bytes = f"VERSION:{self.version}\n".encode() await f.write(version_bytes) offset = len(version_bytes) @@ -93,10 +92,10 @@ async def check_version(self) -> int: async def read(self) -> AsyncIterator[Tuple[bytes, bytes, float]]: # type: ignore async with self.lock: - if not await aiofiles.os.path.exists(self.path): + if not await anyio.Path(self.path).exists(): raise YDocNotFound offset = await self.check_version() - async with aiofiles.open(self.path, "rb") as f: + async with await anyio.open_file(self.path, "rb") as f: await f.seek(offset) data = await f.read() if not data: @@ -115,9 +114,9 @@ async def read(self) -> AsyncIterator[Tuple[bytes, bytes, float]]: # type: igno async def write(self, data: bytes) -> None: parent = Path(self.path).parent async with self.lock: - await aiofiles.os.makedirs(parent, exist_ok=True) + await anyio.Path(parent).mkdir(parents=True, exist_ok=True) await self.check_version() - async with aiofiles.open(self.path, "ab") as f: + async with await anyio.open_file(self.path, "ab") as f: data_len = write_var_uint(len(data)) await f.write(data_len + data) metadata = await self.get_metadata() @@ -183,7 +182,7 @@ def __init__(self, path: str, metadata_callback: Optional[Callable] = None, log= async def init_db(self): create_db = False move_db = False - if not await aiofiles.os.path.exists(self.db_path): + if not await anyio.Path(self.db_path).exists(): create_db = True else: async with self.lock: @@ -203,7 +202,7 @@ async def init_db(self): if move_db: new_path = await get_new_path(self.db_path) self.log.warning(f"YStore version mismatch, moving {self.db_path} to {new_path}") - await aiofiles.os.rename(self.db_path, new_path) + await anyio.Path(self.db_path).rename(new_path) if create_db: async with self.lock: async with aiosqlite.connect(self.db_path) as db: diff --git a/ypy_websocket/yutils.py b/ypy_websocket/yutils.py index a84aa2c..51b4ffa 100644 --- a/ypy_websocket/yutils.py +++ b/ypy_websocket/yutils.py @@ -3,7 +3,7 @@ from pathlib import Path from typing import Optional -import aiofiles.os # type: ignore +import anyio import y_py as Y @@ -144,7 +144,7 @@ async def get_new_path(path: str) -> str: ext = p.suffix p_noext = p.with_suffix("") i = 1 - dir_list = await aiofiles.os.listdir() + dir_list = [p async for p in anyio.Path().iterdir()] while True: new_path = f"{p_noext}({i}){ext}" if new_path not in dir_list: