#!/usr/bin/env python3
# Copyright (c) 2019 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

import SmbusLib
import argparse, sys, re

usage="""
xcvrSmbusAddr <portName>
   This program takes an input name as input and returns the
   SMBus address of the port named by <portName>

   <portName>
      Must be a valid port name such as 'Ethernet1' or 'Fabric1' on a fixed system,
      or 'Ethernet3/1' / 'Fabric3/1' on a modular system.
      Input is case sensitive.

   For example, To get Ethernet5's SMBus address on linecard 6:
      bash# ./xcvrSmbusAddr "Ethernet6/5"
      /scd/08/00/0x50
"""

def parse():
   parser = argparse.ArgumentParser( prog="xcvrSmbusAddr", usage=usage )

   parser.add_argument( "portName", action="store", type=str,
                        help="desired interface (Ex: Ethernet1/1, Fabric1/1)" )
   args = parser.parse_args()

   m = re.match( r'(Ethernet|Fabric)(\d+)(/\d+)?$', args.portName )
   if not m or not m.group( 2 ):
      # pylint: disable-next=consider-using-f-string
      parser.error( "%s is not a valid interface name" % args.portName )

   return args.portName

if __name__ == "__main__":
   portName = parse()
   mapping = SmbusLib.TransceiverSmbusDeviceMap().mapping
   if portName in mapping:
      device = mapping[ portName ]
      addr = ( "/scd/%02d/%02d/0x%02x" % # pylint: disable=consider-using-f-string
               ( device.accelId, device.busId, device.deviceId ) )
      if device.linecardNumber is not None:
         # pylint: disable-next=consider-using-f-string
         addr += " -l %d" % device.linecardNumber
      print( addr )
   else:
      # pylint: disable-next=consider-using-f-string
      print( "Interface %s not found" % portName )
      sys.exit( 1 )
