from typing import List import os from line import Line class LogFileModel: def __init__(self, file): self._file = file def data(self, byte_offset:int, lines: int) -> List[Line]: result : List[Line] = [] # TODO handle lines longer than 4096 bytes with open(self._file, 'r') as f: offset = max(0, byte_offset - 4096) offset = offset - offset % 4096 # align to blocks of 4kb f.seek(offset) while l := f.readline(): new_offset = f.tell() if offset >= byte_offset: line = Line(offset, new_offset-1, l) result.append(line) offset = new_offset if len(result) >= lines: break return result def byte_count(self) -> int: return os.stat(self._file).st_size