Commits

buge committed 3aa903f

Implemented address recognition from string.

  • Participants
  • Parent commits c4906f5

Comments (0)

Files changed (2)

File addrinfo/addresses.py

 
 
 import operator
+import re
 
 from addrinfo.parser import *
-try:
-    from addrinfo.oui import OUIS
-except ImportError:
-    OUIS = {}
-
 
 __all__ = [
     'Address',
 ]
 
 
+#EUI48_PATTERN = re.compile("^[0-9a-fA-F]{2}([-:][0-9a-fA-F]{2}){5}$")
+#EUI64_PATTERN = re.compile("^[0-9a-fA-F]{2}([-:][0-9a-fA-F]{2}){7}$")
+#IPv4_PATTERN = re.compile("^0x[0-9a-fA-F]+|[1-9][0-9]+|0[0-8]+|((\.0x[0-9a-fA-F]+|[1-9][0-9]+|0[0-8]+){3})?$")
+#IPv6_PATTERN = re.compile("^([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4})+)?::([0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4})+)?|[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$")
+#IPv6_PATTERN = re.compile("([0-9a-fA-F]{1,4}(:[0-9a-fA-F])*)?::([0-9a-fA-F]{1,4}(:[0-9a-fA-F])*)?")
+
+try:
+    from addrinfo.oui import OUIS
+except ImportError:
+    OUIS = {}
+
+
 class Address(object):
 
     @classmethod
     def value_of(cls, value):
-        # TODO implement
-        raise NotImplementedError("not yet implemented")
+        # TODO Hmm... I think we can do better than this
+        if "::" in value:
+            return IPv6Address.value_of(value)
+        if re.match(r"[0-9a-fA-F]{2}([-:][0-9a-fA-F]{2})+$", value):
+            return EUIAddress.value_of(value)
+        if ":" in value:
+            return IPv6Address.value_of(value)
+        return IPv4Address.value_of(value)
 
     def __init__(self, address):
         a = int(address)
 class EUIAddress(Address):
 
     @classmethod
+    def value_of(cls, value):
+        eui_matches = len(re.findall(r"(\A|[-:])[0-9a-fA-F]{2}", value))
+        if eui_matches <= 6:
+            return EUI48Address.value_of(value)
+        return EUI64Address.value_of(value)
+
+    @classmethod
     def get_hardware_address(cls):
         from uuid import getnode
         return EUI48Address(getnode())

File tests/addresses_testcase.py

 from addrinfo.addresses import _AddressFormatter
 
 
+#class TestModule(unittest.TestCase):
+#
+#    def test__eui48_pattern__hyphens(self):
+#        match = EUI48_PATTERN.match("01-23-45-67-89-AB")
+#        self.assertTrue(match is not None)
+#
+#    def test__eui48_pattern__colons(self):
+#        match = EUI48_PATTERN.match("01:23:45:67:89:AB")
+#        self.assertTrue(match is not None)
+#
+#    def test__eui48_pattern__smallcaps(self):
+#        match = EUI48_PATTERN.match("01:23:45:67:89:ab")
+#        self.assertTrue(match is not None)
+#
+#    def test__eui64_pattern__hyphens(self):
+#        match = EUI64_PATTERN.match("01-23-45-67-89-AB-CD-EF")
+#        self.assertTrue(match is not None)
+#
+#    def test__eui64_pattern__colons(self):
+#        match = EUI64_PATTERN.match("01:23:45:67:89:AB:CD:EF")
+#        self.assertTrue(match is not None)
+#
+#    def test__eui64_pattern__smallcaps(self):
+#        match = EUI64_PATTERN.match("01:23:45:67:89:ab:cd:ef")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv4_pattern__dot_decimal(self):
+#        match = IPv4_PATTERN.match("192.0.2.235")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv4_pattern__dot_hexadecimal(self):
+#        match = IPv4_PATTERN.match("0xC0.0x00.0x02.0xEB")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv4_pattern__dot_octal(self):
+#        match = IPv4_PATTERN.match("0300.0000.0002.0353")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv4_pattern__dot_mixed(self):
+#        match = IPv4_PATTERN.match("192.0x00.0002.235")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv4_pattern__hexadecimal(self):
+#        match = IPv4_PATTERN.match("0xC00002EB")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv4_pattern__decimal(self):
+#        match = IPv4_PATTERN.match("3221226219")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv4_pattern__octal(self):
+#        match = IPv4_PATTERN.match("030000001353")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv6_pattern__full(self):
+#        match = IPv6_PATTERN.match("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv6_pattern__no_leading_zeros(self):
+#        match = IPv6_PATTERN.match("2001:db8:85a3:0:0:8a2e:370:7334")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv6_pattern__grouped(self):
+#        match = IPv6_PATTERN.match("2001:db8:85a3::8a2e:370:7334")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv6_pattern__loopback(self):
+#        match = IPv6_PATTERN.match("::1")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv6_pattern__unspecified(self):
+#        match = IPv6_PATTERN.match("::")
+#        self.assertTrue(match is not None)
+#
+#    def test__ipv6_pattern__dotted_quad(self):
+#        match = IPv6_PATTERN.match("::ffff:192.0.2.128")
+#        self.assertTrue(match is not None)
+
+
 class TestAddress(Address):
     """Short address for Address unit tests."""
 
 
 class AddressTestCase(unittest.TestCase):
 
