Implemented live development server
This commit is contained in:
0
src/cpl_cli/live_server/__init__.py
Normal file
0
src/cpl_cli/live_server/__init__.py
Normal file
23
src/cpl_cli/live_server/file_change_handler.py
Normal file
23
src/cpl_cli/live_server/file_change_handler.py
Normal 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()
|
38
src/cpl_cli/live_server/live_server.py
Normal file
38
src/cpl_cli/live_server/live_server.py
Normal 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)
|
Reference in New Issue
Block a user