pull/628/head
Mr Chen 7 months ago
parent 13a7c20764
commit e1dd781b73
  1. 32
      web/admin/plugins/__init__.py
  2. 16
      web/core/mw.py
  3. 104
      web/utils/mwplugin.py

@ -17,6 +17,8 @@ from utils.mwplugin import MwPlugin
import core.mw as mw
pg = MwPlugin.instance()
blueprint = Blueprint('plugins', __name__, url_prefix='/plugins', template_folder='../../templates/default')
@blueprint.route('/index', endpoint='index')
def index():
@ -47,12 +49,10 @@ def list():
if not mw.isNumber(page):
page = 0
pg = MwPlugin.instance()
# pg.getList(plugins_type, search, int(page))
return pg.getList(plugins_type, search, int(page))
# 图片文件读取
# 文件读取
@blueprint.route('/file', endpoint='file', methods=['GET'])
def file():
name = request.args.get('name', '')
@ -77,4 +77,30 @@ def file():
content = open(file, 'rb').read()
return content
# 插件设置页
@blueprint.route('/setting', endpoint='setting', methods=['GET'])
def setting():
name = request.args.get('name', '')
html = mw.getPluginDir() + '/' + name + '/index.html'
return mw.readFile(html)
# 插件统一回调入口API
@blueprint.route('/run', endpoint='run', methods=['GET','POST'])
def run():
name = request.form.get('name', '')
func = request.form.get('func', '')
version = request.form.get('version', '')
args = request.form.get('args', '')
script = request.form.get('script', 'index')
pg = MwPlugin.instance()
data = pg.run(name, func, version, args, script)
if data[1] == '':
r = mw.returnData(True, "OK", data[0].strip())
else:
r = mw.returnData(False, data[1].strip())
return r

@ -115,6 +115,14 @@ def getUniqueId():
unique_id = "{0}".format(str_time)
return unique_id
def returnData(status, msg, data=None):
return {'status': status, 'msg': msg, 'data': data}
def returnJson(status, msg, data=None):
if data == None:
return getJson({'status': status, 'msg': msg})
return getJson({'status': status, 'msg': msg, 'data': data})
def readFile(filename):
# 读文件内容
try:
@ -155,6 +163,14 @@ def systemdCfgDir():
# local test
return "/tmp"
def getJson(data):
import json
return json.dumps(data)
def getObjectByJson(data):
import json
return json.loads(data)
def getSslCrt():
if os.path.exists('/etc/ssl/certs/ca-certificates.crt'):

@ -13,6 +13,25 @@ import threading
import json
import core.mw as mw
import threading
import multiprocessing
class pa_thread(threading.Thread):
def __init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
self.result = self.func(*self.args)
def getResult(self):
try:
return self.result
except Exception:
return None
class MwPlugin(object):
def_plugin_type = [
@ -271,6 +290,37 @@ class MwPlugin(object):
info.append(plugin_t[index])
return info
# 检查插件状态
def checkStatusThreads(self, info, i):
if not info['setup']:
return False
data = self.run(info['name'], 'status', info['setup_version'])
if data[0] == 'start':
return True
else:
return False
# 多线程检查插件状态
def checkStatusMThreads(self, info):
try:
threads = []
ntmp_list = range(len(info))
for i in ntmp_list:
t = pa_thread(self.checkStatusThreads,(info[i], i))
threads.append(t)
for i in ntmp_list:
threads[i].start()
for i in ntmp_list:
threads[i].join()
for i in ntmp_list:
t = threads[i].getResult()
info[i]['status'] = t
except Exception as e:
print('checkStatusMThreads:', str(e))
return info
def getAllPluginList(
self,
@ -294,6 +344,8 @@ class MwPlugin(object):
_info = info[start:end]
# print(info)
_info = self.checkStatusMThreads(_info)
return (_info, len(info))
def getList(
@ -316,9 +368,59 @@ class MwPlugin(object):
args['tojs'] = 'getSList'
args['row'] = size
rdata['list'] = mw.getPage(args)
return rdata
# shell/bash方式调用
def run(self, name, func,
version: str | None = '',
args: str | None = '',
script: str | None = 'index',
):
path = self.__plugin_dir + '/' + name + '/' + script + '.py'
if not os.path.exists(path):
path = self.__plugin_dir + '/' + name + '/' + name + '.py'
py = 'python3 ' + path
if args == '':
py_cmd = py + ' ' + func + ' ' + version
else:
py_cmd = py + ' ' + func + ' ' + version + ' ' + args
if not os.path.exists(path):
return ('', '')
py_cmd = 'cd ' + mw.getPanelDir() + " && "+ py_cmd
data = mw.execShell(py_cmd)
# print(data)
if mw.isDebugMode():
print('run:', py_cmd)
print(data)
# print os.path.exists(py_cmd)
return (data[0].strip(), data[1].strip())
# 映射包调用
def callback(self, name, func,
args: str | None = '',
script: str | None = 'index',
):
package = self.__plugin_dir + '/' + name
if not os.path.exists(package):
return (False, "插件不存在!")
if not package in sys.path:
sys.path.append(package)
eval_str = "__import__('" + script + "')." + func + '(' + args + ')'
newRet = None
try:
newRet = eval(eval_str)
except Exception as e:
print(mw.getTracebackInfo())
if mw.isDebugMode():
print('callback', eval_str)
return (True, newRet)
Loading…
Cancel
Save