U .Ѫ_7@s|dZddlZddlmZmZddZeZddZdd Z d d Z dd dZ dddZ dddZ ddZddZddZdS)z) Shared logic for various address types. N)_range_is_strcCs^g}tddd}tdD]@}ddg}|D]}d|d@||<|dL}q*|d|q|S) z :return: A 256 element list containing 8-bit binary digit strings. The list index value is equivalent to its bit string value. NZ01)rrangeappendjoin)lookupZ bits_per_bytenumbitsir@/Users/cjl/code/aws-vpc-ipam/lambda/netaddr/strategy/__init__.py bytes_to_bitss    rcCsTt|dsdSt||krdSd|d}|D] }d|krF|ks.ndSq.dS)a6 :param words: A sequence of unsigned integer word values. :param word_size: Width (in bits) of each unsigned integer word value. :param num_words: Number of unsigned integer words expected. :return: ``True`` if word sequence is valid for this address type, ``False`` otherwise. __iter__FrrT)hasattrlen)words word_size num_wordsmax_wordrrrr valid_words!s   rcCs~d||d}d|kr$|ks6ntdt|d|d}g}t|D]"}||@}|t|||L}qNtt|S)aJ :param int_val: Unsigned integer to be divided into words of equal size. :param word_size: Width (in bits) of each unsigned integer word value. :param num_words: Number of unsigned integer words expected. :return: A tuple contain unsigned integer word values split according to provided arguments. rrrzinteger out of bounds: %r!) IndexErrorhexr r inttuplereversed)int_valrrmax_intrr_wordrrr int_to_words;s    r&cCsPt|||std|fd}tt|D] \}}|}|||>}||B}q*|S)a5 :param words: A sequence of unsigned integer word values. :param word_size: Width (in bits) of each unsigned integer word value. :param num_words: Number of unsigned integer words expected. :return: An unsigned integer that is equivalent to value represented by word sequence. z"invalid integer word sequence: %r!r)r ValueError enumerater!)rrrr"rrr%rrr words_to_intVs   r)r cCs~t|s dS|dkr ||d}t||kr0dSd|d}z(dt|dkrX|krbnnWdSWntk rxYnXdS)al :param bits: A network address in a delimited binary string format. :param width: Maximum width (in bits) of a network address (excluding delimiters). :param word_sep: (optional) character or string used to delimit word groups (default: '', no separator). :return: ``True`` if network address is valid, ``False`` otherwise. Fr rrrT)rreplacerrr')rwidthword_sepr#rrr valid_bitsms     r-cCs8t|||std|f|dkr.||d}t|dS)a :param bits: A network address in a delimited binary string format. :param width: Maximum width (in bits) of a network address (excluding delimiters). :param word_sep: (optional) character or string used to delimit word groups (default: '', no separator). :return: An unsigned integer that is equivalent to value represented by network address in readable binary form. z#invalid readable binary string: %r!r r)r-r'r*r)rr+r,rrr bits_to_ints  r.cCsg}t|||D]b}g}|r8|t|d@|dL}q|d|pPd|}d||| d}||q|dkrt|std|f||S)a :param int_val: An unsigned integer. :param word_size: Width (in bits) of each unsigned integer word value. :param num_words: Number of unsigned integer words expected. :param word_sep: (optional) character or string used to delimit word groups (default: '', no separator). :return: A network address in a delimited binary string format that is equivalent in value to unsigned integer. rr 0Nz#word separator is not a string: %r!)r&r BYTES_TO_BITSreverser rr')r"rrr,Z bit_wordsr%rZbit_strrrr int_to_bitss  r3cCst|s dS|dsdS|dd}t||kr6dSd|d}z(dt|dkr^|krhnnWdSWntk r~YnXdS)a :param bin_val: A network address in Python's binary representation format ('0bxxx'). :param width: Maximum width (in bits) of a network address (excluding delimiters). :return: ``True`` if network address is valid, ``False`` otherwise. F0br rrrT)r startswithr*rrr')bin_valr+r#rrr valid_bins      r7c Csg}z t|}Wn`tk rp|}|dkrL|d@}|t||dL}q$|dtddd|}YnXt|dd |krt d |f|S) z :param int_val: An unsigned integer. :param width: Maximum allowed width (in bits) of a unsigned integer. :return: Equivalent string value in Python's binary representation format ('0bxxx'). rr/rr4z ^[0]+([01]+)$z\1r rNz binary string out of bounds: %s!) bin NameErrorr r1r2_resubr rr)r"r+Z bin_tokensr6rr%rrr int_to_bins   r<cCs*t||std|ft|dddS)a9 :param bin_val: A string containing an unsigned integer in Python's binary representation format ('0bxxx'). :param width: Maximum allowed width (in bits) of a unsigned integer. :return: An unsigned integer that is equivalent to value represented by Python binary string format. z%not a valid Python binary string: %r!r4r r)r7r'rr*)r6r+rrr bin_to_ints r=)r )r )r )__doc__rer:netaddr.compatrrrr1rr&r)r-r.r3r7r<r=rrrrs  "