import pprint
from fbrehm.common.getopt import BaseOptParser
+from net.ll_hw_address import LlHardwareAddress
revision = '$Revision$'
revision = re.sub( r'\$', '', revision )
opt_parser.parser.print_help( sys.stderr )
sys.exit(1)
+for address in opt_parser.args:
+ try:
+ addr_object = LlHardwareAddress( address = address, verbose = verbose )
+ except ValueError as e:
+ msg = "Could not determine low level hardware address {0!r}.".format( address )
+ print >> sys.stderr, msg
+
# vim: fileencoding=utf-8 filetype=python ts=4 expandtab
__version__ = '0.1.0'
__license__ = 'GPL3'
+class LlHardwareAddressError(Exception):
+ """Base class for exceptions in this module."""
+ pass
+class LlHardwareAddress(LoggingObject):
+ """Class for encapsulation of a low level hardware address.
+ """
+
+ #------------------------------------------------------
+ def __init__( self, address = None,
+ verbose = 0, ):
+ """Constructor.
+ @param address: The address to store
+ @type address: str or None
+ @param verbose: verbosity level (default: 0)
+ @type verbose: int
+ @return: None
+ @rtype: None
+ """
+
+ super( LlHardwareAddress, self ).__init__( verbose = verbose )
+
+ # Other properties
+
+ self.address = None
+ self.type = None
+
+ self._import_address( address )
+
+ #------------------------------------------------------
+ def _import_address( self, address ):
+ '''This private function imports the given address in self.address
+ and determines the type of the address.
+ In case of an impossible hardware address it raises an ValueError.
+ @param address: The address to store
+ @type address: str
+ @return: None
+ @rtype: None
+ '''
+
+ if address is None:
+ raise ValueError( "No address given." )
+ return
+
+ if not isinstance( address, str ):
+ raise ValueError( "Invalid address given." )
+ return
+
+ address = re.sub( r'^\s*', '', address )
+ address = re.sub( r'\s*$', '', address )
+
+ if address == '':
+ raise ValueError( "Invalid (empty) address given." )
+ return
+
+ self.logger.debug( "Importing address {0!r} ...".format( address ) )
+ pp = pprint.PrettyPrinter( indent = 4, depth = 6, width = 120 )
+
+ hexdigit = r'[0-9a-z]'
+ max_hexbyte = hexdigit + r'{1,2}'
+
+ match = re.search( r'^(?:([0-9a-z]{1,2})[:-]){5}([0-9a-z]{1,2})$', address, re.IGNORECASE )
+ if match is not None:
+ match_list = re.split( r'[:-]', address )
+ if self.verbose >= 3:
+ self.logger.debug( "Found match list: {0!r}.".format( pp.pformat( match_list ) ) )
+ byte_list = []
+ for byte in match_list[:]:
+ val = int( byte, 16 )
+ byte_list.append(val)
+ self.address = tuple( byte_list[:] )
+ self.type = 'mac'
+ if self.verbose >= 2:
+ self.logger.debug( "Imported MAC address: {0!r}.".format( pp.pformat( self.address ) ) )
+ return
+
+ match_str = r'^0x'
+ for i in range(8):
+ match_str += r'([0-9a-z]{2})'
+ match_str += r'$'
+
+ match = re.search( match_str, address, re.IGNORECASE )
+ if match is not None:
+ match_list = match.groups()
+ if self.verbose >= 3:
+ self.logger.debug( "Found match list: {0!r}.".format( pp.pformat( match_list ) ) )
+ byte_list = []
+ for byte in match_list[:]:
+ val = int( byte, 16 )
+ byte_list.append(val)
+ self.address = tuple( byte_list[:] )
+ self.type = 'guid'
+ if self.verbose >= 2:
+ self.logger.debug( "Imported Infiniband GUID: {0!r}.".format( pp.pformat( self.address ) ) )
+ return
+
+ raise ValueError( "Could not import address {0!r}.".format( address ) )
+ return
# vim: fileencoding=utf-8 filetype=python ts=4 expandtab