mirror of
				https://github.com/LiteyukiStudio/LiteyukiBot.git
				synced 2025-10-30 23:46:30 +00:00 
			
		
		
		
	✨ 新增observer类和开发调试器
This commit is contained in:
		| @@ -1,21 +0,0 @@ | ||||
| # -*- coding: utf-8 -*- | ||||
| """ | ||||
| Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved  | ||||
|  | ||||
| @Time    : 2024/8/11 下午8:50 | ||||
| @Author  : snowykami | ||||
| @Email   : snowykami@outlook.com | ||||
| @File    : __init__.py.py | ||||
| @Software: PyCharm | ||||
| """ | ||||
| from liteyuki.core import IS_MAIN_PROCESS | ||||
| from liteyuki.plugin import PluginMetadata | ||||
|  | ||||
| from .observer import * | ||||
|  | ||||
| __plugin_meta__ = PluginMetadata( | ||||
|     name="代码热重载监视", | ||||
| ) | ||||
|  | ||||
| if IS_MAIN_PROCESS: | ||||
|     config = get_config("liteyuki.reload") | ||||
| @@ -1,44 +0,0 @@ | ||||
| # -*- coding: utf-8 -*- | ||||
| """ | ||||
| Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved  | ||||
|  | ||||
| @Time    : 2024/8/11 下午10:01 | ||||
| @Author  : snowykami | ||||
| @Email   : snowykami@outlook.com | ||||
| @File    : observer.py | ||||
| @Software: PyCharm | ||||
| """ | ||||
| from watchdog.events import FileSystemEventHandler | ||||
| from watchdog.observers import Observer | ||||
| from liteyuki import get_config, logger, get_bot | ||||
|  | ||||
| liteyuki_bot = get_bot() | ||||
|  | ||||
| if get_config("debug", False): | ||||
|  | ||||
|     src_directories = ( | ||||
|             "src/nonebot_plugins", | ||||
|             "src/utils", | ||||
|     ) | ||||
|     src_excludes_extensions = ( | ||||
|             "pyc", | ||||
|     ) | ||||
|     logger.debug("Liteyuki Reload enabled, watching for file changes...") | ||||
|  | ||||
|     class CodeModifiedHandler(FileSystemEventHandler): | ||||
|         """ | ||||
|         Handler for code file changes | ||||
|         """ | ||||
|         def on_modified(self, event): | ||||
|             if event.src_path.endswith( | ||||
|                     src_excludes_extensions) or event.is_directory or "__pycache__" in event.src_path: | ||||
|                 return | ||||
|             logger.info(f"{event.src_path} modified, reloading bot...") | ||||
|             liteyuki_bot.restart_process("nonebot") | ||||
|  | ||||
|     code_modified_handler = CodeModifiedHandler() | ||||
|  | ||||
|     observer = Observer() | ||||
|     for directory in src_directories: | ||||
|         observer.schedule(code_modified_handler, directory, recursive=True) | ||||
|     observer.start() | ||||
| @@ -15,14 +15,13 @@ from liteyuki.plugin import PluginMetadata | ||||
| from liteyuki import get_bot | ||||
| from liteyuki.comm import Channel, set_channel | ||||
| from liteyuki.core import IS_MAIN_PROCESS | ||||
|  | ||||
| from .nb_utils import adapter_manager, driver_manager | ||||
|  | ||||
| __plugin_meta__ = PluginMetadata( | ||||
|     name="NoneBot2启动器", | ||||
| ) | ||||
|  | ||||
| liteyuki = get_bot() | ||||
|  | ||||
|  | ||||
| def nb_run(chan_active: "Channel", chan_passive: "Channel", **kwargs): | ||||
|     """ | ||||
| @@ -39,7 +38,7 @@ def nb_run(chan_active: "Channel", chan_passive: "Channel", **kwargs): | ||||
|     set_channel("nonebot-active", chan_active) | ||||
|     set_channel("nonebot-passive", chan_passive) | ||||
|  | ||||
|     kwargs.update(kwargs.get("nonebot", {}))    # nonebot配置优先 | ||||
|     kwargs.update(kwargs.get("nonebot", {}))  # nonebot配置优先 | ||||
|     nonebot.init(**kwargs) | ||||
|  | ||||
|     driver_manager.init(config=kwargs) | ||||
| @@ -50,6 +49,11 @@ def nb_run(chan_active: "Channel", chan_passive: "Channel", **kwargs): | ||||
|  | ||||
|  | ||||
| if IS_MAIN_PROCESS: | ||||
|     from .dev_reloader import * | ||||
|  | ||||
|     liteyuki = get_bot() | ||||
|  | ||||
|  | ||||
|     @liteyuki.on_after_start | ||||
|     def start_run_nonebot(): | ||||
|         liteyuki.process_manager.add_target(name="nonebot", target=nb_run, args=(), kwargs=liteyuki.config) | ||||
|   | ||||
							
								
								
									
										22
									
								
								liteyuki/plugins/nonebot_launcher/dev_reloader.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								liteyuki/plugins/nonebot_launcher/dev_reloader.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| # -*- coding: utf-8 -*- | ||||
| """ | ||||
| NoneBot 开发环境重载监视器 | ||||
| """ | ||||
| import os.path | ||||
|  | ||||
| from liteyuki.dev import observer | ||||
| from liteyuki import get_bot, logger | ||||
| from watchdog.events import FileSystemEvent | ||||
|  | ||||
| liteyuki = get_bot() | ||||
|  | ||||
| exclude_extensions = (".pyc", ".pyo") | ||||
|  | ||||
|  | ||||
| @observer.on_file_system_event( | ||||
|     directories=("src/nonebot_plugins",), | ||||
|     event_filter=lambda event: not event.src_path.endswith(exclude_extensions) and ("__pycache__" not in event.src_path ) and os.path.isfile(event.src_path) | ||||
| ) | ||||
| def restart_nonebot_process(event: FileSystemEvent): | ||||
|     logger.debug(f"File {event.src_path} changed, reloading nonebot...") | ||||
|     liteyuki.restart_process("nonebot") | ||||
		Reference in New Issue
	
	Block a user