-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugload.py
125 lines (100 loc) · 3.79 KB
/
plugload.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
#Created by: Ali B Othman
#Version : 1.1.0
import sys, os
from importlib import machinery
import tokenize
import easylogging
PluginFolder = "plugins"
MainModule = "__init__"
Version = "1.1.0"
pl = {}
easylogging.logname('Ploader')
####################################### Functions from (imp) lib ####################################
PY_SOURCE = 1
PY_COMPILED = 2
C_EXTENSION = 3
def find_module(name, path=None):
"""**DEPRECATED**
Search for a module.
If path is omitted or None, search for a built-in, frozen or special
module and continue search in sys.path. The module name cannot
contain '.'; to search for a submodule of a package, pass the
submodule name and the package's __path__.
Note : This function from (imp) library
"""
if not isinstance(name, str):
raise TypeError("'name' must be a str, not {}".format(type(name)))
elif not isinstance(path, (type(None), list)):
# Backwards-compatibility
raise RuntimeError("'path' must be None or a list, "
"not {}".format(type(path)))
if path is None:
if is_builtin(name):
return None, None, ('', '', C_BUILTIN)
elif is_frozen(name):
return None, None, ('', '', PY_FROZEN)
else:
path = sys.path
for entry in path:
package_directory = os.path.join(entry, name)
for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]:
package_file_name = '__init__' + suffix
file_path = os.path.join(package_directory, package_file_name)
if os.path.isfile(file_path):
return None, package_directory, ('', '', PKG_DIRECTORY)
for suffix, mode, type_ in get_suffixes():
file_name = name + suffix
file_path = os.path.join(entry, file_name)
if os.path.isfile(file_path):
break
else:
continue
break # Break out of outer loop when breaking out of inner loop.
else:
raise ImportError(_ERR_MSG.format(name), name=name)
encoding = None
if 'b' not in mode:
with open(file_path, 'rb') as file:
encoding = tokenize.detect_encoding(file.readline)[0]
file = open(file_path, mode, encoding=encoding)
return file, file_path, (suffix, mode, type_)
def get_suffixes():
"""**DEPRECATED**
Note : This function from (imp) library
"""
extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES]
source = [(s, 'r', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES]
bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES]
return extensions + source + bytecode
####################################### End (imp) functions #########################################
def plugi(file, filename, details):
suffix, mode, type_ = details
libname = filename.split('\\')
lib_name = libname[-2]
pl[filename] = lib_name
def getPlugins():
plugins = []
if not PluginFolder in os.listdir() or not os.path.isdir(PluginFolder):
os.mkdir(PluginFolder)
possibleplugins = os.listdir(PluginFolder)
for i in possibleplugins:
location = os.path.join(PluginFolder, i)
if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location):
continue
info = find_module(MainModule, [location])
plugins.append({"name": i, "info": info})
for i in plugins:
plugi(*i['info'])
def run():
getPlugins()
for i in pl:
easylogging.info('Loading plugin : %s' % (pl[i]))
try:
f = open(i, 'r')
exec(f.read())
except :
easylogging.error('Failed load plugin : %s' % (pl[i]))
finally:
f.close()
if __name__ == '__main__':
run()