Exemplo n.º 1
0
    def format(cls, entries, *args, **kwargs):
        attributes = kwargs.get('attributes', [])
        attribute_transform_functions = kwargs.get('attribute_transform_functions', {})
        widths = kwargs.get('widths', [])
        widths = widths or []

        if not widths and attributes:
            # Dynamically calculate column size based on the terminal size
            lines, cols = get_terminal_size()

            if attributes[0] == 'id':
                # consume iterator and save as entries so collection is accessible later.
                entries = [e for e in entries]
                # first column contains id, make sure it's not broken up
                first_col_width = cls._get_required_column_width(values=[e.id for e in entries],
                                                                 minimum_width=MIN_ID_COL_WIDTH)
                cols = (cols - first_col_width)
                col_width = int(math.floor((cols / len(attributes))))
            else:
                col_width = int(math.floor((cols / len(attributes))))
                first_col_width = col_width

            widths = []
            for index in range(0, len(attributes)):
                if index == 0:
                    widths.append(first_col_width)
                else:
                    widths.append(col_width)

        if not attributes or 'all' in attributes:
            attributes = sorted([attr for attr in entries[0].__dict__
                                 if not attr.startswith('_')])

        # Determine table format.
        if len(attributes) == len(widths):
            # Customize width for each column.
            columns = zip(attributes, widths)
        else:
            # If only 1 width value is provided then
            # apply it to all columns else fix at 28.
            width = widths[0] if len(widths) == 1 else 28
            columns = zip(attributes,
                          [width for i in range(0, len(attributes))])

        # Format result to table.
        table = PrettyTable()
        for column in columns:
            table.field_names.append(column[0])
            table.max_width[column[0]] = column[1]
        table.padding_width = 1
        table.align = 'l'
        table.valign = 't'
        for entry in entries:
            # TODO: Improve getting values of nested dict.
            values = []
            for field_name in table.field_names:
                if '.' in field_name:
                    field_names = field_name.split('.')
                    value = getattr(entry, field_names.pop(0), {})
                    for name in field_names:
                        value = cls._get_field_value(value, name)
                        if type(value) is str:
                            break
                    value = strutil.unescape(value)
                    values.append(value)
                else:
                    value = cls._get_simple_field_value(entry, field_name)
                    transform_function = attribute_transform_functions.get(field_name,
                                                                          lambda value: value)
                    value = transform_function(value=value)
                    value = strutil.unescape(value)
                    values.append(value)
            table.add_row(values)
        return table
Exemplo n.º 2
0
    def format(cls, entries, *args, **kwargs):
        attributes = kwargs.get('attributes', [])
        attribute_transform_functions = kwargs.get(
            'attribute_transform_functions', {})
        widths = kwargs.get('widths', [])
        widths = widths or []

        if not widths and attributes:
            # Dynamically calculate column size based on the terminal size
            lines, cols = get_terminal_size()

            if attributes[0] == 'id':
                # consume iterator and save as entries so collection is accessible later.
                entries = [e for e in entries]
                # first column contains id, make sure it's not broken up
                first_col_width = cls._get_required_column_width(
                    values=[e.id for e in entries],
                    minimum_width=MIN_ID_COL_WIDTH)
                cols = (cols - first_col_width)
                col_width = int(math.floor((cols / len(attributes))))
            else:
                col_width = int(math.floor((cols / len(attributes))))
                first_col_width = col_width
            widths = []
            subtract = 0
            for index in range(0, len(attributes)):
                attribute_name = attributes[index]

                if index == 0:
                    widths.append(first_col_width)
                    continue

                if attribute_name in COLORIZED_ATTRIBUTES:
                    current_col_width = COLORIZED_ATTRIBUTES[attribute_name][
                        'col_width']
                    subtract += (current_col_width - col_width)
                else:
                    # Make sure we subtract the added width from the last column so we account
                    # for the fixed width columns and make sure table is not wider than the
                    # terminal width.
                    if index == (len(attributes) - 1) and subtract:
                        current_col_width = (col_width - subtract)

                        if current_col_width <= MIN_COL_WIDTH:
                            # Make sure column width is always grater than MIN_COL_WIDTH
                            current_col_width = MIN_COL_WIDTH
                    else:
                        current_col_width = col_width

                widths.append(current_col_width)

        if not attributes or 'all' in attributes:
            entries = list(entries) if entries else []

            if len(entries) >= 1:
                attributes = entries[0].__dict__.keys()
                attributes = sorted(
                    [attr for attr in attributes if not attr.startswith('_')])
            else:
                # There are no entries so we can't infer available attributes
                attributes = []

        # Determine table format.
        if len(attributes) == len(widths):
            # Customize width for each column.
            columns = zip(attributes, widths)
        else:
            # If only 1 width value is provided then
            # apply it to all columns else fix at 28.
            width = widths[0] if len(widths) == 1 else 28
            columns = zip(attributes,
                          [width for i in range(0, len(attributes))])

        # Format result to table.
        table = PrettyTable()
        for column in columns:
            table.field_names.append(column[0])
            table.max_width[column[0]] = column[1]
        table.padding_width = 1
        table.align = 'l'
        table.valign = 't'
        for entry in entries:
            # TODO: Improve getting values of nested dict.
            values = []
            for field_name in table.field_names:
                if '.' in field_name:
                    field_names = field_name.split('.')
                    value = getattr(entry, field_names.pop(0), {})
                    for name in field_names:
                        value = cls._get_field_value(value, name)
                        if type(value) is str:
                            break
                    value = strutil.strip_carriage_returns(
                        strutil.unescape(value))
                    values.append(value)
                else:
                    value = cls._get_simple_field_value(entry, field_name)
                    transform_function = attribute_transform_functions.get(
                        field_name, lambda value: value)
                    value = transform_function(value=value)
                    value = strutil.strip_carriage_returns(
                        strutil.unescape(value))
                    values.append(value)
            table.add_row(values)

        # width for the note
        global table_width
        try:
            table_width = len(table.get_string().split("\n")[0])
        except IndexError:
            table_width = 0

        return table
