Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from mdbenchmark.mdengines import detect_md_engine, utils
# Order where to look for host templates: HOME -> etc -> package
# home
_loaders = [FileSystemLoader(os.path.join(xdg.XDG_CONFIG_HOME, "MDBenchmark"))]
# allow custom folder for templates. Useful for environment modules
_mdbenchmark_env = os.getenv("MDBENCHMARK_TEMPLATES")
if _mdbenchmark_env is not None:
_loaders.append(FileSystemLoader(_mdbenchmark_env))
# global
_loaders.extend(
[FileSystemLoader(os.path.join(d, "MDBenchmark")) for d in xdg.XDG_CONFIG_DIRS]
)
# from package
_loaders.append(PackageLoader("mdbenchmark", "templates"))
ENV = Environment(loader=ChoiceLoader(_loaders))
def get_possible_hosts():
return ENV.list_templates()
def print_possible_hosts():
all_hosts = get_possible_hosts()
console.info("Available host templates:")
for host in all_hosts:
console.info(host)
def guess_host():
hostname = socket.gethostname()
known_hosts = get_possible_hosts()
loader = None
try:
if Importer.module_path(tp): #if it module path
loader = PackageLoader(tp, "")
except:
loader = FileSystemLoader(tp)
if loader:
if prefix:
tps.append(PrefixLoader({prefix: loader}))
else:
tps.append(loader)
if hasattr(self.app, "templates_environment"):
for tl in tps:
self.app.templates_environment.loader.loaders.append(tl)
else:
self.app.templates_environment = Environment(loader=ChoiceLoader(tps), extensions=self.options["views"]["templates_extensions"])
def initialize(self):
super().initialize()
# FIXME: Is this really the best way to use jinja2 here?
# I can't seem to get the jinja2 env in the base handler to
# actually load templates from arbitrary paths ugh.
jinja2_env = self.settings['jinja2_env']
jinja2_env.loader = jinja2.ChoiceLoader([
jinja2_env.loader,
jinja2.FileSystemLoader(
os.path.join(os.path.dirname(__file__), 'templates')
)
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)
for app in settings.INSTALLED_APPS:
loader_array.append(PackageLoader(app))
bytecode_cache=None
try:
import re, cmemcache
m = re.match(
"memcached://([.\w]+:\d+)", settings.CACHE_BACKEND
)
if m:
mc = cmemcache.Client([m.group(1)], debug=0)
bytecode_cache = MemcachedBytecodeCache(mc)
except ImportError:
pass
env = Environment(loader=ChoiceLoader(loader_array), autoescape=True, bytecode_cache=bytecode_cache, extensions=[Compressor])
from django.template.defaultfilters import date
env.filters['date'] = date
from django.template.defaultfilters import truncatewords_html, pluralize, removetags
env.filters['truncatewords_html'] = truncatewords_html
env.filters['makeplural'] = pluralize
env.filters['removetags'] = removetags
from django.contrib.humanize.templatetags.humanize import naturalday, apnumber
env.filters['naturalday'] = naturalday
env.filters['apnumber'] = apnumber
from posts.templatetags.post_extras import markdowner, esvapi
env.filters['markdown'] = markdowner
env.filters['esv'] = esvapi
def __init__(self, path, templates=None):
self.path = os.path.abspath(path)
# the default loader reads templates from the package
loaders = [PackageLoader("pydap.wsgi", "templates")]
# optionally, the user can also specify a template directory that will
# override the default templates; this should have precedence over the
# default templates
if templates is not None:
loaders.insert(0, FileSystemLoader(templates))
# set the rendering environment; this is also used by pydap responses
# that need to render templates (like HTML, WMS, KML, etc.)
self.env = Environment(loader=ChoiceLoader(loaders))
self.env.filters["datetimeformat"] = datetimeformat
self.env.filters["datetimeformat_iso"] = datetimeformat_iso
self.env.filters["unquote"] = unquote
# cache available handlers, so we don't need to load them every request
self.handlers = load_handlers()
self.wfsettings[entry] = self._load_workflow_settings(settings_file)
self.logger.info("Loaded workflow '" + entry + "' config file")
except Exception as ex:
self.logger.warn("Could not load workflow config file " + settings_file + ": " + str(ex))
continue
if not found:
self.logger.warn("The WORKFLOWS_DIR directory is empty, no workflows could be loaded!")
## set up template loading
loader_data = {}
for workflow in self.workflows:
template_dir = os.path.join(self.config['WORKFLOWS_DIR'],workflow['name'],'templates')
loader_data[workflow['name']] = jinja2.FileSystemLoader(template_dir)
choice_loader = jinja2.ChoiceLoader(
[
self.jinja_loader,
jinja2.PrefixLoader(loader_data,'::')
])
self.jinja_loader = choice_loader
def __init__(self, app):
self.app = app
self._loaders = []
self.layout_loader = None
self.feature_loaders = []
self.macro_loaders = []
self.bottom_loaders = []
self.prefix_loader = PrefixLoader({
# because we don't call MacroLoader's constructor to provide
# our own loaders property, we define the prefix_loader with the
# __macros__ entry
"__macros__": ChoiceLoader(self.macro_loaders),
# these prefixes allow for direct access to templates
# defined by features
"__super__": ChoiceLoader([
ChoiceLoader(self.feature_loaders),
ChoiceLoader(self._iter_feature_blueprint_loaders()),
ChoiceLoader(self.bottom_loaders)])})
def build_jinja_environment():
env = jinja2.Environment(
autoescape=True,
loader=jinja2.ChoiceLoader([
jinja2.FileSystemLoader(CONF.template_path),
jinja2.PackageLoader("trove", "templates")
]))
# Add some basic operation not built-in.
env.globals['max'] = max
env.globals['min'] = min
return env
def __init__(self, package_name, extension, env_globals=None):
if isinstance(package_name, basestring):
package_name = (package_name,)
loader = ChoiceLoader([PackageLoader(name, 'templates') for name in package_name])
self.env = Environment(loader=loader, trim_blocks=True,
lstrip_blocks=True, undefined=StrictUndefined)
self.env.globals['autoindent'] = autoindent
self.env.filters['autoindent'] = autoindent
if env_globals is not None:
self.env.globals.update(env_globals)
else:
env_globals = {}
self.env_globals = env_globals
self.package_names = package_name
self.extension = extension
self.templates = LazyTemplateLoader(self.env, extension)