-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFeatureLoader.py
145 lines (106 loc) · 4.15 KB
/
FeatureLoader.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
145
# -*- coding: utf-8 -*-
# cython: language_level = 3
__author__ = "C418____11 <553515788@qq.com>"
__version__ = "MSS-0.0.3Bata"
import importlib
import os.path
import re
import sys
import traceback
import colorama
from C41811.Config import DefaultConfigPool
from C41811.Config import requireConfig
from C41811.Config.SLProcessors.ruamel_yaml import RuamelYamlSL
from Lib.StdColor import ColorWrite
RuamelYamlSL().registerTo(DefaultConfigPool)
DefaultFeatures = requireConfig(
'', "DefaultFeatures.yaml",
{
"1|WindowTop": True, # 这里的 '|' 是为了读取后进行排序
"2|Opacity": True,
"3|ScanServer": True,
"4|ScanSettings": True,
}
).checkConfig() # todo 排序
_yellow_write = ColorWrite(sys.stdout, colorama.Fore.LIGHTYELLOW_EX)
_blue_write = ColorWrite(sys.stdout, colorama.Fore.LIGHTBLUE_EX)
_red_write = ColorWrite(sys.stdout, colorama.Fore.LIGHTRED_EX)
def _load(name: str, import_path: str):
try:
module = importlib.import_module(f"{import_path}.{name}")
print("Feature loaded successfully:", name, file=_yellow_write)
_show_details(module)
return module
except ImportError as err:
traceback.print_exception(err, file=_red_write)
c = re.compile(r"No\smodule\snamed\s'([^']+)'")
err_module = c.findall(str(err))
if (not err_module) or len(err_module) != 1:
print("Unable to load Feature:", name, " reason:", err, file=_red_write)
return None
err_module = err_module[0]
if err_module == import_path:
print("Feature not found:", f"{import_path}.{name}", file=_blue_write)
return None
if err_module == f"{import_path}.{name}":
print("Feature not found:", f"{import_path}.{name}", file=_blue_write)
return None
print(f"Unable to load Feature '{name}', dependencies may not be installed: '{err_module}'", file=_red_write)
return None
except Exception as err:
traceback.print_exception(err, file=_red_write)
print("Unable to load Feature:", name, " reason:", err, file=_red_write)
return None
def _get_details(module):
detail_dict = {}
for attr in ("__author__", "__description__", "__version__"):
value = getattr(module, attr, None)
if value is not None:
detail_dict[attr] = value
return detail_dict
def _show_details(module):
details = _get_details(module)
if not details:
print(" No details available", file=_yellow_write)
return
key_map = {
"__author__": "Auther",
"__description__": "Desc",
"__version__": "Ver"
}
for attr, value in details.items():
print(f" {key_map[attr]}: {value}", file=_yellow_write)
def load_default_features():
lib_path = os.path.join(os.path.dirname(__file__), "DefaultFeatures")
sys.path.append(lib_path)
loaded_features = {}
for feature in DefaultFeatures:
if not bool(DefaultFeatures[feature]):
print("Feature disabled:", feature, file=_blue_write)
loaded_features[feature] = None
continue
if '|' in feature:
feature = feature.split('|')[1]
loaded_features[feature] = _load(feature, "DefaultFeatures")
return loaded_features
OtherFeatures = requireConfig(
'', "OtherFeatures.yaml",
{}
).checkConfig() # todo 排序
def load_other_features():
lib_path = os.path.join(os.path.dirname(__file__), "OtherFeatures")
sys.path.append(lib_path)
loaded_features = {}
for feature in OtherFeatures:
if feature == "YourFeatureName":
print("YourFeatureName is a reserved keyword, please change it to another name.", file=_red_write)
continue
if not OtherFeatures[feature]:
print("Feature disabled:", feature, file=_blue_write)
loaded_features[feature] = None
continue
if '|' in feature:
feature = feature.split('|')[1]
loaded_features[feature] = _load(feature, "Features")
return loaded_features
__all__ = ("DefaultFeatures", "load_default_features", "OtherFeatures", "load_other_features")