From 4794a4bacbc4d23360689b6f31d1e8620d914228 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Wed, 22 Apr 2020 13:55:33 +0800 Subject: [PATCH] fix import --- nonebot/__init__.py | 4 +- nonebot/command/__init__.py | 2 +- nonebot/command/argfilter/extractors.py | 3 +- nonebot/command/argfilter/validators.py | 2 +- nonebot/command/group.py | 2 +- nonebot/helpers.py | 2 +- nonebot/natural_language.py | 4 +- nonebot/notice_request.py | 2 +- nonebot/plugin.py | 83 +++++++++++++++---------- 9 files changed, 61 insertions(+), 43 deletions(-) diff --git a/nonebot/__init__.py b/nonebot/__init__.py index f4f0a318..2981a888 100644 --- a/nonebot/__init__.py +++ b/nonebot/__init__.py @@ -131,11 +131,11 @@ def on_websocket_connect(func: Callable[[aiocqhttp.Event], Awaitable[None]]) \ from .exceptions import * -from .message import message_preprocessor, Message, MessageSegment +from .command import CommandSession, CommandGroup from .plugin import (on_command, on_natural_language, on_notice, on_request, load_plugin, load_plugins, load_builtin_plugins, get_loaded_plugins) -from .command import CommandSession, CommandGroup +from .message import message_preprocessor, Message, MessageSegment from .natural_language import NLPSession, NLPResult, IntentCommand from .notice_request import NoticeSession, RequestSession from .helpers import context_id diff --git a/nonebot/command/__init__.py b/nonebot/command/__init__.py index 8ec94614..abc06eaa 100644 --- a/nonebot/command/__init__.py +++ b/nonebot/command/__init__.py @@ -8,12 +8,12 @@ from typing import (Tuple, Union, Callable, Iterable, Any, Optional, List, Dict, Awaitable) from aiocqhttp import Event as CQEvent +from aiocqhttp.message import Message from nonebot import NoneBot, permission as perm from nonebot.command.argfilter import ValidateError from nonebot.helpers import context_id, send, render_expression from nonebot.log import logger -from nonebot.message import Message from nonebot.session import BaseSession from nonebot.typing import (CommandName_T, CommandArgs_T, CommandHandler_T, Message_T, State_T, Filter_T) diff --git a/nonebot/command/argfilter/extractors.py b/nonebot/command/argfilter/extractors.py index fbe46bb8..8c8b325a 100644 --- a/nonebot/command/argfilter/extractors.py +++ b/nonebot/command/argfilter/extractors.py @@ -1,7 +1,8 @@ import re from typing import List -from nonebot.message import Message +from aiocqhttp.message import Message + from nonebot.typing import Message_T diff --git a/nonebot/command/argfilter/validators.py b/nonebot/command/argfilter/validators.py index 8dfafa93..463783c1 100644 --- a/nonebot/command/argfilter/validators.py +++ b/nonebot/command/argfilter/validators.py @@ -1,8 +1,8 @@ import re from typing import Callable, Any -from nonebot.command.argfilter import ValidateError from nonebot.typing import Filter_T +from nonebot.command.argfilter import ValidateError class BaseValidator: diff --git a/nonebot/command/group.py b/nonebot/command/group.py index 6f7d4ca8..77e5d7bb 100644 --- a/nonebot/command/group.py +++ b/nonebot/command/group.py @@ -1,6 +1,6 @@ from typing import Union, Callable -from nonebot.command import on_command +from nonebot.plugin import on_command from nonebot.typing import CommandName_T diff --git a/nonebot/helpers.py b/nonebot/helpers.py index 25cceb62..6d4ae344 100644 --- a/nonebot/helpers.py +++ b/nonebot/helpers.py @@ -2,11 +2,11 @@ import hashlib import random from typing import Sequence, Callable, Any +from aiocqhttp.message import escape from aiocqhttp import Event as CQEvent from . import NoneBot from .exceptions import CQHttpError -from .message import escape from .typing import Message_T, Expression_T diff --git a/nonebot/natural_language.py b/nonebot/natural_language.py index a6b09241..554acd16 100644 --- a/nonebot/natural_language.py +++ b/nonebot/natural_language.py @@ -4,11 +4,11 @@ from functools import update_wrapper from typing import Set, Iterable, Optional, Callable, Union, NamedTuple from aiocqhttp import Event as CQEvent +from aiocqhttp.message import Message +from .log import logger from . import NoneBot, permission as perm from .command import call_command -from .log import logger -from .message import Message from .session import BaseSession from .typing import CommandName_T, CommandArgs_T diff --git a/nonebot/notice_request.py b/nonebot/notice_request.py index dff627e3..f455d6da 100644 --- a/nonebot/notice_request.py +++ b/nonebot/notice_request.py @@ -5,8 +5,8 @@ from aiocqhttp import Event as CQEvent from aiocqhttp.bus import EventBus from . import NoneBot -from .exceptions import CQHttpError from .log import logger +from .exceptions import CQHttpError from .session import BaseSession _bus = EventBus() diff --git a/nonebot/plugin.py b/nonebot/plugin.py index b7ef95ee..ff89640c 100644 --- a/nonebot/plugin.py +++ b/nonebot/plugin.py @@ -9,8 +9,8 @@ from typing import Any, Set, Dict, Union, Optional, Iterable, Callable from .log import logger from nonebot import permission as perm from .command import Command, CommandManager -from .natural_language import NLProcessor, NLPManager from .notice_request import _bus, EventHandler +from .natural_language import NLProcessor, NLPManager from .typing import CommandName_T, CommandHandler_T _tmp_command: Set[Command] = set() @@ -49,7 +49,7 @@ class PluginManager: """Register a plugin Args: - name (str): module path + module_path (str): module path plugin (Plugin): Plugin object """ if module_path in cls._plugins: @@ -59,10 +59,10 @@ class PluginManager: @classmethod def get_plugin(cls, module_path: str) -> Optional[Plugin]: - """Get plugin object by plugin path + """Get plugin object by plugin module path Args: - name (str): plugin path + module_path (str): Plugin module path Returns: Optional[Plugin]: Plugin object @@ -71,6 +71,17 @@ class PluginManager: @classmethod def remove_plugin(cls, module_path: str) -> bool: + """Remove a plugin by plugin module path + + ** Warning: This function not remove plugin actually! ** + ** Just remove command, nlprocessor and event handlers ** + + Args: + module_path (str): Plugin module path + + Returns: + bool: Success or not + """ plugin = cls.get_plugin(module_path) if not plugin: warnings.warn(f"Plugin {module_path} not exists") @@ -86,17 +97,18 @@ class PluginManager: return True @classmethod - def switch_plugin_global(cls, name: str, + def switch_plugin_global(cls, + module_path: str, state: Optional[bool] = None) -> None: """Change plugin state globally or simply switch it if `state` is None Args: - name (str): Plugin name + module_path (str): Plugin module path state (Optional[bool]): State to change to. Defaults to None. """ - plugin = cls.get_plugin(name) + plugin = cls.get_plugin(module_path) if not plugin: - warnings.warn(f"Plugin {name} not found") + warnings.warn(f"Plugin {module_path} not found") return for command in plugin.commands: CommandManager.switch_command_global(command.name, state) @@ -111,49 +123,52 @@ class PluginManager: _bus.subscribe(event, event_handler.func) @classmethod - def switch_command_global(cls, name: str, + def switch_command_global(cls, + module_path: str, state: Optional[bool] = None) -> None: """Change plugin command state globally or simply switch it if `state` is None Args: - name (str): Plugin name + module_path (str): Plugin module path state (Optional[bool]): State to change to. Defaults to None. """ - plugin = cls.get_plugin(name) + plugin = cls.get_plugin(module_path) if not plugin: - warnings.warn(f"Plugin {name} not found") + warnings.warn(f"Plugin {module_path} not found") return for command in plugin.commands: CommandManager.switch_command_global(command.name, state) @classmethod - def switch_nlprocessor_global(cls, name: str, + def switch_nlprocessor_global(cls, + module_path: str, state: Optional[bool] = None) -> None: """Change plugin nlprocessor state globally or simply switch it if `state` is None Args: - name (str): Plugin name + module_path (str): Plugin module path state (Optional[bool]): State to change to. Defaults to None. """ - plugin = cls.get_plugin(name) + plugin = cls.get_plugin(module_path) if not plugin: - warnings.warn(f"Plugin {name} not found") + warnings.warn(f"Plugin {module_path} not found") return for processor in plugin.nl_processors: NLPManager.switch_nlprocessor_global(processor, state) @classmethod - def switch_eventhandler_global(cls, name: str, + def switch_eventhandler_global(cls, + module_path: str, state: Optional[bool] = None) -> None: """Change plugin event handler state globally or simply switch it if `state` is None Args: - name (str): Plugin name + module_path (str): Plugin module path state (Optional[bool]): State to change to. Defaults to None. """ - plugin = cls.get_plugin(name) + plugin = cls.get_plugin(module_path) if not plugin: - warnings.warn(f"Plugin {name} not found") + warnings.warn(f"Plugin {module_path} not found") return for event_handler in plugin.event_handlers: for event in event_handler.events: @@ -163,52 +178,54 @@ class PluginManager: event] and state != False: _bus.subscribe(event, event_handler.func) - def switch_plugin(self, name: str, state: Optional[bool] = None) -> None: + def switch_plugin(self, module_path: str, + state: Optional[bool] = None) -> None: """Change plugin state or simply switch it if `state` is None Tips: This method will only change the state of the plugin's commands and natural language processors since change - state of the event handler partially is meaningless. + state of the event handler for message is meaningless. Args: - name (str): Plugin name + module_path (str): Plugin module path state (Optional[bool]): State to change to. Defaults to None. """ - plugin = self.get_plugin(name) + plugin = self.get_plugin(module_path) if not plugin: - warnings.warn(f"Plugin {name} not found") + warnings.warn(f"Plugin {module_path} not found") return for command in plugin.commands: self.cmd_manager.switch_command(command.name, state) for nl_processor in plugin.nl_processors: self.nlp_manager.switch_nlprocessor(nl_processor, state) - def switch_command(self, name: str, state: Optional[bool] = None) -> None: + def switch_command(self, module_path: str, + state: Optional[bool] = None) -> None: """Change plugin command state or simply switch it if `state` is None Args: - name (str): Plugin name + module_path (str): Plugin module path state (Optional[bool]): State to change to. Defaults to None. """ - plugin = self.get_plugin(name) + plugin = self.get_plugin(module_path) if not plugin: - warnings.warn(f"Plugin {name} not found") + warnings.warn(f"Plugin {module_path} not found") return for command in plugin.commands: self.cmd_manager.switch_command(command.name, state) - def switch_nlprocessor(self, name: str, + def switch_nlprocessor(self, module_path: str, state: Optional[bool] = None) -> None: """Change plugin nlprocessor state or simply switch it if `state` is None Args: - name (str): Plugin name + module_path (str): Plugin module path state (Optional[bool]): State to change to. Defaults to None. """ - plugin = self.get_plugin(name) + plugin = self.get_plugin(module_path) if not plugin: - warnings.warn(f"Plugin {name} not found") + warnings.warn(f"Plugin {module_path} not found") return for processor in plugin.nl_processors: self.nlp_manager.switch_nlprocessor(processor, state)