Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions server/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
aionotify
aiofile
aiohttp
sh
31 changes: 5 additions & 26 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -38,34 +33,18 @@ 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))

# Send websocket response
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

Expand Down