1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Instantiate filter plugins only once to save some efficiency.

This commit is contained in:
Michael DeHaan 2013-04-20 09:51:20 -04:00
parent bac3449db7
commit 817b0cdc80

View file

@ -29,6 +29,25 @@ import subprocess
import datetime
import pwd
class Globals(object):
FILTERS = None
def __init__(self):
pass
def _get_filters():
''' return filter plugin instances '''
if Globals.FILTERS is not None:
return Globals.FILTERS
from ansible import utils
plugins = [ x for x in utils.plugins.filter_loader.all()]
filters = {}
for fp in plugins:
filters.update(fp.filters())
Globals.FILTERS = filters
return Globals.FILTERS
class Flags:
LEGACY_TEMPLATE_WARNING = False
@ -365,12 +384,9 @@ def template_from_file(basedir, path, vars):
return lookup(*args, basedir=basedir, **kwargs)
environment = jinja2.Environment(loader=loader, trim_blocks=True, extensions=jinja_exts)
for filter_plugin in utils.plugins.filter_loader.all():
filters = filter_plugin.filters()
if not isinstance(filters, dict):
raise errors.AnsibleError("FilterModule.filters should return a dict.")
environment.filters.update(filters)
environment.filters.update(_get_filters())
environment.globals['lookup'] = my_lookup
try:
data = codecs.open(realpath, encoding="utf8").read()
except UnicodeDecodeError:
@ -421,21 +437,6 @@ def template_from_file(basedir, path, vars):
res = res + '\n'
return template(basedir, res, vars)
def _get_filter_plugins():
global FILTER_PLUGINS
if FILTER_PLUGINS is not None:
return FILTER_PLUGINS
FILTER_PLUGINS = {}
from ansible import utils
for filter_plugin in utils.plugins.filter_loader.all():
filters = filter_plugin.filters()
if not isinstance(filters, dict):
raise errors.AnsibleError("FilterModule.filters should return a dict.")
FILTER_PLUGINS.update(filters)
return FILTER_PLUGINS
def _smush_braces(data):
''' smush Jinaj2 braces so unresolved templates like {{ foo }} don't get parsed weird by key=value code '''
while data.find('{{ ') != -1:
@ -453,7 +454,7 @@ def template_from_string(basedir, data, vars):
if type(data) == str:
data = unicode(data, 'utf-8')
environment = jinja2.Environment(trim_blocks=True, undefined=StrictUndefined)
environment.filters.update(_get_filter_plugins())
environment.filters.update(_get_filters())
environment.template_class = J2Template
# TODO: may need some way of using lookup plugins here seeing we aren't calling