U
    £�©jOZ  ã                   @   s¼  d dl Z d dlZd dlZd dlZd dlmZmZ ddlmZ ddl	m
Z
 dZdZe d¡Zed	d
hƒZed	d
dhƒZed	d
ddddddddh
ƒZed	d
ddhƒZeddhƒZeddddddddhƒZeddhƒZeedƒedƒhƒZeed	ƒedƒhƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZeedœdd„Z e!e!e"d œd!d"„Z#e!e$d#œd$d%„Z%ee!d#œd&d'„Z&ee$e!f e"d(œd)d*„Z'ee$e!f e"e"d+œd,d-„Z(dNe!e"e"d/œd0d1„Z)e!e"d(œd2d3„Z*e!e"d(œd4d5„Z+e!dd(œd6d7„Z,e!ee"d8œd9d:„Z-dOe!ee"e"d;œd<d=„Z.ee!e$e/f dd(œd>d?„Z0e!e$d(œd@dA„Z1ee!e$e/f e!d(œdBdC„Z2dPe!e"e"e!dEœdFdG„Z3dQee!e$e/f e"e"e"e"e$dHœdIdJ„Z4dRee!e$e/f e"e"e"e!dKœdLdM„Z5dS )Sé    N)ÚOptionalÚUnioné   )Úidnadata)Úintranges_containé	   s   xn--u   [.ã€‚ï¼Žï½¡]ÚRÚALÚANÚENÚESÚCSÚETÚONÚBNÚNSMÚLÚDc                   @   s   e Zd ZdZdS )Ú	IDNAErrorz5Base exception for all IDNA-encoding related problemsN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú-/tmp/pip-unpacked-wheel-j_8njjuc/idna/core.pyr      s   r   c                   @   s   e Zd ZdZdS )ÚIDNABidiErrorz;Exception when bidirectional requirements are not satisfiedNr   r   r   r   r   r   !   s   r   c                   @   s   e Zd ZdZdS )ÚInvalidCodepointz<Exception when a disallowed or unallocated codepoint is usedNr   r   r   r   r   r   '   s   r   c                   @   s   e Zd ZdZdS )ÚInvalidCodepointContextzCException when the codepoint is not valid in the context it is usedNr   r   r   r   r   r   -   s   r   )ÚcpÚreturnc                 C   s0   t  t| ƒ¡}|dkr,t  t| ƒ¡s,tdƒ‚|S )Nr   z Unknown character in unicodedata)ÚunicodedataÚ	combiningÚchrÚnameÚ
ValueError)r   Úvr   r   r   Ú_combining_class3   s    r'   )r   Úscriptr    c                 C   s   t t| ƒtj| ƒS )N)r   Úordr   Úscripts)r   r(   r   r   r   Ú
_is_script:   s    r+   )Úsr    c                 C   s
   |   d¡S )NÚpunycode)Úencode©r,   r   r   r   Ú	_punycode>   s    r0   c                 C   s   d| d›�S )NzU+Z04Xr   r/   r   r   r   Ú_unotB   s    r1   )Úlabelr    c                 C   s   t | ƒdkS )uÍ  Check that a label does not exceed the maximum permitted length.

    Per :rfc:`1035` (and :rfc:`5891` Â§4.2.4) a DNS label must not exceed
    63 octets. The argument may be either a :class:`str` (a U-label, where
    length is measured in characters) or :class:`bytes` (an A-label, where
    length is measured in octets).

    :param label: The label to check.
    :returns: ``True`` if the label is within the length limit, otherwise
        ``False``.
    é?   ©Úlen©r2   r   r   r   Úvalid_label_lengthF   s    r7   )ÚdomainÚtrailing_dotr    c                 C   s   t | ƒ|rdndkS )a¶  Check that a full domain name does not exceed the maximum length.

    Per :rfc:`1035`, a domain name is limited to 253 octets when no trailing
    dot is present, or 254 octets when one is included.

    :param domain: The full (possibly multi-label) domain name.
    :param trailing_dot: ``True`` if ``domain`` includes a trailing ``.``.
    :returns: ``True`` if the domain is within the length limit, otherwise
        ``False``.
    éþ   éý   r4   )r8   r9   r   r   r   Úvalid_string_lengthU   s    r<   F)r2   Ú	check_ltrr    c           	      C   sh  d}t | dƒD ]>\}}t |¡}|dkr@tdt| ƒ› d|› �ƒ‚|tkrd}q|sZ|sZdS t | d ¡}|tkrvd}n"|dkr„d}ntd	t| ƒ› d
�ƒ‚d}d}t | dƒD ]ª\}}t |¡}|�r|tkrÚtd|› d�ƒ‚|tkrèd}n|dkrôd}|t	k�rT|�s
|}n||k�rTtdƒ‚qª|t
k�r8td|› d�ƒ‚|tk�rHd}qª|dkrªd}qª|�sdtdƒ‚dS )a!  Validate the Bidi Rule from :rfc:`5893` for a single label.

    The Bidi Rule constrains how bidirectional characters (Hebrew, Arabic,
    etc.) may appear within a label. By default the check is only applied
    when the label contains at least one right-to-left character (Unicode
    bidirectional categories ``R``, ``AL``, or ``AN``); set ``check_ltr``
    to ``True`` to apply it to LTR-only labels as well.

    :param label: The label to validate, as a Unicode string.
    :param check_ltr: If ``True``, apply the rules even when the label
        contains no RTL characters.
    :returns: ``True`` if the label satisfies the Bidi Rule.
    :raises IDNABidiError: If any of Bidi Rule conditions 1-6 are violated,
        or if the directional category of a codepoint cannot be determined.
    Fr   Ú z Unknown directionality in label ú at position Tr   r   zFirst codepoint in label z" must be directionality L, R or ALNz,Invalid direction for codepoint at position z in a right-to-left labelr   z2Can not mix numeral types in a right-to-left labelz in a left-to-right labelz0Label ends with illegal codepoint directionality)Ú	enumerater!   Úbidirectionalr   ÚreprÚ_bidi_rtl_categoriesÚ_bidi_rtl_firstÚ_bidi_rtl_allowedÚ_bidi_rtl_valid_endingÚ_bidi_rtl_numericÚ_bidi_ltr_allowedÚ_bidi_ltr_valid_ending)	r2   r=   Ú
bidi_labelÚidxr   Ú	directionÚrtlÚvalid_endingÚnumber_typer   r   r   Ú
check_bidic   sP    






rP   c                 C   s"   t  | d ¡d dkrtdƒ‚dS )u^  Reject labels that begin with a combining mark.

    Per :rfc:`5891` Â§4.2.3.2 a label must not start with a character of
    Unicode general category ``M`` (Mark).

    :param label: The label to check.
    :returns: ``True`` if the first character is not a combining mark.
    :raises IDNAError: If the label begins with a combining character.
    r   ÚMz0Label begins with an illegal combining characterT)r!   Úcategoryr   r6   r   r   r   Úcheck_initial_combiner­   s    
rS   c                 C   s<   | dd… dkrt dƒ‚| d dks0| d dkr8t dƒ‚d	S )
u§  Validate the hyphen restrictions for a label.

    Per :rfc:`5891` Â§4.2.3.1 a label must not start or end with a hyphen
    (``U+002D``), and must not have hyphens in both the third and fourth
    positions (the prefix reserved for A-labels).

    :param label: The label to check.
    :returns: ``True`` if the hyphen restrictions are satisfied.
    :raises IDNAError: If any of the hyphen restrictions are violated.
    é   é   z--z4Label has disallowed hyphens in 3rd and 4th positionr   ú-éÿÿÿÿz)Label must not start or end with a hyphenT)r   r6   r   r   r   Úcheck_hyphen_ok¼   s
    rX   c                 C   s   t  d| ¡| krtdƒ‚dS )z¬Require that a label is in Unicode Normalization Form C.

    :param label: The label to check.
    :raises IDNAError: If ``label`` differs from its NFC normalisation.
    ÚNFCz%Label must be in Normalization Form CN)r!   Ú	normalizer   r6   r   r   r   Ú	check_nfcÎ   s    r[   )r2   Úposr    c                 C   s&  t | | ƒ}|dkrò|dkr8tt | |d  ƒƒtkr8dS d}t|d ddƒD ]@}t ¡  t | | ƒ¡}|t dƒkrvqLqL|tkrˆd} qŽqL qŽqL|s–dS d}t|d t| ƒƒD ]@}t ¡  t | | ƒ¡}|t dƒkrÖq¬q¬|t	krèd} qîq¬ qîq¬|S |dk�r|dk�ott | |d  ƒƒtkS dS d	S )
aŸ  Validate the CONTEXTJ rules from :rfc:`5892` Appendix A.

    These rules govern the contextual use of the joiner codepoints
    ``U+200C`` (ZERO WIDTH NON-JOINER, Appendix A.1) and ``U+200D``
    (ZERO WIDTH JOINER, Appendix A.2) within a label.

    :param label: The label containing the codepoint.
    :param pos: Index of the joiner codepoint within ``label``.
    :returns: ``True`` if the codepoint at ``pos`` satisfies its CONTEXTJ
        rule, ``False`` otherwise (including when the codepoint at
        ``pos`` is not a recognised joiner).
    :raises ValueError: If an adjacent codepoint has no Unicode name when
        determining its combining class.
    i   r   r   TFrW   ÚTi   N)
