本文整理汇总了Python中st2common.util.jinja.get_jinja_environment函数的典型用法代码示例。如果您正苦于以下问题:Python get_jinja_environment函数的具体用法?Python get_jinja_environment怎么用?Python get_jinja_environment使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_jinja_environment函数的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_filter_from_yaml_string
def test_filter_from_yaml_string(self):
env = jinja_utils.get_jinja_environment()
expected_obj = {'a': 'b', 'c': {'d': 'e', 'f': 1, 'g': True}}
obj_yaml_str = ("---\n"
"a: b\n"
"c:\n"
" d: e\n"
" f: 1\n"
" g: true\n")
template = '{{k1 | from_yaml_string}}'
obj_str = env.from_string(template).render({'k1': obj_yaml_str})
obj = eval(obj_str)
self.assertDictEqual(obj, expected_obj)
# With KeyValueLookup object
env = jinja_utils.get_jinja_environment()
obj_yaml_str = ("---\n"
"- a\n"
"- b\n"
"- c\n")
expected_obj = ['a', 'b', 'c']
template = '{{ k1 | from_yaml_string }}'
lookup = KeyValueLookup(scope=FULL_SYSTEM_SCOPE, key_prefix='b')
lookup._value_cache['b'] = obj_yaml_str
obj_str = env.from_string(template).render({'k1': lookup})
obj = eval(obj_str)
self.assertEqual(obj, expected_obj)
开发者ID:nzlosh,项目名称:st2,代码行数:30,代码来源:test_jinja_render_data_filters.py
示例2: test_filters_regex_substring
def test_filters_regex_substring(self):
env = jinja_utils.get_jinja_environment()
# Normal (match)
template = r'{{input_str | regex_substring("([0-9]{3} \w+ (?:Ave|St|Dr))")}}'
actual = env.from_string(template).render(
{'input_str': 'My address is 123 Somewhere Ave. See you soon!'}
)
expected = '123 Somewhere Ave'
self.assertEqual(actual, expected)
# Selecting second match explicitly
template = r'{{input_str | regex_substring("([0-9]{3} \w+ (?:Ave|St|Dr))", 1)}}'
actual = env.from_string(template).render(
{'input_str': 'Your address is 567 Elsewhere Dr. My address is 123 Somewhere Ave.'}
)
expected = '123 Somewhere Ave'
self.assertEqual(actual, expected)
# Selecting second match explicitly, but doesn't exist
template = r'{{input_str | regex_substring("([0-9]{3} \w+ (?:Ave|St|Dr))", 1)}}'
with self.assertRaises(IndexError):
actual = env.from_string(template).render(
{'input_str': 'Your address is 567 Elsewhere Dr.'}
)
# No match
template = r'{{input_str | regex_substring("([0-3]{3} \w+ (?:Ave|St|Dr))")}}'
with self.assertRaises(IndexError):
actual = env.from_string(template).render(
{'input_str': 'My address is 986 Somewhere Ave. See you soon!'}
)
开发者ID:nzlosh,项目名称:st2,代码行数:32,代码来源:test_jinja_render_regex_filters.py
示例3: test_version_match
def test_version_match(self):
env = jinja_utils.get_jinja_environment()
template = '{{version | version_match(">0.10.0")}}'
actual = env.from_string(template).render({"version": "0.10.1"})
expected = "True"
self.assertEqual(actual, expected)
actual = env.from_string(template).render({"version": "0.1.1"})
expected = "False"
self.assertEqual(actual, expected)
template = '{{version | version_match("<0.10.0")}}'
actual = env.from_string(template).render({"version": "0.1.0"})
expected = "True"
self.assertEqual(actual, expected)
actual = env.from_string(template).render({"version": "1.1.0"})
expected = "False"
self.assertEqual(actual, expected)
template = '{{version | version_match("==0.10.0")}}'
actual = env.from_string(template).render({"version": "0.10.0"})
expected = "True"
self.assertEqual(actual, expected)
actual = env.from_string(template).render({"version": "0.10.1"})
expected = "False"
self.assertEqual(actual, expected)
开发者ID:Pulsant,项目名称:st2,代码行数:26,代码来源:test_jinja_render_version_filters.py
示例4: test_version_strip_patch
def test_version_strip_patch(self):
env = jinja_utils.get_jinja_environment()
template = "{{version | version_strip_patch}}"
actual = env.from_string(template).render({"version": "0.10.1"})
expected = "0.10"
self.assertEqual(actual, expected)
开发者ID:Pulsant,项目名称:st2,代码行数:7,代码来源:test_jinja_render_version_filters.py
示例5: test_filter_to_yaml_string
def test_filter_to_yaml_string(self):
env = jinja_utils.get_jinja_environment()
obj = {'a': 'b', 'c': {'d': 'e', 'f': 1, 'g': True}}
template = '{{k1 | to_yaml_string}}'
obj_yaml_str = env.from_string(template).render({'k1': obj})
actual_obj = yaml.safe_load(obj_yaml_str)
self.assertDictEqual(obj, actual_obj)
开发者ID:nzlosh,项目名称:st2,代码行数:8,代码来源:test_jinja_render_data_filters.py
示例6: setUp
def setUp(self):
super(JinjaUtilsDecryptTestCase, self).setUp()
crypto_key_path = cfg.CONF.keyvalue.encryption_key_path
crypto_key = read_crypto_key(key_path=crypto_key_path)
self.secret = 'Build a wall'
self.secret_value = symmetric_encrypt(encrypt_key=crypto_key, plaintext=self.secret)
self.env = jinja_utils.get_jinja_environment()
开发者ID:StackStorm,项目名称:st2,代码行数:9,代码来源:test_jinja_render_crypto_filters.py
示例7: __init__
def __init__(self, config=None, action_service=None):
super(FormatResultAction, self).__init__(config=config, action_service=action_service)
api_url = os.environ.get('ST2_ACTION_API_URL', None)
token = os.environ.get('ST2_ACTION_AUTH_TOKEN', None)
self.client = Client(api_url=api_url, token=token)
self.jinja = jinja_utils.get_jinja_environment()
self.jinja.tests['in'] = lambda item, list: item in list
path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(path, 'templates/default.j2'), 'r') as f:
self.default_template = f.read()
开发者ID:automotola,项目名称:st2,代码行数:11,代码来源:format_execution_result.py
示例8: test_dirname
def test_dirname(self):
env = jinja_utils.get_jinja_environment()
template = '{{k1 | dirname}}'
actual = env.from_string(template).render({'k1': '/some/path/to/file.txt'})
self.assertEqual(actual, '/some/path/to')
actual = env.from_string(template).render({'k1': '/some/path/to/dir'})
self.assertEqual(actual, '/some/path/to')
actual = env.from_string(template).render({'k1': '/some/path/to/dir/'})
self.assertEqual(actual, '/some/path/to/dir')
开发者ID:StackStorm,项目名称:st2,代码行数:12,代码来源:test_jinja_render_path_filters.py
示例9: test_filters_regex_replace
def test_filters_regex_replace(self):
env = jinja_utils.get_jinja_environment()
template = '{{k1 | regex_replace("x", "y")}}'
actual = env.from_string(template).render({'k1': 'xyz'})
expected = 'yyz'
self.assertEqual(actual, expected)
template = '{{k1 | regex_replace("(blue|white|red)", "color")}}'
actual = env.from_string(template).render({'k1': 'blue socks and red shoes'})
expected = 'color socks and color shoes'
self.assertEqual(actual, expected)
开发者ID:nzlosh,项目名称:st2,代码行数:12,代码来源:test_jinja_render_regex_filters.py
示例10: test_to_human_time_filter
def test_to_human_time_filter(self):
env = jinja_utils.get_jinja_environment()
template = '{{k1 | to_human_time_from_seconds}}'
actual = env.from_string(template).render({'k1': 12345})
self.assertEqual(actual, '3h25m45s')
actual = env.from_string(template).render({'k1': 0})
self.assertEqual(actual, '0s')
self.assertRaises(AssertionError, env.from_string(template).render,
{'k1': 'stuff'})
开发者ID:lyandut,项目名称:st2,代码行数:12,代码来源:test_jinja_render_time_filters.py
示例11: test_jsonpath_query_static
def test_jsonpath_query_static(self):
env = jinja_utils.get_jinja_environment()
obj = {'people': [{'first': 'James', 'last': 'd'},
{'first': 'Jacob', 'last': 'e'},
{'first': 'Jayden', 'last': 'f'},
{'missing': 'different'}],
'foo': {'bar': 'baz'}}
template = '{{ obj | jsonpath_query("people[*].first") }}'
actual_str = env.from_string(template).render({'obj': obj})
actual = eval(actual_str)
expected = ['James', 'Jacob', 'Jayden']
self.assertEqual(actual, expected)
开发者ID:lyandut,项目名称:st2,代码行数:13,代码来源:test_jinja_render_jsonpath_query_filters.py
示例12: test_filter_from_json_string
def test_filter_from_json_string(self):
env = jinja_utils.get_jinja_environment()
expected_obj = {'a': 'b', 'c': {'d': 'e', 'f': 1, 'g': True}}
obj_json_str = '{"a": "b", "c": {"d": "e", "f": 1, "g": true}}'
template = '{{k1 | from_json_string}}'
obj_str = env.from_string(template).render({'k1': obj_json_str})
obj = eval(obj_str)
self.assertDictEqual(obj, expected_obj)
# With KeyValueLookup object
env = jinja_utils.get_jinja_environment()
obj_json_str = '["a", "b", "c"]'
expected_obj = ['a', 'b', 'c']
template = '{{ k1 | from_json_string}}'
lookup = KeyValueLookup(scope=FULL_SYSTEM_SCOPE, key_prefix='a')
lookup._value_cache['a'] = obj_json_str
obj_str = env.from_string(template).render({'k1': lookup})
obj = eval(obj_str)
self.assertEqual(obj, expected_obj)
开发者ID:nzlosh,项目名称:st2,代码行数:23,代码来源:test_jinja_render_data_filters.py
示例13: test_jsonpath_query_no_results
def test_jsonpath_query_no_results(self):
env = jinja_utils.get_jinja_environment()
obj = {'people': [{'first': 'James', 'last': 'd'},
{'first': 'Jacob', 'last': 'e'},
{'first': 'Jayden', 'last': 'f'},
{'missing': 'different'}],
'foo': {'bar': 'baz'}}
query = "query_returns_no_results"
template = '{{ obj | jsonpath_query(query) }}'
actual_str = env.from_string(template).render({'obj': obj,
'query': query})
actual = eval(actual_str)
expected = None
self.assertEqual(actual, expected)
开发者ID:lyandut,项目名称:st2,代码行数:15,代码来源:test_jinja_render_jsonpath_query_filters.py
示例14: test_version_compare
def test_version_compare(self):
env = jinja_utils.get_jinja_environment()
template = '{{version | version_compare("0.10.0")}}'
actual = env.from_string(template).render({"version": "0.9.0"})
expected = "-1"
self.assertEqual(actual, expected)
template = '{{version | version_compare("0.10.0")}}'
actual = env.from_string(template).render({"version": "0.10.1"})
expected = "1"
self.assertEqual(actual, expected)
template = '{{version | version_compare("0.10.0")}}'
actual = env.from_string(template).render({"version": "0.10.0"})
expected = "0"
self.assertEqual(actual, expected)
开发者ID:Pulsant,项目名称:st2,代码行数:17,代码来源:test_jinja_render_version_filters.py
示例15: test_filters_regex_search
def test_filters_regex_search(self):
env = jinja_utils.get_jinja_environment()
template = '{{k1 | regex_search("x")}}'
actual = env.from_string(template).render({'k1': 'xyz'})
expected = 'True'
self.assertEqual(actual, expected)
template = '{{k1 | regex_search("y")}}'
actual = env.from_string(template).render({'k1': 'xyz'})
expected = 'True'
self.assertEqual(actual, expected)
template = '{{k1 | regex_search("^v(\\d+\\.)?(\\d+\\.)?(\\*|\\d+)$")}}'
actual = env.from_string(template).render({'k1': 'v0.10.1'})
expected = 'True'
self.assertEqual(actual, expected)
开发者ID:nzlosh,项目名称:st2,代码行数:17,代码来源:test_jinja_render_regex_filters.py
示例16: render_template
def render_template(value, context=None):
"""
Render provided template with the provided context.
:param value: Template string.
:type value: ``str``
:param context: Template context.
:type context: ``dict``
"""
assert isinstance(value, six.string_types)
context = context or {}
env = get_jinja_environment(allow_undefined=False) # nosec
template = env.from_string(value)
rendered = template.render(context)
return rendered
开发者ID:LindsayHill,项目名称:st2,代码行数:18,代码来源:templating.py
示例17: test_filter_decrypt_kv
def test_filter_decrypt_kv(self):
secret = 'Build a wall'
crypto_key_path = cfg.CONF.keyvalue.encryption_key_path
crypto_key = read_crypto_key(key_path=crypto_key_path)
secret_value = symmetric_encrypt(encrypt_key=crypto_key, plaintext=secret)
KeyValuePair.add_or_update(KeyValuePairDB(name='k8', value=secret_value,
scope=FULL_SYSTEM_SCOPE,
secret=True))
env = jinja_utils.get_jinja_environment()
context = {}
context.update({SYSTEM_SCOPE: KeyValueLookup(scope=SYSTEM_SCOPE)})
context.update({
DATASTORE_PARENT_SCOPE: {
SYSTEM_SCOPE: KeyValueLookup(scope=FULL_SYSTEM_SCOPE)
}
})
template = '{{st2kv.system.k8 | decrypt_kv}}'
actual = env.from_string(template).render(context)
self.assertEqual(actual, secret)
开发者ID:lyandut,项目名称:st2,代码行数:21,代码来源:test_jinja_render_crypto_filters.py
示例18: _split_params
import six
import networkx as nx
from jinja2 import meta
from st2common import log as logging
from st2common.constants.action import ACTION_CONTEXT_KV_PREFIX
from st2common.constants.keyvalue import SYSTEM_SCOPE
from st2common.exceptions.param import ParamException
from st2common.services.keyvalues import KeyValueLookup
from st2common.util.casts import get_cast
from st2common.util.compat import to_unicode
from st2common.util import jinja as jinja_utils
LOG = logging.getLogger(__name__)
ENV = jinja_utils.get_jinja_environment()
__all__ = [
'render_live_params',
'render_final_params',
]
def _split_params(runner_parameters, action_parameters, mixed_params):
def pf(params, skips):
result = {k: v for k, v in six.iteritems(mixed_params)
if k in params and k not in skips}
return result
return (pf(runner_parameters, {}), pf(action_parameters, runner_parameters))
开发者ID:maniacs-ops,项目名称:st2,代码行数:29,代码来源:param.py
示例19: test_tab
def test_tab(self):
env = jinja_utils.get_jinja_environment()
template = '{{ test_str | json_escape }}'
actual = env.from_string(template).render({'test_str': 'foo \t bar'})
expected = 'foo \\t bar'
self.assertEqual(actual, expected)
开发者ID:lyandut,项目名称:st2,代码行数:6,代码来源:test_jinja_render_json_escape_filters.py
注:本文中的st2common.util.jinja.get_jinja_environment函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论