phi/src/phi/cli/utils.py

26 lines
785 B
Python

# -*- encoding: utf-8 -*-
import os.path
from jinja2 import Environment, ChoiceLoader, FileSystemLoader, PackageLoader
def get_jinja_env():
pkg_path_templates = os.path.realpath(os.path.join(__name__, "./templates"))
loaders = [
FileSystemLoader(pkg_path_templates),
PackageLoader("phi.cli", package_path="templates"),
]
return Environment(loader=ChoiceLoader(loaders))
def generate_from_templates(config):
env = get_jinja_env()
for t in env.list_templates(extensions="j2"):
tmpl = env.get_template(t)
content = tmpl.render(**config)
# This is loaded from a resource and won't be None
name = tmpl.name.strip(".j2")
# name = os.path.basename(tmpl.filename).strip(".j2")
yield (name, content)