Beispiel #1
0
 async def start(self) -> None:
     if self.started:
         self.log.warning("Ignoring start() call to already started plugin")
         return
     elif not self.enabled:
         self.log.warning("Plugin disabled, not starting.")
         return
     cls = await self.loader.load()
     config_class = cls.get_config_class()
     if config_class:
         try:
             base = await self.loader.read_file("base-config.yaml")
             self.base_cfg = RecursiveDict(yaml.load(base.decode("utf-8")),
                                           CommentedMap)
         except (FileNotFoundError, KeyError):
             self.base_cfg = None
         self.config = config_class(self.load_config, lambda: self.base_cfg,
                                    self.save_config)
     self.plugin = cls(self.client.client, self.loop,
                       self.client.http_client, self.id, self.log,
                       self.config, self.mb_config["plugin_directories.db"])
     try:
         await self.plugin.start()
     except Exception:
         self.log.exception("Failed to start instance")
         self.db_instance.enabled = False
         return
     self.started = True
     self.log.info(
         f"Started instance of {self.loader.id} v{self.loader.version} "
         f"with user {self.client.id}")
Beispiel #2
0
 async def start(self) -> None:
     if self.started:
         self.log.warning("Ignoring start() call to already started plugin")
         return
     elif not self.enabled:
         self.log.warning("Plugin disabled, not starting.")
         return
     if not self.client or not self.loader:
         self.log.warning("Missing plugin instance dependencies, attempting to load...")
         if not self.load():
             return
     cls = await self.loader.load()
     config_class = cls.get_config_class()
     if config_class:
         try:
             base = await self.loader.read_file("base-config.yaml")
             self.base_cfg = RecursiveDict(yaml.load(base.decode("utf-8")), CommentedMap)
         except (FileNotFoundError, KeyError):
             self.base_cfg = None
         self.config = config_class(self.load_config, lambda: self.base_cfg, self.save_config)
     self.plugin = cls(client=self.client.client, loop=self.loop, http=self.client.http_client,
                       instance_id=self.id, log=self.log, config=self.config,
                       database=self.inst_db, webapp=self.inst_webapp,
                       webapp_url=self.inst_webapp_url)
     try:
         await self.plugin.internal_start()
     except Exception:
         self.log.exception("Failed to start instance")
         self.db_instance.enabled = False
         return
     self.started = True
     self.inst_db_tables = None
     self.log.info(f"Started instance of {self.loader.meta.id} v{self.loader.meta.version} "
                   f"with user {self.client.id}")
def recursive_get(data: Dict[str, Any], key: str) -> Any:
    key, next_key = RecursiveDict.parse_key(key)
    if next_key is not None:
        next_data = data.get(key, None)
        if not next_data:
            return None
        return recursive_get(next_data, next_key)
    return data.get(key, None)
def recursive_del(data: Dict[str, any], key: str) -> bool:
    key, next_key = RecursiveDict.parse_key(key)
    if next_key is not None:
        if key not in data:
            return False
        next_data = data.get(key, {})
        return recursive_del(next_data, next_key)
    if key in data:
        del data[key]
        return True
    return False
def recursive_set(data: Dict[str, Any], key: str, value: Any) -> bool:
    key, next_key = RecursiveDict.parse_key(key)
    if next_key is not None:
        if key not in data:
            data[key] = {}
        next_data = data.get(key, {})
        if not isinstance(next_data, dict):
            return False
        return recursive_set(next_data, next_key, value)
    data[key] = value
    return True
Beispiel #6
0
 def load_base() -> RecursiveDict[CommentedMap]:
     return RecursiveDict(config.load_base()["plugin_config"], CommentedMap)
Beispiel #7
0
    async def start(self) -> None:
        if self.started:
            self.log.warning("Ignoring start() call to already started plugin")
            return
        elif not self.enabled:
            self.log.warning("Plugin disabled, not starting.")
            return
        if not self.client or not self.loader:
            self.log.warning("Missing plugin instance dependencies, attempting to load...")
            if not await self.load():
                return
        cls = await self.loader.load()
        if self.loader.meta.webapp and self.inst_webapp is None:
            self.log.debug("Enabling webapp after plugin meta reload")
            self.enable_webapp()
        elif not self.loader.meta.webapp and self.inst_webapp is not None:
            self.log.debug("Disabling webapp after plugin meta reload")
            self.disable_webapp()
        if self.loader.meta.database:
            await self.start_database(cls.get_db_upgrade_table())
        config_class = cls.get_config_class()
        if config_class:
            try:
                base = await self.loader.read_file("base-config.yaml")
                self.base_cfg = RecursiveDict(yaml.load(base.decode("utf-8")), CommentedMap)
                buf = io.StringIO()
                yaml.dump(self.base_cfg._data, buf)
                self.base_cfg_str = buf.getvalue()
            except (FileNotFoundError, KeyError):
                self.base_cfg = None
                self.base_cfg_str = None
            if self.base_cfg:
                base_cfg_func = self.base_cfg.clone
            else:

                def base_cfg_func() -> None:
                    return None

            self.config = config_class(self.load_config, base_cfg_func, self.save_config)
        self.plugin = cls(
            client=self.client.client,
            loop=self.maubot.loop,
            http=self.client.http_client,
            instance_id=self.id,
            log=self.log,
            config=self.config,
            database=self.inst_db,
            loader=self.loader,
            webapp=self.inst_webapp,
            webapp_url=self.inst_webapp_url,
        )
        try:
            await self.plugin.internal_start()
        except Exception:
            self.log.exception("Failed to start instance")
            await self.update_enabled(False)
            return
        self.started = True
        self.inst_db_tables = None
        self.log.info(
            f"Started instance of {self.loader.meta.id} v{self.loader.meta.version} "
            f"with user {self.client.id}"
        )
Beispiel #8
0
def recursive_get(data: Dict[str, Any], key: str) -> Any:
    key, next_key = RecursiveDict.parse_key(key)
    if next_key is not None:
        return recursive_get(data[key], next_key)
    return data[key]