Returns string containing representation of the table data.
(self, rows)
| 52 | self.prettytable = prettytable |
| 53 | |
| 54 | def format(self, rows): |
| 55 | """Returns string containing representation of the table data.""" |
| 56 | |
| 57 | def decode_if_needed(row): |
| 58 | if sys.version_info.major >= 3: |
| 59 | return row |
| 60 | # prettytable will decode with 'strict' if the string is not already unicode, |
| 61 | # we should do it here to handle invalid characters. |
| 62 | return [entry.decode('UTF-8', 'replace') if isinstance(entry, str) else entry |
| 63 | for entry in row] |
| 64 | |
| 65 | # Clear rows that already exist in the table. |
| 66 | self.prettytable.clear_rows() |
| 67 | try: |
| 68 | for row in rows: |
| 69 | self.prettytable.add_row(decode_if_needed(row)) |
| 70 | return self.prettytable.get_string() |
| 71 | except Exception as e: |
| 72 | # beeswax returns each row as a tab separated string. If a string column |
| 73 | # value in a row has tabs, it will break the row split. Default to displaying |
| 74 | # raw results. This will change with a move to hiveserver2. Reference: IMPALA-116 |
| 75 | error_msg = ("Prettytable cannot resolve string columns values that have " |
| 76 | "embedded tabs. Reverting to tab delimited text output") |
| 77 | print(error_msg, file=sys.stderr) |
| 78 | print('{0}: {1}'.format(type(e), str(e)), file=sys.stderr) |
| 79 | |
| 80 | return '\n'.join(['\t'.join(decode_if_needed(row)) for row in rows]) |
| 81 | |
| 82 | |
| 83 | class DelimitedOutputFormatter(object): |