| 117 | |
| 118 | |
| 119 | class VerticalOutputFormatter(DelimitedOutputFormatter): |
| 120 | def __init__(self, column_names): |
| 121 | DelimitedOutputFormatter.__init__(self, field_delim="\n") |
| 122 | self.column_names = column_names |
| 123 | self.column_name_max_len = max([len(s) for s in column_names]) |
| 124 | |
| 125 | def format(self, rows): |
| 126 | """Returns string containing UTF-8-encoded representation of the table data.""" |
| 127 | # csv.writer expects a file handle to the input. |
| 128 | temp_buffer = StringIO() |
| 129 | writer = csv.writer(temp_buffer, delimiter=self.field_delim, |
| 130 | lineterminator='\n', quoting=csv.QUOTE_MINIMAL) |
| 131 | for r, row in enumerate(rows): |
| 132 | if sys.version_info.major == 2: |
| 133 | row = [val.encode('utf-8', 'replace') if isinstance(val, unicode) # noqa: F821 |
| 134 | else val for val in row] |
| 135 | writer.writerow(["************************************** " |
| 136 | + str(r + 1) + ".row **************************************"]) |
| 137 | for c, val in enumerate(row): |
| 138 | row[c] = self.column_names[c].rjust(self.column_name_max_len) + ": " + val |
| 139 | writer.writerow(row) |
| 140 | # The CSV writer produces an extra newline. Strip that extra newline (and |
| 141 | # only that extra newline). csv wraps newlines for data values in quotes, |
| 142 | # so rstrip will be limited to the extra newline. |
| 143 | if sys.version_info.major == 2: |
| 144 | # Python 2 is in encoded Unicode bytes, so this needs to be a bytes \n. |
| 145 | rows = temp_buffer.getvalue().rstrip(b'\n') |
| 146 | else: |
| 147 | rows = temp_buffer.getvalue().rstrip('\n') |
| 148 | temp_buffer.close() |
| 149 | return rows |
| 150 | |
| 151 | |
| 152 | class OutputStream(object): |
no outgoing calls
no test coverage detected