Exemplo n.º 3
0
Arquivo: table.py Projeto: lyandut/st2
    def format(cls, entries, *args, **kwargs):
        attributes = kwargs.get('attributes', [])
        attribute_transform_functions = kwargs.get('attribute_transform_functions', {})
        widths = kwargs.get('widths', [])
        widths = widths or []

        if not widths and attributes:
            # Dynamically calculate column size based on the terminal size
            lines, cols = get_terminal_size()

            if attributes[0] == 'id':
                # consume iterator and save as entries so collection is accessible later.
                entries = [e for e in entries]
                # first column contains id, make sure it's not broken up
                first_col_width = cls._get_required_column_width(values=[e.id for e in entries],
                                                                 minimum_width=MIN_ID_COL_WIDTH)
                cols = (cols - first_col_width)
                col_width = int(math.floor((cols / len(attributes))))
            else:
                col_width = int(math.floor((cols / len(attributes))))
                first_col_width = col_width
            widths = []
            subtract = 0
            for index in range(0, len(attributes)):
                attribute_name = attributes[index]

                if index == 0:
                    widths.append(first_col_width)
                    continue

                if attribute_name in COLORIZED_ATTRIBUTES:
                    current_col_width = COLORIZED_ATTRIBUTES[attribute_name]['col_width']
                    subtract += (current_col_width - col_width)
                else:
                    # Make sure we subtract the added width from the last column so we account
                    # for the fixed width columns and make sure table is not wider than the
                    # terminal width.
                    if index == (len(attributes) - 1) and subtract:
                        current_col_width = (col_width - subtract)

                        if current_col_width <= MIN_COL_WIDTH:
                            # Make sure column width is always grater than MIN_COL_WIDTH
                            current_col_width = MIN_COL_WIDTH
                    else:
                        current_col_width = col_width

                widths.append(current_col_width)

        if not attributes or 'all' in attributes:
            entries = list(entries) if entries else []

            if len(entries) >= 1:
                attributes = list(entries[0].__dict__.keys())
                attributes = sorted([attr for attr in attributes if not attr.startswith('_')])
            else:
                # There are no entries so we can't infer available attributes
                attributes = []

        # Determine table format.
        if len(attributes) == len(widths):
            # Customize width for each column.
            columns = zip(attributes, widths)
        else:
            # If only 1 width value is provided then
            # apply it to all columns else fix at 28.
            width = widths[0] if len(widths) == 1 else 28
            columns = zip(attributes,
                          [width for i in range(0, len(attributes))])

        # Format result to table.
        table = PrettyTable()
        for column in columns:
            table.field_names.append(column[0])
            table.max_width[column[0]] = column[1]
        table.padding_width = 1
        table.align = 'l'
        table.valign = 't'
        for entry in entries:
            # TODO: Improve getting values of nested dict.
            values = []
            for field_name in table.field_names:
                if '.' in field_name:
                    field_names = field_name.split('.')
                    value = getattr(entry, field_names.pop(0), {})
                    for name in field_names:
                        value = cls._get_field_value(value, name)
                        if type(value) is str:
                            break
                    value = strutil.strip_carriage_returns(strutil.unescape(value))
                    values.append(value)
                else:
                    value = cls._get_simple_field_value(entry, field_name)
                    transform_function = attribute_transform_functions.get(field_name,
                                                                           lambda value: value)
                    value = transform_function(value=value)
                    value = strutil.strip_carriage_returns(strutil.unescape(value))
                    values.append(value)
            table.add_row(values)

        # width for the note
        try:
            cls.table_width = len(table.get_string().split("\n")[0])
        except IndexError:
            cls.table_width = 0

        return table