-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigscan_app.py
More file actions
70 lines (50 loc) · 1.91 KB
/
Copy pathzigscan_app.py
File metadata and controls
70 lines (50 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""Entry point for the packaged app.
Two jobs in one binary, chosen by argv:
(no arguments) start the console and open the technician's browser
--capture-worker run one capture and exit
The second exists because the console runs captures in a separate process, so a
hung radio cannot take the whole app with it. Frozen, `sys.executable` is this
binary and there is no .py file to hand it — so it re-invokes itself with a flag
instead. See paths.worker_argv().
"""
from __future__ import annotations
import os
import sys
import threading
import time
import webbrowser
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "tools"))
import paths # noqa: E402
def run_worker() -> int:
import capture # noqa: PLC0415
sys.argv = [sys.argv[0], *sys.argv[2:]]
return capture.main()
def run_console() -> int:
import console # noqa: PLC0415
paths.ensure_data()
port = int(os.environ.get("ZIGSCAN_PORT", "8477"))
url = f"http://127.0.0.1:{port}/"
# Open the browser from a thread, after the server is actually accepting
# connections. Opening it first shows the technician a connection error and
# teaches them the tool is broken.
def open_when_up() -> None:
import socket # noqa: PLC0415
for _ in range(60):
with socket.socket() as probe:
if probe.connect_ex(("127.0.0.1", port)) == 0:
webbrowser.open(url)
return
time.sleep(0.25)
threading.Thread(target=open_when_up, daemon=True).start()
print(f"zigscan — {url}")
print(f"capturas en {paths.CAPTURES}")
sys.argv = [sys.argv[0], "--port", str(port)]
return console.main()
def main() -> int:
if len(sys.argv) > 1 and sys.argv[1] == "--capture-worker":
return run_worker()
return run_console()
if __name__ == "__main__":
sys.exit(main())