import{_ as n,o as a,c as s,e as t}from"./app-BQBtFyJe.js";const e={},p=t(`

def load_plugin(module_path: str | Path) -> Optional[Plugin]

加载单个插件,可以是本地插件或是通过 pip 安装的插件。

参数:

module_path: 插件名称 \`path.to.your.plugin\`

或插件路径 \`pathlib.Path(path/to/your/plugin)\`
源代码
def load_plugin(module_path: str | Path) -> Optional[Plugin]:
    """加载单个插件,可以是本地插件或是通过 \`pip\` 安装的插件。

    参数:
        module_path: 插件名称 \`path.to.your.plugin\`
        或插件路径 \`pathlib.Path(path/to/your/plugin)\`
    """
    module_path = path_to_module_name(Path(module_path)) if isinstance(module_path, Path) else module_path
    try:
        module = import_module(module_path)
        _plugins[module.__name__] = Plugin(name=module.__name__, module=module, module_name=module_path)
        if module.__dict__.get('__plugin_metadata__', None):
            metadata: 'PluginMetadata' = module.__dict__['__plugin_metadata__']
            display_name = module.__name__.split('.')[-1]
        elif module.__dict__.get('__liteyuki_plugin_meta__', None):
            metadata: 'PluginMetadata' = module.__dict__['__liteyuki_plugin_meta__']
            display_name = format_display_name(f"{metadata.name}({module.__name__.split('.')[-1]})", metadata.type)
        elif module.__dict__.get('__plugin_meta__', None):
            metadata: 'PluginMetadata' = module.__dict__['__plugin_meta__']
            display_name = format_display_name(f"{metadata.name}({module.__name__.split('.')[-1]})", metadata.type)
        else:
            logger.opt(colors=True).warning(f'The metadata of Liteyuki plugin "{module.__name__}" is not specified, use empty.')
            metadata = PluginMetadata(name=module.__name__)
            display_name = module.__name__.split('.')[-1]
        _plugins[module.__name__].metadata = metadata
        logger.opt(colors=True).success(f'Succeeded to load liteyuki plugin "{display_name}"')
        return _plugins[module.__name__]
    except Exception as e:
        logger.opt(colors=True).success(f'Failed to load liteyuki plugin "<r>{module_path}</r>"')
        traceback.print_exc()
        return None

def load_plugins() -> set[Plugin]

导入文件夹下多个插件

参数:

plugin_dir: 文件夹路径

ignore_warning: 是否忽略警告,通常是目录不存在或目录为空
源代码
def load_plugins(*plugin_dir: str, ignore_warning: bool=True) -> set[Plugin]:
    """导入文件夹下多个插件

    参数:
        plugin_dir: 文件夹路径
        ignore_warning: 是否忽略警告,通常是目录不存在或目录为空
    """
    plugins = set()
    for dir_path in plugin_dir:
        if not os.path.exists(dir_path):
            if not ignore_warning:
                logger.warning(f"Plugins dir '{dir_path}' does not exist.")
            continue
        if not os.listdir(dir_path):
            if not ignore_warning:
                logger.warning(f"Plugins dir '{dir_path}' is empty.")
            continue
        if not os.path.isdir(dir_path):
            if not ignore_warning:
                logger.warning(f"Plugins dir '{dir_path}' is not a directory.")
            continue
        for f in os.listdir(dir_path):
            path = Path(os.path.join(dir_path, f))
            module_name = None
            if os.path.isfile(path) and f.endswith('.py') and (f != '__init__.py'):
                module_name = f'{path_to_module_name(Path(dir_path))}.{f[:-3]}'
            elif os.path.isdir(path) and os.path.exists(os.path.join(path, '__init__.py')):
                module_name = path_to_module_name(path)
            if module_name:
                load_plugin(module_name)
                if _plugins.get(module_name):
                    plugins.add(_plugins[module_name])
    return plugins

def format_display_name(display_name: str, plugin_type: PluginType) -> str

设置插件名称颜色,根据不同类型插件设置颜色

Args:

display_name: 插件名称

plugin_type: 插件类型

Returns:

str: 设置后的插件名称 <y>name</y>
源代码
def format_display_name(display_name: str, plugin_type: PluginType) -> str:
    """
    设置插件名称颜色,根据不同类型插件设置颜色
    Args:
        display_name: 插件名称
        plugin_type: 插件类型

    Returns:
        str: 设置后的插件名称 <y>name</y>
    """
    color = 'y'
    match plugin_type:
        case PluginType.APPLICATION:
            color = 'm'
        case PluginType.TEST:
            color = 'g'
        case PluginType.MODULE:
            color = 'e'
        case PluginType.SERVICE:
            color = 'c'
    return f'<{color}>{display_name} [{plugin_type.name}]</{color}>'

var module_path = path_to_module_name(Path(module_path)) if isinstance(module_path, Path) else module_path

var plugins = set()

var color = 'y'

var module = import_module(module_path)

var display_name = module.__name__.split('.')[-1]

var path = Path(os.path.join(dir_path, f))

var module_name = None

var color = 'm'

var color = 'g'

var color = 'e'

var color = 'c'

var display_name = format_display_name(f"{metadata.name}({module.__name__.split('.')[-1]})", metadata.type)

var module_name = f'{path_to_module_name(Path(dir_path))}.{f[:-3]}'

var display_name = format_display_name(f"{metadata.name}({module.__name__.split('.')[-1]})", metadata.type)

var metadata = PluginMetadata(name=module.__name__)

var display_name = module.__name__.split('.')[-1]

var module_name = path_to_module_name(path)

`,34),o=[p];function l(i,c){return a(),s("div",null,o)}const d=n(e,[["render",l],["__file","load.html.vue"]]),r=JSON.parse(`{"path":"/dev/api/plugin/load.html","title":"liteyuki.plugin.load","lang":"zh-CN","frontmatter":{"title":"liteyuki.plugin.load","order":1,"icon":"laptop-code","category":"API","description":"def load_plugin(module_path: str | Path) -> Optional[Plugin] 加载单个插件,可以是本地插件或是通过 pip 安装的插件。 参数: 源代码 def load_plugins() -> set[Plugin] 导入文件夹下多个插件 参数: 源代码 def format_display_name(d...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/plugin/load.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/plugin/load.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot 轻雪机器人"}],["meta",{"property":"og:title","content":"liteyuki.plugin.load"}],["meta",{"property":"og:description","content":"def load_plugin(module_path: str | Path) -> Optional[Plugin] 加载单个插件,可以是本地插件或是通过 pip 安装的插件。 参数: 源代码 def load_plugins() -> set[Plugin] 导入文件夹下多个插件 参数: 源代码 def format_display_name(d..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2024-08-21T09:59:21.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-21T09:59:21.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.plugin.load\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-21T09:59:21.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"def load_plugin(module_path: str | Path) -> Optional[Plugin]","slug":"def-load-plugin-module-path-str-path-optional-plugin","link":"#def-load-plugin-module-path-str-path-optional-plugin","children":[]},{"level":3,"title":"def load_plugins() -> set[Plugin]","slug":"def-load-plugins-set-plugin","link":"#def-load-plugins-set-plugin","children":[]},{"level":3,"title":"def format_display_name(display_name: str, plugin_type: PluginType) -> str","slug":"def-format-display-name-display-name-str-plugin-type-plugintype-str","link":"#def-format-display-name-display-name-str-plugin-type-plugintype-str","children":[]},{"level":3,"title":"var module_path = path_to_module_name(Path(module_path)) if isinstance(module_path, Path) else module_path","slug":"var-module-path-path-to-module-name-path-module-path-if-isinstance-module-path-path-else-module-path","link":"#var-module-path-path-to-module-name-path-module-path-if-isinstance-module-path-path-else-module-path","children":[]},{"level":3,"title":"var plugins = set()","slug":"var-plugins-set","link":"#var-plugins-set","children":[]},{"level":3,"title":"var color = 'y'","slug":"var-color-y","link":"#var-color-y","children":[]},{"level":3,"title":"var module = import_module(module_path)","slug":"var-module-import-module-module-path","link":"#var-module-import-module-module-path","children":[]},{"level":3,"title":"var display_name = module.__name__.split('.')[-1]","slug":"var-display-name-module-name-split-1","link":"#var-display-name-module-name-split-1","children":[]},{"level":3,"title":"var path = Path(os.path.join(dir_path, f))","slug":"var-path-path-os-path-join-dir-path-f","link":"#var-path-path-os-path-join-dir-path-f","children":[]},{"level":3,"title":"var module_name = None","slug":"var-module-name-none","link":"#var-module-name-none","children":[]},{"level":3,"title":"var color = 'm'","slug":"var-color-m","link":"#var-color-m","children":[]},{"level":3,"title":"var color = 'g'","slug":"var-color-g","link":"#var-color-g","children":[]},{"level":3,"title":"var color = 'e'","slug":"var-color-e","link":"#var-color-e","children":[]},{"level":3,"title":"var color = 'c'","slug":"var-color-c","link":"#var-color-c","children":[]},{"level":3,"title":"var display_name = format_display_name(f\\"{metadata.name}({module.__name__.split('.')[-1]})\\", metadata.type)","slug":"var-display-name-format-display-name-f-metadata-name-module-name-split-1-metadata-type","link":"#var-display-name-format-display-name-f-metadata-name-module-name-split-1-metadata-type","children":[]},{"level":3,"title":"var module_name = f'{path_to_module_name(Path(dir_path))}.{f[:-3]}'","slug":"var-module-name-f-path-to-module-name-path-dir-path-f-3","link":"#var-module-name-f-path-to-module-name-path-dir-path-f-3","children":[]},{"level":3,"title":"var display_name = format_display_name(f\\"{metadata.name}({module.__name__.split('.')[-1]})\\", metadata.type)","slug":"var-display-name-format-display-name-f-metadata-name-module-name-split-1-metadata-type-1","link":"#var-display-name-format-display-name-f-metadata-name-module-name-split-1-metadata-type-1","children":[]},{"level":3,"title":"var metadata = PluginMetadata(name=module.__name__)","slug":"var-metadata-pluginmetadata-name-module-name","link":"#var-metadata-pluginmetadata-name-module-name","children":[]},{"level":3,"title":"var display_name = module.__name__.split('.')[-1]","slug":"var-display-name-module-name-split-1-1","link":"#var-display-name-module-name-split-1-1","children":[]},{"level":3,"title":"var module_name = path_to_module_name(path)","slug":"var-module-name-path-to-module-name-path","link":"#var-module-name-path-to-module-name-path","children":[]}],"git":{"createdTime":1724031826000,"updatedTime":1724234361000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":4}]},"readingTime":{"minutes":2.22,"words":665},"filePathRelative":"dev/api/plugin/load.md","localizedDate":"2024年8月19日","autoDesc":true}`);export{d as comp,r as data};