A class to manage the system configuration of AstrBot, aka ACM
| 29 | |
| 30 | |
| 31 | class AstrBotConfigManager: |
| 32 | """A class to manage the system configuration of AstrBot, aka ACM""" |
| 33 | |
| 34 | def __init__( |
| 35 | self, |
| 36 | default_config: AstrBotConfig, |
| 37 | ucr: UmopConfigRouter, |
| 38 | sp: SharedPreferences, |
| 39 | ) -> None: |
| 40 | self.sp = sp |
| 41 | self.ucr = ucr |
| 42 | self.confs: dict[str, AstrBotConfig] = {} |
| 43 | """uuid / "default" -> AstrBotConfig""" |
| 44 | self.confs["default"] = default_config |
| 45 | self.abconf_data = None |
| 46 | self._load_all_configs() |
| 47 | |
| 48 | def _get_abconf_data(self) -> dict: |
| 49 | """获取所有的 abconf 数据""" |
| 50 | if self.abconf_data is None: |
| 51 | self.abconf_data = self.sp.get( |
| 52 | "abconf_mapping", |
| 53 | {}, |
| 54 | scope="global", |
| 55 | scope_id="global", |
| 56 | ) |
| 57 | return self.abconf_data |
| 58 | |
| 59 | def _load_all_configs(self) -> None: |
| 60 | """Load all configurations from the shared preferences.""" |
| 61 | abconf_data = self._get_abconf_data() |
| 62 | self.abconf_data = abconf_data |
| 63 | for uuid_, meta in abconf_data.items(): |
| 64 | filename = meta["path"] |
| 65 | conf_path = os.path.join(get_astrbot_config_path(), filename) |
| 66 | if os.path.exists(conf_path): |
| 67 | conf = AstrBotConfig(config_path=conf_path) |
| 68 | self.confs[uuid_] = conf |
| 69 | else: |
| 70 | logger.warning( |
| 71 | f"Config file {conf_path} for UUID {uuid_} does not exist, skipping.", |
| 72 | ) |
| 73 | continue |
| 74 | |
| 75 | def _load_conf_mapping(self, umo: str | MessageSession) -> ConfInfo: |
| 76 | """获取指定 umo 的配置文件 uuid, 如果不存在则返回默认配置(返回 "default") |
| 77 | |
| 78 | Returns: |
| 79 | ConfInfo: 包含配置文件的 uuid, 路径和名称等信息, 是一个 dict 类型 |
| 80 | |
| 81 | """ |
| 82 | # uuid -> { "path": str, "name": str } |
| 83 | abconf_data = self._get_abconf_data() |
| 84 | |
| 85 | if isinstance(umo, MessageSession): |
| 86 | umo = str(umo) |
| 87 | else: |
| 88 | try: |