188
188
import gi
189
189
from gi.repository import GLib
190
190
191
+ # Log to debug as we don't care at all when user does not have this
192
+ try:
193
+ import colored_traceback.always
194
+ logging.debug("Found colored_traceback for colored crash tracebacks")
195
+ except ModuleNotFoundError:
196
+ logging.debug("Unable to import colored_traceback, tracebacks will be dull.")
197
+ except Exception:
198
+ logging.warning("Error trying to import colored_traceback, tracebacks will be dull.")
199
+
200
+ try:
201
+ from jxlpy import JXLImagePlugin
202
+ # We've already logged this once to INFO from t_draw, so just log to DEBUG
203
+ logging.debug("Found jxlpy for JPEG XL support")
204
+ except ModuleNotFoundError:
205
+ logging.warning("Unable to import jxlpy, JPEG XL support will be disabled.")
206
+ except Exception:
207
+ logging.exception("Unknown error trying to import jxlpy, JPEG XL support will be disabled.")
208
+
209
+ try:
210
+ import setproctitle
211
+ except ModuleNotFoundError:
212
+ logging.warning("Unable to import setproctitle, won't be setting process title.")
213
+ except Exception:
214
+ logging.exception("Unknown error trying to import setproctitle, won't be setting process title.")
215
+ else:
216
+ setproctitle.setproctitle("tauonmb")
217
+
218
+ # try:
219
+ # import rpc
220
+ # discord_allow = True
221
+ # except Exception:
222
+ # logging.exception("Unable to import rpc, Discord Rich Presence will be disabled.")
223
+ try:
224
+ from lynxpresence import Presence, ActivityType
225
+ except ModuleNotFoundError:
226
+ logging.warning("Unable to import lynxpresence, Discord Rich Presence will be disabled.")
227
+ except Exception:
228
+ logging.exception("Unknown error trying to import lynxpresence, Discord Rich Presence will be disabled.")
229
+ else:
230
+ import asyncio
231
+
232
+ try:
233
+ import opencc
234
+ except ModuleNotFoundError:
235
+ logging.warning("Unable to import opencc, Traditional and Simplified Chinese searches will not be usable interchangeably.")
236
+ except Exception:
237
+ logging.exception("Unknown error trying to import opencc, Traditional and Simplified Chinese searches will not be usable interchangeably.")
238
+
239
+ try:
240
+ import natsort
241
+ except ModuleNotFoundError:
242
+ logging.warning("Unable to import natsort, playlists may not sort as intended!")
243
+ except Exception:
244
+ logging.exception("Unknown error trying to import natsort, playlists may not sort as intended!")
245
+
191
246
if TYPE_CHECKING:
192
247
from ctypes import CDLL
193
248
from io import BufferedReader, BytesIO
202
257
from pylast import LastFMNetwork
203
258
from collections.abc import Callable
204
259
260
+ # Detect platform
261
+ macos = False
262
+ msys = False
263
+ system = "Linux"
264
+ arch = platform.machine()
265
+ platform_release = platform.release()
266
+ platform_system = platform.system()
267
+ win_ver = 0
268
+ if platform_system == "Windows":
269
+ try:
270
+ win_ver = int(platform_release)
271
+ except Exception:
272
+ logging.exception("Failed getting Windows version from platform.release()")
273
+
274
+ if sys.platform == "win32":
275
+ # system = 'Windows'
276
+ system = "Linux"
277
+ msys = True
278
+ if msys:
279
+ import gi
280
+ from gi.repository import GLib
281
+ else:
282
+ import win32con
283
+ import win32api
284
+ import win32gui
285
+ import win32ui
286
+ import comtypes
287
+ import atexit
288
+ else:
289
+ system = "Linux"
290
+ import fcntl
291
+
292
+ if sys.platform == "darwin":
293
+ macos = True
294
+
295
+ if system == "Linux" and not macos and not msys:
296
+ from tauon.t_modules.t_dbus import Gnome
297
+
205
298
CONTROL_CHAR_RE = re.compile(r"[\x00-\x08\x0B\x0C\x0E-\x1F]")
206
299
207
300
class LoadImageAsset:
@@ -5390,6 +5483,9 @@ class Tauon:
5390
5483
"""Root class for everything Tauon"""
5391
5484
5392
5485
def __init__(self, holder: Holder, bag: Bag, gui: GuiVar) -> None:
5486
+ self.use_cc = is_module_loaded("opencc")
5487
+ self.use_natsort = is_module_loaded("natsort")
5488
+
5393
5489
self.bag = bag
5394
5490
self.mpt = bag.mpt
5395
5491
self.gme = bag.gme
@@ -5410,7 +5506,6 @@ def __init__(self, holder: Holder, bag: Bag, gui: GuiVar) -> None:
5410
5506
self.download_directories = bag.download_directories
5411
5507
self.launch_prefix = bag.launch_prefix
5412
5508
self.overlay_texture_texture = bag.overlay_texture_texture
5413
- self.use_natsort = bag.use_natsort
5414
5509
self.de_notify_support = bag.de_notify_support
5415
5510
self.old_window_position = bag.old_window_position
5416
5511
self.cache_directory = bag.dirs.cache_directory
@@ -16684,7 +16779,7 @@ def key_fullpath(self, index: int):
16684
16779
def sort_path_pl(self, pl: int, custom_list: list[int] | None = None) -> None:
16685
16780
target = self.pctl.multi_playlist[pl].playlist_ids if custom_list is None else custom_list
16686
16781
16687
- if self.bag. use_natsort and False:
16782
+ if self.use_natsort and False:
16688
16783
target[:] = natsort.os_sorted(target, key=self.key_fullpath)
16689
16784
else:
16690
16785
target.sort(key=self.key_filepath)
@@ -36132,7 +36227,6 @@ class Bag:
36132
36227
last_fm_enable: bool
36133
36228
de_notify_support: bool
36134
36229
wayland: bool
36135
- use_natsort: bool
36136
36230
should_save_state: bool
36137
36231
desktop: str | None
36138
36232
system: str
@@ -36186,7 +36280,12 @@ class Formats:
36186
36280
Archive: set[str]
36187
36281
36188
36282
36189
- def is_module_loaded(module_name: str) -> bool:
36283
+ def is_module_loaded(module_name: str, object_name: str = "") -> bool:
36284
+ """Check if a module is loaded, to determine which features we should enable
36285
+
36286
+ See https://stackoverflow.com/a/30483269/8962143 for more details"""
36287
+ if object_name:
36288
+ return module_name in sys.modules and object_name in sys.modules[module_name]
36190
36289
return module_name in sys.modules
36191
36290
36192
36291
def get_cert_path(holder: Holder) -> str:
@@ -37481,6 +37580,9 @@ def worker4(tauon: Tauon) -> None:
37481
37580
return
37482
37581
37483
37582
def worker2(tauon: Tauon) -> None:
37583
+ if tauon.use_cc:
37584
+ s2t = opencc.OpenCC("s2t")
37585
+ t2s = opencc.OpenCC("t2s")
37484
37586
search_over = tauon.search_over
37485
37587
while True:
37486
37588
tauon.worker2_lock.acquire()
@@ -37552,7 +37654,7 @@ def worker2(tauon: Tauon) -> None:
37552
37654
year_mode = True
37553
37655
37554
37656
cn_mode = False
37555
- if use_cc and re.search(r"[\u4e00-\u9fff\u3400-\u4dbf\u20000-\u2a6df\u2a700-\u2b73f\u2b740-\u2b81f\u2b820-\u2ceaf\uf900-\ufaff\u2f800-\u2fa1f]", o_text):
37657
+ if tauon. use_cc and re.search(r"[\u4e00-\u9fff\u3400-\u4dbf\u20000-\u2a6df\u2a700-\u2b73f\u2b740-\u2b81f\u2b820-\u2ceaf\uf900-\ufaff\u2f800-\u2fa1f]", o_text):
37556
37658
t_cn = s2t.convert(o_text)
37557
37659
s_cn = t2s.convert(o_text)
37558
37660
cn_mode = True
@@ -38374,7 +38476,7 @@ def gets(direc: str, force_scan: bool = False) -> None:
38374
38476
38375
38477
try:
38376
38478
items_in_dir = os.listdir(direc)
38377
- if use_natsort:
38479
+ if tauon. use_natsort:
38378
38480
items_in_dir = natsort.os_sorted(items_in_dir)
38379
38481
else:
38380
38482
items_in_dir.sort()
@@ -38803,111 +38905,6 @@ def menu_is_open() -> bool:
38803
38905
return True
38804
38906
return False
38805
38907
38806
- # BEGIN CODE
38807
-
38808
- # Log to debug as we don't care at all when user does not have this
38809
- try:
38810
- import colored_traceback.always
38811
- logging.debug("Found colored_traceback for colored crash tracebacks")
38812
- except ModuleNotFoundError:
38813
- logging.debug("Unable to import colored_traceback, tracebacks will be dull.")
38814
- except Exception:
38815
- logging.warning("Error trying to import colored_traceback, tracebacks will be dull.")
38816
-
38817
- try:
38818
- from jxlpy import JXLImagePlugin
38819
- # We've already logged this once to INFO from t_draw, so just log to DEBUG
38820
- logging.debug("Found jxlpy for JPEG XL support")
38821
- except ModuleNotFoundError:
38822
- logging.warning("Unable to import jxlpy, JPEG XL support will be disabled.")
38823
- except Exception:
38824
- logging.exception("Unknown error trying to import jxlpy, JPEG XL support will be disabled.")
38825
-
38826
- try:
38827
- import setproctitle
38828
- except ModuleNotFoundError:
38829
- logging.warning("Unable to import setproctitle, won't be setting process title.")
38830
- except Exception:
38831
- logging.exception("Unknown error trying to import setproctitle, won't be setting process title.")
38832
- else:
38833
- setproctitle.setproctitle("tauonmb")
38834
-
38835
- # try:
38836
- # import rpc
38837
- # discord_allow = True
38838
- # except Exception:
38839
- # logging.exception("Unable to import rpc, Discord Rich Presence will be disabled.")
38840
- discord_allow = False
38841
- try:
38842
- from lynxpresence import Presence, ActivityType
38843
- except ModuleNotFoundError:
38844
- logging.warning("Unable to import lynxpresence, Discord Rich Presence will be disabled.")
38845
- except Exception:
38846
- logging.exception("Unknown error trying to import lynxpresence, Discord Rich Presence will be disabled.")
38847
- else:
38848
- import asyncio
38849
- discord_allow = True
38850
-
38851
- use_cc = False
38852
- try:
38853
- import opencc
38854
- except ModuleNotFoundError:
38855
- logging.warning("Unable to import opencc, Traditional and Simplified Chinese searches will not be usable interchangeably.")
38856
- except Exception:
38857
- logging.exception("Unknown error trying to import opencc, Traditional and Simplified Chinese searches will not be usable interchangeably.")
38858
- else:
38859
- s2t = opencc.OpenCC("s2t")
38860
- t2s = opencc.OpenCC("t2s")
38861
- use_cc = True
38862
-
38863
- use_natsort = False
38864
- try:
38865
- import natsort
38866
- except ModuleNotFoundError:
38867
- logging.warning("Unable to import natsort, playlists may not sort as intended!")
38868
- except Exception:
38869
- logging.exception("Unknown error trying to import natsort, playlists may not sort as intended!")
38870
- else:
38871
- use_natsort = True
38872
-
38873
- # Detect platform
38874
- macos = False
38875
- msys = False
38876
- system = "Linux"
38877
- arch = platform.machine()
38878
- platform_release = platform.release()
38879
- platform_system = platform.system()
38880
- win_ver = 0
38881
- if platform_system == "Windows":
38882
- try:
38883
- win_ver = int(platform_release)
38884
- except Exception:
38885
- logging.exception("Failed getting Windows version from platform.release()")
38886
-
38887
- if sys.platform == "win32":
38888
- # system = 'Windows'
38889
- system = "Linux"
38890
- msys = True
38891
- if msys:
38892
- import gi
38893
- from gi.repository import GLib
38894
- else:
38895
- import win32con
38896
- import win32api
38897
- import win32gui
38898
- import win32ui
38899
- import comtypes
38900
- import atexit
38901
- else:
38902
- system = "Linux"
38903
- import fcntl
38904
-
38905
- if sys.platform == "darwin":
38906
- macos = True
38907
-
38908
- if system == "Linux" and not macos and not msys:
38909
- from tauon.t_modules.t_dbus import Gnome
38910
-
38911
38908
holder = t_bootstrap.holder
38912
38909
t_window = holder.t_window
38913
38910
renderer = holder.renderer
@@ -38937,7 +38934,6 @@ def menu_is_open() -> bool:
38937
38934
logging.info(f"Window size: {window_size}; Logical size: {logical_size}")
38938
38935
38939
38936
tls_context = setup_tls(holder)
38940
-
38941
38937
try:
38942
38938
# Pylast needs to be imported AFTER setup_tls() else pyinstaller breaks
38943
38939
import pylast
@@ -38946,11 +38942,13 @@ def menu_is_open() -> bool:
38946
38942
logging.exception("PyLast module not found, last fm will be disabled.")
38947
38943
last_fm_enable = False
38948
38944
38945
+ discord_allow = is_module_loaded("lynxpresence", "ActivityType")
38946
+ ctypes = sys.modules.get("ctypes") # Fetch from loaded modules
38947
+
38949
38948
if sys.platform == "win32" and msys:
38950
38949
font_folder = str(install_directory / "fonts")
38951
38950
if os.path.isdir(font_folder):
38952
38951
logging.info(f"Fonts directory: {font_folder}")
38953
- import ctypes
38954
38952
38955
38953
fc = ctypes.cdll.LoadLibrary("libfontconfig-1.dll")
38956
38954
fc.FcConfigReference.restype = ctypes.c_void_p
@@ -39498,7 +39496,6 @@ def menu_is_open() -> bool:
39498
39496
mac_minimize=mac_minimize,
39499
39497
msys=msys,
39500
39498
phone=phone,
39501
- use_natsort=use_natsort,
39502
39499
should_save_state=True,
39503
39500
old_window_position=old_window_position,
39504
39501
xdpi=xdpi,
0 commit comments