<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> __revision__ = "<STR_LIT:3>" <EOL> __date__ = "<STR_LIT>" <EOL> from paste . util import intset <EOL> import socket <EOL> import six <EOL> class IP4Range ( intset . IntSet ) : <EOL> """<STR_LIT>""" <EOL> _MINIP4 = <NUM_LIT:0> <EOL> _MAXIP4 = ( <NUM_LIT:1> << <NUM_LIT:32> ) - <NUM_LIT:1> <EOL> _UNITYTRANS = "<STR_LIT>" . join ( [ chr ( n ) for n in range ( <NUM_LIT> ) ] ) <EOL> _IPREMOVE = "<STR_LIT>" <EOL> def __init__ ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:1> and isinstance ( args [ <NUM_LIT:0> ] , IP4Range ) : <EOL> super ( IP4Range , self ) . __init__ ( args [ <NUM_LIT:0> ] ) <EOL> return <EOL> args = list ( args ) <EOL> for i in range ( len ( args ) ) : <EOL> argval = args [ i ] <EOL> if isinstance ( argval , str ) : <EOL> if "<STR_LIT>" in argval : <EOL> args [ i ] = self . _parseRange ( * argval . split ( "<STR_LIT>" , <NUM_LIT:1> ) ) <EOL> continue <EOL> elif "<STR_LIT:/>" in argval : <EOL> args [ i ] = self . _parseMask ( * argval . split ( "<STR_LIT:/>" , <NUM_LIT:1> ) ) <EOL> else : <EOL> args [ i ] = self . _parseAddrRange ( argval ) <EOL> elif isinstance ( argval , tuple ) : <EOL> if len ( tuple ) != <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> addr1 , addr2 = argval <EOL> if isinstance ( addr1 , str ) : <EOL> addr1 = self . _parseAddrRange ( addr1 ) [ <NUM_LIT:0> ] <EOL> elif not isinstance ( addr1 , six . integer_types ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if isinstance ( addr2 , str ) : <EOL> addr2 = self . _parseAddrRange ( addr2 ) [ <NUM_LIT:1> ] <EOL> elif not isinstance ( addr2 , six . integer_types ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> args [ i ] = ( addr1 , addr2 ) <EOL> elif not isinstance ( argval , six . integer_types ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> super ( IP4Range , self ) . __init__ ( min = self . _MINIP4 , max = self . _MAXIP4 , * args ) <EOL> def _parseRange ( self , addr1 , addr2 ) : <EOL> naddr1 , naddr1len = _parseAddr ( addr1 ) <EOL> naddr2 , naddr2len = _parseAddr ( addr2 ) <EOL> if naddr2len < naddr1len : <EOL> naddr2 += naddr1 & ( ( ( <NUM_LIT:1> << ( ( naddr1len - naddr2len ) * <NUM_LIT:8> ) ) - <NUM_LIT:1> ) << <EOL> ( naddr2len * <NUM_LIT:8> ) ) <EOL> naddr2len = naddr1len <EOL> elif naddr2len > naddr1len : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> naddr1 <<= ( <NUM_LIT:4> - naddr1len ) * <NUM_LIT:8> <EOL> naddr2 <<= ( <NUM_LIT:4> - naddr2len ) * <NUM_LIT:8> <EOL> naddr2 += ( <NUM_LIT:1> << ( ( <NUM_LIT:4> - naddr2len ) * <NUM_LIT:8> ) ) - <NUM_LIT:1> <EOL> return ( naddr1 , naddr2 ) <EOL> def _parseMask ( self , addr , mask ) : <EOL> naddr , naddrlen = _parseAddr ( addr ) <EOL> naddr <<= ( <NUM_LIT:4> - naddrlen ) * <NUM_LIT:8> <EOL> try : <EOL> if not mask : <EOL> masklen = <NUM_LIT:0> <EOL> else : <EOL> masklen = int ( mask ) <EOL> if not <NUM_LIT:0> <= masklen <= <NUM_LIT:32> : <EOL> raise ValueError <EOL> except ValueError : <EOL> try : <EOL> mask = _parseAddr ( mask , False ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> remaining = <NUM_LIT:0> <EOL> masklen = <NUM_LIT:0> <EOL> if not mask : <EOL> masklen = <NUM_LIT:0> <EOL> else : <EOL> while not ( mask & <NUM_LIT:1> ) : <EOL> remaining += <NUM_LIT:1> <EOL> while ( mask & <NUM_LIT:1> ) : <EOL> mask >>= <NUM_LIT:1> <EOL> masklen += <NUM_LIT:1> <EOL> if remaining + masklen != <NUM_LIT:32> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> naddr1 = naddr & ( ( ( <NUM_LIT:1> << masklen ) - <NUM_LIT:1> ) << ( <NUM_LIT:32> - masklen ) ) <EOL> naddr2 = naddr1 + ( <NUM_LIT:1> << ( <NUM_LIT:32> - masklen ) ) - <NUM_LIT:1> <EOL> return ( naddr1 , naddr2 ) <EOL> def _parseAddrRange ( self , addr ) : <EOL> naddr , naddrlen = _parseAddr ( addr ) <EOL> naddr1 = naddr << ( ( <NUM_LIT:4> - naddrlen ) * <NUM_LIT:8> ) <EOL> naddr2 = ( ( naddr << ( ( <NUM_LIT:4> - naddrlen ) * <NUM_LIT:8> ) ) + <EOL> ( <NUM_LIT:1> << ( ( <NUM_LIT:4> - naddrlen ) * <NUM_LIT:8> ) ) - <NUM_LIT:1> ) <EOL> return ( naddr1 , naddr2 ) <EOL> def _int2ip ( self , num ) : <EOL> rv = [ ] <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> rv . append ( str ( num & <NUM_LIT:255> ) ) <EOL> num >>= <NUM_LIT:8> <EOL> return "<STR_LIT:.>" . join ( reversed ( rv ) ) <EOL> def iteraddresses ( self ) : <EOL> """<STR_LIT>""" <EOL> for v in super ( IP4Range , self ) . __iter__ ( ) : <EOL> yield self . _int2ip ( v ) <EOL> def iterranges ( self ) : <EOL> """<STR_LIT>""" <EOL> for r in self . _ranges : <EOL> if r [ <NUM_LIT:1> ] - r [ <NUM_LIT:0> ] == <NUM_LIT:1> : <EOL> yield self . _int2ip ( r [ <NUM_LIT:0> ] ) <EOL> else : <EOL> yield '<STR_LIT>' % ( self . _int2ip ( r [ <NUM_LIT:0> ] ) , self . _int2ip ( r [ <NUM_LIT:1> ] - <NUM_LIT:1> ) ) <EOL> def itermasks ( self ) : <EOL> """<STR_LIT>""" <EOL> for r in self . _ranges : <EOL> for v in self . _itermasks ( r ) : <EOL> yield v <EOL> def _itermasks ( self , r ) : <EOL> ranges = [ r ] <EOL> while ranges : <EOL> cur = ranges . pop ( ) <EOL> curmask = <NUM_LIT:0> <EOL> while True : <EOL> curmasklen = <NUM_LIT:1> << ( <NUM_LIT:32> - curmask ) <EOL> start = ( cur [ <NUM_LIT:0> ] + curmasklen - <NUM_LIT:1> ) & ( ( ( <NUM_LIT:1> << curmask ) - <NUM_LIT:1> ) << ( <NUM_LIT:32> - curmask ) ) <EOL> if start >= cur [ <NUM_LIT:0> ] and start + curmasklen <= cur [ <NUM_LIT:1> ] : <EOL> break <EOL> else : <EOL> curmask += <NUM_LIT:1> <EOL> yield "<STR_LIT>" % ( self . _int2ip ( start ) , curmask ) <EOL> if cur [ <NUM_LIT:0> ] < start : <EOL> ranges . append ( ( cur [ <NUM_LIT:0> ] , start ) ) <EOL> if cur [ <NUM_LIT:1> ] > start + curmasklen : <EOL> ranges . append ( ( start + curmasklen , cur [ <NUM_LIT:1> ] ) ) <EOL> __iter__ = iteraddresses <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> rv = [ ] <EOL> for start , stop in self . _ranges : <EOL> if stop - start == <NUM_LIT:1> : <EOL> rv . append ( "<STR_LIT>" % ( self . _int2ip ( start ) , ) ) <EOL> else : <EOL> rv . append ( "<STR_LIT>" % ( self . _int2ip ( start ) , <EOL> self . _int2ip ( stop - <NUM_LIT:1> ) ) ) <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , "<STR_LIT:U+002C>" . join ( rv ) ) <EOL> def _parseAddr ( addr , lookup = True ) : <EOL> if lookup and any ( ch not in IP4Range . _IPREMOVE for ch in addr ) : <EOL> try : <EOL> addr = socket . gethostbyname ( addr ) <EOL> except socket . error : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> naddr = <NUM_LIT:0> <EOL> for naddrpos , part in enumerate ( addr . split ( "<STR_LIT:.>" ) ) : <EOL> if naddrpos >= <NUM_LIT:4> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> if not part : <EOL> part = <NUM_LIT:0> <EOL> else : <EOL> part = int ( part ) <EOL> if not <NUM_LIT:0> <= part < <NUM_LIT> : <EOL> raise ValueError <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> naddr <<= <NUM_LIT:8> <EOL> naddr += part <EOL> return naddr , naddrpos + <NUM_LIT:1> <EOL> def ip2int ( addr , lookup = True ) : <EOL> return _parseAddr ( addr , lookup = lookup ) [ <NUM_LIT:0> ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> x = IP4Range ( "<STR_LIT>" ) <EOL> y = IP4Range ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> print ( x ) <EOL> for val in x . itermasks ( ) : <EOL> print ( val ) <EOL> for val in y . itermasks ( ) : <EOL> print ( val ) <EOL> for val in ( x | y ) . itermasks ( ) : <EOL> print ( val ) <EOL> for val in ( x ^ y ) . iterranges ( ) : <EOL> print ( val ) <EOL> for val in x : <EOL> print ( val ) </s>
