intermediate state after fixing the logfilemodel test

This commit is contained in:
2022-12-17 10:06:15 +01:00
parent 5df0dc0c82
commit a06a2d01f3
5 changed files with 87 additions and 38 deletions

View File

@@ -73,7 +73,7 @@ class LogFileModel:
offset = new_offset
def read_word_at(self, byte_offset: int) -> (str, int, int):
lines = self.data(byte_offset, 0, 1)
lines = self.data(byte_offset, 0, 1, 0, -1)
if len(lines) == 0:
return "", -1, -1
line: Line = lines[0]
@@ -99,7 +99,7 @@ class LogFileModel:
def _is_word_char(self, char: str) -> bool:
return re.match(r"\w", char) is not None
def data(self, byte_offset: int, scroll_lines: int, lines: int) -> List[Line]:
def data(self, byte_offset: int, scroll_lines: int, lines: int, range_start: int, range_end: int) -> List[Line]:
# print("data(%s, %s, %s)" % (byte_offset, scroll_lines, lines))
lines_before_offset: List[Line] = []
lines_after_offset: List[Line] = []
@@ -107,19 +107,23 @@ class LogFileModel:
lines_to_return = math.ceil(lines)
# TODO handle lines longer than 4096 bytes
# TODO abort file open after a few secons: https://docs.python.org/3/library/signal.html#example
# TODO abort file open after a few seconds: https://docs.python.org/3/library/signal.html#example
with open(self._file, 'rb') as f:
offset = min(byte_offset, self.byte_count())
# print("offset: %s byte_count: %d" % (offset, self.byte_count()))
offset = max(0, offset - self.settings.max_line_length())
offset = max(0,
max(range_start - self.settings.max_line_length(), offset - self.settings.max_line_length()))
f.seek(offset)
while l := f.readline():
new_offset = f.tell()
if 0 <= range_end < new_offset:
break
line = Line(offset, new_offset, l.decode("utf8", errors="ignore"))
if line.byte_end() <= byte_offset: # line.byte_end() returns the end byte +1
lines_before_offset.append(line)
if line.byte_offset() >= range_start: # only add if in range
lines_before_offset.append(line)
else:
lines_after_offset.append(line)
offset = f.tell()