Templates: use jinja2
This commit is contained in:
parent
003e774fc4
commit
25b0b714e1
2 changed files with 18 additions and 11 deletions
|
@ -1,11 +1,11 @@
|
|||
<network>
|
||||
<name>{name}</name>
|
||||
<uuid>{uuid}</uuid>
|
||||
<bridge name='virbr{bridge_id}' stp='on' delay='0'/>
|
||||
<mac address='{mac}'/>
|
||||
<domain name='{name}'/>
|
||||
<ip address='{addrv4}' netmask='{netmaskv4}'>
|
||||
<name>{{ name }}</name>
|
||||
<uuid>{{ uuid }}</uuid>
|
||||
<bridge name='virbr{{ bridge_id }}' stp='on' delay='0'/>
|
||||
<mac address='{{ mac }}'/>
|
||||
<domain name='{{ name }}'/>
|
||||
<ip address='{{ addrv4 }}' netmask='{{ netmaskv4 }}'>
|
||||
</ip>
|
||||
<ip family='ipv6' address='{addrv6}' prefix='{prefixv6}'>
|
||||
<ip family='ipv6' address='{{ addrv6 }}' prefix='{{ prefixv6 }}'>
|
||||
</ip>
|
||||
</network>
|
||||
|
|
|
@ -1,16 +1,23 @@
|
|||
""" Reads an XML template from a file """
|
||||
|
||||
import os
|
||||
import jinja2
|
||||
|
||||
|
||||
class XMLTemplate:
|
||||
""" Reads and instanciates a template from a file """
|
||||
|
||||
base_path = os.path.dirname(os.path.abspath(__file__))
|
||||
env = jinja2.Environment(
|
||||
loader=jinja2.FileSystemLoader(base_path),
|
||||
autoescape=jinja2.select_autoescape(
|
||||
enabled_extensions=("xml"), default_for_string=True,
|
||||
),
|
||||
)
|
||||
|
||||
def __init__(self, path):
|
||||
self.path = os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
|
||||
with open(self.path, "r") as handle:
|
||||
self.template_str = handle.read()
|
||||
self.template = self.env.get_template(path)
|
||||
|
||||
def inst(self, *args, **kwargs):
|
||||
""" instanciates the template """
|
||||
return self.template_str.format(*args, **kwargs)
|
||||
return self.template.render(*args, **kwargs)
|
||||
|
|
Loading…
Reference in a new issue