Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def render(template, context):
"""Wrapper to the jinja2 render method from a template file
Parameters
----------
template : str
Path to template file.
context : dict
Dictionary with kwargs context to populate the template
"""
path, filename = os.path.split(template)
return jinja2.Environment(
loader=jinja2.FileSystemLoader(path or './')
).get_template(filename).render(context)
def update_index(gans):
""" Update the index.html text file from a Jinja2 template """
import jinja2 as j2
try:
gans.sort(key=lambda v: ((int(v['Year']) if v['Year'].isdigit() else v['Year'])
, (int(v['Month']) if v['Month'].isdigit() else v['Month'])), reverse=True)
except:
pass
j2_env = j2.Environment(loader=j2.FileSystemLoader('.'),
trim_blocks=True, lstrip_blocks=True)
j2_env.globals['nowts'] = datetime.datetime.now()
with open('docs/index.html', 'w') as fid:
print(j2_env.get_template('INDEX.j2.md').render(gans=gans), file=fid)
def load_template(template):
env = Environment(
loader=FileSystemLoader(
settings.NGINX_TEMPLATES
)
)
return env.get_template(template)
def __init__(self):
jj.Environment.__init__(self)
self.trim_blocks = True
# Load templates from within the package
self.loader = jj.ChoiceLoader([
jj.FileSystemLoader(self.user_template_folder()),
jj.PackageLoader('autostatistical', 'templates')
])
# Custom formatting filters
self.filters['dateformat'] = self.dateformat
self.filters['round'] = self.round
self.filters['signif'] = self.signif
self.filters['floatcolumn'] = self.floatcolumn
self.filters['signifcolumn'] = self.signifcolumn
self.filters['intcolumn'] = self.intcolumn
self.filters['strcolumn'] = self.strcolumn
self.filters['default'] = self.default
def to_rgb(color):
color = color.lstrip('#')
r, g, b = map(lambda x: int(x, 16), [color[:2], color[2:4], color[4:]])
return 'rgb({},{},{})'.format(r, g, b)
def wrap(text, width):
if not isinstance(text, str):
return text
return textwrap.wrap(
text, break_long_words=False, break_on_hyphens=False, width=width)
template_dir = os.path.abspath(str(ROOT / 'templates'))
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(searchpath=template_dir))
env.filters['wrap'] = wrap
env.filters['rgb'] = to_rgb
###############################################################################
# Covers
###############################################################################
def random_cover(title, author):
font_size_title = 96
font_size_author = 48
font = rand.choice(fonts)
template = rand.choice(templates)
colors = rand.choice(color_schemes)
color1, color2, color3, color4, color5 = colors
def deco(*a, **b):
template_dict = func(*a, **b)
if not TvbProfile.current.web.RENDER_HTML:
return template_dict
### Generate HTML given the path to the template and the data dictionary.
loader = TemplateLoader()
template = loader.load(template_path)
stream = template.generate(**template_dict)
return stream.render('xhtml')
return deco
return dec
# TODO: this decorator is temporary, until PR#47 is merged
env = Environment(loader=FileSystemLoader(os.path.join(os.path.dirname(TvbProfile.current.web.TEMPLATE_ROOT), 'jinja')),
autoescape=select_autoescape(
enabled_extensions=('html', 'xml', 'js', 'jinja2'),
default_for_string=True),
lstrip_blocks=True,
trim_blocks=True)
def using_jinja_template(template_name):
"""
Decorator that renders a template
"""
template_path = template_name + '.jinja2'
def dec(func):
@wraps(func)
def deco(*a, **b):
template_dict = func(*a, **b)
def populate_template(template_dir, **kwargs):
env = Environment(
# keep python templates valid python
block_start_string='#{%',
loader=FileSystemLoader(template_dir),
# trim_blocks=True,
# lstrip_blocks=True,
)
template = env.get_template('citrix_adc_appfwsettings.template')
stream = template.stream(**kwargs)
#output_file = 'citrix_adc_appfw_profile.py'
output_file = os.path.join(HERE, '..', '..', '..', 'ansible-modules', 'citrix_adc_appfw_settings.py')
stream.dump(
output_file,
encoding='utf-8'
)
def __init__(self, globalconfig, subcfg):
super(Backend, self).__init__(globalconfig, subcfg)
self.list_fname = self.build_filename(globalconfig, subcfg)
self.template_name = subcfg['template']
self.cfg = subcfg
self.env = jinja2.Environment(loader=jinja2.FileSystemLoader(globalconfig.getpath('template_path', 'tracker')),
trim_blocks=True, lstrip_blocks=True)
self.env.filters['js_datetime'] = self._js_datetime_filter
self.env.filters['utc_datetime'] = self._utc_datetime_filter
self.horizon_hours = globalconfig.get('horizon_hours','tracker')
def render_jinja2(recipe_dir):
import jinja2
loaders = [jinja2.FileSystemLoader(recipe_dir)]
env = jinja2.Environment(loader=jinja2.ChoiceLoader(loaders))
template = env.get_or_select_template('meta.yaml')
return template.render(environment=env)
header_code += f.read()
encrypted_code_maps = {}
for file_name in os.listdir(cl_kernel_dir):
file_path = os.path.join(cl_kernel_dir, file_name)
if file_path[-3:] == ".cl":
with open(file_path, "r") as f:
code_str = ""
for line in f.readlines():
if "#include " in line:
code_str += header_code
else:
code_str += line
encrypted_code_maps[file_name[:-3]] = encrypt_code(code_str)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(sys.path[0]))
cpp_cl_encrypted_kernel = env.get_template(
'str2map.h.jinja2').render(
maps=encrypted_code_maps,
variable_name='opencl_kernel_string_map')
with open(output_path, "w") as w_file:
w_file.write(cpp_cl_encrypted_kernel)
print('Generate OpenCL kernel done.')