Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
e8ad9867
Commit
e8ad9867
authored
Mar 10, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add helper functions for template engine
parent
aa7e5719
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
128 additions
and
0 deletions
+128
-0
__init__.py
src/opnsense/service/modules/addons/__init__.py
+32
-0
template_helpers.py
src/opnsense/service/modules/addons/template_helpers.py
+83
-0
template.py
src/opnsense/service/modules/template.py
+5
-0
example_simple_page.txt
...service/templates/OPNsense/Sample/example_simple_page.txt
+8
-0
No files found.
src/opnsense/service/modules/addons/__init__.py
0 → 100644
View file @
e8ad9867
"""
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
"""
src/opnsense/service/modules/addons/template_helpers.py
0 → 100644
View file @
e8ad9867
"""
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
src/opnsense/service/modules/template.py
View file @
e8ad9867
...
...
@@ -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
...
...
src/opnsense/service/templates/OPNsense/Sample/example_simple_page.txt
View file @
e8ad9867
...
...
@@ -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 %}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment