Skip to content

Commit 061f428

Browse files
authored
[show/fgnhg.py] Fix whitespace issues (sonic-net#1211)
- Replace tabs with spaces - Fix alignment - Remove trailing whitespace - Remove spaces where not necessary, add spaces where necessary - Reorganize imports per PEP8 standards
1 parent acfa824 commit 061f428

File tree

1 file changed

+111
-108
lines changed

1 file changed

+111
-108
lines changed

show/fgnhg.py

+111-108
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
import click
21
import ipaddress
3-
from tabulate import tabulate
2+
from collections import OrderedDict
3+
4+
import click
5+
import utilities_common.cli as clicommon
46
from swsssdk import ConfigDBConnector
57
from swsssdk import SonicV2Connector
6-
import utilities_common.cli as clicommon
7-
from collections import OrderedDict
8+
from tabulate import tabulate
9+
810

911
@click.group(cls=clicommon.AliasedGroup)
1012
def fgnhg():
1113
"""Show FGNHG information"""
1214
pass
1315

16+
1417
@fgnhg.command()
1518
@click.argument('nhg', required=False)
1619
def active_hops(nhg):
1720
config_db = ConfigDBConnector()
1821
config_db.connect()
1922
fg_nhg_prefix_table = {}
20-
fg_nhg_alias = {}
23+
fg_nhg_alias = {}
2124
fg_nhg_prefix_table = config_db.get_table('FG_NHG_PREFIX')
22-
25+
2326
for key, value in fg_nhg_prefix_table.items():
24-
fg_nhg_alias[key] = value['FG_NHG']
25-
27+
fg_nhg_alias[key] = value['FG_NHG']
28+
2629
state_db = SonicV2Connector(host='127.0.0.1')
2730
state_db.connect(state_db.STATE_DB, False) # Make one attempt only STATE_DB
28-
31+
2932
TABLE_NAME_SEPARATOR = '|'
3033
prefix = 'FG_ROUTE_TABLE' + TABLE_NAME_SEPARATOR
3134
_hash = '{}{}'.format(prefix, '*')
@@ -34,68 +37,68 @@ def active_hops(nhg):
3437
t_dict = {}
3538
table = []
3639
output_dict = {}
37-
40+
3841
if nhg is None:
39-
for nhg_prefix in table_keys :
40-
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
42+
for nhg_prefix in table_keys:
43+
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
4144
vals = sorted(set([val for val in t_dict.values()]))
42-
for nh_ip in vals:
43-
if nhg_prefix in output_dict:
44-
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
45-
else:
46-
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
47-
48-
nhg_prefix_report = (nhg_prefix.split("|")[1])
49-
header = ["FG_NHG_PREFIX", "Active Next Hops"]
45+
for nh_ip in vals:
46+
if nhg_prefix in output_dict:
47+
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
48+
else:
49+
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
50+
51+
nhg_prefix_report = (nhg_prefix.split("|")[1])
52+
header = ["FG_NHG_PREFIX", "Active Next Hops"]
5053
formatted_nhps = ','.replace(',', '\n').join(output_dict[nhg_prefix])
5154
table.append([nhg_prefix_report, formatted_nhps])
5255

53-
click.echo(tabulate(table, header, tablefmt = "grid"))
54-
56+
click.echo(tabulate(table, header, tablefmt="grid"))
57+
5558
else:
56-
for nhg_prefix, alias in fg_nhg_alias.items():
57-
if nhg == alias:
58-
if ":" in nhg_prefix:
59-
for key in table_keys:
60-
mod_key = key.split("|")[1].split("/")[0]
61-
mod_nhg_prefix = nhg_prefix.split("/")[0]
62-
if ipaddress.ip_address(unicode(mod_key)).exploded == ipaddress.ip_address(unicode(mod_nhg_prefix)).exploded:
59+
for nhg_prefix, alias in fg_nhg_alias.items():
60+
if nhg == alias:
61+
if ":" in nhg_prefix:
62+
for key in table_keys:
63+
mod_key = key.split("|")[1].split("/")[0]
64+
mod_nhg_prefix = nhg_prefix.split("/")[0]
65+
if ipaddress.ip_address(unicode(mod_key)).exploded == ipaddress.ip_address(unicode(mod_nhg_prefix)).exploded:
6366
t_dict = state_db.get_all(state_db.STATE_DB, key)
64-
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
67+
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
6568
else:
66-
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
67-
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
68-
69-
vals = sorted(set([val for val in t_dict.values()]))
70-
71-
for nh_ip in vals:
72-
if nhg_prefix in output_dict:
73-
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
74-
else:
75-
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
76-
77-
nhg_prefix_report = (nhg_prefix.split("|")[1])
78-
formatted_nhps = ','.replace(',', '\n').join(output_dict[nhg_prefix])
79-
table.append([nhg_prefix_report, formatted_nhps])
80-
header = ["FG_NHG_PREFIX", "Active Next Hops"]
81-
click.echo(tabulate(table, header, tablefmt = "grid"))
82-
69+
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
70+
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
71+
72+
vals = sorted(set([val for val in t_dict.values()]))
73+
74+
for nh_ip in vals:
75+
if nhg_prefix in output_dict:
76+
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
77+
else:
78+
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
79+
80+
nhg_prefix_report = (nhg_prefix.split("|")[1])
81+
formatted_nhps = ','.replace(',', '\n').join(output_dict[nhg_prefix])
82+
table.append([nhg_prefix_report, formatted_nhps])
83+
header = ["FG_NHG_PREFIX", "Active Next Hops"]
84+
click.echo(tabulate(table, header, tablefmt="grid"))
85+
8386

8487
@fgnhg.command()
8588
@click.argument('nhg', required=False)
8689
def hash_view(nhg):
8790
config_db = ConfigDBConnector()
8891
config_db.connect()
8992
fg_nhg_prefix_table = {}
90-
fg_nhg_alias = {}
93+
fg_nhg_alias = {}
9194
fg_nhg_prefix_table = config_db.get_table('FG_NHG_PREFIX')
92-
95+
9396
for key, value in fg_nhg_prefix_table.items():
94-
fg_nhg_alias[key] = value['FG_NHG']
95-
96-
state_db = SonicV2Connector(host='127.0.0.1')
97+
fg_nhg_alias[key] = value['FG_NHG']
98+
99+
state_db = SonicV2Connector(host='127.0.0.1')
97100
state_db.connect(state_db.STATE_DB, False) # Make one attempt only STATE_DB
98-
101+
99102
TABLE_NAME_SEPARATOR = '|'
100103
prefix = 'FG_ROUTE_TABLE' + TABLE_NAME_SEPARATOR
101104
_hash = '{}{}'.format(prefix, '*')
@@ -107,63 +110,63 @@ def hash_view(nhg):
107110
bank_dict = {}
108111

109112
if nhg is None:
110-
for nhg_prefix in table_keys :
111-
bank_dict = {}
112-
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
113+
for nhg_prefix in table_keys:
114+
bank_dict = {}
115+
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
113116
vals = sorted(set([val for val in t_dict.values()]))
114117

115-
for nh_ip in vals:
116-
bank_ids = sorted([int(k) for k, v in t_dict.items() if v == nh_ip])
117-
118-
bank_ids = [str(x) for x in bank_ids]
119-
120-
if nhg_prefix in output_dict:
121-
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
122-
else:
123-
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
124-
bank_dict[nh_ip.split("@")[0]] = bank_ids
125-
126-
bank_dict = OrderedDict(sorted(bank_dict.items()))
127-
nhg_prefix_report = (nhg_prefix.split("|")[1])
128-
header = ["FG_NHG_PREFIX", "Next Hop", "Hash buckets"]
129-
130-
for nhip,val in bank_dict.items():
131-
formatted_banks = ','.replace(',', '\n').join(bank_dict[nhip])
132-
table.append([nhg_prefix_report, nhip, formatted_banks])
133-
134-
click.echo(tabulate(table, header, tablefmt = "grid"))
135-
118+
for nh_ip in vals:
119+
bank_ids = sorted([int(k) for k, v in t_dict.items() if v == nh_ip])
120+
121+
bank_ids = [str(x) for x in bank_ids]
122+
123+
if nhg_prefix in output_dict:
124+
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
125+
else:
126+
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
127+
bank_dict[nh_ip.split("@")[0]] = bank_ids
128+
129+
bank_dict = OrderedDict(sorted(bank_dict.items()))
130+
nhg_prefix_report = (nhg_prefix.split("|")[1])
131+
header = ["FG_NHG_PREFIX", "Next Hop", "Hash buckets"]
132+
133+
for nhip, val in bank_dict.items():
134+
formatted_banks = ','.replace(',', '\n').join(bank_dict[nhip])
135+
table.append([nhg_prefix_report, nhip, formatted_banks])
136+
137+
click.echo(tabulate(table, header, tablefmt="grid"))
138+
136139
else:
137-
for nhg_prefix, alias in fg_nhg_alias.items():
138-
if nhg == alias:
139-
if ":" in nhg_prefix:
140-
for key in table_keys:
141-
mod_key = key.split("|")[1].split("/")[0]
142-
mod_nhg_prefix = nhg_prefix.split("/")[0]
143-
if ipaddress.ip_address(unicode(mod_key)).exploded == ipaddress.ip_address(unicode(mod_nhg_prefix)).exploded:
140+
for nhg_prefix, alias in fg_nhg_alias.items():
141+
if nhg == alias:
142+
if ":" in nhg_prefix:
143+
for key in table_keys:
144+
mod_key = key.split("|")[1].split("/")[0]
145+
mod_nhg_prefix = nhg_prefix.split("/")[0]
146+
if ipaddress.ip_address(unicode(mod_key)).exploded == ipaddress.ip_address(unicode(mod_nhg_prefix)).exploded:
144147
t_dict = state_db.get_all(state_db.STATE_DB, key)
145-
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
148+
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
146149
else:
147-
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
148-
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
149-
150-
vals = sorted(set([val for val in t_dict.values()]))
151-
152-
for nh_ip in vals:
153-
bank_ids = sorted([int(k) for k, v in t_dict.items() if v == nh_ip])
154-
bank_ids = [str(x) for x in bank_ids]
155-
if nhg_prefix in output_dict:
156-
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
157-
else:
158-
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
159-
bank_dict[nh_ip.split("@")[0]] = bank_ids
160-
161-
nhg_prefix_report = (nhg_prefix.split("|")[1])
162-
bank_dict = OrderedDict(sorted(bank_dict.items()))
163-
header = ["FG_NHG_PREFIX", "Next Hop", "Hash buckets"]
164-
165-
for nhip,val in bank_dict.items():
166-
formatted_banks = ','.replace(',', '\n').join(bank_dict[nhip])
167-
table.append([nhg_prefix_report, nhip, formatted_banks])
168-
169-
click.echo(tabulate(table, header, tablefmt = "grid"))
150+
nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
151+
t_dict = state_db.get_all(state_db.STATE_DB, nhg_prefix)
152+
153+
vals = sorted(set([val for val in t_dict.values()]))
154+
155+
for nh_ip in vals:
156+
bank_ids = sorted([int(k) for k, v in t_dict.items() if v == nh_ip])
157+
bank_ids = [str(x) for x in bank_ids]
158+
if nhg_prefix in output_dict:
159+
output_dict[nhg_prefix].append(nh_ip.split("@")[0])
160+
else:
161+
output_dict[nhg_prefix] = [nh_ip.split("@")[0]]
162+
bank_dict[nh_ip.split("@")[0]] = bank_ids
163+
164+
nhg_prefix_report = (nhg_prefix.split("|")[1])
165+
bank_dict = OrderedDict(sorted(bank_dict.items()))
166+
header = ["FG_NHG_PREFIX", "Next Hop", "Hash buckets"]
167+
168+
for nhip, val in bank_dict.items():
169+
formatted_banks = ','.replace(',', '\n').join(bank_dict[nhip])
170+
table.append([nhg_prefix_report, nhip, formatted_banks])
171+
172+
click.echo(tabulate(table, header, tablefmt="grid"))

0 commit comments

Comments
 (0)