-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (36 loc) · 1.4 KB
/
main.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
import wordmachine
import sys
import os
import importlib
from importlib import util
machine = wordmachine.WordMachine()
machine.debug = False
PLUGINS_ENABLED = True
content = ""
with open(sys.argv[1], "r", encoding='utf-8') as f:
content = f.read()
machine.program = content
machine.initprogram = content
def load_plugins():
if not os.path.exists("plugins"):
os.makedirs("plugins")
def walk_and_load(directory):
for item in os.scandir(directory):
if item.is_file() and item.name.endswith(".py"):
try:
rel_path = os.path.relpath(item.path, "plugins")
module_name = os.path.splitext(rel_path.replace(os.sep, "."))[0]
spec = importlib.util.spec_from_file_location(module_name, item.path)
plugin = importlib.util.module_from_spec(spec)
spec.loader.exec_module(plugin)
if hasattr(plugin, "plugin_init"):
plugin.plugin_init(machine)
except Exception as e:
print(f"Failed to load plugin {item.path}: {e}")
elif item.is_dir() and not item.name.startswith("."):
walk_and_load(item.path)
walk_and_load("plugins")
if PLUGINS_ENABLED:
load_plugins()
#print(machine.charmap)
machine.execute(machine.program)