Completed
Pull Request — master (#2842)
by Edward
05:24
created

RenderTemplateAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 8 2
A __init__() 0 4 2
1
#!/usr/bin/env python
2
3
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
4
# contributor license agreements.  See the NOTICE file distributed with
5
# this work for additional information regarding copyright ownership.
6
# The ASF licenses this file to You under the Apache License, Version 2.0
7
# (the "License"); you may not use this file except in compliance with
8
# the License.  You may obtain a copy of the License at
9
#
10
#     http://www.apache.org/licenses/LICENSE-2.0
11
#
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
17
18
import os
19
20
from st2actions.runners.pythonrunner import Action
21
from st2common.util import jinja as jinja_utils
22
23
24
class RenderTemplateAction(Action):
25
    def __init__(self, config=None, action_service=None):
26
        super(RenderTemplateAction, self).__init__(config=config, action_service=action_service)
27
        self.jinja = jinja_utils.get_jinja_environment(allow_undefined=True)
28
        self.jinja.tests['in'] = lambda item, list: item in list
29
30
    def run(self, template_path, context):
31
        path = os.path.dirname(os.path.realpath(__file__))
32
        with open(os.path.join(path, template_path), 'r') as f:
33
            template = f.read()
34
35
        result = self.jinja.from_string(template).render(context)
36
37
        return result
38