Beispiel #1
0
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()
Beispiel #2
0
    def GET(self, path=""):
        template_name = "code/code_edit.html"
        path = xutils.get_argument("path", "")
        key = xutils.get_argument("key", "")
        type = xutils.get_argument("type", "")
        readonly = False
        name, ext = os.path.splitext(path)
        if ext.lower() == ".md":
            template_name = "code/code_editPC.html"

        kw = Storage()

        # 处理嵌入页面
        handle_embed(kw)

        if path == "":
            return xtemplate.render(template_name,
                                    content="",
                                    error="path is empty")
        else:
            error = ""
            warn = ""
            try:
                path = self.resolve_path(path, type)
                max_file_size = xconfig.MAX_TEXT_SIZE
                if xutils.get_file_size(path, format=False) >= max_file_size:
                    warn = "文件过大,只显示部分内容"
                    readonly = True
                content = xutils.readfile(path, limit=max_file_size)
                plugin_name = fsutil.get_relative_path(path,
                                                       xconfig.PLUGINS_DIR)
                # 使用JavaScript来处理搜索关键字高亮问题
                # if key != "":
                #     content = xutils.html_escape(content)
                #     key     = xhtml_escape(key)
                #     content = textutil.replace(content, key, htmlutil.span("?", "search-key"), ignore_case=True, use_template=True)
                return xtemplate.render(template_name,
                                        show_preview=can_preview(path),
                                        readonly=readonly,
                                        error=error,
                                        warn=warn,
                                        pathlist=xutils.splitpath(path),
                                        name=os.path.basename(path),
                                        path=path,
                                        content=content,
                                        plugin_name=plugin_name,
                                        lines=content.count("\n") + 1,
                                        **kw)
            except Exception as e:
                xutils.print_exc()
                error = e
            return xtemplate.render(template_name,
                                    path=path,
                                    name="",
                                    readonly=readonly,
                                    error=error,
                                    lines=0,
                                    content="",
                                    **kw)
Beispiel #3
0
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()