Skip to content

Commit

Permalink
[sfpshow] Gracefully handle improper 'specification_compliance' field (
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
jleveque authored May 7, 2021
1 parent 9a88cb6 commit a71ff02
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions scripts/sfpshow
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
which accesses the transceiver directly.
"""

import ast
import os
import re
import sys
Expand Down Expand Up @@ -273,10 +274,15 @@ class SFPShow(object):
output += '{}{}: {}\n'.format(indent, QSFP_DATA_MAP[key], sfp_info_dict[key])
else:
output += '{}{}:\n'.format(indent, QSFP_DATA_MAP['specification_compliance'])
spefic_compliance_dict = eval(sfp_info_dict['specification_compliance'])
sorted_compliance_key_table = natsorted(spefic_compliance_dict)
for compliance_key in sorted_compliance_key_table:
output += '{}{}: {}\n'.format((indent * 2), compliance_key, spefic_compliance_dict[compliance_key])

spec_compliance_dict = {}
try:
spec_compliance_dict = ast.literal_eval(sfp_info_dict['specification_compliance'])
sorted_compliance_key_table = natsorted(spec_compliance_dict)
for compliance_key in sorted_compliance_key_table:
output += '{}{}: {}\n'.format((indent * 2), compliance_key, spec_compliance_dict[compliance_key])
except ValueError as e:
output += '{}N/A\n'.format((indent * 2))
else:
output += '{}{}: {}\n'.format(indent, QSFP_DATA_MAP[key], sfp_info_dict[key])

Expand Down

0 comments on commit a71ff02

Please sign in to comment.