pvl/dns/reverse.py
changeset 740 74352351d6f5
parent 499 51bab9649e77
--- a/pvl/dns/reverse.py	Tue Mar 10 00:11:43 2015 +0200
+++ b/pvl/dns/reverse.py	Tue Mar 10 00:26:31 2015 +0200
@@ -1,4 +1,4 @@
-import ipaddr
+import ipaddress
 import math
 
 def reverse_ipv4 (ip) :
@@ -56,7 +56,7 @@
         # take the suffix
         nibbles = nibbles[-(hostbits / 4):]
         
-        # reverse in hex
+        # reveurse in hex
         return '.'.join(reversed(["{0:x}".format(x) for x in nibbles]))
 
     else :
@@ -80,53 +80,55 @@
     """
 
     for i in xrange(0, len(parts), 4) :
-        yield ''.join(parts[i:i+4])
+        yield u''.join(parts[i:i+4])
     
     # suffix ::
     if len(parts) < 32 :
-        yield ''
-        yield ''
+        yield u''
+        yield u''
 
 def parse_prefix (prefix) :
     """
-        Return an ipaddr.IPNetwork from given filesystem-compatbile IPv4/IPv6 prefix-ish variant.
+        Return an ipaddress.IPNetwork from given filesystem-compatbile IPv4/IPv6 prefix-ish variant.
 
         Supports partial IPv4 prefixes on octet boundaries.
         Supports partial IPv6 prefixes on nibble boundaries.
         Supports IPv4 prefxies using "-" as the prefix separator in place of "/".
 
-        >>> parse_prefix('127.0.0.0/8')
-        IPv4Network('127.0.0.0/8')
-        >>> parse_prefix('192.0.2.128/26')
-        IPv4Network('192.0.2.128/26')
-        >>> parse_prefix('192.0.2.128-26')
-        IPv4Network('192.0.2.128/26')
-        >>> parse_prefix('127.')
-        IPv4Network('127.0.0.0/8')
-        >>> parse_prefix('10')
-        IPv4Network('10.0.0.0/8')
-        >>> parse_prefix('192.168')
-        IPv4Network('192.168.0.0/16')
-        >>> parse_prefix('fe80::')
-        IPv6Network('fe80::/16')
-        >>> parse_prefix('2001:db8::')
-        IPv6Network('2001:db8::/32')
-        >>> parse_prefix('2001:db8:1:2')
-        IPv6Network('2001:db8:1:2::/64')
+        >>> print parse_prefix('127.0.0.0/8')
+        127.0.0.0/8
+        >>> print parse_prefix('192.0.2.128/26')
+        192.0.2.128/26
+        >>> print parse_prefix('192.0.2.128-26')
+        192.0.2.128/26
+        >>> print parse_prefix('127.')
+        127.0.0.0/8
+        >>> print parse_prefix('10')
+        10.0.0.0/8
+        >>> print parse_prefix('192.168')
+        192.168.0.0/16
+        >>> print parse_prefix('fe80::')
+        fe80::/16
+        >>> print parse_prefix('2001:db8::')
+        2001:db8::/32
+        >>> print parse_prefix('2001:db8:1:2')
+        2001:db8:1:2::/64
     """
 
+    prefix = unicode(prefix)
+
     if '/' in prefix :
-        return ipaddr.IPNetwork(prefix)
+        return ipaddress.ip_network(prefix)
     
     elif '-' in prefix :
-        return ipaddr.IPNetwork(prefix.replace('-', '/'))
+        return ipaddress.ip_network(prefix.replace('-', '/'))
 
     elif '.' in prefix or prefix.isdigit() :
         parts = prefix.rstrip('.').split('.')
         prefixlen = len(parts) * 8
         
-        return ipaddr.IPv4Network('{prefix}/{prefixlen}'.format(
-            prefix      = '.'.join(parts + ['0' for i in xrange(4 - len(parts))]),
+        return ipaddress.IPv4Network(u'{prefix}/{prefixlen}'.format(
+            prefix      = u'.'.join(parts + [u'0' for i in xrange(4 - len(parts))]),
             prefixlen   = prefixlen,
         ))
    
@@ -134,8 +136,8 @@
         parts = list(_split_ipv6_parts(prefix))
         prefixlen = len(parts) * 4
 
-        return ipaddr.IPv6Network('{prefix}/{prefixlen}'.format(
-            prefix      = ':'.join(_build_ipv6_parts(parts)),
+        return ipaddress.IPv6Network(u'{prefix}/{prefixlen}'.format(
+            prefix      = u':'.join(_build_ipv6_parts(parts)),
             prefixlen   = prefixlen,
         ))