-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
228 lines (169 loc) · 6.21 KB
/
Copy pathmain.py
File metadata and controls
228 lines (169 loc) · 6.21 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import sys, os
os.environ["QT_LOGGING_RULES"] = (
"qt.multimedia=false;"
"qt.multimedia.*=false;"
"qt.multimedia.ffmpeg*=false;"
#"qt.pyside.libpyside.warning=true;"
)
from typing import TextIO, TYPE_CHECKING
from PySide6.QtCore import Qt, QThreadPool, QIODeviceBase, qInstallMessageHandler
from PySide6.QtNetwork import QLocalSocket, QLocalServer
from config import Config
if TYPE_CHECKING:
from PySide6.QtWidgets import QApplication
from ui.main_window import MainWindow
from ui.tab import ImgTab
LOG_FILE = "./last.log"
class SingleInstanceServer:
SERVER_NAME = "media-curator-single-instance"
TIMEOUT = 3000
def __init__(self, mainWindow: 'MainWindow'):
self.mainWindow = mainWindow
self.server = QLocalServer(mainWindow)
self.server.setSocketOptions(QLocalServer.SocketOption.UserAccessOption)
self.server.setListenBacklogSize(1)
self.server.setMaxPendingConnections(8)
self.server.newConnection.connect(self._onConnection)
def start(self):
if self.server.listen(self.SERVER_NAME):
return
QLocalServer.removeServer(self.SERVER_NAME)
if not self.server.listen(self.SERVER_NAME):
print(f"Warning: Failed to start single instance server. Opening new files will start a new media-curator instance. ({self.server.errorString()})")
def _onConnection(self):
conn = self.server.nextPendingConnection()
conn.disconnected.connect(lambda: self._onRecv(conn)) # Read after disconnect to avoid reading partial messages
def _onRecv(self, conn: QLocalSocket):
data = conn.readAll().data()
conn.deleteLater()
import msgpack
paths: list[str] = msgpack.unpackb(data)
if paths:
self.mainWindow.addTabWithPaths(paths)
self.mainWindow.raise_()
self.mainWindow.activateWindow()
@classmethod
def trySendPaths(cls) -> bool:
socket = QLocalSocket()
socket.connectToServer(cls.SERVER_NAME, QIODeviceBase.OpenModeFlag.WriteOnly)
if not socket.waitForConnected(cls.TIMEOUT):
return False
try:
import msgpack
paths = sys.argv[1:]
data: bytes = msgpack.packb(paths)
# Always send paths, even when empty, to activate MainWindow
socket.write(data)
socket.waitForBytesWritten(cls.TIMEOUT)
return True
except:
import traceback
traceback.print_exc()
return False
finally:
socket.disconnectFromServer()
class Tee:
def __init__(self, stdOut: TextIO, fileOut: TextIO):
self.stdOut = stdOut
self.fileOut = fileOut
def write(self, data):
self.stdOut.write(data)
self.fileOut.write(data)
def flush(self):
self.stdOut.flush()
self.fileOut.flush()
def fileno(self):
# Return the real stdout's fileno so child process pipe inheritance works
return self.stdOut.fileno()
def initLogging():
streams = list[TextIO]()
# stdout is None when started with "pythonw.exe" under Windows
if sys.stdout:
# Python switches to block buffering when started with pipes
sys.stdout.reconfigure(line_buffering=True)
streams.append(sys.stdout)
try:
logFile = open(LOG_FILE, "wt", buffering=1, encoding="utf-8", errors="replace")
streams.append(logFile)
except Exception as ex:
if streams:
streams[0].write(f"Warning: Failed to open '{LOG_FILE}' for writing: {ex} ({type(ex).__name__})\n")
if len(streams) == 2:
out = Tee(*streams)
elif streams:
out = streams[0]
else:
return
sys.stdout = out
sys.stderr = out
import logging
logging.basicConfig(stream=out, level=logging.INFO)
class QtLogFilter:
def __init__(self):
self.lastLine = None
def __call__(self, msgType, context, message: str):
if message != self.lastLine:
self.lastLine = message
print(message)
def applyStyle(app: 'QApplication'):
match Config.colorScheme:
case "dark": colorSchemeOverride = Qt.ColorScheme.Dark
case "light": colorSchemeOverride = Qt.ColorScheme.Light
case _: colorSchemeOverride = None
if colorSchemeOverride is not None:
app.styleHints().setColorScheme(colorSchemeOverride)
colorScheme = colorSchemeOverride
else:
colorScheme = app.styleHints().colorScheme()
# Reload style after setting color scheme to apply palette colors
if Config.qtStyle:
app.setStyle(Config.qtStyle)
elif colorSchemeOverride is not None:
app.setStyle(app.style().name())
from lib import colorlib
colorlib.initColors(colorScheme)
def loadInitialPaths(win: 'MainWindow'):
tab: ImgTab = win.tabWidget.currentWidget()
if len(sys.argv) > 1:
# Skip first (script name)
itArg = iter(sys.argv)
next(itArg)
tab.filelist.loadAll(itArg)
elif Config.pathDebugLoad:
tab.filelist.load(Config.pathDebugLoad)
def restoreWindows(win: 'MainWindow'):
for winName in Config.windowOpen:
win.toggleAuxWindow(winName)
def main() -> int:
from PySide6.QtWidgets import QApplication
from PySide6.QtGui import QPixmapCache
from ui.main_window import MainWindow
from lib import filelist
os.environ["QT_SCALE_FACTOR"] = str(Config.guiScale)
logFilter = QtLogFilter()
qInstallMessageHandler(logFilter)
app = QApplication([])
QPixmapCache.setCacheLimit(24)
applyStyle(app)
threadCount = QThreadPool.globalInstance().maxThreadCount()
threadCount = max(threadCount // 2, 4)
QThreadPool.globalInstance().setMaxThreadCount(threadCount)
del threadCount
win = MainWindow(app)
win.show()
if Config.singleInstance:
instanceServer = SingleInstanceServer(win)
instanceServer.start()
filelist.resetReadExtensions()
loadInitialPaths(win)
restoreWindows(win)
return app.exec()
if __name__ == "__main__":
if SingleInstanceServer.trySendPaths():
sys.exit(0)
initLogging()
if not Config.load():
sys.exit(1)
exitCode = main()
Config.save()
sys.exit(exitCode)