Debian Codegraph @

heatclient/common/format_utils.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12 #
13 # Copyright 2015 IBM Corp.
14
15 import sys
16
17 from osc_lib.command import command
18
19
20 class RawFormat(command.ShowOne):
21
22 def produce_output(self, parsed_args, column_names, data):
23 if data is None:
24 return
25
26 self.formatter.emit_one(column_names, data,
27 self.app.stdout, parsed_args)
28
29
30 class JsonFormat(RawFormat):
31
32 @property
33 def formatter_default(self):
34 return 'json'
35
36
37 class YamlFormat(RawFormat):
38
39 @property
40 def formatter_default(self):
41 return 'yaml'
42
43
44 class ShellFormat(RawFormat):
45
46 @property
47 def formatter_default(self):
48 return 'shell'
49
50
51 class ValueFormat(RawFormat):
52
53 @property
54 def formatter_default(self):
55 return 'value'
56
57
58 def indent_and_truncate(txt, spaces=0, truncate=False, truncate_limit=10,
59 truncate_prefix=None, truncate_postfix=None):
60 """Indents supplied multiline text by the specified number of spaces
61
62 """
63 if txt is None:
64 return
65 lines = str(txt).splitlines()
66 if truncate and len(lines) > truncate_limit:
67 lines = lines[-truncate_limit:]
68 if truncate_prefix is not None:
69 lines.insert(0, truncate_prefix)
70 if truncate_postfix is not None:
71 lines.append(truncate_postfix)
72
73 if spaces > 0:
74 lines = [" " * spaces + line for line in lines]
75 return '\n'.join(lines)
76
77
78 def print_software_deployment_output(data, name, out=sys.stdout, long=False):
79 """Prints details of the software deployment for user consumption
80
81 The format attempts to be valid yaml, but is primarily aimed at showing
82 useful information to the user in a helpful layout.
83 """
84 if data is None:
85 data = {}
86 if name in ('deploy_stdout', 'deploy_stderr'):
87 output = indent_and_truncate(
88 data.get(name),
89 spaces=4,
90 truncate=not long,
91 truncate_prefix='...',
92 truncate_postfix='(truncated, view all with --long)')
93 out.write(f' {name}: |\n{output}\n')
94 else:
95 out.write(f' {name}: {data.get(name)}\n')