本文整理匯總了Python中jinja2.BaseLoader方法的典型用法代碼示例。如果您正苦於以下問題:Python jinja2.BaseLoader方法的具體用法?Python jinja2.BaseLoader怎麽用?Python jinja2.BaseLoader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類jinja2
的用法示例。
在下文中一共展示了jinja2.BaseLoader方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_template
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def get_template(template):
"""Return a Jinja2 template by filename
Args:
template (str): Name of the template to return
Returns:
A Jinja2 Template object
"""
from cloud_inquisitor.database import db
tmpl = db.Template.find_one(template_name=template)
if not tmpl:
raise InquisitorError('No such template found: {}'.format(template))
tmplenv = Environment(loader=BaseLoader, autoescape=True)
tmplenv.filters['json_loads'] = json.loads
tmplenv.filters['slack_quote_join'] = lambda data: ', '.join('`{}`'.format(x) for x in data)
return tmplenv.from_string(tmpl.template)
示例2: _loaders
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def _loaders(self) -> Generator[BaseLoader, None, None]:
loader = self.app.jinja_loader
if loader is not None:
yield loader
for blueprint in self.app.iter_blueprints():
loader = blueprint.jinja_loader
if loader is not None:
yield loader
示例3: get_rendered_json
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def get_rendered_json(self, json_string, pipeline_vars=None):
"""Takes a string of a manual template and renders it as a Jinja2 template, returning the result
Args:
json_string (str): pipeline in jinja/json format
pipeline_vars (dict): key/value pairs of variables the pipline expects
Returns:
str: pipeline json after Jinja is rendered"""
try:
if TEMPLATES_PATH:
loader = jinja2.FileSystemLoader(TEMPLATES_PATH)
else:
loader = jinja2.BaseLoader()
jinja_template = jinja2.Environment(loader=loader).from_string(json_string)
# Get any pipeline args defined in pipeline.json, default to empty dict if none defined
pipeline_args = dict()
except jinja2.TemplateNotFound:
# Log paths searched for debugging, then re-raise
message = 'Jinja2 TemplateNotFound exception in paths {paths}'.format(paths=loader.searchpath)
self.log.error(message)
raise
# Expose permitted functions and variables to the template
pipeline_args.update(get_jinja_functions())
pipeline_args.update(get_jinja_variables(self))
# If any args set in the pipeline file add them to the pipeline_args.variables
if pipeline_vars is not None:
pipeline_args["template_variables"] = pipeline_vars
# Render the template
return jinja_template.render(pipeline_args)
示例4: serialize
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def serialize(self, format='json-ld', with_mime=False,
template=None, prefix=None, fields=None, **kwargs):
js = self.jsonld(prefix=prefix, **kwargs)
if template is not None:
rtemplate = Environment(loader=BaseLoader).from_string(template)
content = rtemplate.render(**self)
mimetype = 'text'
elif fields is not None:
# Emulate field selection by constructing a template
content = json.dumps(jmespath.search(fields, js))
mimetype = 'text'
elif format == 'json-ld':
content = json.dumps(js, indent=2, sort_keys=True)
mimetype = "application/json"
elif format in ['turtle', 'ntriples']:
content = json.dumps(js, indent=2, sort_keys=True)
logger.debug(js)
context = [self._context, {'prefix': prefix, '@base': prefix}]
g = Graph().parse(
data=content,
format='json-ld',
prefix=prefix,
context=context)
logger.debug(
'Parsing with prefix: {}'.format(kwargs.get('prefix')))
content = g.serialize(format=format,
prefix=prefix).decode('utf-8')
mimetype = 'text/{}'.format(format)
else:
raise Error('Unknown outformat: {}'.format(format))
if with_mime:
return content, mimetype
else:
return content
示例5: expand_model
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def expand_model(model_config: str, model_parameters: dict):
"""
Expands the jinja template which is the model using the variables in
`model_parameters`
Parameters
----------
model_config: str
Jinja template which when expanded becomes a valid model config json.
model_parameters:
Parameters for the model config.
Raises
------
ValueError
If an undefined variable is used in the model_config.
Returns
-------
str
The model config with variables expanded
"""
try:
model_template = jinja2.Environment(
loader=jinja2.BaseLoader(), undefined=jinja2.StrictUndefined
).from_string(model_config)
model_config = model_template.render(**model_parameters)
except jinja2.exceptions.UndefinedError as e:
raise ValueError("Model parameter missing value!") from e
logger.info(f"Expanded model config: {model_config}")
return yaml.safe_load(model_config)
示例6: _render_options_form
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def _render_options_form(self, profile_list):
self._profile_list = self._init_profile_list(profile_list)
profile_form_template = Environment(loader=BaseLoader).from_string(self.profile_form_template)
return profile_form_template.render(profile_list=self._profile_list)
示例7: final_command
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def final_command(s):
tmpl = Environment(loader=BaseLoader).from_string(s)
return tmpl.render(pipdeptree=pipdeptree_path)
示例8: __init__
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def __init__(self, template_dir: Optional[str] = None) -> None:
self.template = None # type: Optional[_jinja2.Template]
if not template_dir:
loader = _jinja2.BaseLoader()
else:
loader = _jinja2.FileSystemLoader(template_dir)
self.env = _jinja2.Environment(loader=loader, autoescape=True,
undefined=_jinja2.StrictUndefined)
示例9: __init__
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def __init__(self, container_or_service):
if isinstance(container_or_service, Service):
title = f'dockupdater has updated services!'
else:
title = f'dockupdater has updated containers!'
template = Environment(loader=BaseLoader).from_string(container_or_service.config.template)
body = template.render(object=container_or_service)
super().__init__(title, body)
示例10: process_template
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def process_template(file_src):
""" Process Jinja2 template input
:param file_src: file contents
:type file_src: str
"""
def tmpl_sqrt(x):
""" Square-root of 'x' """
return math.sqrt(x)
def tmpl_cubert(x):
""" Cube-root of 'x' """
return x ** (1.0 / 3.0) if x >= 0 else -(-x) ** (1.0 / 3.0)
def tmpl_pow(x, y):
""" 'x' to the power 'y' """
return math.pow(x, y)
# Check if it is possible to import 'jinja2'
try:
import jinja2
except ImportError:
raise GeomdlException("Please install 'jinja2' package to use templated input: pip install jinja2")
# Replace jinja2 template tags for compatibility
fsrc = file_src.replace("{%", "<%").replace("%}", "%>").replace("{{", "<{").replace("}}", "}>")
# Generate Jinja2 environment
env = jinja2.Environment(
loader=jinja2.BaseLoader(),
trim_blocks=True,
block_start_string='<%', block_end_string='%>',
variable_start_string='<{', variable_end_string='}>'
).from_string(fsrc)
# Load custom functions into the Jinja2 environment
template_funcs = dict(
knot_vector=utilities.generate_knot_vector,
sqrt=tmpl_sqrt,
cubert=tmpl_cubert,
pow=tmpl_pow,
)
for k, v in template_funcs.items():
env.globals[k] = v
# Process Jinja2 template functions & variables inside the input file
return env.render()
示例11: __init__
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def __init__(self, loader=None, global_vars=None):
"""
:param loader: The loader to supply to the environment.
:type loader: :py:class:`jinja2.BaseLoader`
:param dict global_vars: Additional global variables for the environment.
"""
self.logger = logging.getLogger('KingPhisher.TemplateEnvironment')
autoescape = jinja2.select_autoescape(['html', 'htm', 'xml'], default_for_string=False)
extensions = ['jinja2.ext.autoescape', 'jinja2.ext.do']
super(TemplateEnvironmentBase, self).__init__(autoescape=autoescape, extensions=extensions, loader=loader, trim_blocks=True)
# misc. string filters
self.filters['cardinalize'] = boltons.strutils.cardinalize
self.filters['ordinalize'] = boltons.strutils.ordinalize
self.filters['pluralize'] = boltons.strutils.pluralize
self.filters['singularize'] = boltons.strutils.singularize
self.filters['possessive'] = lambda word: word + ('\'' if word.endswith('s') else '\'s')
self.filters['encode'] = self._filter_encode
self.filters['decode'] = self._filter_decode
self.filters['hash'] = self._filter_hash
# counter part to https://jinja.readthedocs.io/en/stable/templates.html#tojson
self.filters['fromjson'] = self._filter_json
# time filters
self.filters['strftime'] = self._filter_strftime
self.filters['timedelta'] = self._filter_timedelta
self.filters['tomorrow'] = lambda dt: dt + datetime.timedelta(days=1)
self.filters['next_week'] = lambda dt: dt + datetime.timedelta(weeks=1)
self.filters['next_month'] = lambda dt: dt + datetime.timedelta(days=30)
self.filters['next_year'] = lambda dt: dt + datetime.timedelta(days=365)
self.filters['yesterday'] = lambda dt: dt + datetime.timedelta(days=-1)
self.filters['last_week'] = lambda dt: dt + datetime.timedelta(weeks=-1)
self.filters['last_month'] = lambda dt: dt + datetime.timedelta(days=-30)
self.filters['last_year'] = lambda dt: dt + datetime.timedelta(days=-365)
# global variables
self.globals['version'] = version.version
# global functions
self.globals['fetch'] = self._func_fetch
self.globals['parse_user_agent'] = ua_parser.parse_user_agent
self.globals['password_is_complex'] = utilities.password_is_complex
self.globals['random_integer'] = random.randint
# additional globals
self.globals.update(global_vars or {})
示例12: render_template
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def render_template(template_source, template_vars=None, template_internal_vars=None, loader=None):
macros = [
"""
{% macro collect(parts) -%}
{% set comma = joiner() %}
{% for part in glob(parts) %}
{{ comma() }}
{% include part %}
{% endfor %}
{%- endmacro %}
""",
"""
{% macro exists_set_param(setting_name, value, default_value=None, comma=True) -%}
{% if value is defined or default_value is not none %}
{% if comma %} , {% endif %}
{% if default_value is not none %}
"{{ setting_name }}": {{ value | default(default_value) | tojson }}
{% else %}
"{{ setting_name }}": {{ value | tojson }}
{% endif %}
{% endif %}
{%- endmacro %}
"""
]
# place helpers dict loader first to prevent users from overriding our macros.
env = jinja2.Environment(
loader=jinja2.ChoiceLoader([
jinja2.DictLoader({"rally.helpers": "".join(macros)}),
jinja2.BaseLoader(),
loader
])
)
if template_vars:
for k, v in template_vars.items():
env.globals[k] = v
# ensure that user variables never override our internal variables
if template_internal_vars:
for macro_type in template_internal_vars:
for env_global_key, env_global_value in template_internal_vars[macro_type].items():
getattr(env, macro_type)[env_global_key] = env_global_value
template = env.from_string(template_source)
return template.render()
示例13: execute
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import BaseLoader [as 別名]
def execute(self, context):
ti = context['ti']
host, dagid, taskid, exectime = ti.hostname.split('.')[0], ti.dag_id, ti.task_id, ti.execution_date.isoformat()
hook = HiveCliHook(
hive_cli_conn_id=self.hive_cli_conn_id,
mapred_queue=self.mapred_queue,
mapred_job_name='Airflow HiveEmailOperator task for {}.{}.{}.{}'.format(host, dagid, taskid, exectime)
)
hook.hive_cli_params = '-S' # suppress hive junk output
output = hook.run_cli(hql=self.hql, schema=self.schema, hive_conf={'hive.cli.print.header': 'true'})
output_rows = [line for line in output.split('\n') if line]
col_names = output_rows[0].split('\t')
output_rows = output_rows[1:]
if len(output_rows) > self.cutoff:
msg = 'The query returned > {} rows.. Adding tsv as an attachment.'.format(self.cutoff)
logging.warn(msg)
f = tempfile.NamedTemporaryFile(delete=False)
f.write(output)
f.close()
self.files = [f.name]
self.html_content = '{}<br>Dag id: {}<br>Task id: {}<br>Execution Time: {}'.format(msg, dagid,
taskid, exectime)
else:
context.update({
'hql': self.hql,
'rows': output_rows,
'col_names': col_names
})
if not self.html_content:
check_path = os.path.join(os.path.dirname(__file__), '..', 'templates', 'hive_email_default.html')
else:
dag_path = conf.get('core', 'dags_folder')
check_path = os.path.join(dag_path, os.path.dirname(context['dag'].filepath), self.html_content)
if os.path.exists(check_path):
path, filename = os.path.split(os.path.abspath(check_path))
template = Environment(loader=FileSystemLoader(path)).get_template(filename)
logging.info("Using templated file located at: {path}".format(path=check_path))
else:
template = Environment(loader=BaseLoader()).from_string(self.html_content)
self.html_content = template.render(**context)
super(HiveEmailOperator, self).execute(context)
# delete the temp file after successfully attached to email
if len(output_rows) > self.cutoff:
os.unlink(f.name)