r)   r'   Ú_virama_combining_classÚranger   Újoining_typesÚgetÚ_bidi_joiner_l_or_dr5   Ú_bidi_joiner_r_or_d)r2   r\   Úcp_valueÚokÚiÚjoining_typer   r   r   Úvalid_contextjØ   s8     
"rh   )r2   r\   Ú	exceptionr    c                 C   sr  t | | ƒ}|dkr\d|  k o.t| ƒd k n  oZt | |d  ƒdkoZt | |d  ƒdkS |dkr–|t| ƒd k r’t| ƒdkr’t| |d  dƒS dS |dks¦|d	krÄ|dkrÀt| |d  d
ƒS dS |dk�r| D ]6}|dkràqÒt|dƒ�st|dƒ�st|dƒrÒ dS qÒdS d|  k�r&dk�r>n ntdd„ | D ƒƒ S d|  k�rVdk�rnn ntdd„ | D ƒƒ S dS )a‹  Validate the CONTEXTO rules from :rfc:`5892` Appendix A.

    Covers the contextual rules for codepoints such as MIDDLE DOT
    (``U+00B7``), Greek lower numeral sign, Hebrew punctuation, Katakana
    middle dot, and the Arabic-Indic / Extended Arabic-Indic digit ranges.

    :param label: The label containing the codepoint.
    :param pos: Index of the codepoint within ``label``.
    :param exception: Reserved for forward compatibility; currently unused.
    :returns: ``True`` if the codepoint at ``pos`` satisfies its CONTEXTO
        rule, ``False`` otherwise (including when the codepoint is not a
        recognised CONTEXTO codepoint).
    é·   r   r   él   iu  ÚGreekFió  iô  ÚHebrewiû0  u   ãƒ»ÚHiraganaÚKatakanaÚHanTé`  éi  c                 s   s*   | ]"}d t |ƒ  kodkn  V  qdS )éð  éù  N©r)   ©Ú.0r   r   r   r   Ú	<genexpr>4  s     z!valid_contexto.<locals>.<genexpr>rs   rt   c                 s   s*   | ]"}d t |ƒ  kodkn  V  qdS )rq   rr   Nru   rv   r   r   r   rx   7  s     )r)   r5   r+   Úany)r2   r\   ri   rd   r   r   r   r   Úvalid_contexto  s.    H
"rz   c                 C   s˜  t | ttfƒr|  d¡} t| ƒdkr,tdƒ‚t| dd�s@tdƒ‚t| ƒ t| ƒ t	| ƒ t
| ƒD �](\}}t|ƒ}t|tjd ƒr†q`q`t|tjd ƒ�rz4t| |ƒsÊtd	t|ƒ› d
|d › dt| ƒ› �ƒ‚W nL tk
�r } z,tdt|ƒ› d|d › dt| ƒ› �ƒ|‚W 5 d}~X Y nX q`t|tjd ƒ�rbt| |ƒ�sŠtdt|ƒ› d
|d › dt| ƒ› �ƒ‚q`tdt|ƒ› d|d › dt| ƒ› d�ƒ‚q`t| ƒ dS )a8  Run the full set of IDNA 2008 validity checks on a single label.

    Applies, in order: NFC normalisation (:func:`check_nfc`), hyphen
    restrictions (:func:`check_hyphen_ok`), the no-leading-combiner rule
    (:func:`check_initial_combiner`), per-codepoint validity (PVALID,
    CONTEXTJ, CONTEXTO classes from :rfc:`5892`), and the Bidi Rule
    (:func:`check_bidi`).

    :param label: The label to validate. ``bytes`` or ``bytearray`` input
        is decoded as UTF-8 first.
    :raises IDNAError: If the label is empty or fails a structural rule.
    :raises InvalidCodepoint: If the label contains a DISALLOWED or
        UNASSIGNED codepoint.
    :raises InvalidCodepointContext: If a CONTEXTJ or CONTEXTO codepoint
        is not valid in its context.
    :raises IDNABidiError: If the Bidi Rule is violated.
    zutf-8r   zEmpty LabelT©r9   úLabel too longÚPVALIDÚCONTEXTJzJoiner ú not allowed at position r   ú in z%Unknown codepoint adjacent to joiner r?   NÚCONTEXTOú
