Ñò î¡ßNc @ sŽ d Z d d k l Z l Z d d k l Z l Z l Z l Z d „ Z d „ Z d „ Z d „ Z d „ Z d „ Z d e f d „ ƒ YZ d S( sb Routines and classes for supporting and expressing IP address ranges using a glob style syntax. iÿÿÿÿ( t AddrFormatErrort AddrConversionError( t IPRanget IPAddresst IPNetworkt iprange_to_cidrsc C s~ t | d ƒ p t St } t } | i d ƒ } t | ƒ d j o t Sx/| D]'} d | j o° | o t St } | o t Sg } | i d ƒ D] } | t | ƒ q” ~ \ } } | | j o t Sd | j o d j n p t Sd | j o d j n p t SqO | d j o t } qO | t j o t S| t j o t Sd t | ƒ j o d j n p t SqO Wt S( s‰ @param ipglob: An IP address range in a glob-style format. @return: C{True} if IP range glob is valid, C{False} otherwise. t splitt .i t -i iþ i iÿ t *( t hasattrt FalseR t lent Truet int( t ipglobt seen_hyphent seen_asteriskt octetst octett _[1]t it octet1t octet2( ( s3 /usr/lib/python2.6/site-packages/netaddr/ip/glob.pyt valid_glob s> 6 $ c C sö t | ƒ p t d | ƒ ‚ n g } g } xž | i d ƒ D] } d | j o5 | i d ƒ } | i | d ƒ | i | d ƒ q= | d j o | i d ƒ | i d ƒ q= | i | ƒ | i | ƒ q= Wt d i | ƒ ƒ t d i | ƒ ƒ f S( s÷ A function that accepts a glob-style IP range and returns the component lower and upper bound IP address. @param ipglob: an IP address range in a glob-style format. @return: a tuple contain lower and upper bound IP objects. s# not a recognised IP glob range: %r!R R i i R t 0t 255( R R R t appendR t join( R t start_tokenst end_tokensR t tokens( ( s3 /usr/lib/python2.6/site-packages/netaddr/ip/glob.pyt glob_to_iptuple@ s c C sí t | ƒ p t d | ƒ ‚ n g } g } xž | i d ƒ D] } d | j o5 | i d ƒ } | i | d ƒ | i | d ƒ q= | d j o | i d ƒ | i d ƒ q= | i | ƒ | i | ƒ q= Wt d i | ƒ d i | ƒ ƒ S( sÁ A function that accepts a glob-style IP range and returns the equivalent IP range. @param ipglob: an IP address range in a glob-style format. @return: an IPRange object. s# not a recognised IP glob range: %r!R R i i R R R ( R R R R R R ( R R R R R ( ( s3 /usr/lib/python2.6/site-packages/netaddr/ip/glob.pyt glob_to_iprange^ s c C sÏ t | ƒ } t | ƒ } | i d j o | i d j o t d ƒ ‚ n d „ } g } y | | | ƒ } | i | ƒ WnQ t j oE x@ t | | ƒ D]* } | | d | d ƒ } | i | ƒ q˜ Wn X| S( s A function that accepts an arbitrary start and end IP address or subnet and returns one or more glob-style IP ranges. @param start: the start IP address or subnet. @param end: the end IP address or subnet. @return: a list containing one or more IP globs. i s! IP glob ranges only support IPv4!c S s^ g } t | ƒ i d ƒ D] } | t | ƒ q ~ } g } t | ƒ i d ƒ D] } | t | ƒ qP ~ } g } t } t } xÐ t d ƒ D] } | | | | j o | i t | | ƒ ƒ q‹ | | d j o( | | d j o | i d ƒ t } q‹ | p@ | p) | i d | | | | f ƒ t } qMt d ƒ ‚ q‹ t d ƒ ‚ q‹ Wd i | ƒ S( NR i i iÿ R s %s-%ss, only 1 hyphenated octet per IP glob allowed!sN asterisks are not allowed' ' before hyphenated octets!( t strR R R t rangeR R R R ( t lbt ubR t _t t1t _[2]t t2R R R R ( ( s3 /usr/lib/python2.6/site-packages/netaddr/ip/glob.pyt _iprange_to_glob s( 66 " i iÿÿÿÿ( R t versionR R R ( t startt endR* t globsR t cidr( ( s3 /usr/lib/python2.6/site-packages/netaddr/ip/glob.pyt iprange_to_globs| s ! c C s t t | ƒ Œ S( sï A function that accepts a glob-style IP range and returns a list of one or more IP CIDRs that exactly matches it. @param ipglob: an IP address range in a glob-style format. @return: a list of one or more IP objects. ( R R ( R ( ( s3 /usr/lib/python2.6/site-packages/netaddr/ip/glob.pyt glob_to_cidrsÁ s c C sN t | ƒ } t | d | d ƒ } t | ƒ d j o t d ƒ ‚ n | d S( sÿ A function that accepts an IP subnet in a glob-style format and returns a list of CIDR subnets that exactly matches the specified glob. @param cidr: an IP object CIDR subnet. @return: a list of one or more IP addresses and subnets. i iÿÿÿÿi s bad CIDR to IP glob conversion!( R R0 R R ( R/ t ipR. ( ( s3 /usr/lib/python2.6/site-packages/netaddr/ip/glob.pyt cidr_to_globÍ s t IPGlobc B sh e Z d Z d Z d „ Z d „ Z d „ Z d „ Z d „ Z e e e d d ƒ Z d „ Z d „ Z RS( s¤ Represents an IP address range using a glob-style syntax (x.x.x-y.*). Individual octets can be represented using the following shortcuts : 1. C{*} - the asterisk octet (represents values 0 through 255) 2. C{'x-y'} - the hyphenated octet (represents values x through y) A few basic rules also apply : 1. x must always be greater than y, therefore : - x can only be 0 through 254 - y can only be 1 through 255 2. only one hyphenated octet per IP glob is allowed 3. only asterisks are permitted after a hyphenated octet Example IP globs :: '192.0.2.1' # a single address '192.0.2.0-31' # 32 addresses '192.0.2.*' # 256 addresses '192.0.2-3.*' # 512 addresses '192.0-1.*.*' # 131,072 addresses '*.*.*.*' # the whole IPv4 address space Aside ===== I{IP glob ranges are not directly equivalent to CIDR blocks. They can represent address ranges that do not fall on strict bit mask boundaries. They are suitable for use in configuration files, being more obvious and readable than their CIDR counterparts, especially for admins and end users with little or no networking knowledge or experience.} I{All CIDR addresses can always be represented as IP globs but the reverse is not always true.} t _globc C sK t | ƒ \ } } t t | ƒ i | | ƒ t | i | i ƒ d | _ d S( Ni ( R t superR4 t __init__R0 t _startt _endt glob( t selfR R, R- ( ( s3 /usr/lib/python2.6/site-packages/netaddr/ip/glob.pyR7 s c C s t t | ƒ i ƒ S( s. @return: Pickled state of an C{IPGlob} object.( R6 R4 t __getstate__( R; ( ( s3 /usr/lib/python2.6/site-packages/netaddr/ip/glob.pyR<