Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

46 lines
1.7KB

  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. logger.debug(f"loading plugin config, plugin_name={self.name}, conf={plugin_conf}")
  22. return plugin_conf
  23. def save_config(self, config: dict):
  24. try:
  25. plugin_config[self.name] = config
  26. # 写入全局配置
  27. global_config_path = "./plugins/config.json"
  28. if os.path.exists(global_config_path):
  29. with open(global_config_path, "w", encoding='utf-8') as f:
  30. json.dump(plugin_config, f, indent=4, ensure_ascii=False)
  31. # 写入插件配置
  32. plugin_config_path = os.path.join(self.path, "config.json")
  33. if os.path.exists(plugin_config_path):
  34. with open(plugin_config_path, "w", encoding='utf-8') as f:
  35. json.dump(config, f, indent=4, ensure_ascii=False)
  36. except Exception as e:
  37. logger.warn("save plugin config failed: {}".format(e))
  38. def get_help_text(self, **kwargs):
  39. return "暂无帮助信息"