49 lines
1.8KB

  1. import os
  2. import json
  3. from config import pconf, plugin_config, conf
  4. from common.log import logger
  5. class Plugin:
  6. def __init__(self):
  7. self.handlers = {}
  8. def load_config(self) -> dict:
  9. """
  10. 加载当前插件配置
  11. :return: 插件配置字典
  12. """
  13. # 优先获取 plugins/config.json 中的全局配置
  14. plugin_conf = pconf(self.name)
  15. if not plugin_conf:
  16. # 全局配置不存在,则获取插件目录下的配置
  17. plugin_config_path = os.path.join(self.path, "config.json")
  18. if os.path.exists(plugin_config_path):
  19. with open(plugin_config_path, "r", encoding="utf-8") as f:
  20. plugin_conf = json.load(f)
  21. # 写入全局配置内存
  22. plugin_config[self.name] = plugin_conf
  23. logger.debug(f"loading plugin config, plugin_name={self.name}, conf={plugin_conf}")
  24. return plugin_conf
  25. def save_config(self, config: dict):
  26. try:
  27. plugin_config[self.name] = config
  28. # 写入全局配置
  29. global_config_path = "./plugins/config.json"
  30. if os.path.exists(global_config_path):
  31. with open(global_config_path, "w", encoding='utf-8') as f:
  32. json.dump(plugin_config, f, indent=4, ensure_ascii=False)
  33. # 写入插件配置
  34. plugin_config_path = os.path.join(self.path, "config.json")
  35. if os.path.exists(plugin_config_path):
  36. with open(plugin_config_path, "w", encoding='utf-8') as f:
  37. json.dump(config, f, indent=4, ensure_ascii=False)
  38. except Exception as e:
  39. logger.warn("save plugin config failed: {}".format(e))
  40. def get_help_text(self, **kwargs):
  41. return "暂无帮助信息"