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 csv format.
19 from .base import ListFormatter
20 from cliff import columns
24 if sys.version_info[0] == 3:
27 import unicodecsv as csv
30 class CSVLister(ListFormatter):
34 'minimal': csv.QUOTE_MINIMAL,
35 'nonnumeric': csv.QUOTE_NONNUMERIC,
36 'none': csv.QUOTE_NONE,
39 def add_argument_group(self, parser):
40 group = parser.add_argument_group('CSV Formatter')
43 choices=sorted(self.QUOTE_MODES.keys()),
46 help='when to include quotes, defaults to nonnumeric',
49 def emit_list(self, column_names, data, stdout, parsed_args):
50 writer = csv.writer(stdout,
51 quoting=self.QUOTE_MODES[parsed_args.quote_mode],
52 lineterminator=os.linesep,
55 writer.writerow(column_names)
58 [(six.text_type(c.machine_readable())
59 if isinstance(c, columns.FormattableColumn)