Commit e8ad9867 authored by Ad Schellevis's avatar Ad Schellevis

add helper functions for template engine

parent aa7e5719
"""
Copyright (c) 2015 Ad Schellevis
part of OPNsense (https://www.opnsense.org/)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------------
package : configd
"""
"""
Copyright (c) 2015 Ad Schellevis
part of OPNsense (https://www.opnsense.org/)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------------
package : configd
"""
class Helpers(object):
def __init__(self,template_in_data):
""" initialize template helpers
:param template_in_data:
:return:
"""
self._template_in_data = template_in_data
def getNodeByTag(self,tag):
""" get tree node by tag
:param tag: tag in dot notation (section.item)
:return: dict or None if not found
"""
node = self._template_in_data
for item in tag.split('.'):
if node.has_key(item):
node=node[item]
else:
# not found
return None
# path found, return
return node
def exists(self,tag):
"""
check if node exists in dictionary structure
:param tag: tag in dot notation (section.item)
:return: boolean
"""
if self.getNodeByTag(tag):
return True
else:
return False
def getIterator(self,*parms):
"""
get iterator for given tags
:param parms: list of tags (in dot notation )
:return: list containing all collected detail items
"""
result = []
for tag in parms:
node = self.getNodeByTag(tag)
if node is not None:
if type(node) == list:
result += node
else:
result.append(node)
return result
\ No newline at end of file
......@@ -39,6 +39,7 @@ import os.path
import collections
import copy
import jinja2
import addons.template_helpers
class Template(object):
......@@ -199,6 +200,7 @@ class Template(object):
def generate(self,module_name,create_directory=True):
""" generate configuration files using bound config and template data
......@@ -229,6 +231,9 @@ class Template(object):
cnf_data = copy.deepcopy(self._config)
cnf_data['TARGET_FILTERS'] = result_filenames[filename]
# link template helpers
self._j2_env.globals['helpers'] = addons.template_helpers.Helpers(cnf_data)
# make sure we're only rendering output once
if filename not in result:
# render page and write to disc
......
......@@ -30,3 +30,11 @@ and a short list of firewall rules created (multiple rule items in filter sectio
The full documentation for the template engine can be found at : http://jinja.pocoo.org/docs/dev/templates/
A sample with multiple output files ( for example based on interface ) can be found in the example_config.txt template
{% if helpers.exists('filter.rule') %}
filter.rule exists
{% endif %}
{% for item in helpers.getIterator('filter.rule') %}
{{ item.type }}
{% endfor %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment