File: ebr_dbg.py

package info (click to toggle)
networking-mlnx 1%3A13.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 704 kB
  • sloc: python: 4,863; sh: 180; makefile: 36
file content (73 lines) | stat: -rwxr-xr-x 2,331 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
#!/usr/bin/python
# Copyright 2013 Mellanox Technologies, Ltd
#
# 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.

from __future__ import print_function
import sys

from networking_mlnx.eswitchd.cli import conn_utils
from networking_mlnx.eswitchd.cli import exceptions

action = sys.argv[1]
client = conn_utils.ConnUtil()


def pprint_table(out, table):
    """Prints out a table of data, padded for alignment

    @param out: Output stream (file-like object)
    @param table: The table to print. A list of lists.
    Each row must have the same number of columns.
    """

    def get_max_width(table, index):
        """Get the maximum width of the given column index"""
        return max([len(str(row[index])) for row in table])

    col_paddings = []

    for i in range(len(table[0])):
        col_paddings.append(get_max_width(table, i))

    for row in table:
        # left col
        print(row[0].ljust(col_paddings[0] + 1), file=out)
        # rest of the cols
        for i in range(1, len(row)):
            col = str(row[i]).rjust(col_paddings[i] + 2)
            print(col, file=out)
        print(file=out)


def main():
    if action == 'get-tables':
        fabric = sys.argv[2]
        try:
            result = client.get_tables(fabric)
            for fabric, tables in result.items():
                print("FABRIC = %s" % fabric)
                print("========================")
                for table, data in tables.items():
                    print("TABLE: %s" % table)
                    pprint_table(sys.stdout, data)
                    print("========================")
        except exceptions.MlxException as e:
            sys.stderr.write("Error in get-tables command")
            sys.stderr.write(e.message)
            sys.exit(1)
        sys.exit(0)

if __name__ == '__main__':
    main()