async def _ws(): async with aiohttp.ClientSession() as client: async with client.ws_connect( f"{self._api_root}all?sessionKey={self._session_key}", **kwargs) as ws: while True: try: event = await ws.receive_json() except TypeError: if ws.closed: logger.warning( 'Websocket closed, try relogin in 10s') await asyncio.sleep(10) await self.relogin() return logger.error(f"TypeError in parsing ws event") continue if not event: continue logger.debug(f"[event] {event}") try: event = self.as_event(event) if event: self.handle_event_nowait(event) except Exception as e: logger.critical(e) return
async def prepare(self, method=None): if self.id: return elif method and method != self.method: if self.task: logger.warning( f"Unused file upload cache {self.method} {method}") self.method = method return await self._prepare() elif self.task: await self.task else: raise ValueError("No upload method specified for File!")
def remove_service(key: Union[str, Service]) -> bool: """Remove a service by service object or key This function only remove service from global list :param key: Service object or key :return: Success or not """ service = get_service(key) if isinstance(key, str) else key if not service: logger.warning(f"Service {key} not exists") return False del _loaded_services[service.key] return True
async def reload_plugin(key: Union[str, Plugin]) -> Optional[Plugin]: """Reload a Plugin :param key: Plugin object or module path or name :return: Success or not """ plugin = get_plugin(key) if isinstance(key, str) else key if not plugin: logger.warning(f"Plugin {key} not exists") return None module_path = plugin.path return await load_plugin( module_path=module_path) if await unload_plugin(plugin) else None
def add_plugin(plugin: Plugin, force: bool = False) -> None: """Register a Plugin :param plugin: Plugin object :param force: Ignore existed :return: """ if not force and plugin.path in _loaded_plugins: logger.warning(f"Plugin {plugin} already exists") return if plugin.name: _loaded_plugins[plugin.name] = plugin if plugin.path: _loaded_plugins[plugin.path] = plugin
def remove_plugin(key: Union[str, Plugin]) -> bool: """Remove a Plugin :param key: Plugin object or module path or name :return: Success or not """ plugin = get_plugin(key) if isinstance(key, str) else key if not plugin: logger.warning(f"Plugin {key} not exists") return False if plugin.name in _loaded_plugins: del _loaded_plugins[plugin.name] if plugin.path in _loaded_plugins: del _loaded_plugins[plugin.path] return True
async def unload_plugin(key: Union[str, Plugin], forced: bool = False) -> bool: """Unload a Plugin :param key: Plugin object or module path or name :param forced: Forced to unload :return: Success or not """ plugin = get_plugin(key) if isinstance(key, str) else key if not plugin: logger.warning(f"Plugin {key} not exists") return False plugin_name = plugin.name await meta_provider.send( MetaEvent(MetaEventType.PluginUnload, plugin=plugin)) for service in plugin.services.values(): try: remove_service(service) except Exception as e: logger.exception(e) logger.error(f'Failed to unload service "{service}", error: {e}') result = remove_plugin(plugin) if not result and not forced: return False if plugin.path: for module in list( filter(lambda x: x.startswith(plugin.path), sys.modules.keys())): del sys.modules[module] meta_provider.send_nowait( MetaEvent(MetaEventType.PluginUnloaded, plugin=plugin)) logger.info(f'Succeeded to unload Plugin "{plugin_name}"') return True
def register_session(session: BotSession, qq: ContactIdType = None): qq = qq or session.qq if qq in _sessions: logger.warning(f'A session already registered to {qq} !') _sessions[qq] = session _sessions_inverse[id(session)] = qq