本文整理汇总了Python中jinja2.sandbox.SandboxedEnvironment方法的典型用法代码示例。如果您正苦于以下问题:Python sandbox.SandboxedEnvironment方法的具体用法?Python sandbox.SandboxedEnvironment怎么用?Python sandbox.SandboxedEnvironment使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jinja2.sandbox
的用法示例。
在下文中一共展示了sandbox.SandboxedEnvironment方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def main():
"""Regenerate pages listed in global `REGEN` list."""
env = SandboxedEnvironment(undefined=StrictUndefined)
for target, get_data, template in REGEN:
print("Regenerating", target)
data = get_data()
template_text = requests.get(template).text
try:
rendered = env.from_string(template_text).render(**data)
except TemplateError as error:
print("Error while regenerating", target)
print(error)
return 1
with open(target, "w") as target:
target.write(rendered)
return 0
示例2: main
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def main():
"""Regenerate pages listed in global `REGEN` list."""
env = SandboxedEnvironment(undefined=StrictUndefined)
for target, get_data, template in REGEN:
print("Regenerating", target)
data = get_data()
if Path(template).exists():
with open(template) as fd:
template_text = fd.read()
else:
template_text = requests.get(template).text
try:
rendered = env.from_string(template_text).render(**data)
except TemplateError as error:
print("Error while regenerating", target)
print(error)
return 1
with open(target, "w") as target:
target.write(rendered)
return 0
示例3: __init__
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def __init__(self, outpath, tmpltpath):
"""
Parameters
----------
outpath : string
Directory path for RST output files
tmpltpath : string
Directory path for autosummary template files
"""
self.outpath = outpath
self.template_loader = FileSystemLoader(tmpltpath)
self.template_env = SandboxedEnvironment(loader=self.template_loader)
self.template_env.filters['underline'] = _underline
self.template_env.filters['escape'] = rst_escape
self.template_env.filters['e'] = rst_escape
self.template = self.template_env.get_template('module.rst')
示例4: __init__
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def __init__(self, app, extra_context):
template_loader = BuiltinTemplateLoader()
template_loader.init(app.builder)
template_env = SandboxedEnvironment(loader=template_loader)
template_env.filters['rst_escape'] = rst_escape_filter
template_env.filters['underline'] = underline_filter
template_env.filters['as_extlink'] = as_extlink_filter
template_env.filters['prefixes'] = prefixes_filter
template_env.filters['rst_link'] = rst_link_filter
self.env = template_env
self.templates: Dict[str, Any] = {}
self.extra_context = extra_context
示例5: render
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def render(self, context, language_code=DEFAULT_LANG):
"""
Render this notification template with given context and language
Returns a dict containing all content fields of the template. Example:
{'subject': 'bar', 'body': 'baz', 'html_body': '<b>foobar</b>'}
"""
env = SandboxedEnvironment(trim_blocks=True, lstrip_blocks=True, undefined=StrictUndefined)
env.filters['format_datetime'] = format_datetime
logger.debug('Rendering template for notification %s' % self.type)
activate(language_code)
try:
rendered_notification = {
attr: env.from_string(getattr(self, attr)).render(context)
for attr in ('subject', 'html_body')
}
if self.body:
rendered_notification['body'] = env.from_string(self.body).render(context)
else:
# if text body is empty use html body without tags as text body
rendered_notification['body'] = strip_tags(rendered_notification['html_body'])
return rendered_notification
except TemplateError as e:
raise NotificationTemplateException(e) from e
示例6: __init__
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def __init__(self, engine):
# Autoescape needs to be False to avoid html-encoding ampersands in emails. This
# does not create security vulns as on the frontend, html is escaped using Handlebars,
# and in emails, html is allowed and ampersands are escaped with markdown's renderer.
self.env = SandboxedEnvironment(autoescape=False)
self.engine = engine
self.active = {}
self.data = {}
示例7: test_sanitize_unicode_dict
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def test_sanitize_unicode_dict():
from jinja2.sandbox import SandboxedEnvironment
from iris.utils import sanitize_unicode_dict
# Use jinja the same way as in sender
env = SandboxedEnvironment(autoescape=False)
template = env.from_string('{{var}} {{var2}} {{nested.nest}}')
bad_context = {'var': b'\xe2\x80\x99', 'var2': 2, 'nested': {'nest': b'\xe2\x80\x99'}}
assert '\\xe2' in template.render(**bad_context)
good_render = template.render(**sanitize_unicode_dict(bad_context))
assert '\\xe2' not in good_render
assert b'\xe2\x80\x99'.decode('utf-8') in good_render
示例8: test_item_and_attribute
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def test_item_and_attribute(self):
from jinja2.sandbox import SandboxedEnvironment
for env in Environment(), SandboxedEnvironment():
# the |list is necessary for python3
tmpl = env.from_string('{{ foo.items()|list }}')
assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
tmpl = env.from_string('{{ foo|attr("items")()|list }}')
assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"
tmpl = env.from_string('{{ foo["items"] }}')
assert tmpl.render(foo={'items': 42}) == '42'
示例9: __init__
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def __init__(self,
job_name,
query_project=None,
location='US',
default_project=None,
default_dataset=None,
json_credentials_path=None):
"""
:param job_name: used as job name prefix
:param query_project: project used to submit queries
:param location: BigQuery defaults to 'US'
:param default_project: project to use when tablespec does not specify
project
:param default_dataset: dataset to use when tablespec does not specify
dataset, if default_project is also set
:param json_credentials_path: (optional) path to service account JSON
credentials file
"""
self.job_id_prefix = job_name + '-'
self.query_project = query_project
self.location = location
if default_project is None and query_project is not None:
self.default_project = query_project
else:
self.default_project = default_project
self.json_credentials_path = json_credentials_path
self.default_dataset = default_dataset
self.bq = None
self.jinja2 = SandboxedEnvironment()
示例10: process_template
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def process_template(content: str) -> str:
env = SandboxedEnvironment()
template = env.from_string(content)
context = {
"current_user_id": ExtraCache.current_user_id,
"current_username": ExtraCache.current_username,
}
return template.render(context)
示例11: __init__
# 需要导入模块: from jinja2 import sandbox [as 别名]
# 或者: from jinja2.sandbox import SandboxedEnvironment [as 别名]
def __init__(
self,
database: "Database",
query: Optional["Query"] = None,
table: Optional["SqlaTable"] = None,
extra_cache_keys: Optional[List[Any]] = None,
**kwargs: Any,
) -> None:
self.database = database
self.query = query
self.schema = None
if query and query.schema:
self.schema = query.schema
elif table:
self.schema = table.schema
extra_cache = ExtraCache(extra_cache_keys)
self.context = {
"url_param": extra_cache.url_param,
"current_user_id": extra_cache.current_user_id,
"current_username": extra_cache.current_username,
"cache_key_wrapper": extra_cache.cache_key_wrapper,
"filter_values": filter_values,
"form_data": {},
}
self.context.update(kwargs)
self.context.update(jinja_base_context)
if self.engine:
self.context[self.engine] = self
self.env = SandboxedEnvironment()