You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.7 KiB

1 year ago
  1. #coding: utf-8
  2. import os,sys,json
  3. import core.include.public as public
  4. #执行模块方法(模块名,方法名,参数)
  5. def module_run(module_name, def_name, def_args):
  6. if not module_name or not def_name: return public.returnMsg(False,'模块名称和模块方法名称不能为空!')
  7. if not path_check(module_name) or not path_check(def_name): return public.returnMsg(False,'模块名或方法名不能包含特殊符号!')
  8. panel_path = public.get_panel_path()
  9. filename = "{}/modules/{}Module/{}Module.py".format(panel_path,module_name,module_name)
  10. if not os.path.exists(filename):
  11. filename = "{}/modules/{}Module/main.py".format(panel_path,module_name)
  12. if not os.path.exists(filename):
  13. filename = "{}/plugin/{}/main.py".format(panel_path,module_name)
  14. if not os.path.exists(filename):
  15. filename = "{}/plugin/{}Module/{}Plugin.py".format(panel_path,module_name,module_name)
  16. if not os.path.exists(filename):
  17. return public.returnMsg(False,'指定模块或插件不存在')
  18. _obj = public.get_script_object(filename)
  19. if not _obj: return public.returnMsg(False,'模块加载失败: %s' % module_name)
  20. if hasattr(_obj, "items") and hasattr(_obj, "setdefault"):
  21. return _obj
  22. class_name = "main"
  23. if not hasattr(_obj, class_name):
  24. return public.returnMsg(False,'找不到入口类: %s' % class_name)
  25. class_obj = getattr(_obj,class_name, None)
  26. if not class_obj:
  27. return public.returnMsg(False,'获取入口类失败' % module_name)
  28. try:
  29. class_func = class_obj()
  30. except:
  31. return public.returnMsg(False,'模块入口实例化失败' % module_name)
  32. if not hasattr(class_func, def_name):
  33. return public.returnMsg(False,'在[%s]模块中找不到[%s]方法' % (class_name,def_name))
  34. def_func = getattr(class_func, def_name, None)
  35. if not def_func:
  36. return public.returnMsg(False,'获取方法失败')
  37. if 'module_get_object' in def_args:
  38. return def_func
  39. result = def_func(def_args)
  40. return result
  41. #获取指定模块对象(文件全路径)
  42. def get_module(filename):
  43. if not filename: return public.returnMsg(False,'模块路径不能为空!')
  44. if "./" in filename: return public.returnMsg(False,'模块路径不能为相对路径')
  45. return public.get_script_object(filename)
  46. #检查路径是否合法
  47. def path_check(path):
  48. list = ["./","..",",",";",":","?","'","\"","<",">","|","\\","\n","\r","\t","\b","\a","\f","\v","*","%","&","$","#","@","!","~","`","^","(",")","+","=","{","}","[","]"]
  49. for i in path:
  50. if i in list:
  51. return False
  52. return True