Implemented live development server

This commit is contained in:
2021-03-12 22:53:02 +01:00
parent 86089a037c
commit 4af18b6c70
7 changed files with 142 additions and 2 deletions

View File

View File

@@ -0,0 +1,23 @@
from watchdog.events import FileSystemEventHandler
from cpl.console.console import Console
from cpl_cli.live_server.live_server import LiveServerThread
class FileChangeHandler(FileSystemEventHandler):
def __init__(self, live_server: LiveServerThread):
FileSystemEventHandler.__init__(self)
self._live_server = live_server
def on_any_event(self, event):
if event.is_directory:
return None
elif event.event_type == 'modified':
# Event is modified, you can process it now
if str(event.src_path).endswith('.py'):
Console.write_line(f'Detected change in {event.src_path}')
self._live_server.kill_application()
self._live_server.start()

View File

@@ -0,0 +1,38 @@
import os
import subprocess
import sys
import threading
from datetime import datetime
from cpl.console import Console
class LiveServerThread(threading.Thread):
def __init__(self, path: str):
threading.Thread.__init__(self)
self._path = path
self._main = ''
self._command = []
@property
def command(self) -> list[str]:
return self._command
@property
def main(self) -> str:
return self._main
def run(self):
self._main = os.path.join(self._path, 'main.py')
if not os.path.isfile(self._main):
Console.error('Entry point main.py does not exist')
return
Console.write_line('Read successfully')
now = datetime.now()
Console.write_line(f'Started at {now.strftime("%Y-%m-%d %H:%M:%S")}\n\n')
self._command = [sys.executable, self._main, ''.join(sys.argv[2:])]
subprocess.run(self._command)