Add jinja filter to get host from variable

Change-Id: Ideb6323f7192814f6d0ff74a33d34329f603ecb2
This commit is contained in:
Artur Zarzycki 2016-11-09 12:37:45 +01:00
parent 27a75e0e48
commit 455d7c5cda
1 changed files with 7 additions and 0 deletions

View File

@ -2,6 +2,8 @@ import os
import jinja2
from six.moves.urllib import parse as urlparse
class SilentUndefined(jinja2.Undefined):
def _fail_with_undefined_error(self, *args, **kwargs):
@ -15,6 +17,10 @@ class SilentUndefined(jinja2.Undefined):
_fail_with_undefined_error
def get_host(path):
return urlparse.urlsplit(path).netloc
def jinja_render(path, context, functions=(), ignore_undefined=False):
kwargs = {}
if ignore_undefined:
@ -24,6 +30,7 @@ def jinja_render(path, context, functions=(), ignore_undefined=False):
env = jinja2.Environment(loader=jinja2.FileSystemLoader(
os.path.dirname(path)), **kwargs)
env.filters['host'] = get_host
for func in functions:
env.globals[func.__name__] = func
content = env.get_template(os.path.basename(path)).render(context)