1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
import ast |
17
|
|
|
import logging |
18
|
|
|
import sys |
19
|
|
|
|
20
|
|
|
import yaml |
21
|
|
|
|
22
|
|
|
from st2client import formatters |
23
|
|
|
from st2client.utils import jsutil |
24
|
|
|
from st2client.utils import strutil |
25
|
|
|
from st2client.utils.color import DisplayColors |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
LOG = logging.getLogger(__name__) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class ExecutionResult(formatters.Formatter): |
32
|
|
|
|
33
|
|
|
@classmethod |
34
|
|
|
def format(cls, entry, *args, **kwargs): |
35
|
|
|
attrs = kwargs.get('attributes', []) |
36
|
|
|
key = kwargs.get('key', None) |
37
|
|
|
if key: |
38
|
|
|
output = jsutil.get_value(entry.result, key) |
39
|
|
|
else: |
40
|
|
|
# drop entry to the dict so that jsutil can operate |
41
|
|
|
entry = vars(entry) |
42
|
|
|
output = '' |
43
|
|
|
for attr in attrs: |
44
|
|
|
value = jsutil.get_value(entry, attr) |
45
|
|
|
if (isinstance(value, basestring) and len(value) > 0 and |
|
|
|
|
46
|
|
|
value[0] in ['{', '['] and value[len(value) - 1] in ['}', ']']): |
47
|
|
|
new_value = ast.literal_eval(value) |
48
|
|
|
if type(new_value) in [dict, list]: |
49
|
|
|
value = new_value |
50
|
|
|
if type(value) in [dict, list]: |
51
|
|
|
# 1. To get a nice overhang indent get safe_dump to generate output with |
52
|
|
|
# the attribute key and then remove the attribute key from the string. |
53
|
|
|
# 2. Drop the trailing newline |
54
|
|
|
# 3. Set width to maxint so pyyaml does not split text. Anything longer |
55
|
|
|
# and likely we will see other issues like storage :P. |
56
|
|
|
formatted_value = yaml.safe_dump({attr: value}, |
57
|
|
|
default_flow_style=False, |
58
|
|
|
width=sys.maxint, |
59
|
|
|
indent=2)[len(attr) + 2:-1] |
60
|
|
|
value = ('\n' if isinstance(value, dict) else '') + formatted_value |
61
|
|
|
output += ('\n' if output else '') + '%s: %s' % \ |
62
|
|
|
(DisplayColors.colorize(attr, DisplayColors.BLUE), value) |
63
|
|
|
return strutil.unescape(output) |
64
|
|
|
|