Skip to content

Commit

Permalink
Separate reader functions
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloLec committed Sep 29, 2021
1 parent 55d11fa commit 4538ee6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 74 deletions.
77 changes: 3 additions & 74 deletions livelog/__main__.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,12 @@
# -*- coding: utf-8 -*-

from typing import Union
from sys import argv
from os import system, name, SEEK_END, _exit
from shutil import get_terminal_size
from time import sleep
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, DirModifiedEvent, FileModifiedEvent
from colorama import Style


def tail(file_name: str, lines: int):
"""Emulate tail command behavior by printing n last lines.
Args:
file_name (str): Log file name
lines (int): Number of lines to print
"""

pos = lines + 1
rows = []
with open(file_name) as f:
while len(rows) <= lines:
try:
f.seek(-pos, 2)
except IOError:
f.seek(0)
break
finally:
rows = list(f)
pos *= 2
print("".join(rows[-lines:]))


class ReadFile(FileSystemEventHandler):
"""Watchdog handler to monitor file changes."""

def __init__(self, file: str):
"""ReadFile initialization.
Args:
file (str): Path of file to monitor
"""

self._file = file
self.on_modified(event=None)

def clear(self):
"""Clear terminal."""

_ = system("cls") if name == "nt" else system("clear")

def on_modified(self, event: Union[DirModifiedEvent, FileModifiedEvent, None]):
"""File modification callback.
Args:
event (Union[DirModifiedEvent, FileModifiedEvent, None]): Watchdog event
"""

self.clear()
tail(self._file, get_terminal_size(fallback=(120, 50))[1])
print(Style.RESET_ALL)

from os import _exit
from .reader import start_reader

if __name__ == "__main__":
if len(argv) == 1:
print("! No file specified !")
_exit(1)

file = argv[1]
event_handler = ReadFile(file=file)
observer = Observer()
observer.schedule(event_handler, file, recursive=True)
observer.start()

try:
while True:
sleep(1)

finally:
observer.stop()
observer.join()
start_reader(file=argv[1])
84 changes: 84 additions & 0 deletions livelog/reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-

from typing import Union
from sys import argv
from os import system, name, SEEK_END
from shutil import get_terminal_size
from time import sleep
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler, DirModifiedEvent, FileModifiedEvent
from colorama import Style


def tail(file_name: str, lines: int):
"""Emulate tail command behavior by printing n last lines.
Args:
file_name (str): Log file name
lines (int): Number of lines to print
"""

pos = lines + 1
rows = []
with open(file_name) as f:
while len(rows) <= lines:
try:
f.seek(-pos, 2)
except IOError:
f.seek(0)
break
finally:
rows = list(f)
pos *= 2
print("".join(rows[-lines:]))


class ReadFile(FileSystemEventHandler):
"""Watchdog handler to monitor file changes."""

def __init__(self, file: str):
"""ReadFile initialization.
Args:
file (str): Path of file to monitor
"""

self._file = file
self.on_modified(event=None)

def clear(self):
"""Clear terminal."""

_ = system("cls") if name == "nt" else system("clear")

def on_modified(self, event: Union[DirModifiedEvent, FileModifiedEvent, None]):
"""File modification callback.
Args:
event (Union[DirModifiedEvent, FileModifiedEvent, None]): Watchdog event
"""

self.clear()
tail(self._file, get_terminal_size(fallback=(120, 50))[1])
print(Style.RESET_ALL)


def start_reader(file: str):
"""Start reader process.
Args:
file (str): File to be read
"""

event_handler = ReadFile(file=file)
observer = Observer()
observer.schedule(event_handler, file, recursive=True)
observer.start()

try:
while True:
sleep(1)

finally:
observer.stop()
observer.join()

0 comments on commit 4538ee6

Please sign in to comment.