#NEWLINE# def parse_ip_network( module , addr , implicit_prefix = False , flags = 0 ) : #NEWLINE# #INDENT# if isinstance ( addr , tuple ) : #NEWLINE# #INDENT# if ( len ( addr ) != 2 ) : #NEWLINE# #INDENT# raise AddrFormatError ( ( 'invalid %s tuple!' % module . family_name ) ) #NEWLINE# #UNINDENT# ( value , prefixlen ) = addr #NEWLINE# if ( not ( 0 <= value <= module . max_int ) ) : #NEWLINE# #INDENT# raise AddrFormatError ( ( 'invalid address value for %s tuple!' % module . family_name ) ) #NEWLINE# #UNINDENT# if ( not ( 0 <= prefixlen <= module . width ) ) : #NEWLINE# #INDENT# raise AddrFormatError ( ( 'invalid prefix for %s tuple!' % module . family_name ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( addr , _str_type ) : #NEWLINE# #INDENT# if implicit_prefix : #NEWLINE# #INDENT# addr = cidr_abbrev_to_verbose ( addr ) #NEWLINE# #UNINDENT# if ( '/' in addr ) : #NEWLINE# #INDENT# ( val1 , val2 ) = addr . split ( '/' , 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# val1 = addr #NEWLINE# val2 = None #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# ip = IPAddress ( val1 , module . version , flags = INET_PTON ) #NEWLINE# #UNINDENT# except AddrFormatError : #NEWLINE# #INDENT# if ( module . version == 4 ) : #NEWLINE# #INDENT# expanded_addr = _ipv4 . expand_partial_address ( val1 ) #NEWLINE# ip = IPAddress ( expanded_addr , module . version , flags = INET_PTON ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise AddrFormatError ( ( 'invalid IPNetwork address %s!' % addr ) ) #NEWLINE# #UNINDENT# #UNINDENT# value = ip . _value #NEWLINE# try : #NEWLINE# #INDENT# prefixlen = int ( val2 ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# if ( val2 is None ) : #NEWLINE# #INDENT# prefixlen = module . width #NEWLINE# #UNINDENT# #UNINDENT# except ValueError : #NEWLINE# #INDENT# mask = IPAddress ( val2 , module . version , flags = INET_PTON ) #NEWLINE# if mask . is_netmask ( ) : #NEWLINE# #INDENT# prefixlen = module . netmask_to_prefix [ mask . _value ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if mask . is_hostmask ( ) : #NEWLINE# #INDENT# prefixlen = module . hostmask_to_prefix [ mask . _value ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise AddrFormatError ( ( 'addr %r is not a valid IPNetwork!' % addr ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( not ( 0 <= prefixlen <= module . width ) ) : #NEWLINE# #INDENT# raise AddrFormatError ( ( 'invalid prefix for %s address!' % module . family_name ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise TypeError ( ( 'unexpected type %s for addr arg' % type ( addr ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( flags & NOHOST ) : #NEWLINE# #INDENT# netmask = module . prefix_to_netmask [ prefixlen ] #NEWLINE# value = ( value & netmask ) #NEWLINE# #UNINDENT# return ( value , prefixlen )