Source code for helpers

import gzip
import os
from pathlib import Path


# Kanged from here and there
# e.g. https://stackoverflow.com/a/76733254/1895378
# XXX: Not good at all, doesn't even handle exponential search
[docs] def tail(filename, n): """Return the last *n* lines of a file (supports gzip). .. versionadded:: 0.0.2 """ file_path = Path(filename) if file_path.suffix == ".gz": with gzip.open(file_path, "rb") as f: try: f.seek(-n * 1024, os.SEEK_END) lines = f.readlines()[-n:] return [line.decode("utf-8").rstrip("\n") for line in lines] except EOFError: return "" else: with open(file_path) as f: f.seek(-n * 1024, os.SEEK_END) lines = f.readlines()[-n:] return lines
# XXX: Another hack