Skip to content

Commit

Permalink
templates: added ability to set custom template directory
Browse files Browse the repository at this point in the history
Useful for custom projects outside of the s2e-env module.
  • Loading branch information
adrianherrera committed Dec 31, 2018
1 parent 1e5763c commit 22d02c0
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions s2e_env/utils/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
from .memoize import memoize


TEMPLATES_DIR = os.path.join(os.path.dirname(__file__), '..', 'templates')
DEFAULT_TEMPLATES_DIR = os.path.join(os.path.dirname(__file__), '..',
'templates')


def _datetimefilter(value, format_='%H:%M %d-%m-%Y'):
Expand All @@ -40,41 +41,45 @@ def _datetimefilter(value, format_='%H:%M %d-%m-%Y'):


@memoize
def _init_template_env(templates_dir=TEMPLATES_DIR):
def _init_template_env(templates_dir=None):
"""
Initialize the jinja2 templating environment using the templates in the
given directory.
"""
if not templates_dir:
templates_dir = DEFAULT_TEMPLATES_DIR

env = Environment(loader=FileSystemLoader(templates_dir),
autoescape=False, undefined=StrictUndefined)
env.filters['datetimefilter'] = _datetimefilter

return env


def render_template(context, template, path=None, executable=False):
def render_template(context, template, output_path=None, templates_dir=None,
executable=False):
"""
Renders the ``template`` template with the given ``context``. The result is
written to ``path``. If ``path`` is not specified, the result is
returned as a string
returned as a string and written to ``output_path`` (if specified).
A directory containing the Jinja templates can optionally be specified.
"""
env = _init_template_env()
data = env.get_template(template).render(context)
env = _init_template_env(templates_dir)

rendered_data = env.get_template(template).render(context)

# Remove trailing spaces
cleaned_lines = []
for line in data.splitlines():
for line in rendered_data.splitlines():
cleaned_lines.append(line.rstrip())
data = '\n'.join(cleaned_lines)

if not path:
return data
rendered_data = '\n'.join(cleaned_lines)

with open(path, 'w') as f:
f.write(data)
if output_path:
with open(output_path, 'w') as f:
f.write(rendered_data)

if executable:
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IEXEC)
if executable:
st = os.stat(output_path)
os.chmod(output_path, st.st_mode | stat.S_IEXEC)

return True
return rendered_data

0 comments on commit 22d02c0

Please sign in to comment.