-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |