summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--python/vyos/template.py21
1 files changed, 15 insertions, 6 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py
index e4b253ed3..85a0143c0 100644
--- a/python/vyos/template.py
+++ b/python/vyos/template.py
@@ -17,11 +17,9 @@ import os
from jinja2 import Environment
from jinja2 import FileSystemLoader
-
from vyos.defaults import directories
from vyos.util import chmod, chown, makedir
-
# reuse the same Environment to improve performance
_templates_env = {
False: Environment(loader=FileSystemLoader(directories['templates'])),
@@ -32,6 +30,13 @@ _templates_mem = {
True: {},
}
+def vyos_address_from_cidr(text):
+ from ipaddress import ip_network
+ return ip_network(text).network_address
+
+def vyos_netmask_from_cidr(text):
+ from ipaddress import ip_network
+ return ip_network(text).netmask
def render(destination, template, content, trim_blocks=False, formater=None, permission=None, user=None, group=None):
"""
@@ -42,8 +47,8 @@ def render(destination, template, content, trim_blocks=False, formater=None, per
This classes cache the renderer, so rendering the same file multiple time
does not cause as too much overhead. If use everywhere, it could be changed
- and load the template from python environement variables from an import
- python module generated when the debian package is build
+ and load the template from python environement variables from an import
+ python module generated when the debian package is build
(recovering the load time and overhead caused by having the file out of the code)
"""
@@ -54,11 +59,15 @@ def render(destination, template, content, trim_blocks=False, formater=None, per
# Setup a renderer for the given template
# This is cached and re-used for performance
if template not in _templates_mem[trim_blocks]:
- _templates_mem[trim_blocks][template] = _templates_env[trim_blocks].get_template(template)
+ _env = _templates_env[trim_blocks]
+ _env.filters['address_from_cidr'] = vyos_address_from_cidr
+ _env.filters['netmask_from_cidr'] = vyos_netmask_from_cidr
+ _templates_mem[trim_blocks][template] = _env.get_template(template)
+
template = _templates_mem[trim_blocks][template]
# As we are opening the file with 'w', we are performing the rendering
- # before calling open() to not accidentally erase the file if the
+ # before calling open() to not accidentally erase the file if the
# templating fails
content = template.render(content)