def GET(self, name=""): display_name = xutils.unquote(name) name = xutils.get_real_path(display_name) if not name.endswith(".py"): name += ".py" script_name = "plugins/" + name if not os.path.exists(os.path.join(xconfig.PLUGINS_DIR, name)): error = "file `%s` not found" % script_name return xtemplate.render("error.html", error=error) try: try: cacheutil.zadd("plugins.history", time.time(), os.path.splitext(display_name)[0]) except TypeError: cacheutil.delete("plugins.history") cacheutil.zadd("plugins.history", time.time(), os.path.splitext(display_name)[0]) vars = dict() vars["script_name"] = script_name xutils.load_script(script_name, vars) main_class = vars.get("Main") if main_class != None: return main_class().render() else: return xtemplate.render("error.html", error="class `Main` not found!") except: error = xutils.print_exc() return xtemplate.render("error.html", error=error)
def POST(self): sys.stdout.record() try: name = xutils.get_argument("name") path = xutils.get_argument("path") confirmed = xutils.get_argument("confirmed") == "true" input = xutils.get_argument("input", "") vars = dict() xutils.load_script(name, vars=vars) main_func = vars.get("main", None) if main_func is not None: main_func(path=path, confirmed=confirmed, input=input) else: print("main(**kw)方法未定义") except Exception as e: xutils.print_exc() header = "执行 %s<hr>" % name # footer = "\n%s\n执行完毕,请确认下一步操作" % line footer = '' result = sys.stdout.pop_record() html = header + xutils.mark_text(result) html += '''<input id="inputText" class="col-md-12" placeholder="请输入参数" value="%s">''' % input html += '''<div><button class="btn-danger" onclick="runPlugin('%s', true)">确认执行</button></div>''' % name return dict(code="success", data=html)
def load_plugin(name): log_plugin_visit(name) context = xconfig.PLUGINS.get(name) if xconfig.DEBUG or context is None: script_name = "plugins/" + name if not os.path.exists(os.path.join(xconfig.PLUGINS_DIR, name)): return None vars = dict() vars["script_name"] = script_name xutils.load_script(script_name, vars) main_class = vars.get("Main") return main_class else: return context.clazz
def load_plugins(dirname): if not xconfig.LOAD_PLUGINS_ON_INIT: return xconfig.PLUGINS = {} for fname in os.listdir(dirname): fpath = os.path.join(dirname, fname) if os.path.isfile(fpath) and fname.endswith(".py"): script_name = "plugins/" + fname vars = dict() vars["script_name"] = script_name vars["fpath"] = fpath try: module = xutils.load_script(script_name, vars) main_class = vars.get("Main") if main_class != None: main_class.fname = fname main_class.fpath = fpath instance = main_class() context = PluginContext() context.fname = fname context.name = os.path.splitext(fname)[0] context.title = getattr(instance, "title", "") context.category = xutils.attrget(instance, "category") if hasattr(main_class, 'on_init'): instance.on_init(context) context.clazz = main_class xconfig.PLUGINS[fname] = context except: xutils.print_exc()
def load_plugin_file(fpath, fname=None): if fname is None: fname = os.path.basename(fpath) dirname = os.path.dirname(fpath) # plugin name pname = fsutil.get_relative_path(fpath, xconfig.PLUGINS_DIR) vars = dict() vars["script_name"] = pname vars["fpath"] = fpath try: module = xutils.load_script(fname, vars, dirname=dirname) main_class = vars.get("Main") if main_class != None: main_class.fname = fname main_class.fpath = fpath instance = main_class() context = PluginContext() context.fname = fname context.fpath = fpath context.name = os.path.splitext(fname)[0] context.title = getattr(instance, "title", "") context.category = xutils.attrget(instance, "category") context.required_role = xutils.attrget(instance, "required_role") context.url = "/plugins/%s" % pname if hasattr(main_class, 'on_init'): instance.on_init(context) context.clazz = main_class xconfig.PLUGINS_DICT[pname] = context except: # TODO 增加异常日志 xutils.print_exc()
def POST(self): func_ret = None sys.stdout.record() original_name = "" try: name = xutils.get_argument("name") path = xutils.get_argument("path") original_name = name confirmed = xutils.get_argument("confirmed") == "true" name, input = textutil.parse_simple_command(name) name = xconfig.get_alias(name, name) new_command = "%s %s" % (name, input) name, input = textutil.parse_simple_command(new_command) if not name.endswith(".py"): name += ".py" if input == "": input = xutils.get_argument("input", "") vars = dict() script_name = os.path.join("commands", name) script_path = os.path.join(xconfig.COMMANDS_DIR, name) if not os.path.exists(script_path): suggest_commands(name) else: xutils.load_script(script_name, vars=vars) main_func = vars.get("main", None) if main_func is not None: real_path = xutils.get_real_path(path) func_ret = main_func(path=real_path, confirmed=confirmed, input=input) else: print("main(**kw)方法未定义") except Exception as e: xutils.print_exc() header = "执行 %s<hr>" % name # footer = "\n%s\n执行完毕,请确认下一步操作" % line footer = '' result = sys.stdout.pop_record() if xutils.get_doctype(result) == "html": html = header + result[6:] else: html = header + xutils.mark_text(result) html += '''<input id="inputText" class="col-md-12 hide" placeholder="请输入参数" value="">''' html += '''<div><button class="btn-danger" onclick="runPlugin('%s', true)">确认执行</button></div>''' % original_name return dict(code="success", data=html, name=name)
def load_plugins(dirname): if not xconfig.LOAD_PLUGINS_ON_INIT: return for fname in os.listdir(dirname): fpath = os.path.join(dirname, fname) if os.path.isfile(fpath) and fname.endswith(".py"): script_name = "plugins/" + fname vars = dict() vars["script_name"] = script_name try: xutils.load_script(script_name, vars) main_class = vars.get("Main") if main_class != None: if hasattr(main_class, 'on_init'): main_class().on_init() except: xutils.print_exc()
def GET(self, name=""): if not name.endswith(".py"): name += ".py" script_name = "plugins/" + name if not os.path.exists(os.path.join(xconfig.PLUGINS_DIR, name)): error = "file `%s` not found" % script_name return xtemplate.render("error.html", error=error) try: self.history.put( Storage(name=os.path.splitext(name)[0], link=web.ctx.path)) vars = dict() vars["script_name"] = script_name xutils.load_script(script_name, vars) main_class = vars.get("Main") if main_class != None: return main_class().render() else: return xtemplate.render("error.html", error="class `Main` not found!") except: error = xutils.print_exc() return xtemplate.render("error.html", error=error)
def load_plugin(name): user_name = xauth.current_name() add_visit_log(user_name, name, "/plugins/" + name) context = xconfig.PLUGINS_DICT.get(name) if xconfig.DEBUG or context is None: script_name = "plugins/" + name fpath = os.path.join(xconfig.PLUGINS_DIR, name) if not os.path.exists(fpath): return None vars = dict() vars["script_name"] = script_name vars["fpath"] = fpath xutils.load_script(script_name, vars) main_class = vars.get("Main") main_class.fpath = fpath # 发现了新的插件,先临时重新加载一下,后续优化成按需加载的模式 load_plugins() return main_class else: return context.clazz
def load_plugin_file(fpath, fname=None): if not is_plugin_file(fpath): return if fname is None: fname = os.path.basename(fpath) dirname = os.path.dirname(fpath) # 相对于插件目录的名称 plugin_name = fsutil.get_relative_path(fpath, xconfig.PLUGINS_DIR) vars = dict() vars["script_name"] = plugin_name vars["fpath"] = fpath try: module = xutils.load_script(fname, vars, dirname=dirname) main_class = vars.get("Main") if main_class != None: # 实例化插件 main_class.fname = fname main_class.fpath = fpath instance = main_class() context = PluginContext() context.fname = fname context.fpath = fpath context.name = os.path.splitext(fname)[0] context.title = getattr(instance, "title", "") context.category = xutils.attrget(instance, "category") context.required_role = xutils.attrget(instance, "required_role") context.url = "/plugins/%s" % plugin_name context.clazz = main_class context.edit_link = "code/edit?path=" + fpath context.link = context.url # 初始化插件 if hasattr(main_class, 'on_init'): instance.on_init(context) # 注册插件 xconfig.PLUGINS_DICT[plugin_name] = context return context except: # TODO 增加异常日志 xutils.print_exc()