forked from strob/drift3
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrift_gui.py
142 lines (97 loc) · 2.65 KB
/
drift_gui.py
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
#!/usr/bin/env python3
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import pyqtSignal
import logging
import os
import subprocess
import threading
import webbrowser
import sys
__version__ = "4.4.1"
BUNDLE = hasattr(sys, "frozen")
def get_open_port(desired=0):
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(("", desired))
except socket.error:
return get_open_port(0)
s.listen(1)
port = s.getsockname()[1]
s.close()
return port
PORT = get_open_port(9899)
def get_binary(name):
if BUNDLE:
return os.path.abspath(
os.path.join(getattr(sys, "_MEIPASS", ""), os.pardir, "Resources", name)
)
return name
def get_cwd():
if BUNDLE:
return os.path.abspath(
os.path.join(getattr(sys, "_MEIPASS", ""), os.pardir, "Resources", "serve-dist")
)
return os.path.abspath("")
S_PROC = None
devnull = None # open(os.devnull, 'w')
def serve(port):
global S_PROC
S_PROC = subprocess.Popen(
["./serve", str(port)],
cwd=os.path.join(get_cwd()),
stdout=devnull,
stderr=devnull,
)
gentle_running = get_open_port(8765) != 8765
# Start a thread for the web server.
webthread = threading.Thread(target=serve, args=(PORT,))
webthread.start()
def open_browser():
webbrowser.open("http://localhost:%d/" % (PORT))
def open_about():
webbrowser.open("http://localhost:%d/about.html" % (PORT))
def open_gentle():
webbrowser.open("https://lowerquality.com/gentle")
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
w.resize(250, 150)
w.setWindowTitle("Drift4")
def quit_server():
app.exit()
layout = QtWidgets.QVBoxLayout()
w.setLayout(layout)
txt = QtWidgets.QLabel(
"""Drift v%s
Words and intonation."""
% (__version__)
)
layout.addWidget(txt)
if not gentle_running:
gtxt = QtWidgets.QLabel(
"""Gentle is not running.
Please install and run Gentle before running Drift."""
)
layout.addWidget(gtxt)
gbtn = QtWidgets.QPushButton("Download Gentle")
gbtn.setStyleSheet("font-weight: bold;")
layout.addWidget(gbtn)
gbtn.clicked.connect(open_gentle)
else:
btn = QtWidgets.QPushButton("Open in browser")
btn.setStyleSheet("font-weight: bold;")
layout.addWidget(btn)
btn.clicked.connect(open_browser)
abt = QtWidgets.QPushButton("About Drift")
layout.addWidget(abt)
abt.clicked.connect(open_about)
quitb = QtWidgets.QPushButton("Quit")
layout.addWidget(quitb)
quitb.clicked.connect(quit_server)
w.show()
w.raise_()
w.activateWindow()
app.exec_()
logging.info("Waiting for server to quit.")
S_PROC.kill()
S_PROC.wait()