add typer

This commit is contained in:
Ross
2024-12-09 09:26:12 +00:00
parent 92289831c6
commit 15f2e4a6e4
+51 -35
View File
@@ -37,7 +37,7 @@ from typing import Optional
from fastapi import Request
from fastapi.responses import RedirectResponse
from starlette.middleware.base import BaseHTTPMiddleware
#import typer
import typer
#typer_app = typer.Typer()
@@ -61,9 +61,11 @@ from starlette.middleware.base import BaseHTTPMiddleware
SOCKET_PATH = 'tcp://localhost:9976'
EXPORT_PATH = Path("C:/uploader/export")
PROCESS_PATH = Path("C:/uploader/to_process")
ANON_PATH = Path("C:/uploader/anon")
WORK_DIR = Path("C:/uploader")
EXPORT_PATH = WORK_DIR / Path("export/")
PROCESS_PATH = WORK_DIR / Path("to_process/")
ANON_PATH = WORK_DIR / Path("anon/")
EXPORT_PATH.mkdir(exist_ok=True, parents=True)
PROCESS_PATH.mkdir(exist_ok=True, parents=True)
@@ -152,34 +154,34 @@ if use_zmq:
logger.error("invalid message")
if use_nng:
async def read_nng_messages():
print("start nng")
try:
with pynng.Pair0(listen=SOCKET_PATH) as sub:
#sub.subscribe('')
while not app.is_stopped:
msg = await sub.arecv_msg()
data = msg.bytes.decode()
logger.debug(f'Received msg {data}')
match data:
case "loaded":
for client in Client.instances.values():
if not client.has_socket_connection:
continue
with client:
ui.notify("Export done", color='positive')
global REFRESH_TRIGGERED
REFRESH_TRIGGERED = True
logger.debug("loaded")
case _:
logger.error("invalid message")
except pynng.exceptions.AddressInUse:
logger.debug("Address in use")
with pynng.Pair0(dial=SOCKET_PATH) as sub:
await sub.asend(b"loaded")
sys.exit(0)
async def read_nng_messages():
print("start nng")
try:
with pynng.Pair0(listen=SOCKET_PATH) as sub:
#sub.subscribe('')
while not app.is_stopped:
msg = await sub.arecv_msg()
data = msg.bytes.decode()
logger.debug(f'Received msg {data}')
match data:
case "loaded":
for client in Client.instances.values():
if not client.has_socket_connection:
continue
with client:
ui.notify("Export done", color='positive')
global REFRESH_TRIGGERED
REFRESH_TRIGGERED = True
logger.debug("loaded")
case _:
logger.error("invalid message")
except pynng.exceptions.AddressInUse:
logger.debug("Address in use")
with pynng.Pair0(dial=SOCKET_PATH) as sub:
await sub.asend(b"loaded")
sys.exit(0)
#asyncio.create_task(read_nng_messages())
#asyncio.create_task(read_nng_messages())
@@ -636,13 +638,27 @@ def main_page():
asyncio.create_task(watch_for_refresh())
if __name__ in {"__main__", "__mp_main__"}:
freeze_support()
if use_zmq:
app.on_startup(read_loop)
def launch_app(work_dir: Path = typer.Option(WORK_DIR, help="Working directory"), nng: bool = typer.Option(use_nng, help="Use nng")):
global WORK_DIR, EXPORT_PATH, PROCESS_PATH, ANON_PATH, use_nng
WORK_DIR = work_dir
EXPORT_PATH = WORK_DIR / Path("export/")
PROCESS_PATH = WORK_DIR / Path("to_process/")
ANON_PATH = WORK_DIR / Path("anon/")
logger.error(ANON_PATH)
if nng:
use_nng = True
if use_nng:
app.on_startup(read_nng_messages)
ui.run(storage_secret="123456", reload=False, show=False)
if __name__ in {"__main__", "__mp_main__"}:
freeze_support()
typer.run(launch_app)