-#   def test__value_of__mac_hypens(self):
-#       expected = EUI48Address(1250999896491)
-#       actual = Address.value_of("01-23-45-67-89-ab")
-#       self.assertEqual(expected, actual)
+    def test__value_of__eui48_hypens(self):
+        expected = EUI48Address(1250999896491)
+        actual = Address.value_of("01-23-45-67-89-ab")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__mac_colons(self):
-#       expected = EUI48Address(1250999896491)
-#       actual = Address.value_of("01:23:45:67:89:ab")
-#       self.assertEqual(expected, actual)
+    def test__value_of__eui48_colons(self):
+        expected = EUI48Address(1250999896491)
+        actual = Address.value_of("01:23:45:67:89:ab")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv4_dot_decimal(self):
-#       expected = IPv4Address(3221226219)
-#       actual = Address.value_of("192.0.2.235")
-#       self.assertEqual(expected, actual)
+    def test__value_of__eui64_hypens(self):
+        expected = EUI64Address(81985529216486895)
+        actual = Address.value_of("01-23-45-67-89-ab-cd-ef")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv4_dot_hexadecimal(self):
-#       expected = IPv4Address(3221226219)
-#       actual = Address.value_of("0xC0.0x00.0x02.0xEB")
-#       self.assertEqual(expected, actual)
+    def test__value_of__eui64_colons(self):
+        # This could really also be an IPv6Address, btw.
+        expected = EUI64Address(81985529216486895)
+        actual = Address.value_of("01:23:45:67:89:ab:cd:ef")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv4_dot_octal(self):
-#       expected = IPv4Address(3221226219)
-#       actual = Address.value_of("0300.0000.0002.0353")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv4_dot_decimal(self):
+        expected = IPv4Address(3221226219)
+        actual = Address.value_of("192.0.2.235")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv4_dot_mixed(self):
-#       expected = IPv4Address(3221226219)
-#       actual = Address.value_of("192.0x00.0002.235")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv4_dot_hexadecimal(self):
+        expected = IPv4Address(3221226219)
+        actual = Address.value_of("0xC0.0x00.0x02.0xEB")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv4_hexadecimal(self):
-#       expected = IPv4Address(3221226219)
-#       actual = Address.value_of("0xC00002EB")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv4_dot_octal(self):
+        expected = IPv4Address(3221226219)
+        actual = Address.value_of("0300.0000.0002.0353")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv4_decimal(self):
-#       expected = IPv4Address(3221226219)
-#       actual = Address.value_of("3221226219")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv4_dot_mixed(self):
+        expected = IPv4Address(3221226219)
+        actual = Address.value_of("192.0x00.0002.235")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv4_octal(self):
-#       expected = IPv4Address(3221226219)
-#       actual = Address.value_of("030000001353")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv4_hexadecimal(self):
+        expected = IPv4Address(3221226219)
+        actual = Address.value_of("0xC00002EB")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv6_full(self):
-#       expected = IPv6Address(42540766452641154071740215577757643572L)
-#       actual = Address.value_of("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv4_decimal(self):
+        expected = IPv4Address(3221226219)
+        actual = Address.value_of("3221226219")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv6_no_leading_zeros(self):
-#       expected = IPv6Address(42540766452641154071740215577757643572L)
-#       actual = Address.value_of("2001:db8:85a3:0:0:8a2e:370:7334")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv4_octal(self):
+        expected = IPv4Address(3221226219)
+        actual = Address.value_of("030000001353")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv6_grouped(self):
-#       expected = IPv6Address(42540766452641154071740215577757643572L)
-#       actual = Address.value_of("2001:db8:85a3::8a2e:370:7334")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv6_full(self):
+        expected = IPv6Address(42540766452641154071740215577757643572L)
+        actual = Address.value_of("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv6_loopback(self):
-#       expected = IPv6Address(1)
-#       actual = Address.value_of("::1")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv6_no_leading_zeros(self):
+        expected = IPv6Address(42540766452641154071740215577757643572L)
+        actual = Address.value_of("2001:db8:85a3:0:0:8a2e:370:7334")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv6_unspecified(self):
-#       expected = IPv6Address(0)
-#       actual = Address.value_of("::")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv6_grouped(self):
+        expected = IPv6Address(42540766452641154071740215577757643572L)
+        actual = Address.value_of("2001:db8:85a3::8a2e:370:7334")
+        self.assertEqual(expected, actual)
 
-#   def test__value_of__ipv6_dotted_quad(self):
-#       expected = IPv6Address(3221226112)
-#       actual = Address.value_of("::ffff:192.0.2.128")
-#       self.assertEqual(expected, actual)
+    def test__value_of__ipv6_loopback(self):
+        expected = IPv6Address(1)
+        actual = Address.value_of("::1")
+        self.assertEqual(expected, actual)
+
+    def test__value_of__ipv6_unspecified(self):
+        expected = IPv6Address(0)
+        actual = Address.value_of("::")
+        self.assertEqual(expected, actual)
+
+    def test__value_of__ipv6_dotted_quad(self):
+        expected = IPv6Address(281473902969472)
+        actual = Address.value_of("::ffff:192.0.2.128")
+        self.assertEqual(expected, actual)
 
     def test__repr(self):
         self.assertEquals("TestAddress(42)", repr(TestAddress(42)))