text
stringlengths
0
828
with statsd.pipeline() as pipe:
pipe.incr(name, count)
pipe.timing(name, int(round(1000 * elapsed)))"
1314,"def setup(template_paths={}, autoescape=False, cache_size=100, auto_reload=True, bytecode_cache=True):
""""""Setup Jinja enviroment
eg. sketch.jinja.setup({
'app': self.config.paths['app_template_basedir'],
'sketch': self.config.paths['sketch_template_dir'],
})
:param template_paths: Dictionary of paths to templates (template_name => template_path)
:param autoescape: Autoescape
:param cache_size:
:param auto_reload:
:param bytecode_cache:
""""""
global _jinja_env, _jinja_loaders
if not _jinja_env:
_jinja_env = JinjaEnviroment(
autoescape=autoescape,
cache_size=cache_size,
auto_reload=auto_reload,
bytecode_cache=None)
# @TODO alter so Marshall is not used
# if bytecode_cache and GAE_CACHE:
# _jinja_env.bytecode_cache = GAEMemcacheBytecodeCache()
if type(template_paths) == type(''):
template_paths = {'site': template_paths}
if len(template_paths) < 1:
logging.exception('Sketch: jinja.setup: no template sets configured')
return False
if len(template_paths) == 1:
template_set_name = template_paths.keys()[0]
tp = template_paths[template_set_name]
if tp in _jinja_loaders:
_jinja_env.loader = _jinja_loaders[tp]
else:
_jinja_env.loader = _jinja_loaders[tp] = jinja2.FileSystemLoader(tp)
return True
if len(template_paths) > 1:
loaders = {}
for dirn, path in template_paths.items():
loaders[dirn] = jinja2.FileSystemLoader(path)
_jinja_env.loader = SubdirLoader(loaders)
return True
logging.error('Sketch: jinja.setup: no template sets configured (fallthrough)')
logging.error(_jinja_loaders)"
1315,"def render(template_name, template_vars={}, template_set='site', template_theme=None, template_extension='html', template_content=None):
""""""Given a template path, a template name and template variables
will return rendered content using jinja2 library
:param template_path: Path to template directory
:param template_name: Name of template
:param vars: (Optional) Template variables
""""""
global _jinja_env
if not _jinja_env:
raise 'Jinja env not setup'
try:
_jinja_env.filters['timesince'] = timesince
_jinja_env.filters['timeuntil'] = timeuntil
_jinja_env.filters['date'] = date_format
_jinja_env.filters['time'] = time_format
_jinja_env.filters['shortdate'] = short_date
_jinja_env.filters['isodate'] = iso_date
_jinja_env.filters['rfcdate'] = rfc2822_date
_jinja_env.filters['tformat'] = datetimeformat
_jinja_env.filters['timestamp'] = timestamp
except NameError as errstr:
logging.info('Helper import error: %s' % errstr)
_template_name = ""%s.%s"" % (template_name, template_extension)
template = _jinja_env.get_template(_template_name, parent=template_theme)
return template.render(template_vars)"
1316,"def compile_file(env, src_path, dst_path, encoding='utf-8', base_dir=''):
""""""Compiles a Jinja2 template to python code.
:param env: a Jinja2 Environment instance.
:param src_path: path to the source file.
:param dst_path: path to the destination file.
:param encoding: template encoding.
:param base_dir: the base path to be removed from the compiled template filename.
""""""
src_file = file(src_path, 'r')
source = src_file.read().decode(encoding)
name = src_path.replace(base_dir, '')
raw = env.compile(source, name=name, filename=name, raw=True)
src_file.close()
dst_file = open(dst_path, 'w')