Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·198 lines (171 loc) · 8.1 KB
/
Copy pathapp.py
File metadata and controls
executable file
·198 lines (171 loc) · 8.1 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
"""TelescopeYoke, the application: one window, no terminal and no browser.
./app.py the application
./app.py --demo the same with a simulated mount and a made-up run
./app.py --rig heq5 the same for one of several telescopes: its own settings and files
./app.py --new-rig heq5 make that rig's settings and put it in the applications menu
It is what the launcher in the applications menu starts (install.sh puts it
there; on Windows, install.ps1 makes the Start Menu shortcut). It starts the
console's server inside this program, on a free port of this computer only,
and shows its page in a window of its own. Closing the window ends whatever
is running and tells the mount to stop.
The window is made with whatever the system already has: GTK and WebKit on
Ubuntu, the WebView2 control through pywebview on Windows, and failing those
the installed Chrome, Chromium or Edge in its application mode. Only if none
of them is there does it fall back to a tab in the ordinary browser.
"""
import os
import sys
if "--demo" in sys.argv[1:]:
os.environ["TY_DEMO"] = "1" # before anything is imported: the demo keeps its own files
if "--rig" in sys.argv[1:-1]: # likewise: which telescope's settings and files this is
os.environ["TY_RIG"] = sys.argv[sys.argv.index("--rig") + 1]
import argparse
import shutil
import subprocess
import tempfile
import threading
import webbrowser
from pathlib import Path
import config
import console
import host
ROOT = Path(__file__).parent
TITLE = "TelescopeYoke"
ICON = ROOT / "console" / "telescopeyoke.png"
SIZE = (1400, 860)
def gtk_window(url, title, busy, self_test=False):
"""A GTK window holding a WebKit view: what Ubuntu has without installing
anything. Returns False if this system has no GTK or WebKit."""
try:
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("WebKit2", "4.1")
from gi.repository import GLib, Gtk, WebKit2
except (ImportError, ValueError):
return False
GLib.set_prgname("telescopeyoke") # matches telescopeyoke.desktop, for the icon in the dock
GLib.set_application_name(title)
window = Gtk.Window(title=title)
window.set_default_size(*SIZE)
if ICON.exists():
window.set_icon_from_file(str(ICON))
view = WebKit2.WebView()
view.get_settings().set_enable_developer_extras(False)
view.connect("context-menu", lambda *a: True) # no "Back / Reload" menu: it is not a browser
window.add(view)
def closing(*_):
doing = busy()
if doing and not self_test:
ask = Gtk.MessageDialog(transient_for=window, modal=True, message_type=Gtk.MessageType.WARNING,
buttons=Gtk.ButtonsType.OK_CANCEL, text=f"{doing} is still running.")
ask.format_secondary_text("Closing TelescopeYoke ends it and stops the mount.")
answer = ask.run()
ask.destroy()
if answer != Gtk.ResponseType.OK:
return True # stay open
return False
window.connect("delete-event", closing)
window.connect("destroy", Gtk.main_quit)
if self_test:
def loaded(_, event):
if event == WebKit2.LoadEvent.FINISHED:
print(f"window: GTK with WebKit; page title: {view.get_title()}", flush=True)
GLib.timeout_add(1500, window.destroy)
view.connect("load-changed", loaded)
view.load_uri(url)
window.show_all()
Gtk.main()
return True
def webview_window(url, title, busy, self_test=False):
"""pywebview: on Windows this is the system's WebView2 control in a
window of its own. Returns False if pywebview is not installed."""
try:
import webview
except ImportError:
return False
window = webview.create_window(title, url, width=SIZE[0], height=SIZE[1], confirm_close=bool(busy()))
if self_test:
def loaded():
print(f"window: pywebview; page title: {window.title}", flush=True)
window.destroy()
window.events.loaded += loaded
webview.start()
return True
def browser_program():
"""An installed browser that can show one page as an application window."""
names = ["msedge", "chrome", "google-chrome", "chromium", "chromium-browser"]
places = [Path(base) / tail for base in (r"C:\Program Files (x86)", r"C:\Program Files")
for tail in (r"Microsoft\Edge\Application\msedge.exe", r"Google\Chrome\Application\chrome.exe")]
return next((found for found in map(shutil.which, names) if found),
next((str(p) for p in places if host.WINDOWS and p.exists()), None))
def app_mode_command(program, url, profile):
"""The browser's application mode: one window, no tabs, no address bar,
and a profile of its own so that it is a separate program that ends when
the window is closed."""
return [program, f"--app={url}", f"--user-data-dir={profile}", f"--window-size={SIZE[0]},{SIZE[1]}",
"--no-first-run", "--no-default-browser-check", f"--class={TITLE}"]
def browser_window(url, title, busy, self_test=False):
program = browser_program()
if not program or self_test:
return False
with tempfile.TemporaryDirectory(prefix="telescopeyoke-window-") as profile:
subprocess.run(app_mode_command(program, url, profile), **host.QUIET)
return True
def tab(url, title, busy, self_test=False):
"""Last resort: a tab in the ordinary browser, and this program waits."""
if self_test:
return False
webbrowser.open(url)
print(f"{title} is open in your browser. Press Ctrl+C here to close it.", flush=True)
try:
threading.Event().wait()
except KeyboardInterrupt:
pass
return True
def run(demo=False, self_test=False, ways=(gtk_window, webview_window, browser_window, tab)):
server, key = console.serve(port=0, demo=demo, mode="app")
jobs = server.RequestHandlerClass.jobs
threading.Thread(target=server.serve_forever, daemon=True).start()
url = f"http://127.0.0.1:{server.server_address[1]}/?key={key}"
busy = lambda: jobs.current["label"] if jobs.current else None
try:
named = " (demo)" if demo else f" ({config.RIG})" if config.RIG else ""
shown = any(way(url, TITLE + named, busy, self_test) for way in ways)
finally:
# Closing the application ends its job and always tells the mount to stop.
if not self_test:
jobs.close()
server.shutdown()
server.server_close()
return shown
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument("--demo", action="store_true",
help="a simulated mount and a made-up imaging run; needs no telescope")
ap.add_argument("--self-test", action="store_true",
help="open the window, say how it was made, and close it again")
ap.add_argument("--install-launcher", action="store_true",
help="put TelescopeYoke in the applications menu (the Start Menu on Windows)")
ap.add_argument("--rig", metavar="NAME",
help="one of several telescopes: its settings are rigs/NAME.toml and its files rigs/NAME/")
ap.add_argument("--new-rig", metavar="NAME",
help="make a rig's settings (from config.toml, or the example) and its menu entry")
args = ap.parse_args()
if args.new_rig:
print(f"settings for the rig: {config.new_rig(args.new_rig)}")
for written in host.install_launcher(ROOT, rig=args.new_rig):
print(f"added {written}")
print(f"Start it from the applications menu as TelescopeYoke ({args.new_rig}), or with "
f"./app.py --rig {args.new_rig}, and set it up on its Settings screen.")
return
if args.install_launcher:
for written in host.install_launcher(ROOT, rig=args.rig):
print(f"added {written}")
return
shown = run(args.demo or config.DEMO, args.self_test)
if args.self_test and not shown:
print("window: none of GTK with WebKit or pywebview is available here", flush=True)
sys.exit(1)
if __name__ == "__main__":
main()