from abc import ABC, abstractmethod from typing import TYPE_CHECKING from flask import Blueprint if TYPE_CHECKING: from tofu_api.app import App class BaseBlueprint(Blueprint, ABC): """ Base class for Flask blueprints. """ # Reference to the application object app: 'App' @property @abstractmethod def name(self) -> str: """ The name of the blueprint. Will be prepended to each endpoint name. """ raise NotImplementedError @property @abstractmethod def import_name(self) -> str: """ The name of the blueprint package. Should be set to `__name__`. """ raise NotImplementedError @property @abstractmethod def url_prefix(self) -> str: """ Prefix for all URLs. """ raise NotImplementedError def __init__(self, app: 'App'): """ Initialize blueprint. Needs the Flask application object. """ super().__init__( name=self.name, import_name=self.import_name, url_prefix=self.url_prefix, ) self.app = app self.init_blueprint() @abstractmethod def init_blueprint(self) -> None: """ Register child blueprints and URL rules. """ raise NotImplementedError