Skip to content

Commit 9d433a2

Browse files
authored
Merge pull request #1638 from C0rn3j/prep-for-wrap
Consolidate imports
2 parents f6c2121 + 9f3bf75 commit 9d433a2

File tree

1 file changed

+111
-114
lines changed

1 file changed

+111
-114
lines changed

src/tauon/t_modules/t_main.py

Lines changed: 111 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,61 @@
188188
import gi
189189
from gi.repository import GLib
190190

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+
191246
if TYPE_CHECKING:
192247
from ctypes import CDLL
193248
from io import BufferedReader, BytesIO
@@ -202,6 +257,44 @@
202257
from pylast import LastFMNetwork
203258
from collections.abc import Callable
204259

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+
205298
CONTROL_CHAR_RE = re.compile(r"[\x00-\x08\x0B\x0C\x0E-\x1F]")
206299

207300
class LoadImageAsset:
@@ -5390,6 +5483,9 @@ class Tauon:
53905483
"""Root class for everything Tauon"""
53915484

53925485
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+
53935489
self.bag = bag
53945490
self.mpt = bag.mpt
53955491
self.gme = bag.gme
@@ -5410,7 +5506,6 @@ def __init__(self, holder: Holder, bag: Bag, gui: GuiVar) -> None:
54105506
self.download_directories = bag.download_directories
54115507
self.launch_prefix = bag.launch_prefix
54125508
self.overlay_texture_texture = bag.overlay_texture_texture
5413-
self.use_natsort = bag.use_natsort
54145509
self.de_notify_support = bag.de_notify_support
54155510
self.old_window_position = bag.old_window_position
54165511
self.cache_directory = bag.dirs.cache_directory
@@ -16684,7 +16779,7 @@ def key_fullpath(self, index: int):
1668416779
def sort_path_pl(self, pl: int, custom_list: list[int] | None = None) -> None:
1668516780
target = self.pctl.multi_playlist[pl].playlist_ids if custom_list is None else custom_list
1668616781

16687-
if self.bag.use_natsort and False:
16782+
if self.use_natsort and False:
1668816783
target[:] = natsort.os_sorted(target, key=self.key_fullpath)
1668916784
else:
1669016785
target.sort(key=self.key_filepath)
@@ -36132,7 +36227,6 @@ class Bag:
3613236227
last_fm_enable: bool
3613336228
de_notify_support: bool
3613436229
wayland: bool
36135-
use_natsort: bool
3613636230
should_save_state: bool
3613736231
desktop: str | None
3613836232
system: str
@@ -36186,7 +36280,12 @@ class Formats:
3618636280
Archive: set[str]
3618736281

3618836282

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]
3619036289
return module_name in sys.modules
3619136290

3619236291
def get_cert_path(holder: Holder) -> str:
@@ -37481,6 +37580,9 @@ def worker4(tauon: Tauon) -> None:
3748137580
return
3748237581

3748337582
def worker2(tauon: Tauon) -> None:
37583+
if tauon.use_cc:
37584+
s2t = opencc.OpenCC("s2t")
37585+
t2s = opencc.OpenCC("t2s")
3748437586
search_over = tauon.search_over
3748537587
while True:
3748637588
tauon.worker2_lock.acquire()
@@ -37552,7 +37654,7 @@ def worker2(tauon: Tauon) -> None:
3755237654
year_mode = True
3755337655

3755437656
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):
3755637658
t_cn = s2t.convert(o_text)
3755737659
s_cn = t2s.convert(o_text)
3755837660
cn_mode = True
@@ -38374,7 +38476,7 @@ def gets(direc: str, force_scan: bool = False) -> None:
3837438476

3837538477
try:
3837638478
items_in_dir = os.listdir(direc)
38377-
if use_natsort:
38479+
if tauon.use_natsort:
3837838480
items_in_dir = natsort.os_sorted(items_in_dir)
3837938481
else:
3838038482
items_in_dir.sort()
@@ -38803,111 +38905,6 @@ def menu_is_open() -> bool:
3880338905
return True
3880438906
return False
3880538907

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-
3891138908
holder = t_bootstrap.holder
3891238909
t_window = holder.t_window
3891338910
renderer = holder.renderer
@@ -38937,7 +38934,6 @@ def menu_is_open() -> bool:
3893738934
logging.info(f"Window size: {window_size}; Logical size: {logical_size}")
3893838935

3893938936
tls_context = setup_tls(holder)
38940-
3894138937
try:
3894238938
# Pylast needs to be imported AFTER setup_tls() else pyinstaller breaks
3894338939
import pylast
@@ -38946,11 +38942,13 @@ def menu_is_open() -> bool:
3894638942
logging.exception("PyLast module not found, last fm will be disabled.")
3894738943
last_fm_enable = False
3894838944

38945+
discord_allow = is_module_loaded("lynxpresence", "ActivityType")
38946+
ctypes = sys.modules.get("ctypes") # Fetch from loaded modules
38947+
3894938948
if sys.platform == "win32" and msys:
3895038949
font_folder = str(install_directory / "fonts")
3895138950
if os.path.isdir(font_folder):
3895238951
logging.info(f"Fonts directory: {font_folder}")
38953-
import ctypes
3895438952

3895538953
fc = ctypes.cdll.LoadLibrary("libfontconfig-1.dll")
3895638954
fc.FcConfigReference.restype = ctypes.c_void_p
@@ -39498,7 +39496,6 @@ def menu_is_open() -> bool:
3949839496
mac_minimize=mac_minimize,
3949939497
msys=msys,
3950039498
phone=phone,
39501-
use_natsort=use_natsort,
3950239499
should_save_state=True,
3950339500
old_window_position=old_window_position,
3950439501
xdpi=xdpi,

0 commit comments

Comments
 (0)