def __call__(self, parser, namespace, values, option_string=None): # Get all the report types and formats. matrix = [] for report_class in get_all_reports(): formats = report_class.get_supported_formats() matrix.append((report_class.names[0], formats)) # Compute a list of unique output formats. all_formats = sorted( {format_ for name, formats in matrix for format_ in formats}, key=lambda fmt: self.format_order.get(fmt, self.format_order_last)) # Bulid a list of rows. rows = [] for name, formats in matrix: xes = ['X' if fmt in formats else '' for fmt in all_formats] rows.append([name] + xes) # Build a description of the rows, a field specificaiton. header = ['Name'] + all_formats field_spec = [(index, name) for index, name in enumerate(header)] # Create and render an ASCII table. table_ = table.create_table(rows, field_spec) sys.stdout.write(table.table_to_text(table_, " ")) sys.exit(0)
def test_table_to_text(self): table_object = self.test_create_table() text = table.table_to_text(table_object, formats={'amount': '>'}) expected = textwrap.dedent("""\ Country Capital Currency Amount ---------- ---------- --------- ------ Malawi Lilongwe Kwacha 0.111 Mali Bamako CFA franc 0.222 Mauritania Nouakchott Ouguiya 0.333 ---------- ---------- --------- ------ """) self.assertEqual(expected, text)