-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_runner.py
More file actions
82 lines (73 loc) · 3.1 KB
/
Copy pathmodule_runner.py
File metadata and controls
82 lines (73 loc) · 3.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
import os
import sys
import logging
import importlib
import traceback
import tempfile
from PIL import Image, ImageTk
import customtkinter as ctk
def run_module_process(module_name, icon_path):
"""Fonction exécutée dans le processus enfant pour lancer le module."""
app_data_dir = os.path.join(os.getenv('LOCALAPPDATA'), 'RetrogamingToolkit')
if not os.path.exists(app_data_dir):
try:
os.makedirs(app_data_dir)
except OSError:
app_data_dir = tempfile.gettempdir()
log_file = os.path.join(app_data_dir, 'retrogaming_toolkit.log')
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(processName)s - %(levelname)s - %(message)s',
filename=log_file,
filemode='a'
)
logger = logging.getLogger(__name__)
# Ensure sys.path is correct in child process
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
toolkit_path = os.path.join(base_path, "Retrogaming-Toolkit-AIO")
if toolkit_path not in sys.path:
sys.path.append(toolkit_path)
else:
# Not frozen, source mode.
# module_runner is in root.
# We need to add Retrogaming-Toolkit-AIO to sys.path
root_dir = os.path.dirname(os.path.abspath(__file__))
toolkit_path = os.path.join(root_dir, "Retrogaming-Toolkit-AIO")
if toolkit_path not in sys.path:
sys.path.append(toolkit_path)
try:
logger.info(f"Child process started for module: {module_name}")
# --- Monkeypatch CTk to inject Icon ---
if icon_path:
icon_path = os.path.abspath(icon_path) # Force absolute
if icon_path and os.path.exists(icon_path):
OriginalCTk = ctk.CTk
class IconizedCTk(OriginalCTk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.after(10, self.set_icon_safe)
def set_icon_safe(self):
try:
# Apply icon
if icon_path.lower().endswith(".ico"):
self.wm_iconbitmap(icon_path)
else:
# Try PNG/Image
icon_img = Image.open(icon_path)
self.iconphoto(False, ImageTk.PhotoImage(icon_img))
logger.info(f"Icon applied from {icon_path}")
except Exception as e:
logger.error(f"Failed to set icon in child process: {e}")
# Apply the patch
ctk.CTk = IconizedCTk
# Import dynamique du module
module = importlib.import_module(module_name)
# Exécution de la fonction main() du module
if hasattr(module, 'main'):
module.main()
else:
logger.error(f"Erreur : Le module {module_name} n'a pas de fonction main()")
except Exception as e:
logger.error(f"Erreur dans le processus enfant pour {module_name}: {e}")
logger.error(traceback.format_exc())