from jinja2 import Environment, BaseLoader, TemplateNotFound
import importlib_resources


class PackageLoader(BaseLoader):
    def __init__(self, path):
        self.path = path

    def get_source(self, environment, template):
        from backendService import templates

        try:
            source = importlib_resources.read_text(templates, template)
        except FileNotFoundError as exc:
            raise TemplateNotFound(template) from exc
        return source, self.path, lambda: True


JINJA_ENV = Environment(loader=PackageLoader("backendService.http.templates"))


def get_template(name):
    return JINJA_ENV.get_template(name)
