import abc from typing import Optional, Type from telebot import TeleBot from telebot.types import CallbackQuery, InlineKeyboardMarkup from ..i18n import I18N class Markup (metaclass=abc.ABCMeta): tag: str def __init__(self, bot: TeleBot, i18n: I18N): self.bot = bot self.t = i18n def __call__(self, *args, **kwargs): return self.build(*args, **kwargs) def check(self, call: CallbackQuery) -> bool: return call.data == self.tag @abc.abstractmethod def build(self, *args, **kwargs) -> Optional[InlineKeyboardMarkup]: pass class DummyMarkup (Markup): tag = "__dummy" def check(self, call: CallbackQuery) -> bool: return True def build(self, *args, **kwargs) -> Optional[InlineKeyboardMarkup]: return None class MarkupManager: def __init__(self, bot: TeleBot, i18n: I18N): self.bot = bot self.i18n = i18n self._prototypes: list[Markup] = [] self._dummy = DummyMarkup(self.bot, self.i18n) def register_prototype(self, markup_proto_class: Type[Markup]): self._prototypes.append(markup_proto_class(self.bot, self.i18n)) def __call__(self, tag: str, *args, **kwargs): return self.get(tag).build(*args, **kwargs) def get(self, tag: str) -> Optional[Markup]: for mp in self._prototypes: if mp.tag == tag: return mp return None