
    kj#X                        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j                  d      Z G d	 d
e      Z G d de      Z G d de      Z G d de      ZdedefdZdededefdZdedefdZdedefdZdeeef   defdZdeeef   dedefdZd1dededefdZdedefdZdedefd Z deddfd!Z!ded"edefd#Z"d1ded"ed$edefd%Z#deeee$f   ddfd&Z%dedefd'Z&deeee$f   defd(Z'd2d)ed*ed+edefd,Z(	 	 	 	 d3deeee$f   d-ed.ed*ed+edefd/Z)	 	 	 d4deeee$f   d-ed.ed*edef
d0Z*y)5    N)OptionalUnion   )idnadata)intranges_contain	   s   xn--u   [.。．｡]c                       e Zd ZdZy)	IDNAErrorz5Base exception for all IDNA-encoding related problemsN__name__
__module____qualname____doc__     6/DATA/.local/lib/python3.12/site-packages/idna/core.pyr
   r
      s    ?r   r
   c                       e Zd ZdZy)IDNABidiErrorz;Exception when bidirectional requirements are not satisfiedNr   r   r   r   r   r      s    Er   r   c                       e Zd ZdZy)InvalidCodepointz<Exception when a disallowed or unallocated codepoint is usedNr   r   r   r   r   r      s    Fr   r   c                       e Zd ZdZy)InvalidCodepointContextzCException when the codepoint is not valid in the context it is usedNr   r   r   r   r   r   !   s    Mr   r   cpreturnc                     t        j                  t        |             }|dk(  r)t        j                  t        |             st	        d      |S )Nr   z Unknown character in unicodedata)unicodedata	combiningchrname
ValueError)r   vs     r   _combining_classr"   '   s@    c"g&AAvB(?@@Hr   scriptc                 N    t        t        |       t        j                  |         S )N)r   ordr   scripts)r   r#   s     r   
_is_scriptr'   /   s    SWh&6&6v&>??r   sc                 $    | j                  d      S )Npunycode)encoder(   s    r   	_punycoder-   3   s    88Jr   c                 $    dj                  |       S )NzU+{:04X})formatr,   s    r   _unotr0   7   s    Qr   labelc                 "    t        |       dkD  ryy)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``.
    ?   FTlenr1   s    r   valid_label_lengthr7   ;   s     5zBr   trailing_dotc                 *    t        |       |rdndkD  ryy)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 label: The full (possibly multi-label) domain name.
    :param trailing_dot: ``True`` if ``label`` includes a trailing ``.``.
    :returns: ``True`` if the domain is within the length limit, otherwise
        ``False``.
          FTr4   )r1   r8   s     r   valid_string_lengthr<   L   s     5zLSc2r   	check_ltrc                    d}t        | d      D ]J  \  }}t        j                  |      }|dk(  r$t        dj	                  t        |       |            |dv sId}L |s|syt        j                  | d         }|dv rd}n+|d	k(  rd}n#t        d
j	                  t        |                   d}d}t        | d      D ]  \  }}t        j                  |      }|rG|dvrt        dj	                  |            |dv rd}n|dk7  rd}|dv sN|s|}S||k7  sYt        d      |dvrt        dj	                  |            |dv rd}|dk7  sd} |st        d      y)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    z1Unknown directionality in label {} at position {})RALANTr   )r@   rA   Lz=First codepoint in label {} must be directionality L, R or ALN)
r@   rA   rB   ENESCSETONBNNSMzGInvalid direction for codepoint at position {} in a right-to-left label)r@   rA   rD   rB   rJ   )rB   rD   z2Can not mix numeral types in a right-to-left label)rC   rD   rE   rF   rG   rH   rI   rJ   zGInvalid direction for codepoint at position {} in a left-to-right label)rC   rD   z0Label ends with illegal codepoint directionality)	enumerater   bidirectionalr   r/   repr)	r1   r=   
bidi_labelidxr   	directionrtlvalid_endingnumber_types	            r   
check_bidirT   \   s   " JUA&R--b1	? S Z Z[_`e[fhk lmm))J ' i ))%(3IK	c	[bbcghmcnoppL!%KUA&R--b1	 !  $$m$t$tux$yzz33#e#$L(""+K"i/+,`aa  PP#$m$t$tux$yzzK'#e#$M 'P NOOr   c                 V    t        j                  | d         d   dk(  rt        d      y)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   s    r   check_initial_combinerrX      s/     E!H%a(C/JKKr   c                 `    | dd dk(  rt        d      | d   dk(  s| d   dk(  rt        d      y	)
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   s    r   check_hyphen_okr^      sC     QqzTNOOQx3%)s*CDDr   c                 L    t        j                  d|       | k7  rt        d      y)zRequire 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   s    r   	check_nfcrb      s*     UE*e3?@@ 4r   posc                    t        | |         }|dk(  r|dkD  r"t        t        | |dz
                 t        k(  ryd}t        |dz
  dd      D ]]  }t	        j
                         j                  t        | |               }|t        d      k(  rA|t        d      t        d	      fv rd} n n |syd}t        |dz   t        |             D ]_  }t	        j
                         j                  t        | |               }|t        d      k(  rA|t        d
      t        d	      fv rd} |S  |S  |S |dk(  r(|dkD  r"t        t        | |dz
                 t        k(  ryyy)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   TFr]   TrC   Dr@   i   )r%   r"   _virama_combining_classranger   joining_typesgetr5   )r1   rc   cp_valueokijoining_types         r   valid_contextjro      sj    5:H67E#'N 348OOsQwB'A#11377E!HFLs3x'#c(CH!55 ( sQwE
