Exemplo n.º 1
0
    def _dict_list_table(self, dict_):
        """Convert dict to listed table."""
        header = ['key', 'value']
        key_row = []
        value_row = []

        for key, value in dict_.items():
            key_row.append(key)
            value_row.append(str(value))

        body = [key_row, value_row]
        summary = 'Total amount of objects'
        table = DynaTable(header, body, summary=summary)

        table_str = ''
        try:
            table_lst = table.assemble_table()
        except IndexError as error:
            msg = 'No results returned, canno\'t create table...'
            self.rbkcli_logger.error('DynamicTableError # ' + msg)
            raise RbkcliException.DynaTableError(msg + '\n')
        for line in table_lst:
            table_str = table_str + line + '\n'

        return table_str
Exemplo n.º 2
0
    def convert_to_html(self):
        """Convert json to table."""
        self.filled_worked = self.selected_data
        body = []
        for head in self.final_fields_order:
            row = []

            if isinstance(self.filled_worked, dict):
                if 'data' in self.filled_worked.keys():
                    self.filled_worked = self.filled_worked[data]
                    for objt in self.filled_worked:
                        try:
                            line = objt[head]
                        except Exception:
                            line = 'N/E'
                        line = str(line)
                        row.append(line)

                else:
                    try:
                        line = self.filled_worked[head]
                        line = str(line)
                    except KeyError:
                        line = 'N/E'
                    row.append(line)

            elif isinstance(self.filled_worked, list):
                for objt in self.filled_worked:
                    try:
                        line = objt[head]
                    except Exception:
                        line = 'N/E'
                    line = str(line)
                    row.append(line)

            body.append(row)

        headers = self.final_fields_order
        summary = ''
        if body:
            summary = '**Total amount of objects [%s]' % str(len(body[0]))

        cmd = '%s %s %s' % (self.req['version'], self.req['endpoint_matched'],
                            self.req['method'])

        table = html_table.Tabelfier(command=cmd,
                                     title='rbkcli command report',
                                     header=headers,
                                     columns=body,
                                     summary=summary)

        try:
            table_str = table.assemble()
        except IndexError as error:
            msg = 'No results returned, canno\'t create table...'
            self.rbkcli_logger.error('DynamicTableError # ' + msg)
            raise RbkcliException.DynaTableError(msg + '\n')

        return table_str
Exemplo n.º 3
0
    def convert_to_table(self):
        """Convert json to table."""
        self.filled_worked = self.selected_data
        body = []
        for head in self.final_fields_order:
            row = []

            if isinstance(self.filled_worked, dict):
                if 'data' in self.filled_worked.keys():
                    self.filled_worked = self.filled_worked[data]
                    for objt in self.filled_worked:
                        try:
                            line = objt[head]
                        except Exception:
                            line = 'N/E'
                        line = str(line)
                        row.append(line)

                else:
                    try:
                        line = self.filled_worked[head]
                        line = str(line)
                    except KeyError:
                        line = 'N/E'
                    row.append(line)

            elif isinstance(self.filled_worked, list):
                for objt in self.filled_worked:
                    try:
                        line = objt[head]
                    except Exception:
                        line = 'N/E'
                    line = str(line)
                    row.append(line)

            body.append(row)

        headers = self.final_fields_order
        summary = 'Total amount of objects'
        table = DynaTable(headers, body, summary=summary)

        table_str = ''
        try:
            table_lst = table.assemble_table()
        except IndexError as error:
            msg = 'No results returned, canno\'t create table...'
            self.rbkcli_logger.error('DynamicTableError # ' + msg)
            raise RbkcliException.DynaTableError(msg + '\n')

        for line in table_lst:
            table_str = table_str + line + '\n'

        return table_str
Exemplo n.º 4
0
    def _get_rows_size(headers_calc, rows_calc):
        """
        Get the size row should be.

        Joins all values in one list and gets the maximum size, of all
        values per row.
        """
        rows_size = []
        for i in enumerate(headers_calc):
            try:
                rows_calc[i[0]].append(headers_calc[i[0]])
                rows_size.append(len(max(rows_calc[i[0]], key=len)))
                rows_calc[i[0]].remove(headers_calc[i[0]])
            except IndexError:
                raise RbkcliException.DynaTableError('Incorrect number of '
                                                     'rows provided')

        return rows_size