lxc-network/lxc_net/jinja_template.py

24 lines
671 B
Python
Raw Normal View History

2020-03-11 11:27:38 +01:00
""" Reads a jinja template from a file """
2020-03-09 12:14:26 +01:00
import os
2020-03-09 13:00:10 +01:00
import jinja2
2020-03-09 12:14:26 +01:00
2020-03-11 11:27:38 +01:00
class JinjaTemplate:
2020-03-09 12:14:26 +01:00
""" Reads and instanciates a template from a file """
base_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates")
2020-03-09 13:00:10 +01:00
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(base_path),
autoescape=jinja2.select_autoescape(
2020-03-11 11:27:38 +01:00
enabled_extensions=("xml", "network"), default_for_string=True,
2020-03-09 13:00:10 +01:00
),
)
2020-03-09 12:14:26 +01:00
def __init__(self, path):
2020-03-09 13:00:10 +01:00
self.template = self.env.get_template(path)
2020-03-09 12:14:26 +01:00
def inst(self, *args, **kwargs):
""" instanciates the template """
2020-03-09 13:00:10 +01:00
return self.template.render(*args, **kwargs)