#NEWLINE# @ classmethod #NEWLINE# def _ip_int_from_string( cls , ip_str ) : #NEWLINE# #INDENT# "Turn an IPv6 ip_str into an integer.\n\n        Args:\n            ip_str: A string, the IPv6 ip_str.\n\n        Returns:\n            An int, the IPv6 address\n\n        Raises:\n            AddressValueError: if ip_str isn't a valid IPv6 Address.\n\n        " #NEWLINE# if ( not ip_str ) : #NEWLINE# #INDENT# raise AddressValueError ( 'Address cannot be empty' ) #NEWLINE# #UNINDENT# parts = ip_str . split ( ':' ) #NEWLINE# _min_parts = 3 #NEWLINE# if ( len ( parts ) < _min_parts ) : #NEWLINE# #INDENT# msg = ( 'At least %d parts expected in %r' % ( _min_parts , ip_str ) ) #NEWLINE# raise AddressValueError ( msg ) #NEWLINE# #UNINDENT# if ( '.' in parts [ ( - 1 ) ] ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ipv4_int = IPv4Address ( parts . pop ( ) ) . _ip #NEWLINE# #UNINDENT# except AddressValueError as exc : #NEWLINE# #INDENT# raise AddressValueError ( ( '%s in %r' % ( exc , ip_str ) ) ) #NEWLINE# #UNINDENT# parts . append ( ( '%x' % ( ( ipv4_int >> 16 ) & 65535 ) ) ) #NEWLINE# parts . append ( ( '%x' % ( ipv4_int & 65535 ) ) ) #NEWLINE# #UNINDENT# _max_parts = ( cls . _HEXTET_COUNT + 1 ) #NEWLINE# if ( len ( parts ) > _max_parts ) : #NEWLINE# #INDENT# msg = ( 'At most %d colons permitted in %r' % ( ( _max_parts - 1 ) , ip_str ) ) #NEWLINE# raise AddressValueError ( msg ) #NEWLINE# #UNINDENT# skip_index = None #NEWLINE# for i in _compat_range ( 1 , ( len ( parts ) - 1 ) ) : #NEWLINE# #INDENT# if ( not parts [ i ] ) : #NEWLINE# #INDENT# if ( skip_index is not None ) : #NEWLINE# #INDENT# msg = ( "At most one '::' permitted in %r" % ip_str ) #NEWLINE# raise AddressValueError ( msg ) #NEWLINE# #UNINDENT# skip_index = i #NEWLINE# #UNINDENT# #UNINDENT# if ( skip_index is not None ) : #NEWLINE# #INDENT# parts_hi = skip_index #NEWLINE# parts_lo = ( ( len ( parts ) - skip_index ) - 1 ) #NEWLINE# if ( not parts [ 0 ] ) : #NEWLINE# #INDENT# parts_hi -= 1 #NEWLINE# if parts_hi : #NEWLINE# #INDENT# msg = "Leading ':' only permitted as part of '::' in %r" #NEWLINE# raise AddressValueError ( ( msg % ip_str ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not parts [ ( - 1 ) ] ) : #NEWLINE# #INDENT# parts_lo -= 1 #NEWLINE# if parts_lo : #NEWLINE# #INDENT# msg = "Trailing ':' only permitted as part of '::' in %r" #NEWLINE# raise AddressValueError ( ( msg % ip_str ) ) #NEWLINE# #UNINDENT# #UNINDENT# parts_skipped = ( cls . _HEXTET_COUNT - ( parts_hi + parts_lo ) ) #NEWLINE# if ( parts_skipped < 1 ) : #NEWLINE# #INDENT# msg = "Expected at most %d other parts with '::' in %r" #NEWLINE# raise AddressValueError ( ( msg % ( ( cls . _HEXTET_COUNT - 1 ) , ip_str ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( parts ) != cls . _HEXTET_COUNT ) : #NEWLINE# #INDENT# msg = "Exactly %d parts expected without '::' in %r" #NEWLINE# raise AddressValueError ( ( msg % ( cls . _HEXTET_COUNT , ip_str ) ) ) #NEWLINE# #UNINDENT# if ( not parts [ 0 ] ) : #NEWLINE# #INDENT# msg = "Leading ':' only permitted as part of '::' in %r" #NEWLINE# raise AddressValueError ( ( msg % ip_str ) ) #NEWLINE# #UNINDENT# if ( not parts [ ( - 1 ) ] ) : #NEWLINE# #INDENT# msg = "Trailing ':' only permitted as part of '::' in %r" #NEWLINE# raise AddressValueError ( ( msg % ip_str ) ) #NEWLINE# #UNINDENT# parts_hi = len ( parts ) #NEWLINE# parts_lo = 0 #NEWLINE# parts_skipped = 0 #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# ip_int = 0 #NEWLINE# for i in range ( parts_hi ) : #NEWLINE# #INDENT# ip_int <<= 16 #NEWLINE# ip_int |= cls . _parse_hextet ( parts [ i ] ) #NEWLINE# #UNINDENT# ip_int <<= ( 16 * parts_skipped ) #NEWLINE# for i in range ( ( - parts_lo ) , 0 ) : #NEWLINE# #INDENT# ip_int <<= 16 #NEWLINE# ip_int |= cls . _parse_hextet ( parts [ i ] ) #NEWLINE# #UNINDENT# return ip_int #NEWLINE# #UNINDENT# except ValueError as exc : #NEWLINE# #INDENT# raise AddressValueError ( ( '%s in %r' % ( exc , ip_str ) ) )