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

Use anyio instead of aiofiles #72

Merged
merged 1 commit into from
Mar 12, 2023
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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
25 changes: 12 additions & 13 deletions ypy_websocket/ystore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -83,20 +82,20 @@ 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)
return offset

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:
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions ypy_websocket/yutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from typing import Optional

import aiofiles.os # type: ignore
import anyio
import y_py as Y


Expand Down Expand Up @@ -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:
Expand Down