本文整理匯總了Python中jinja2.Undefined方法的典型用法代碼示例。如果您正苦於以下問題:Python jinja2.Undefined方法的具體用法?Python jinja2.Undefined怎麽用?Python jinja2.Undefined使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類jinja2
的用法示例。
在下文中一共展示了jinja2.Undefined方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def __init__(self, params):
params = params.copy()
options = params.pop('OPTIONS').copy()
super().__init__(params)
self.context_processors = options.pop('context_processors', [])
environment = options.pop('environment', 'jinja2.Environment')
environment_cls = import_string(environment)
if 'loader' not in options:
options['loader'] = jinja2.FileSystemLoader(self.template_dirs)
options.setdefault('autoescape', True)
options.setdefault('auto_reload', settings.DEBUG)
options.setdefault('undefined',
jinja2.DebugUndefined if settings.DEBUG else jinja2.Undefined)
self.env = environment_cls(**options)
示例2: xproto_fol_to_python_test
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def xproto_fol_to_python_test(policy, fol, model, tag=None):
if isinstance(fol, jinja2.Undefined):
raise Exception("Could not find policy:", policy)
f2p = FOL2Python()
fol_reduced = f2p.hoist_outer(fol)
if fol_reduced in ["True", "False"] and fol != fol_reduced:
raise TrivialPolicy(
"Policy %(name)s trivially reduces to %(reduced)s."
"If this is what you want, replace its contents with %(reduced)s"
% {"name": policy, "reduced": fol_reduced}
)
a = f2p.gen_test_function(fol_reduced, policy, tag="security_check")
return astunparse.unparse(a)
示例3: xproto_fol_to_python_validator
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def xproto_fol_to_python_validator(policy, fol, model, message, tag=None):
if isinstance(fol, jinja2.Undefined):
raise Exception("Could not find policy:", policy)
f2p = FOL2Python()
fol_reduced = f2p.hoist_outer(fol)
if fol_reduced in ["True", "False"] and fol != fol_reduced:
raise TrivialPolicy(
"Policy %(name)s trivially reduces to %(reduced)s."
"If this is what you want, replace its contents with %(reduced)s"
% {"name": policy, "reduced": fol_reduced}
)
a = f2p.gen_validation_function(fol_reduced, policy, message, tag="validator")
return astunparse.unparse(a)
示例4: __init__
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def __init__(self, params):
params = params.copy()
options = params.pop('OPTIONS').copy()
super(Jinja2, self).__init__(params)
self.context_processors = options.pop('context_processors', [])
environment = options.pop('environment', 'jinja2.Environment')
environment_cls = import_string(environment)
if 'loader' not in options:
options['loader'] = jinja2.FileSystemLoader(self.template_dirs)
options.setdefault('autoescape', True)
options.setdefault('auto_reload', settings.DEBUG)
options.setdefault('undefined',
jinja2.DebugUndefined if settings.DEBUG else jinja2.Undefined)
self.env = environment_cls(**options)
示例5: customizable
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def customizable(context, val_list, call_type):
# NOTE(mgoddard): Don't try to customise undefined values. There are cases
# where this might happen, for example using a generic template overrides
# file for building multiple image install types and/or distros, where
# variables are not defined in every case.
if isinstance(val_list, Undefined):
return val_list
name = context['image_name'].replace("-", "_") + "_" + call_type + "_"
if name + "override" in context:
return context[name + "override"]
if name + "append" in context:
val_list.extend(context[name + "append"])
if name + "remove" in context:
for removal in context[name + "remove"]:
if removal in val_list:
val_list.remove(removal)
return val_list
示例6: get_jinja_environment
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def get_jinja_environment(allow_undefined=False, trim_blocks=True, lstrip_blocks=True):
'''
jinja2.Environment object that is setup with right behaviors and custom filters.
:param strict_undefined: If should allow undefined variables in templates
:type strict_undefined: ``bool``
'''
# Late import to avoid very expensive in-direct import (~1 second) when this function
# is not called / used
import jinja2
undefined = jinja2.Undefined if allow_undefined else jinja2.StrictUndefined
env = jinja2.Environment( # nosec
undefined=undefined,
trim_blocks=trim_blocks,
lstrip_blocks=lstrip_blocks
)
env.filters.update(get_filters())
env.tests['in'] = lambda item, list: item in list
return env
示例7: load_jinja
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def load_jinja(
path, tests, filters, classes, mode,
trim_blocks, lstrip_blocks, keep_trailing_newline):
from jinja2.defaults import BLOCK_START_STRING, BLOCK_END_STRING, \
VARIABLE_START_STRING, VARIABLE_END_STRING, \
COMMENT_START_STRING, COMMENT_END_STRING, \
LINE_STATEMENT_PREFIX, LINE_COMMENT_PREFIX, \
NEWLINE_SEQUENCE
undefined = {
'pedantic': jinja.StrictUndefined,
'debug': jinja.DebugUndefined,
None: jinja.Undefined,
}
env = jinja.Environment(
block_start_string=BLOCK_START_STRING,
block_end_string=BLOCK_END_STRING,
variable_start_string=VARIABLE_START_STRING,
variable_end_string=VARIABLE_END_STRING,
comment_start_string=COMMENT_START_STRING,
comment_end_string=COMMENT_END_STRING,
line_statement_prefix=LINE_STATEMENT_PREFIX,
line_comment_prefix=LINE_COMMENT_PREFIX,
trim_blocks=trim_blocks,
lstrip_blocks=lstrip_blocks,
newline_sequence=NEWLINE_SEQUENCE,
keep_trailing_newline=keep_trailing_newline,
extensions=classes,
undefined=undefined[mode],
loader=jinja.FileSystemLoader(path)
)
env.tests.update(tests)
env.filters.update(filters)
return env
示例8: __init__
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def __init__(self, params):
params = params.copy()
options = params.pop('OPTIONS').copy()
super(Jinja2, self).__init__(params)
environment = options.pop('environment', 'jinja2.Environment')
environment_cls = import_string(environment)
options.setdefault('autoescape', True)
options.setdefault('loader', jinja2.FileSystemLoader(self.template_dirs))
options.setdefault('auto_reload', settings.DEBUG)
options.setdefault('undefined',
jinja2.DebugUndefined if settings.DEBUG else jinja2.Undefined)
self.env = environment_cls(**options)
示例9: _j_getenv
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def _j_getenv(x):
if isinstance(x, Undefined):
x = x._undefined_name
return os.getenv(x, '')
示例10: _j_getshell
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def _j_getshell(x):
if isinstance(x, Undefined):
x = x._undefined_name
try:
return subprocess.check_output(x).decode()
except (IOError, OSError):
return ""
示例11: _j_passthrough
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def _j_passthrough(x, funcname):
if isinstance(x, Undefined):
x = x._undefined_name
return "{{%s(%s)}}" % (funcname, x)
示例12: js_filter
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def js_filter(val):
"""Jinja2 filter function 'js' produces JSONified string of the value, without surrounding quotes"""
if val is None or isinstance(val, jinja2.Undefined):
return "null"
js = json_filter(val)
return js[1:-1]
示例13: json_filter
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def json_filter(val, indent=0):
"""Jinja2 filter function 'json' produces JSONified string of the value"""
if val is None or isinstance(val, jinja2.Undefined):
return "null"
return json.dumps(val, indent=indent, sort_keys=True)
示例14: html_filter
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def html_filter(val):
"""Jinja2 filter function 'html' produces HTML-encoded string of the value"""
if isinstance(val, jinja2.Undefined):
return "[undefined]"
return html_escape(val)
示例15: url_filter
# 需要導入模塊: import jinja2 [as 別名]
# 或者: from jinja2 import Undefined [as 別名]
def url_filter(val):
"""Jinja2 filter function 'url' produces URL-encoded string of the value"""
if isinstance(val, jinja2.Undefined):
return "[undefined]"
return urllib.quote(str(val))