+A#11377E!HFLs3x'#c(CH!55 	 	 , 	67E#'N 348OO r   	exceptionc                    t        | |         }|dk(  rDd|cxk  rt        |       dz
  k  r,n yt        | |dz
           dk(  rt        | |dz            dk(  ryy|dk(  r2|t        |       dz
  k  r t        |       dkD  rt        | |dz      d      S y|d	k(  s|d
k(  r|dkD  rt        | |dz
     d      S y|dk(  r4| D ].  }|dk(  r	t        |d      st        |d      st        |d      s. y yd|cxk  rdk  r#n n | D ]  }dt        |      cxk  rdk  s y  yd|cxk  rdk  r#n y| D ]  }dt        |      cxk  rdk  s y  yy)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   TFiu  Greeki  i  Hebrewi0  u   ・HiraganaKatakanaHani`  ii  i  i  )r%   r5   r'   )r1   rc   rp   rk   r   s        r   valid_contextory     s    5:H6s#SZ!^#  5q>"f,U37^1D1N	V	UaCJNeC!Gng66	V	x617eC!Gnh77	V	BX~"j)ZJ-G:VXZ_K`	 
 	(	#e	#BB)6) *  	(	#e	#  BB)6) *  r   c                    t        | t        t        f      r| j                  d      } t	        |       dk(  rt        d      t        |        t        |        t        |        t        |       D ]  \  }}t        |      }t        |t        j                  d         r0t        |t        j                  d         r?	 t        | |      s1t        dj!                  t#        |      |dz   t%        |                   t        |t        j                  d	         r>t)        | |      rt        d
j!                  t#        |      |dz   t%        |                   t+        dj!                  t#        |      |dz   t%        |                    t-        |        y# t&        $ r2 t        dj!                  t#        |      |dz   t%        |                   w xY w)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 LabelPVALIDCONTEXTJz*Joiner {} not allowed at position {} in {}r   z<Unknown codepoint adjacent to joiner {} at position {} in {}CONTEXTO-Codepoint {} not allowed at position {} in {}z-Codepoint {} at position {} of {} not allowedN)
isinstancebytes	bytearraydecoder5   r
   rb   r^   rX   rK   r%   r   r   codepoint_classesro   r   r/   r0   rM   r    ry   r   rT   )r1   rc   r   rk   s       r   check_labelr   M  s   $ %%+,W%
5zQ&&eE5!U#Rr7Xx'A'A('KLx)C)CJ)OP
%eS11DKKERZO]`cd]dfjkpfqr  2 x)C)CJ)OP!%--CJJ5QY?\_bc\ceijoepq  #?FFuXX[^_X_aefkalm - $4 u!  RYYhq$u+ s   ;=F;Gc                     	 | j                  d      }t        |       t        |      st        d      |S # t        $ r Y nw xY wt        |        t        t        |       z   }t        |      st        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.
    asciizLabel too long)r+   ulabelr7   r
   UnicodeEncodeErrorr   _alabel_prefixr-   r1   label_bytess     r   alabelr     s|    ll7+{!+.,--   9U#33Kk*())s   36 	AAc                    t        | t        t        f      s	 | j                  d      }nt        |       }|j                         }|j                  t              rA|t        t              d }|st        d      |j                  d      d   dk(  r't        d      t        |       |j                  d      S 	 |j                  d      } t        |        | S # t        $ r t        |        | cY S w xY w# t        $ r t        d      w xY w)	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 foundr]   r\   z"A-label must not end with a hyphenr*   zInvalid A-label)r   r   r   r+   r   r   lower
startswithr   r5   r
   r   UnicodeErrorr   s     r   r   r     s	    eeY/0	,,w/K
 El##%Kn-!#n"5"78STTg&r*c1@AAK !!'**+"":. L- " 	L	&  +)**+s   C 8C2 C/.C/2Ddomain
std3_rulestransitionalc           
         ddl m } d}t        |       D ]  \  }}t        |      }||dk  r|nt        j                  ||df      dz
     }|d   }	d}
t        |      dk(  r|d   }
|	d	k(  s|	d
k(  r|r	|	dk(  r
|s|
||z  }k|
|	dk(  s|	dk(  r|r|	d
k(  r|r||
z  }|	dk(  rt        dj                  t        |      |dz   t        |                    t        j                  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   rZ   Vrf   3rV   Ir~   r`   )r   rK   r%   bisectbisect_leftr5   r   r/   r0   rM   r   ra   )r   r   r   r   outputrc   char
code_pointuts46rowstatusreplacements              r   uts46_remapr     s   & %Fv&	TY
:+;ZASAST]`jlo_pAqtuAuv!%)x=A"1+KS=Vs]<Vs][ejuj}dNF$cMfmJFcMVbk!Fs]"?FFuZGXZ]`aZacghncop ! '(   //r   strictuts46c                 v   |rt        j                  dt        d       t        | t              s	 t	        | d      } |rt        | ||      } t        | d      st        d      d	}g }|r| j                  d
      }nt        j                  |       }|r|dgk(  rt        d      |d   dk(  r|d= d}|D ]+  }t        |      } | r|j                  |        "t        d       |r|j                  d       dj                  |      } t        | |      st        d      | S # t
        t        f$ r t        d      w xY w)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.rZ   )
