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


import argparse, sys, ModularGenprefdlLib, Cell


usage = """modulargenprefdl <option>

--supervisor
    Returns decoded prefdl for the current supervisor

--midplane
    Returns decoded prefdl for midplane

--switchcard <number>
   Returns decoded prefdl for switch card
   
--fabric <number>
    Returns decoded prefdl for fabric card with <number>

--linecard <number>
    Returns decoded prefdl for linecard with <number>

--abbrev
    Returns only codename and version of the hardware device instead of its
    whole prefdl

--hwEpoch
    Returns only the HwEpoch of the hardware device

--allPrefdlType
    Returns only prefdl type of all hardware devices

These combinations are currently unsupported:
    modulargenprefdl --linecard 3,5
    modulargenprefdl --linecard 3 --supervisor

"""

### EXECUTABLE CODE SECTION ###############################################

parser = argparse.ArgumentParser( usage, add_help=True )
parser.add_argument( "--supervisor", dest="supervisor", default=False,
      action="store_true",
      help="""Returns decoded prefdl for supervisor""" )
parser.add_argument( "--midplane", dest="midplane", default=False,
      action="store_true",
      help="""Returns decoded prefdl for midplane.""" )
parser.add_argument( "--switchcard", dest="switchcard", default=None,
      help="""Returns decoded prefdl for switch card""" )
parser.add_argument( "--fabric", dest="fabric", default=None,
      help="""Returns decoded prefdl for fabric card""" )
parser.add_argument( "--linecard", dest="linecard", default=None,
      help="""Returns decoded prefdl for linecard""" )
parser.add_argument( "--abbrev", dest="abbrev", default=False,
      action="store_true",
      help="""Returns only codename and version of the hardware device
      instead of its whole prefdl""" )
parser.add_argument( "--hwEpoch", dest="hwEpoch", default=False,
      action="store_true",
      help="""Returns only HwEpoch of the hardware devices""" )
parser.add_argument( "--allPrefdlType", dest="allPrefdlType", default=None,
      help="""Returns only prefdl type of all hardware devices""" )

( options, arguments ) = parser.parse_known_args()
prefdl = ""

try:
   modPrefdlReader = ModularGenprefdlLib.ModularPrefdlReader()
except ModularGenprefdlLib.UnsupportedException as e:
   print( e )
   sys.exit( 1 )

if options.supervisor:
   prefdl = modPrefdlReader.readDevicePrefdl( "Supervisor", Cell.cellId(), 
                                              options.abbrev )
elif options.midplane:
   prefdl = modPrefdlReader.readDevicePrefdl( "Midplane", 1, options.abbrev )
elif options.fabric:
   prefdl = modPrefdlReader.readDevicePrefdl( "Fabric", int( options.fabric ),
                                              options.abbrev )
elif options.linecard:
   prefdl = modPrefdlReader.readDevicePrefdl( "Linecard", int( options.linecard ),
                                              options.abbrev )
elif options.switchcard:
   prefdl = modPrefdlReader.readDevicePrefdl( "Switchcard",
                                              int( options.switchcard ),
                                              options.abbrev )
elif options.hwEpoch:
   prefdl = modPrefdlReader.readAllHwEpoch()
elif options.allPrefdlType:
   prefdl = modPrefdlReader.readAllPrefdlType( options.allPrefdlType )
else:
   # Abbrev result is a string since it is not the raw prefdl contents
   prefdl = modPrefdlReader.readAllPrefdlsAbbrev().encode()

if prefdl:
   if isinstance( prefdl, dict ):
      prefdlStr = ""
      for key, value in prefdl.items():
         prefdlStr += f"{key}: {value}\n"
      prefdl = prefdlStr
   if isinstance( prefdl, str ):
      if not prefdl.endswith( '\n' ):
         prefdl += '\n'
      prefdl = prefdl.encode( 'utf-8' )
   getattr( sys.stdout, 'buffer', sys.stdout ).write( prefdl )
else:
   sys.exit( 1 )

