-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin_system.py
144 lines (98 loc) · 4.4 KB
/
plugin_system.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
143
144
# pylint: disable=W4901
# pylint: disable=W0105
# pylint: disable=W0603
# pylint: disable=W0718
# [!!] All these "errors" annoy me!
# [!?] this is MF366's mediocre plugin system
import os
import datetime
from pygame import mixer # for the exe version, the module should have been already compiled
from tkinter import messagebox as mb
from typing import Any
import importlib.machinery
wclassic_vars: dict[str, Any] = {}
_LOG = None
plugin_dir: str = ""
lang: list[str] = []
# [i] Alternative to using deprecated imp module
def load_module_from_source(module_name: str, source_path: str):
"""
load_module_from_source loads a Python module from an absolute location of a *.py file
Args:
module_name (str): the name of the module
source_path (str): the path to the module
Used like:
loaded_module = load_module_from_source("something", "C:\\Users\\user\\johndoe.py")
Returns:
Any: the module
"""
loader = importlib.machinery.SourceFileLoader(module_name, source_path)
spec = importlib.util.spec_from_loader(module_name, loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
return module
now = datetime.datetime.now
def initializer(wclassic_globals):
global wclassic_vars, _LOG, plugin_dir, lang
wclassic_vars = wclassic_globals
_LOG = wclassic_vars["LOG"]
plugin_dir = wclassic_vars["plugin_dir"]
lang = wclassic_vars["lang"]
_LOG.write(f"{str(now())} - Initializing Plugin System: OK\n")
def run_a_plugin(number_or_name: int | str) -> bool:
try:
if isinstance(number_or_name, int):
new_folder = os.path.join(plugin_dir, f"plugin_{number_or_name}")
if not os.path.exists(path=new_folder):
mb.showerror(lang[161], lang[162])
return False
details_file = os.path.join(new_folder, "Details.txt")
with open(details_file, "r", encoding="utf-8") as f:
_title = f.readline().strip()
f.close()
possible_plugin_filepath = os.path.join(new_folder, f"{_title.replace(' ', '_')}.py")
if not os.path.exists(possible_plugin_filepath):
mb.showerror(lang[161], lang[162])
return False
plugin_filepath = possible_plugin_filepath
_LOG.write(f"{str(now())} - Running the plugin number {str(number_or_name)} stored at {plugin_filepath}: OK\n")
mixer.quit()
mixer.init()
mixer.music.load(os.path.join(wclassic_vars['data_dir'], 'sucessful.mp3'))
mixer.music.set_volume(0.5)
mixer.music.play()
module_path = plugin_filepath
module = load_module_from_source(_title.replace(" ", "_"), module_path)
module.start(wclassic_vars)
else:
plugin_filepath: str | None = None
for dir_num in range(1, 1000):
a = os.path.join(plugin_dir, f"plugin_{dir_num}", "Details.txt")
if not os.path.exists(a):
continue
with open(a, "r", encoding="utf-8") as f:
_title = f.readline().strip()
b: str | None = "?"
if number_or_name == _title.replace("_", " "):
b = os.path.join(plugin_dir, f"plugin_{dir_num}", f"{_title.replace(' ', '_')}.py")
if os.path.exists(b):
plugin_filepath = b
break
if plugin_filepath:
_LOG.write(f"{str(now())} - Running the plugin '{str(number_or_name)}' stored at {plugin_filepath}: OK\n")
module_path = plugin_filepath
mixer.quit()
mixer.init()
mixer.music.load(os.path.join(wclassic_vars['data_dir'], 'sucessful.mp3'))
mixer.music.set_volume(0.5)
mixer.music.play()
module = load_module_from_source(_title.replace(" ", "_"), module_path)
module.start(wclassic_vars)
return True
except Exception as e:
mb.showerror(lang[133], lang[134] + f"\n{e}")
if isinstance(number_or_name, int):
_LOG.write(f"{str(now())} - Running the plugin number {str(number_or_name)}: ERROR - {e}\n")
else:
_LOG.write(f"{str(now())} - Running the plugin '{str(number_or_name)}': ERROR - {e}\n")
return False