| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
from ._beta_functions import ToolError |
| 7 |
|
| 8 |
__all__ = ["READ_STREAM_CHUNK_BYTES", "LineRangeCollector", "read_line_range", "read_text_exact", "write_text_exact"] |
| 9 |
|
| 10 |
READ_STREAM_CHUNK_BYTES = 64 * 1024 |
| 11 |
|
| 12 |
|
| 13 |
def read_text_exact(path: Path) -> str: |
| 14 |
"""Read a UTF-8 file without newline translation.""" |
| 15 |
return path.read_bytes().decode("utf-8") |
| 16 |
|
| 17 |
|
| 18 |
def write_text_exact(path: Path, text: str) -> None: |
| 19 |
"""Write a UTF-8 file without newline translation.""" |
| 20 |
path.write_bytes(text.encode("utf-8")) |
| 21 |
|
| 22 |
|
| 23 |
def read_line_range(path: Path, file_path: str, start_line: int, end_line: int, limit: int) -> str: |
| 24 |
"""Return lines `[start_line, end_line]` of `path`, capping the selected bytes at `limit`.""" |
| 25 |
lines = LineRangeCollector(file_path=file_path, start_line=start_line, end_line=end_line, limit=limit) |
| 26 |
if lines.range_is_empty(): |
| 27 |
return "" |
| 28 |
with path.open("rb") as f: |
| 29 |
while chunk := f.read(READ_STREAM_CHUNK_BYTES): |
| 30 |
lines.collect_from(chunk) |
| 31 |
if lines.range_is_collected(): |
| 32 |
break |
| 33 |
return lines.text() |
| 34 |
|
| 35 |
|
| 36 |
class LineRangeCollector: |
| 37 |
"""Collects the bytes of lines `[start_line, end_line]` from consecutive file chunks, capped at `limit`.""" |
| 38 |
|
| 39 |
def __init__(self, *, file_path: str, start_line: int, end_line: int, limit: int) -> None: |
| 40 |
self._file_path = file_path |
| 41 |
self._start_line = start_line |
| 42 |
self._end_line = end_line |
| 43 |
self._start = max(0, start_line - 1) |
| 44 |
self._end = end_line if end_line > 0 else sys.maxsize |
| 45 |
self._limit = limit |
| 46 |
self._line = 0 |
| 47 |
self._collected = bytearray() |
| 48 |
|
| 49 |
def range_is_empty(self) -> bool: |
| 50 |
return self._end <= self._start |
| 51 |
|
| 52 |
def range_is_collected(self) -> bool: |
| 53 |
return self._line >= self._end |
| 54 |
|
| 55 |
def collect_from(self, chunk: bytes) -> None: |
| 56 |
line_start = 0 |
| 57 |
while line_start < len(chunk) and not self.range_is_collected(): |
| 58 |
newline = chunk.find(b"\n", line_start) |
| 59 |
line_end = len(chunk) if newline < 0 else newline |
| 60 |
if self._line >= self._start: |
| 61 |
self._collect(chunk[line_start:line_end], newline_terminated=newline >= 0) |
| 62 |
if newline < 0: |
| 63 |
break |
| 64 |
self._line += 1 |
| 65 |
line_start = newline + 1 |
| 66 |
|
| 67 |
def _collect(self, line_bytes: bytes, *, newline_terminated: bool) -> None: |
| 68 |
self._collected += line_bytes |
| 69 |
if newline_terminated and self._line + 1 < self._end: |
| 70 |
self._collected += b"\n" |
| 71 |
if len(self._collected) > self._limit: |
| 72 |
raise self._over_limit_error() |
| 73 |
|
| 74 |
def _over_limit_error(self) -> ToolError: |
| 75 |
if self._end - self._start == 1: |
| 76 |
return ToolError( |
| 77 |
f"read: line {self._start + 1} of {self._file_path} alone exceeds {self._limit}-byte limit. " |
| 78 |
"The read tool cannot return part of a line, so view_range cannot narrow this further." |
| 79 |
) |
| 80 |
return ToolError( |
| 81 |
f"read: view_range [{self._start_line}, {self._end_line}] of {self._file_path} exceeds {self._limit}-byte limit. " |
| 82 |
"Narrow the view_range to read a smaller portion." |
| 83 |
) |
| 84 |
|
| 85 |
def text(self) -> str: |
| 86 |
return self._collected.decode("utf-8") |