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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
13 """Output formatters using shell syntax.
17 from cliff import columns
23 class ShellFormatter(base.SingleFormatter):
25 def add_argument_group(self, parser):
26 group = parser.add_argument_group(
27 title='shell formatter',
28 description='a format a UNIX shell can parse (variable="value")',
36 help=argparse.SUPPRESS,
43 help='add a prefix to all variable names',
46 def emit_one(self, column_names, data, stdout, parsed_args):
47 variable_names = [c.lower().replace(' ', '_')
50 desired_columns = parsed_args.variables
51 for name, value in zip(variable_names, data):
52 if name in desired_columns or not desired_columns:
53 value = (six.text_type(value.machine_readable())
54 if isinstance(value, columns.FormattableColumn)
56 if isinstance(value, six.string_types):
57 value = value.replace('"', '\\"')
58 if isinstance(name, six.string_types):
59 # Colons and dashes may appear as a resource property but
60 # are invalid to use in a shell, replace them with an
62 name = name.replace(':', '_')
63 name = name.replace('-', '_')
64 stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name, value))