o i¾nhê(ã@s|dZddlmZmZddlmZmZmZmZddl m Z dd„Z dd„Z d d „Z d d „Zd d„Zdd„ZGdd„deƒZdS)zb Routines and classes for supporting and expressing IP address ranges using a glob style syntax. é)ÚAddrFormatErrorÚAddrConversionError)ÚIPRangeÚ IPAddressÚ IPNetworkÚiprange_to_cidrs)Ú_is_strc CsLt|ƒsdSd}d}| d¡}t|ƒdkrdS|D]Š}d|vrm|r$dSd}|r+dSzdd„| d¡Dƒ\}}Wn tyDYdSw||krLdSd|krXd ks\dSdSd |krhd ksldSdSq|d krtd}q|dur{dS|dur‚dSzdt|ƒkr’d ks—WdSWdSWqty£YdSwdS) z‹ :param ipglob: An IP address range in a glob-style format. :return: ``True`` if IP range glob is valid, ``False`` otherwise. FÚ.éú-TcSóg|]}t|ƒ‘qS©©Úint)Ú.0Úir r ú:/usr/local/lib/python3.10/dist-packages/netaddr/ip/glob.pyÚ .ózvalid_glob..réþééÿÚ*)rÚsplitÚlenÚ ValueErrorr)ÚipglobÚ seen_hyphenÚ seen_asteriskÚoctetsÚoctetÚoctet1Úoctet2r r rÚ valid_globs\    ÿÿÿÿÿÿ ÿr#cCs¬t|ƒs td|fƒ‚g}g}| d¡D]3}d|vr.| d¡}| |d¡| |d¡q|dkr=| d¡| d¡q| |¡| |¡qtd |¡ƒtd |¡ƒfS) z÷ 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. ú#not a recognised IP glob range: %r!r r rrrÚ0Ú255)r#rrÚappendrÚjoin©rÚ start_tokensÚ end_tokensr Útokensr r rÚglob_to_iptupleFs      r-cCs¦t|ƒs td|fƒ‚g}g}| d¡D]3}d|vr.| d¡}| |d¡| |d¡q|dkr=| d¡| d¡q| |¡| |¡qtd |¡d |¡ƒS) zÁ 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. r$r r rrrr%r&)r#rrr'rr(r)r r rÚglob_to_iprangeds      r.cCs¤t|ƒ}t|ƒ}|jdkr|jdkrtdƒ‚dd„}g}z|||ƒ}t|ƒs*tdƒ‚| |¡W|StyQt||ƒD]}||d|dƒ}| |¡q=Y|Sw)a 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. r z!IP glob ranges only support IPv4!cSsÖdd„t|ƒ d¡Dƒ}dd„t|ƒ d¡Dƒ}g}d}d}tdƒD]C}||||kr6| t||ƒ¡q"||dkrJ||dkrJ| d ¡d }q"|sb|s^| d ||||f¡d }q"td ƒ‚td ƒ‚d |¡S)NcSr r r©rÚ_r r rr•rz>iprange_to_globs.._iprange_to_glob..r cSr r rr/r r rr–rFr rrrTz%s-%sz,only 1 hyphenated octet per IP glob allowed!z3asterisks are not allowed before hyphenated octets!)ÚstrrÚranger'rr()ÚlbÚubÚt1Út2r,rrrr r rÚ_iprange_to_glob“s,  ÿÿ z*iprange_to_globs.._iprange_to_globzinvalid ip glob createdréÿÿÿÿ)rÚversionrr#r'r)ÚstartÚendr7ÚglobsrÚcidrr r rÚiprange_to_globs‚s& !   ö þör>cCs tt|ƒŽS)zï 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. )rr-)rr r rÚ glob_to_cidrsÌs r?cCs6t|ƒ}t|d|dƒ}t|ƒdkrtdƒ‚|dS)zÿ 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. rr8rzbad CIDR to IP glob conversion!)rr>rr)r=Úipr<r r rÚ cidr_to_globØs   rAcsjeZdZdZdZ‡fdd„Z‡fdd„Z‡fdd„Zd d „Zd d „Z e ee d dƒZ dd„Z dd„Z ‡ZS)ÚIPGlobaº Represents an IP address range using a glob-style syntax ``x.x.x-y.*`` Individual octets can be represented using the following shortcuts : 1. ``*`` - the asterisk octet (represents values ``0`` through ``255``) 2. ``x-y`` - the hyphenated octet (represents values ``x`` through ``y``) A few basic rules also apply : 1. ``x`` must always be less 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 Examples: +------------------+------------------------------+ | IP glob | Description | +==================+==============================+ | ``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 | +------------------+------------------------------+ .. note :: 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. All CIDR addresses can always be represented as IP globs but the reverse is not always true. ©Ú_globcs6t|ƒ\}}tt|ƒ ||¡t|j|jƒd|_dS©Nr)r-ÚsuperrBÚ__init__r>Ú_startÚ_endÚglob)Úselfrr:r;©Ú __class__r rrGs zIPGlob.__init__cstt|ƒ ¡S)z-:return: Pickled state of an `IPGlob` object.)rFrBÚ __getstate__©rKrLr rrNszIPGlob.__getstate__cs(tt|ƒ |¡t|j|jƒd|_dS)z>:param state: data used to unpickle a pickled `IPGlob` object.rN)rFrBÚ __setstate__r>rHrIrJ)rKÚstaterLr rrP#szIPGlob.__setstate__cCs|jS)NrCrOr r rÚ _get_glob(szIPGlob._get_globcCs(t|ƒ\|_|_t|j|jƒd|_dSrE)r-rHrIr>rD)rKrr r rÚ _set_glob+szIPGlob._set_globNz-an arbitrary IP address range in glob format.cCs d|jS)z3:return: IP glob in common representational format.z%s)rJrOr r rÚ__str__2s zIPGlob.__str__cCsd|jj|jfS)z8:return: Python statement to create an equivalent objectz%s('%s'))rMÚ__name__rJrOr r rÚ__repr__6szIPGlob.__repr__)rUÚ __module__Ú __qualname__Ú__doc__Ú __slots__rGrNrPrRrSÚpropertyrJrTrVÚ __classcell__r r rLrrBês-   ÿrBN)rYÚ netaddr.corerrÚ netaddr.iprrrrÚnetaddr.compatrr#r-r.r>r?rArBr r r rÚs 6J