Skip to content

Commit

Permalink
fix compatibility layer code
Browse files Browse the repository at this point in the history
  • Loading branch information
fdev31 committed Jun 11, 2024
1 parent c19983f commit aa913ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions pyprland/aioops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@

__all__ = ["aiopen", "aiexists", "ailistdir"]

import contextlib
import io

try:
import aiofiles.os
from aiofiles import open as aiopen
from aiofiles.os import listdir as ailistdir
from aiofiles.path import exists as aiexists

aiexists = aiofiles.os.path.exists
except ImportError:
import os

async def aiopen(*args, **kwargs) -> io.TextIOWrapper:
"""Async > sync wrapper."""
f = open(*args, **kwargs) # noqa
_orig_readlines = f.readlines
class AsyncFile:
def __init__(self, file: io.TextIOWrapper):
self.file = file

async def readlines(self) -> list[str]:
return self.file.readlines()

async def __aenter__(self):
return self

async def _new_readlines(*args, **kwargs) -> list[str]:
return _orig_readlines(*args, **kwargs)
async def __aexit__(self, exc_type, exc_val, exc_tb):
self.file.close()

f.readlines = _new_readlines
return f
@contextlib.asynccontextmanager
async def aiopen(*args, **kwargs) -> AsyncFile:
yield AsyncFile(open(*args, **kwargs))

async def aiexists(*args, **kwargs) -> bool:
"""Async > sync wrapper."""
Expand Down
2 changes: 1 addition & 1 deletion pyprland/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Package version."""

VERSION = "2.3.7-3"
VERSION = "2.3.7-4"

0 comments on commit aa913ec

Please sign in to comment.