File: columns.py

package info (click to toggle)
python-osc-lib 4.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 820 kB
  • sloc: python: 6,505; makefile: 21; sh: 2
file content (127 lines) | stat: -rw-r--r-- 4,289 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain
#   a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

import operator
import typing as ty

LIST_BOTH = 'both'
LIST_SHORT_ONLY = 'short_only'
LIST_LONG_ONLY = 'long_only'


def get_column_definitions(
    attr_map: list[tuple[str, str, str]], long_listing: bool
) -> tuple[list[str], list[str]]:
    """Return table headers and column names for a listing table.

    An attribute map (attr_map) is a list of table entry definitions
    and the format of the map is as follows:

    :param attr_map: a list of table entry definitions.
        Each entry should be a tuple consisting of
        (API attribute name, header name, listing mode). For example:

        .. code-block:: python

           (
               ('id', 'ID', LIST_BOTH),
               ('name', 'Name', LIST_BOTH),
               ('tenant_id', 'Project', LIST_LONG_ONLY),
           )

        The third field of each tuple must be one of LIST_BOTH,
        LIST_LONG_ONLY (a corresponding column is shown only in a long mode),
        or LIST_SHORT_ONLY (a corresponding column is shown only
        in a short mode).
    :param long_listing: A boolean value which indicates a long listing
        or not. In most cases, parsed_args.long is passed to this argument.
    :return: A tuple of a list of table headers and a list of column names.

    """

    if long_listing:
        headers = [
            hdr
            for col, hdr, listing_mode in attr_map
            if listing_mode in (LIST_BOTH, LIST_LONG_ONLY)
        ]
        columns = [
            col
            for col, hdr, listing_mode in attr_map
            if listing_mode in (LIST_BOTH, LIST_LONG_ONLY)
        ]
    else:
        headers = [
            hdr
            for col, hdr, listing_mode in attr_map
            if listing_mode
            if listing_mode in (LIST_BOTH, LIST_SHORT_ONLY)
        ]
        columns = [
            col
            for col, hdr, listing_mode in attr_map
            if listing_mode
            if listing_mode in (LIST_BOTH, LIST_SHORT_ONLY)
        ]

    return headers, columns


def get_columns(
    item: dict[str, ty.Any],
    attr_map: list[tuple[str, str, str]] | None = None,
) -> tuple[tuple[str, ...], tuple[str, ...]]:
    """Return pair of resource attributes and corresponding display names.

    :param item: a dictionary which represents a resource.
        Keys of the dictionary are expected to be attributes of the resource.
        Values are not referred to by this method.

        .. code-block:: python

           {
               'id': 'myid',
               'name': 'myname',
               'foo': 'bar',
               'tenant_id': 'mytenan',
           }

    :param attr_map: a list of mapping from attribute to display name.
        The same format is used as for get_column_definitions attr_map.

        .. code-block:: python

           (
               ('id', 'ID', LIST_BOTH),
               ('name', 'Name', LIST_BOTH),
               ('tenant_id', 'Project', LIST_LONG_ONLY),
           )

    :return: A pair of tuple of attributes and tuple of display names.

        .. code-block:: python

           (('id', 'name', 'tenant_id', 'foo'),  # attributes
            ('ID', 'Name', 'Project', 'foo')     # display names

        Both tuples of attributes and display names are sorted by display names
        in the alphabetical order.
        Attributes not found in a given attr_map are kept as-is.
    """
    attr_map = attr_map or []
    _attr_map_dict = dict((col, hdr) for col, hdr, listing_mode in attr_map)

    columns = [
        (column, _attr_map_dict.get(column, column)) for column in item.keys()
    ]
    columns = sorted(columns, key=operator.itemgetter(1))
    return (tuple(col[0] for col in columns), tuple(col[1] for col in columns))