Skip to content

Commit a71ff02

Browse files
authored
[sfpshow] Gracefully handle improper 'specification_compliance' field (#1594)
#### What I did Gracefully handle improper 'specification_compliance' field #### How I did it The 'specification_compliance' field of transceiver info is expected to be a string representation of a dictionary. However, there is a chance, upon some kind of platform issue that a vendor's platform API returns something like 'N/A'. In this case, sfpshow would crash. Rather than crash, sfpshow should handle this gracefully and output 'N/A' instead.
1 parent 9a88cb6 commit a71ff02

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

scripts/sfpshow

+10-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
which accesses the transceiver directly.
77
"""
88

9+
import ast
910
import os
1011
import re
1112
import sys
@@ -273,10 +274,15 @@ class SFPShow(object):
273274
output += '{}{}: {}\n'.format(indent, QSFP_DATA_MAP[key], sfp_info_dict[key])
274275
else:
275276
output += '{}{}:\n'.format(indent, QSFP_DATA_MAP['specification_compliance'])
276-
spefic_compliance_dict = eval(sfp_info_dict['specification_compliance'])
277-
sorted_compliance_key_table = natsorted(spefic_compliance_dict)
278-
for compliance_key in sorted_compliance_key_table:
279-
output += '{}{}: {}\n'.format((indent * 2), compliance_key, spefic_compliance_dict[compliance_key])
277+
278+
spec_compliance_dict = {}
279+
try:
280+
spec_compliance_dict = ast.literal_eval(sfp_info_dict['specification_compliance'])
281+
sorted_compliance_key_table = natsorted(spec_compliance_dict)
282+
for compliance_key in sorted_compliance_key_table:
283+
output += '{}{}: {}\n'.format((indent * 2), compliance_key, spec_compliance_dict[compliance_key])
284+
except ValueError as e:
285+
output += '{}N/A\n'.format((indent * 2))
280286
else:
281287
output += '{}{}: {}\n'.format(indent, QSFP_DATA_MAP[key], sfp_info_dict[key])
282288

0 commit comments

Comments
 (0)