Debian Codegraph @

osc_lib/utils/columns.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import operator
14 import typing as ty
15
16 LIST_BOTH = 'both'
17 LIST_SHORT_ONLY = 'short_only'
18 LIST_LONG_ONLY = 'long_only'
19
20
21 def get_column_definitions(
22 attr_map: list[tuple[str, str, str]], long_listing: bool
23 ) -> tuple[list[str], list[str]]:
24 """Return table headers and column names for a listing table.
25
26 An attribute map (attr_map) is a list of table entry definitions
27 and the format of the map is as follows:
28
29 :param attr_map: a list of table entry definitions.
30 Each entry should be a tuple consisting of
31 (API attribute name, header name, listing mode). For example:
32
33 .. code-block:: python
34
35 (
36 ('id', 'ID', LIST_BOTH),
37 ('name', 'Name', LIST_BOTH),
38 ('tenant_id', 'Project', LIST_LONG_ONLY),
39 )
40
41 The third field of each tuple must be one of LIST_BOTH,
42 LIST_LONG_ONLY (a corresponding column is shown only in a long mode),
43 or LIST_SHORT_ONLY (a corresponding column is shown only
44 in a short mode).
45 :param long_listing: A boolean value which indicates a long listing
46 or not. In most cases, parsed_args.long is passed to this argument.
47 :return: A tuple of a list of table headers and a list of column names.
48
49 """
50
51 if long_listing:
52 headers = [
53 hdr
54 for col, hdr, listing_mode in attr_map
55 if listing_mode in (LIST_BOTH, LIST_LONG_ONLY)
56 ]
57 columns = [
58 col
59 for col, hdr, listing_mode in attr_map
60 if listing_mode in (LIST_BOTH, LIST_LONG_ONLY)
61 ]
62 else:
63 headers = [
64 hdr
65 for col, hdr, listing_mode in attr_map
66 if listing_mode
67 if listing_mode in (LIST_BOTH, LIST_SHORT_ONLY)
68 ]
69 columns = [
70 col
71 for col, hdr, listing_mode in attr_map
72 if listing_mode
73 if listing_mode in (LIST_BOTH, LIST_SHORT_ONLY)
74 ]
75
76 return headers, columns
77
78
79 def get_columns(
80 item: dict[str, ty.Any],
81 attr_map: list[tuple[str, str, str]] | None = None,
82 ) -> tuple[tuple[str, ...], tuple[str, ...]]:
83 """Return pair of resource attributes and corresponding display names.
84
85 :param item: a dictionary which represents a resource.
86 Keys of the dictionary are expected to be attributes of the resource.
87 Values are not referred to by this method.
88
89 .. code-block:: python
90
91 {
92 'id': 'myid',
93 'name': 'myname',
94 'foo': 'bar',
95 'tenant_id': 'mytenan',
96 }
97
98 :param attr_map: a list of mapping from attribute to display name.
99 The same format is used as for get_column_definitions attr_map.
100
101 .. code-block:: python
102
103 (
104 ('id', 'ID', LIST_BOTH),
105 ('name', 'Name', LIST_BOTH),
106 ('tenant_id', 'Project', LIST_LONG_ONLY),
107 )
108
109 :return: A pair of tuple of attributes and tuple of display names.
110
111 .. code-block:: python
112
113 (('id', 'name', 'tenant_id', 'foo'), # attributes
114 ('ID', 'Name', 'Project', 'foo') # display names
115
116 Both tuples of attributes and display names are sorted by display names
117 in the alphabetical order.
118 Attributes not found in a given attr_map are kept as-is.
119 """
120 attr_map = attr_map or []
121 _attr_map_dict = dict((col, hdr) for col, hdr, listing_mode in attr_map)
122
123 columns = [
124 (column, _attr_map_dict.get(column, column)) for column in item.keys()
125 ]
126 columns = sorted(columns, key=operator.itemgetter(1))
127 return (tuple(col[0] for col in columns), tuple(col[1] for col in columns))