ó C…#Wc@s,dZddlZdefd„ƒYZdS(s! Class representing IP Addresses iÿÿÿÿNt IPAddresscBs­eZdZdZdZd d d„Zd„Zed„ƒZ e j d„ƒZ ed„ƒZ e j d„ƒZ e e d „ƒZe d „ƒZd „Ze d „ƒZRS(s¤Simple class to represent IP addresses This implementation represents purely the IP. Netmask, if needed, should be represented by a second IPAddress. s0.0.0.0sNo leading zeroscCs(d|_d|_||_||_dS(N(tNonet_addresst_netmasktaddresstnetmask(tselfRR((s ip_util.pyt__init__.s   cCs|jS(N(R(R((s ip_util.pyt__str__4scCs†g}|jdkr!tj}n6x$|jD]}|jt|ƒƒq+Wdj|ƒ}|jr~dt|jƒƒ}||S|SdS(s#Returns this IP address as a stringt.t/N( RRRtDEFAULTtappendtstrtjoinRtnetmask_prefix(RtsegmentsRtsegmentR((s ip_util.pyR7s  cCs+|dk rtj|ƒ}n||_dS(sSets the addressN(RRtconvert_addressR(RR((s ip_util.pyRGs cCs|jS(N(R(R((s ip_util.pyRNscCs=|dk r0t|ƒ}tj|dtƒ}n||_dS(Nt check_netmask(RR RRtTrueR(Rtnew_mask((s ip_util.pyRRs  cCsptj|ƒ}t|ƒdkr0tdƒ‚n|rltj|ƒ}d|jdƒkrltdƒ‚qln|S(s Convert a string into an array of ints. Also serves as a validation function - strings not of the correct form will raise a ValueError is Bad lengtht1sNot a valid netmask(Rtincremental_checktlent ValueErrortas_binary_stringtlstrip(RRRtbin_rep((s ip_util.pyRZscCs)djg|D]}dj|ƒ^q ƒS(s¾Convert a set of 4 octets into its binary string representation For example, 255.255.255.0 becomes: 11111111 11111111 11111111 00000000 (without the spaces) ts{0:08b}(Rtformat(Rtx((s ip_util.pyRjscCstj|jƒ}|jdƒS(sÂConvert a long form netmask into a short form (CIDR). e.g. 255.255.255.0 -> /24 The returned value is undefined for IPAddress's not representing valid netmasks. R(RRRtcount(Rt binary_rep((s ip_util.pyRtscCs8|s gS|jdƒr(tdƒ‚n|jdƒrFtdƒ‚n|jdƒ}g}t|ƒdkr|tdƒ‚nxµ|D]­}|s•qƒnt|ƒdkrÉ|dd krÉttjƒ‚ntjd |ƒdkrðtd ƒ‚nt |ƒ}|dks|d kr#td ƒ‚qƒ|j |ƒqƒW|S(sÚIncrementally check an IP Address. Useful for checking a partial address, e.g., one that is partly typed into the UI Returns a list of valid segments parsed Raises ValueError if invalid R s*An IP address may not begin with a period.s..s%Periods must be separated by numbers.isToo many octetsiit0s^[0-9]*$s)Only numbers and '.' (period) are allowediÿs"Values should be between 0 and 255N( t startswithRtendswithtsplitRRtMSG_NO_LEADING_ZEROStretsearchRtintR (RtipRRtint_seg((s ip_util.pyR~s, " N(t__name__t __module__t__doc__R R&RRRtpropertyRtsetterRt staticmethodtFalseRRRR(((s ip_util.pyR#s  (R.R'tobjectR(((s ip_util.pyts