]> Frank Brehm's Git Trees - my-stuff/ipv6-hostpart.git/commitdiff
Bis zum Importieren gekommen
authorFrank Brehm <frank@brehm-online.com>
Sun, 2 Jan 2011 18:05:11 +0000 (18:05 +0000)
committerFrank Brehm <frank@brehm-online.com>
Sun, 2 Jan 2011 18:05:11 +0000 (18:05 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/ipv6-hostpart/trunk@172 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

bin/ipv6-hostpart.py
bin/net/__init__.py [new file with mode: 0755]
bin/net/ll_hw_address.py

index 46943a927ddbfe165887b0c0f043582e3d3dc8dd..4bc8bda5dd6d59560101d90f779838c8db0bab5a 100755 (executable)
@@ -9,6 +9,7 @@ import re
 import pprint
 
 from fbrehm.common.getopt import BaseOptParser
+from net.ll_hw_address import LlHardwareAddress
 
 revision = '$Revision$'
 revision = re.sub( r'\$', '', revision )
@@ -36,4 +37,11 @@ if len( opt_parser.args ) < 1:
     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
diff --git a/bin/net/__init__.py b/bin/net/__init__.py
new file mode 100755 (executable)
index 0000000..e6b8243
--- /dev/null
@@ -0,0 +1,9 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+'''
+@author: Frank Brehm
+@contact: frank@brehm-online.com
+@copyright: (c) 2010 - 2011 by Frank Brehm, Berlin
+@summary: All my own stuff for networking
+'''
+# vim: fileencoding=utf-8 filetype=python ts=4
index 12a029ebd1d6c87f5170dda5bac1a7ddf3c920a9..67852944cdb610e26a7b36c80be7f0bd1eb343f7 100755 (executable)
@@ -24,6 +24,103 @@ __contact__    = 'frank@brehm-online.com'
 __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