Codepoint z of z not allowed)Ú
isinstanceÚbytesÚ	bytearrayÚdecoder5   r   r<   r[   rX   rS   r@   r)   r   r   Úcodepoint_classesrh   r   r1   rB   r%   rz   r   rP   )r2   r\   r   rd   Úerrr   r   r   Úcheck_label<  s@    

 ÿ ÿþ ÿ*r‰   c                 C   sf   z(|   d¡}t|ƒ t|ƒs$tdƒ‚|W S  tk
r<   Y nX t| ƒ tt| ƒ }t|ƒsbtdƒ‚|S )u  Convert a single U-label into its A-label form.

    The result is the ASCII-Compatible Encoding (ACE) form per :rfc:`5891`
    Â§4: the label is validated, Punycode-encoded, and prefixed with
    ``xn--``. Pure ASCII labels that are already valid IDNA labels are
    returned unchanged (as :class:`bytes`).

    :param label: The label to convert, as a Unicode string.
    :returns: The A-label as ASCII-encoded :class:`bytes`.
    :raises IDNAError: If the label is invalid or the resulting A-label
        exceeds 63 octets.
    Úasciir|   )r.   Úulabelr7   r   ÚUnicodeEncodeErrorr‰   Ú_alabel_prefixr0   )r2   Úlabel_bytesr   r   r   Úalabelu  s    
r�   c              
   C   sä   t | ttfƒs@z|  d¡}W qH tk
r<   t| ƒ |  Y S X nt| ƒ}| ¡ }| t¡rŠ|t	tƒd… }|svt
dƒ‚| d¡rœt
dƒ‚nt|ƒ | d¡S z| d¡} W n, tk
rÖ } zt
dƒ|‚W 5 d}~X Y nX t| ƒ | S )aÿ  Convert a single A-label into its U-label form.

    Performs the inverse of :func:`alabel`: an ``xn--``-prefixed label is
    Punycode-decoded and validated. Labels that are already Unicode (or
    plain ASCII without the ACE prefix) are validated and returned as a
    Unicode string.

    :param label: The label to convert. ``bytes`` or ``bytearray`` input
        is treated as ASCII.
    :returns: The U-label as a Unicode string.
    :raises IDNAError: If the label is malformed or fails validation.
    rŠ   Nz5Malformed A-label, no Punycode eligible content foundó   -z"A-label must not end with a hyphenr-   zInvalid A-label)rƒ   r„   r…   r.   rŒ   r‰   ÚlowerÚ
startswithr�   r5   r   Úendswithr†   ÚUnicodeError)r2   rŽ   rˆ   r   r   r   r‹   ”  s,    



r‹   T)r8   Ú
std3_rulesÚtransitionalr    c              	   C   s,  ddl m } d}t| ƒD �]\}}t|ƒ}||dk r8|nt ||df¡d  }|d }	d}
t|ƒdkrn|d }
|	d	kp˜|	d
kr„| p˜|	dko˜| o˜|
dk}|
dk	oÂ|	dkpÂ|	dkr¸| pÂ|	d
koÂ|}|rÒ||7 }q|rì|
dk	sât‚||
7 }q|	dkrøqqtdt|ƒ› d|d › dt	| ƒ› �ƒ‚qt
 d|¡S )u®  Apply the UTS #46 character mapping to a domain string.

    Implements the mapping table from `UTS #46 Â§4
    <https://www.unicode.org/reports/tr46/>`_: each character is kept,
    replaced, or rejected based on its status (``V``, ``M``, ``D``, ``3``,
    ``I``). The result is returned in Normalisation Form C.

    :param domain: The full domain name to remap.
    :param std3_rules: If ``True``, apply the stricter STD3 ASCII rules
        (status ``3`` codepoints raise instead of being kept or mapped).
    :param transitional: If ``True``, use transitional processing (status
        ``D`` codepoints are mapped instead of kept). Transitional
        processing has been removed from UTS #46 and this option is
        retained only for backwards compatibility.
    :returns: The remapped domain, in Normalisation Form C.
    :raises InvalidCodepoint: If the domain contains a disallowed
        codepoint under the chosen rules.
    r   )Ú	uts46datar>   é   ÚZNé   rT   ÚVr   Ú3rQ   ÚIr‚   r   r€   rY   )r—   r@   r)   ÚbisectÚbisect_leftr5   ÚAssertionErrorr   r1   rB   r!   rZ   )r8   r•   r–   r—   Úoutputr\   ÚcharÚ
code_pointÚuts46rowÚstatusÚreplacementZ
keep_as_isZuse_replacementr   r   r   Úuts46_remap½  s.    $*ÿ ÿ

