def find_job_in_job_list(self, job_id): job_list = output_parser.table(self.cli.freezer_scheduler(action='job-list', flags='-c test_node')) for row in job_list['values']: if row[0].strip() == job_id.strip(): return row self.fail('Could not find job: {}'.format(job_id))
def parse_show(self, raw_output): """Return list of dicts with item values parsed from cli output.""" items = [] table_ = output_parser.table(raw_output) for row in table_['values']: item = {} item[row[0]] = row[1] items.append(item) return items
def multi_line_row_table(output_lines, group_by_column_index=0): parsed_table = output_parser.table(output_lines) rows = parsed_table['values'] row_index = 0 def get_column_index(column_name, headers, default): return next( (i for i, h in enumerate(headers) if h.lower() == column_name), default ) if group_by_column_index is None: group_by_column_index = get_column_index( 'id', parsed_table['headers'], 0) def is_embedded_table(parsed_rows): def is_table_border(t): return six.text_type(t).startswith('+') return (isinstance(parsed_rows, list) and len(parsed_rows) > 3 and is_table_border(parsed_rows[0]) and is_table_border(parsed_rows[-1])) def merge_cells(master_cell, value_cell): if value_cell: if not isinstance(master_cell, list): master_cell = [master_cell] master_cell.append(value_cell) if is_embedded_table(master_cell): return multi_line_row_table('\n'.join(master_cell), None) return master_cell def is_empty_row(row): empty_cells = 0 for cell in row: if cell == '': empty_cells += 1 return len(row) == empty_cells while row_index < len(rows): row = rows[row_index] line_with_value = row_index > 0 and row[group_by_column_index] == '' if line_with_value and not is_empty_row(row): rows[row_index - 1] = list(map(merge_cells, rows[row_index - 1], rows.pop(row_index))) else: row_index += 1 return parsed_table
def multi_line_row_table(output_lines, group_by_column_index=0): parsed_table = output_parser.table(output_lines) rows = parsed_table['values'] row_index = 0 def get_column_index(column_name, headers, default): return next( (i for i, h in enumerate(headers) if h.lower() == column_name), default) if group_by_column_index is None: group_by_column_index = get_column_index('id', parsed_table['headers'], 0) def is_embedded_table(parsed_rows): def is_table_border(t): return six.text_type(t).startswith('+') return (isinstance(parsed_rows, list) and len(parsed_rows) > 3 and is_table_border(parsed_rows[0]) and is_table_border(parsed_rows[-1])) def merge_cells(master_cell, value_cell): if value_cell: if not isinstance(master_cell, list): master_cell = [master_cell] master_cell.append(value_cell) if is_embedded_table(master_cell): return multi_line_row_table('\n'.join(master_cell), None) return master_cell def is_empty_row(row): empty_cells = 0 for cell in row: if cell == '': empty_cells += 1 return len(row) == empty_cells while row_index < len(rows): row = rows[row_index] line_with_value = row_index > 0 and row[group_by_column_index] == '' if line_with_value and not is_empty_row(row): rows[row_index - 1] = list( map(merge_cells, rows[row_index - 1], rows.pop(row_index))) else: row_index += 1 return parsed_table
def __init__(self, out): """This parses an output table with any number of headers, and any number of entries: +--------------------------------------+----------+---------+ | id | name | type | +--------------------------------------+----------+---------+ | e658a875-1024-4f88-a347-e5b244ec5a10 | aaa.com. | PRIMARY | +--------------------------------------+----------+---------+ | 98d1fb5f-2954-448e-988e-6f1df0f24c52 | bbb.com. | PRIMARY | +--------------------------------------+----------+---------+ These are then accessible as: model[0].name == 'aaa.com.' model[1].name == 'bbb.com.' """ table = output_parser.table(out) for entry in table['values']: self.append(ListEntryModel(table['headers'], entry))
def __init__(self, out): """This parses output with fields and values like: +----------------+------------------------------+ | Field | Value | +----------------+------------------------------+ | action | CREATE | | created_at | 2015-08-20T17:22:17.000000 | | description | None | +----------------+------------------------------+ These are then accessible as: model.action model.created_at model.description """ table = output_parser.table(out) # Because the output_parser handles Values with multiple lines # in additional Field/Value pairs with Field name '', the following # code is necessary to aggregate Values. # # The list of Field/Value pairs is in-order, so we can append Value # continuation to the previously seen Field, with a newline separator. value_lines = [] prev_field = None for field, value in table['values']: if field == '': value_lines.append(value) setattr(self, prev_field, '\n'.join(value_lines)) else: setattr(self, field, value) prev_field = field value_lines = [value]
def get_table_struct(self, command, params=""): """Get table structure i.e. header of table.""" return output_parser.table(self.murano(command, params=params))['headers']
def test_table_with_invalid_line(self): output_lines = self.OUTPUT_LINES + "aaaa" actual = output_parser.table(output_lines) self.assertIsInstance(actual, dict) self.assertEqual(self.EXPECTED_TABLE, actual)
def test_table_with_list(self): output_lines = self.OUTPUT_LINES.split('\n') actual = output_parser.table(output_lines) self.assertIsInstance(actual, dict) self.assertEqual(self.EXPECTED_TABLE, actual)
def test_table_with_normal_values(self): actual = output_parser.table(self.OUTPUT_LINES) self.assertIsInstance(actual, dict) self.assertEqual(self.EXPECTED_TABLE, actual)
def test_table_with_list(self): output_lines = self.OUTPUT_LINES.split("\n") actual = output_parser.table(output_lines) self.assertIsInstance(actual, dict) self.assertEqual(self.EXPECTED_TABLE, actual)