Skip to content

Commit 32dc55f

Browse files
author
Onur Rauf Bingol
committed
Initial commit of Jinja2 template processing function
1 parent 50e8b2f commit 32dc55f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

geomdl/_exchange.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,55 @@
1212
from . import NURBS, compatibility
1313

1414

15+
def process_template(file_src):
16+
""" Process Jinja2 template input
17+
18+
:param file_src: file contents
19+
:type file_src: str
20+
"""
21+
def t_sqrt(val):
22+
""" Square-root of the input value """
23+
return math.sqrt(val)
24+
25+
def t_cubert(val):
26+
""" Cube-root of the input value """
27+
return val**(1.0 / 3.0) if val >= 0 else -(-val)**(1.0 / 3.0)
28+
29+
def t_pow(val, pow):
30+
""" 'val' to the power 'pow' """
31+
return math.pow(val, pow)
32+
33+
# Check if it is possible to import 'jinja2'
34+
try:
35+
import jinja2
36+
except ImportError:
37+
print("Please install 'jinja2' package to use templated input: pip install jinja2")
38+
return
39+
40+
# Replace jinja2 template tags for compatibility
41+
fsrc = file_src.replace("{%", "<%").replace("%}", "%>").replace("{{", "<{").replace("}}", "}>")
42+
43+
# Generate Jinja2 environment
44+
env = jinja2.Environment(
45+
loader=jinja2.BaseLoader(),
46+
trim_blocks=True,
47+
block_start_string='<%', block_end_string='%>',
48+
variable_start_string='<{', variable_end_string='}>'
49+
).from_string(fsrc)
50+
51+
# Load custom functions into the Jinja2 environment
52+
template_funcs = dict(
53+
sqrt=t_sqrt,
54+
cubert=t_cubert,
55+
pow=t_pow,
56+
)
57+
for k, v in template_funcs.items():
58+
env.globals[k] = v
59+
60+
# Process Jinja2 template functions & variables inside the input file
61+
return env.render()
62+
63+
1564
def read_file(file_name, **kwargs):
1665
binary = kwargs.get('binary', False)
1766
skip_lines = kwargs.get('skip_lines', 0)

geomdl/exchange.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ def import_txt(file_name, two_dimensional=False, **kwargs):
3030
# Import surface control points from a text file (2-dimensional file)
3131
surf_ctrlpts, size_u, size_v = exchange.import_txt(file_name="control_points.txt", two_dimensional=True)
3232
33+
If keyword argument ``jinja=True`` is set, then the input file is processed as a `Jinja2 <http://jinja.pocoo.org/>`_
34+
template. You can also use the following convenience template functions which correspond to the given mathematical
35+
equations:
36+
37+
* ``sqrt(x)``: :math:`\\sqrt{x}`
38+
* ``cubert(x)``: :math:`\\sqrt[3]{x}`
39+
* ``pow(x, y)``: :math:`x^{y}`
40+
3341
You may set the file delimiters using the keyword arguments ``separator`` and ``col_separator``, respectively.
3442
``separator`` is the delimiter between the coordinates of the control points. It could be comma
3543
``1, 2, 3`` or space ``1 2 3`` or something else. ``col_separator`` is the delimiter between the control
@@ -63,6 +71,11 @@ def import_txt(file_name, two_dimensional=False, **kwargs):
6371
# Read file
6472
content = _exchange.read_file(file_name)
6573

74+
# Are we using a Jinja2 template?
75+
j2tmpl = kwargs.get('jinja2', False)
76+
if j2tmpl:
77+
content = _exchange.process_template(content)
78+
6679
# File delimiters
6780
col_sep = kwargs.get('col_separator', ";")
6881
sep = kwargs.get('separator', ",")

0 commit comments

Comments
 (0)