From e8ad98673a86eae2b3725831e0dbebc7890683f4 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 10 Mar 2015 20:06:31 +0100 Subject: [PATCH] add helper functions for template engine --- .../service/modules/addons/__init__.py | 32 +++++++ .../modules/addons/template_helpers.py | 83 +++++++++++++++++++ src/opnsense/service/modules/template.py | 5 ++ .../OPNsense/Sample/example_simple_page.txt | 8 ++ 4 files changed, 128 insertions(+) create mode 100644 src/opnsense/service/modules/addons/__init__.py create mode 100644 src/opnsense/service/modules/addons/template_helpers.py diff --git a/src/opnsense/service/modules/addons/__init__.py b/src/opnsense/service/modules/addons/__init__.py new file mode 100644 index 000000000..ee42c7718 --- /dev/null +++ b/src/opnsense/service/modules/addons/__init__.py @@ -0,0 +1,32 @@ +""" + 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 +""" + diff --git a/src/opnsense/service/modules/addons/template_helpers.py b/src/opnsense/service/modules/addons/template_helpers.py new file mode 100644 index 000000000..2eb9eda97 --- /dev/null +++ b/src/opnsense/service/modules/addons/template_helpers.py @@ -0,0 +1,83 @@ +""" + 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 diff --git a/src/opnsense/service/modules/template.py b/src/opnsense/service/modules/template.py index 79e28be85..5fa113936 100644 --- a/src/opnsense/service/modules/template.py +++ b/src/opnsense/service/modules/template.py @@ -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 diff --git a/src/opnsense/service/templates/OPNsense/Sample/example_simple_page.txt b/src/opnsense/service/templates/OPNsense/Sample/example_simple_page.txt index 565a0f51c..8c19cbb9e 100644 --- a/src/opnsense/service/templates/OPNsense/Sample/example_simple_page.txt +++ b/src/opnsense/service/templates/OPNsense/Sample/example_simple_page.txt @@ -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 %}