level12 / keg

Keg: more than Flask
Other
12 stars 9 forks source link

`develop templates` command is broken #172

Open rsyring opened 3 years ago

rsyring commented 3 years ago

We use Jinja private methods and they have changed their signature. My goal with this command is to get a sense of where templates are being loaded from. I was able to hack together this:

@dev_command.command('templates', short_help=_('Show paths searched for a template.'))
@flask.cli.with_appcontext
def templates_command():
    app = flask.current_app
    have_loaders = [app] + [bp for bp in app.iter_blueprints() if bp.jinja_loader]
    result = defaultdict(list)

    for app_or_bp in have_loaders:
        jinja_loader = app_or_bp.jinja_loader
        if hasattr(jinja_loader, 'loaders'):
            # ChoiceLoader has a list of loaders
            loaders = jinja_loader.loaders
        else:
            loaders = [jinja_loader]

        for loader in loaders:
            # PackageLoader
            if hasattr(loader, '_template_root'):
                result[app_or_bp.name].append(loader._template_root)
            else:
                # FileSystemLoader
                result[app_or_bp.name].append(loader.searchpath)

    for source_name in sorted(result.keys()):
        #search_paths = sorted(result[source_name])
        click.echo(source_name)
        for root_path in result[source_name]:
            click.echo('    {}'.format(root_path))

I think the old command would have told you every template and where it was being loaded from. That's helpful, but when you have a blueprint (or something) with a loader mis-configured, it won't actually find the template and so it won't be listed with the old command. The above command actually lists the loaders and where they are sourcing templates from, which helps identify mis-configured loaders, which is what I think I really care about.

rsyring commented 3 years ago

Maybe we don't need this anymore. Just turn on: EXPLAIN_TEMPLATE_LOADING in Flask?