From 49877acff4d4464d8f60ca78bebd662eccf291b3 Mon Sep 17 00:00:00 2001 From: bsq Date: Sun, 2 Oct 2022 11:27:54 +0700 Subject: [PATCH] Replace aiofile with sh tail --- server/requirements.txt | 3 +-- server/server.py | 31 +++++-------------------------- 2 files changed, 6 insertions(+), 28 deletions(-) diff --git a/server/requirements.txt b/server/requirements.txt index 79aff91..4999119 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,3 +1,2 @@ -aionotify -aiofile aiohttp +sh \ No newline at end of file diff --git a/server/server.py b/server/server.py index 719cf93..e59e0bd 100755 --- a/server/server.py +++ b/server/server.py @@ -17,13 +17,8 @@ # under the License. from pathlib import Path -from contextlib import closing -from asyncio import get_event_loop - from aiohttp import web -from aionotify import Watcher, Flags -from aiofile import AIOFile, LineReader - +from sh import tail async def handle(request): @@ -38,6 +33,7 @@ async def handle(request): cleanfn = Path(rawfn).name fnpath = Path().cwd() / cleanfn + if not fnpath.is_file(): raise web.HTTPNotFound(reason='File {} not found'.format(rawfn)) @@ -45,27 +41,10 @@ async def handle(request): ws = web.WebSocketResponse() await ws.prepare(request) - # Create watcher for file - with closing(Watcher()) as watcher: - watcher.watch(path=str(fnpath), flags=Flags.MODIFY) - - await watcher.setup(get_event_loop()) - print('Watcher created for "{}"'.format(fnpath)) - - async with AIOFile(fnpath, mode='r', encoding='utf-8') as afd: - print('Sending lines!') - reader = LineReader(afd) - - async for line in reader: - print(line, end='') - await ws.send_str(line) + - while True: - event = await watcher.get_event() - print('Got event! {}'.format(event)) - async for line in reader: - print(line, end='') - await ws.send_str(line) + for line in tail("-f", fnpath, _iter=True): + await ws.send_str(line) return ws