You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
2.0KB

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