Skip to content

Commit

Permalink
Actually fix AsyncPath.read_text() this time
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdelorenzo committed Mar 9, 2021
1 parent 5932d20 commit 9f7e123
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
27 changes: 9 additions & 18 deletions aiopath/handle.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations
from typing import AsyncIterable, Union, List
from typing import AsyncIterable, Union
from pathlib import Path
import io

from aiofile import AIOFile, LineReader


BEGINNING: int = 0
NO_SIZE: int = 0
CHUNK_SIZE: int = 1_096

SEP: str = '\n'
Expand All @@ -23,14 +24,6 @@ async def read_lines(
errors: str = ERRORS,
**kwargs
) -> AsyncIterable[str]:
buffer = io.BytesIO()

wrapper = io.TextIOWrapper(
buffer,
encoding=encoding,
errors=errors,
)

if hasattr(path, 'resolve'):
try:
path = str(await path.resolve())
Expand All @@ -46,7 +39,7 @@ async def read_lines(
offset=offset
)

#### Python 3.8+
# Python 3.8+
# while line := await reader.readline():
# buffer.write(line)
# buffer.seek(BEGINNING)
Expand All @@ -59,9 +52,7 @@ async def read_lines(
if not line:
break

buffer.write(line)
buffer.seek(BEGINNING)
yield wrapper.readline()
yield line.decode(encoding, errors=errors)


async def read_full_file(
Expand All @@ -75,11 +66,11 @@ async def read_full_file(
) -> str:
lines_gen = read_lines(
path,
line_sep,
chunk_size,
offset,
encoding,
errors
line_sep=line_sep,
chunk_size=chunk_size,
offset=offset,
encoding=encoding,
errors=errors
)

with io.StringIO() as string:
Expand Down
9 changes: 5 additions & 4 deletions aiopath/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
from .selectors import _make_selector
from .flavours import _async_windows_flavour, _async_posix_flavour
from .wrap import coro_as_method_coro, func_as_method_coro, to_thread
from .handle import read_lines, read_full_file
from .handle import read_full_file


DEFAULT_ENCODING: str = 'utf-8'
ON_ERRORS: str = 'ignore'


getcwd = func_as_corofunc(os.getcwd)
Expand Down Expand Up @@ -145,7 +146,7 @@ def open(
mode: str = 'r',
buffering: int = -1,
encoding: Optional[str] = DEFAULT_ENCODING,
errors: Optional[str] = None,
errors: Optional[str] = ON_ERRORS,
newline: Optional[str] = None
) -> AIOFile:
return AIOFile(
Expand All @@ -157,7 +158,7 @@ def open(
async def read_text(
self,
encoding: Optional[str] = DEFAULT_ENCODING,
errors: Optional[str] = None
errors: Optional[str] = ON_ERRORS
) -> str:
path = str(await self.resolve())

Expand All @@ -184,7 +185,7 @@ async def write_text(
self,
data: str,
encoding: Optional[str] = 'utf-8',
errors: Optional[str] = None,
errors: Optional[str] = ON_ERRORS,
newline: Optional[str] = None
) -> int:
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

PKG_NAME = "aiopath"
NAME = 'aiopath'
VERSION = "0.2.5"
VERSION = "0.2.6"
LICENSE = "LGPL-3.0"

DESC = "📁 Async pathlib for Python"
Expand Down

0 comments on commit 9f7e123

Please sign in to comment.