initial commit

This commit is contained in:
2021-10-24 12:47:46 +02:00
commit 58e3983e49
5 changed files with 194 additions and 0 deletions

30
logFileModel.py Normal file
View File

@@ -0,0 +1,30 @@
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