stacklevelr   zGshould pass a unicode string to the function rather than a byte string.Tr8   Domain too longF.r?   Empty domainr]   Empty labelr      .)warningswarnDeprecationWarningr   strUnicodeDecodeError	TypeErrorr
   r   r<   split_unicode_dots_rer   appendjoin)	r(   r   r   r   r   r8   resultlabelsr1   s	            r   r+   r+     sK   8 M		
 a	gAwA :|4 qt4)**LF!''*Vt^''bzR2J5MMM!M**  c		&Aq,/)**HA #I. 	geff	gs   D D8c                    t        | t              s	 t        | d      } |rt        | |d      } t        | d      st	        d      d}g }|st        j                  |       }n| j                  d      }|r|dgk(  rt	        d	      |d
   s|d
= d}|D ]+  }t        |      } | r|j                  |        "t	        d       |r|j                  d       dj                  |      S # t        t        f$ r t	        d      w xY w)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-labelFTr   r   r   r?   r   r]   r   )r   r   r   r   r
   r   r<   r   r   r   r   r   )r(   r   r   r   r8   r   r   r1   s           r   r   r   D  s   . a	8AwA :u- qt4)**LF!''*Vt^''":2J5MMM!M**  b88F7 #I. 	8677	8s   C# #C>)F)TF)FFFF)FFF)+r   rer   r   typingr   r   r?   r   	intrangesr   rg   r   compiler   r   r
   r   r   r   intr"   r   boolr'   r   r-   r0   r7   r<   rT   rX   r^   rb   ro   ry   r   r   r   r   r   r+   r   r   r   r   <module>r      s    	   "  ( 2:::; 	 		I 		y 		i 	  @3 @ @ @      S  S  eE3J/ D "uUCZ0    Rc Rd Rt Rj# $ 3 4 $AS AT A7# 7C 7D 7t4# 4C 4D 4T 4n5uS%23 5 5p# % >&%UI-. &3 &R+0 +0 +0D +0UX +0` FS%"#FF F 	F
 F FV 	5S%"#55 5 	5
 	5r   