32 lines
959 B
Python
32 lines
959 B
Python
from typing import List
|
|
import os
|
|
from line import Line
|
|
|
|
from settings import Settings
|
|
|
|
|
|
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 - Settings.max_line_length())
|
|
offset = offset - offset % Settings.max_line_length() # 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 |