(r§   )r,   ÚstrictÚuts46r•   r–   r    c           
   
   C   s*  |rt jdtdd� t| tƒs^zt| dƒ} W n0 ttfk
r\ } ztdƒ|‚W 5 d}~X Y nX |rnt| ||ƒ} t	| dd�s‚td	ƒ‚d
}g }|r˜|  
d¡nt 
| ¡}|r°|dgkr¸tdƒ‚|d dkrÎ|d= d}|D ]$}	t|	ƒ} | rî| | ¡ qÒtdƒ‚qÒ|�r| d¡ d |¡} t	| |ƒ�s&td	ƒ‚| S )aA  Encode a Unicode domain name into its ASCII (A-label) form.

    Splits the input on label separators (only ``U+002E`` if ``strict`` is
    set; otherwise also IDEOGRAPHIC FULL STOP ``U+3002``, FULLWIDTH FULL
    STOP ``U+FF0E``, and HALFWIDTH IDEOGRAPHIC FULL STOP ``U+FF61``),
    encodes each label with :func:`alabel`, and rejoins them with ``.``.
    Optionally pre-processes the input through :func:`uts46_remap`.

    :param s: The domain name to encode.
    :param strict: If ``True``, only ``U+002E`` is recognised as a label
        separator.
    :param uts46: If ``True``, apply UTS #46 mapping before encoding.
    :param std3_rules: Forwarded to :func:`uts46_remap` when ``uts46`` is
        ``True``.
    :param transitional: Forwarded to :func:`uts46_remap` when ``uts46``
        is ``True``. Deprecated: emits a :class:`DeprecationWarning` and
        will be removed in a future version.
    :returns: The encoded domain as ASCII :class:`bytes`.
    :raises IDNAError: If the domain is empty, contains an invalid label,
        or exceeds the maximum domain length.
    zuTransitional processing has been removed from UTS #46. The transitional argument will be removed in a future version.rT   )Ú
stacklevelrŠ   zGshould pass a unicode string to the function rather than a byte string.NTr{   úDomain too longFÚ.r>   úEmpty domainrW   úEmpty labeló    ó   .)ÚwarningsÚwarnÚDeprecationWarningrƒ   ÚstrÚUnicodeDecodeErrorÚ	TypeErrorr   r§   r<   ÚsplitÚ_unicode_dots_rer�   ÚappendÚjoin)
r,   r¨   r©   r•   r–   rˆ   r9   ÚresultÚlabelsr2   r   r   r   r.   ô  sD    ü



r.   )r,   r¨   r©   r•   r    c           	   
   C   sø   t | tƒsJzt| dƒ} W n0 ttfk
rH } ztdƒ|‚W 5 d}~X Y nX |rZt| |dƒ} t| dd�sntdƒ‚d}g }|r„|  d¡nt | ¡}|rœ|d	gkr¤td
ƒ‚|d s¶|d= d}|D ]$}t	|ƒ} | rÖ| 
| ¡ qºtdƒ‚qº|rî| 
d	¡ d |¡S )aà  Decode an A-label-encoded domain name back to Unicode.

    Splits the input on label separators (see :func:`encode` for the
    rules), decodes each label with :func:`ulabel`, and rejoins them
    with ``.``. Optionally pre-processes the input through
    :func:`uts46_remap`.

    :param s: The domain name to decode.
    :param strict: If ``True``, only ``U+002E`` is recognised as a label
        separator.
    :param uts46: If ``True``, apply UTS #46 mapping before decoding.
    :param std3_rules: Forwarded to :func:`uts46_remap` when ``uts46`` is
        ``True``.
    :returns: The decoded domain as a Unicode string.
    :raises IDNAError: If the input is not valid ASCII, contains an
        invalid label, or is empty.
    rŠ   zInvalid ASCII in A-labelNFTr{   r«   r¬   r>   r­   rW   r®   )rƒ   r´   rµ   r¶   r   r§   r<   r·   r¸   r‹   r¹   rº   )	r,   r¨   r©   r•   rˆ   r9   r»   r¼   r2   r   r   r   r†   :  s2    


r†   )F)F)TF)FFFF)FFF)6rž   Úrer!   r±   Útypingr   r   r>   r   Ú	intrangesr   r^   r�   Úcompiler¸   Ú	frozensetrD   rC   rE   rF   rG   rH   rI   r)   rb   rc   r”   r   r   r   r   Úintr'   r´   Úboolr+   r„   r0   r1   r7   r<   rP   rS   rX   r[   rh   rz   r…   r‰   r�   r‹   r§   r.   r†   r   r   r   r   Ú<module>   sx   
J
6.9)9    ûúH   üû