Refactor oemmap lookup

Previously, looking up an unknown OEM resulted in an exception being
logged:

exception while get_oem_handler, oemid:{'device_id': 32, 'device_revision': 1, 'manufacturer_id': 10876, 'product_id': 2414, 'firmware_version': '1.73'}
Traceback (most recent call last):
  File "/omd/sites/plutex/local/lib/python3/pyghmi/ipmi/oem/lookup.py", line 43, in get_oem_handler
    return (oemmap[oemid['manufacturer_id']].OEMHandler(oemid,
KeyError: 10876

So while I was at it, I also reduced duplicate code.

Signed-off-by: Jan-Philipp Litza <jpl@plutex.de>
Change-Id: Ib2483aeb3f92bcafbaa877eb1c0318a385f97474
This commit is contained in:
Jan-Philipp Litza 2021-12-17 14:25:50 +01:00
parent 3606780826
commit e2b2f6806b
1 changed files with 8 additions and 10 deletions

View File

@ -33,18 +33,16 @@ oemmap = {
def get_oem_handler(oemid, ipmicmd, *args):
try:
# first try to find with composite key manufacturer_id.product_id,
# if found return directly
# then try to find with manufacturer_id
item = '{}.{}'.format(oemid['manufacturer_id'], oemid['product_id'])
# first try to find with composite key manufacturer_id.product_id,
# if found return directly
# then try to find with manufacturer_id
for item in (
'{}.{}'.format(oemid['manufacturer_id'], oemid['product_id']),
oemid['manufacturer_id'],
):
if item in oemmap:
return (oemmap[item].OEMHandler(oemid, ipmicmd, *args), True)
return (oemmap[oemid['manufacturer_id']].OEMHandler(oemid,
ipmicmd, *args), True)
except KeyError:
logger.exception(
'exception while get_oem_handler, oemid:{0}'.format(oemid))
else:
return generic.OEMHandler(oemid, ipmicmd, *args), False