
    AHjJ                        U d dl mZ d dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZmZmZmZmZmZ d dlmZmZ dd	lmZmZ dd
lmZmZmZmZ ddlmZ ddl m!Z! ddl"m#Z# dZ$dZ%dZ&dZ'i a(ee)e#f   e*d<    ejV                  d      Z,dee)e#f   fdZ-de)de#ddfdZ.de)ddfdZ/ e	dd      	 d8de0de)de0fd        Z1d9de2fd!Z3	 	 	 d:de0d"e2d#ee   dee)e#f   fd$Z4 G d% d&e      Z5 G d' d(e5      Z6 G d) d*e5      Z7dddde6fd+ee)ef   de0d,e0d-e0d.e0d#ee   deeee)   e2ef   eddef   f   fd/Z8	 	 	 	 	 d;d+ee)ef   de0d,e0d-e0d.e0d#ee   dee)   fd0Z9	 	 	 	 	 	 d<d+ee)ef   de0d1e0d,e0d-e0d.e0d#ee   deee)e!f      fd2Z:	 	 	 	 	 d;d+ee)ef   de0d,e0d-e0d.e0d#ee   deed3   ee)e)e)f   f   fd4Z;	 	 	 d=d5ee)ef   d-e0d.e0d#ee   de0f
d6Z<d9de)ddfd7Z=y)>    )unicode_literalsN)open)	lru_cache)isabs)DictListOptionalTupleTypeUnion)SplitResulturlsplit   )BaseTLDSourceParserRegistry)	TldBadUrlTldDomainNotFoundTldImproperlyConfigured
TldIOError)project_dir)Result)TriezArtur Barseghyanz2013-2026 Artur Barseghyanz,MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later)BaseMozillaTLDSourceParser MozillaPublicOnlyTLDSourceParserMozillaTLDSourceParserr   get_fldget_tldget_tld_namesget_tld_names_containeris_tld	parse_tldpop_tld_names_containerprocess_urlprotocol_rereset_tld_names	tld_namesupdate_tld_namesupdate_tld_names_cliupdate_tld_names_containerr&   z^[a-z0-9.+:-]*//returnc                      t         S )zCGet container of all tld names.

    :return:
    :rtype dict:
    r&        A/root/tools/cai/cai_env/lib/python3.12/site-packages/tld/utils.pyr   r   6   s
     r.   tld_names_local_pathtrie_objc                     |t         | <   y)ziUpdate TLD Names container item.

    :param tld_names_local_path:
    :param trie_obj:
    :return:
    Nr,   )r0   r1   s     r/   r)   r)   @   s     '/I"#r.   c                 0    t         j                  | d       y)zTRemove TLD names container item.

    :param tld_names_local_path:
    :return:
    N)r&   popr0   s    r/   r"   r"   N   s     MM&-r.      T)maxsizetypedFfail_silently
parser_uidc                 N   g }|j                   }|rGt        j                  |d      }|rs|j                  rg ||j	                  |              t        |      S t        j
                         D ]-  \  }}|s	|j                  s ||j	                  |              / t        |      S )zUUpdate TLD names.

    :param fail_silently:
    :param parser_uid:
    :return:
    Nr9   )appendr   get
source_urlr'   itemsall)r9   r:   resultsresults_append
parser_clss        r/   r'   r'   X   s     G^^N\\*d3
*//++-+H w< '/nn&6 	"J
j33//m/L	 w<r.   c                 .   t        j                  d      }|j                  dddd       |j                  dd	d
dd       |j                  | | nt        j
                  dd       }|j                  }|j                  }t        t        ||             S )zCLI wrapper for update_tld_names.

    Since update_tld_names returns True on success, we need to negate the
    result to match CLI semantics.
    zUpdate TLD names)descriptionr:   ?Nz*UID of the parser to update TLD names for.)nargsdefaulthelpz--fail-silentlyr9   F
store_truezFail silently)destrI   actionrJ   r   )r:   r9   )
argparseArgumentParseradd_argument
parse_argssysargvr:   r9   intr'   )rS   parserargsr:   r9   s        r/   r(   r(   t   s     $$1CDF
9	      T%5T388AB<HDJ&&M
-PP r.   retry_countparser_classc                 8    |st         }|j                  | |      S )a  Build the ``tlds`` list if empty. Recursive.

    :param fail_silently: If set to True, no exceptions are raised and None
        is returned on failure.
    :param retry_count: If greater than 1, we raise an exception in order
        to avoid infinite loops.
    :param parser_class:
    :type fail_silently: bool
    :type retry_count: int
    :type parser_class: BaseTLDSourceParser
    :return: List of TLD names
    :rtype: obj:`tld.utils.Trie`
    r9   rW   )r   r   )r9   rW   rX   s      r/   r   r      s)    $ -%%# &  r.   c                   >    e Zd Ze	 ddededeeee	f      fd       Z
y)r   r9   rW   r*   c                    |dkD  r	|ryt         t        }| j                  |v r|| j                     |S 	 t        | j                        r| j                  }nt	        | j                        }t        |dd      }t               }|j                  }d}| j                  }	|D ]F  }
d|
v rd}|r|	s n:d	|
v r|
j                         d   }
|
d
   dv r. ||
j                          |       H t        | j                  |       |j                          	 	 |j                          |S # t        $ rM | j                  |       |dz  }| j                  ||      cY 	 j                          S # t         $ r Y S w xY wt         $ r.}|r%Y d}~	 j                          y# t         $ r Y yw xY w|d}~ww xY w# t         $ r Y |S w xY w# 	 j                          w # t         $ r Y w w xY wxY w)z[Parse.

        :param fail_silently:
        :param retry_count:
        :return:
        r   Nrutf8)encodingFz===BEGIN PRIVATE DOMAINS===Tz// xn--r   )/
)privater<   rZ   )r   r&   
local_pathr   r   codecs_openr   addinclude_privatesplitstripr)   closeIOErrorr'   r   	Exception)clsr9   rW   
_tld_namesrc   
local_filetrietrie_addprivate_sectionrf   lineerrs               r/   r   z(BaseMozillaTLDSourceParser.get_tld_names   s    ?   

 NNj(3>>*62	S^^$ ^^
(8
$ZvFJ6DxxH#O!11O" E0D8&*O"? $::<?D7k)DJJL>ODE  's~~t<   " )  	  } =1K$$+ %    "   	  " 	 			    " s   CD# F1 #3F.G E))	E54E58F. F)G F 	F$#F$'F))F..G 1	F>=F>G#GG#	G G#G  G#N)Fr   )__name__
__module____qualname__classmethodboolrT   r	   r   strr   r   r-   r.   r/   r   r      sA    =>N N7:N	$sDy/	"N Nr.   r   c                   <    e Zd ZU dZdZeed<   dZeed<   dZeed<   y)	r   Mozilla TLD source.mozillauidz4https://publicsuffix.org/list/public_suffix_list.datr?   zres/effective_tld_names.dat.txtrc   N)	rt   ru   rv   __doc__r}   ry   __annotations__r?   rc   r-   r.   r/   r   r     s#    CLJL7J7r.   r   c                   J    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed	<   y
)r   r{   mozilla_public_onlyr}   z?https://publicsuffix.org/list/public_suffix_list.dat?publiconlyr?   z+res/effective_tld_names_public_only.dat.txtrc   Frf   N)rt   ru   rv   r~   r}   ry   r   r?   rc   rf   rx   r-   r.   r/   r   r   
  s5    $C$I   DJC!OT!r.   r   urlfix_protocolsearch_publicsearch_privatec                    |s|st        d      t        ||      }t        | t              s7|r(t        j                  | j                               sd|  } 	 t        |       }n| }|j                  }	|	s|rdd|fS t        |       |	j                         }	|	j                  d      r|	j                  d      }	|	j                  d      }
|j                  }||   j                  }d}d}d}t!        |
      }t#        |dz
  d	d	      D ]z  }|
|   }|j$                   ng||j&                  k(  r nV|j$                  j)                  |      }||j$                  j)                  d
      }| n|dz  }|}|j*                  sw|}|}| |(|j*                  r|s|j,                  r|s|j,                  r|rdd|fS t/        |	      ||k(  rd	}nt1        d||z
        }|
||fS # t        $ r}|r
dd| fcY d}~S |d}~ww xY w)zProcess URL.

    :param parser_class:
    :param url:
    :param fail_silently:
    :param fix_protocol:
    :param search_public:
    :param search_private:
    :return:
    zJEither `search_public` or `search_private` (or both) shall be set to True.)r9   rX   zhttps://N)r   .r   r   *)domain_name)r   r   
isinstancer   r$   matchlowerr   
ValueErrorhostnamer   endswithrstriprg   rc   rootlenrangechildren	exceptionr>   leafrb   r   max)r   r9   r   r   r   rX   rm   
parsed_urler   domain_partsr0   nodecurrent_length
tld_lengthr   len_domain_partsipartchild
non_zero_is                        r/   r#   r#     sA   $ ^%
 	
 #,J c;' 1 1#))+ >SE"C	!#J 
 %%Kz))$$##%K C !((-$$S)L'22 *+00DNJE<(#a'R0 A ==  4>>!!!$' =MM%%c*E = 	!99'JE7> 


emm5==z))#<<:%
,z9:
Z//_  	T3&		s$   G 	G8%G3+G81G33G8c                     d|v rt        d      |s|rt        nt        }t        | |||||      \  }}}	|y|J |dk  r|	j                  S dj                  ||dz
  d       S )a?  Extract the first level domain.

    Extract the top level domain based on the mozilla's effective TLD names
    dat file. Returns a string. May throw ``TldBadUrl`` or
    ``TldDomainNotFound`` exceptions if there's bad URL provided or no TLD
    match found respectively.

    :param url: URL to get top level domain from.
    :param fail_silently: If set to True, no exceptions are raised and None
        is returned on failure.
    :param fix_protocol: If set to True, missing or wrong protocol is
        ignored (https is appended instead).
    :param search_public: If set to True, search in public domains.
    :param search_private: If set to True, search in private domains.
    :param parser_class:
    :type url: str | SplitResult
    :type fail_silently: bool
    :type fix_protocol: bool
    :type search_public: bool
    :type search_private: bool
    :return: String with top level domain (if ``as_object`` argument
        is set to False) or a ``tld.utils.Result`` object (if ``as_object``
        argument is set to True); returns None on failure.
    :rtype: str
    	as_objectzH`as_object` argument is deprecated for `get_fld`. Use `get_tld` instead.r   r9   r   r   r   rX   Nr   r   r   )r   r   r   r#   r   join)
r   r9   r   r   r   rX   kwargsr   r   r   s
             r/   r   r     s    D f%
 	

   #1 	 ,7#!#%!,(L*j  !!!A~"""88La!1233r.   r   c                    |s|rt         nt        }t        | |||||      \  }}}	|y|J |s%|dk  r|	j                  S dj	                  ||d       S |dk  r$d}
d}|	j                  J d       |	j                  }nBdj	                  |d|dz
         }
dj	                  ||dz
  |       }dj	                  ||d       }t        |
|||	      S )	a  Extract the top level domain.

    Extract the top level domain based on the mozilla's effective TLD names
    dat file. Returns a string. May throw ``TldBadUrl`` or
    ``TldDomainNotFound`` exceptions if there's bad URL provided or no TLD
    match found respectively.

    :param url: URL to get top level domain from.
    :param fail_silently: If set to True, no exceptions are raised and None
        is returned on failure.
    :param as_object: If set to True, ``tld.utils.Result`` object is returned,
        ``domain``, ``suffix`` and ``tld`` properties.
    :param fix_protocol: If set to True, missing or wrong protocol is
        ignored (https is appended instead).
    :param search_public: If set to True, search in public domains.
    :param search_private: If set to True, search in private domains.
    :param parser_class:
    :type url: str | SplitResult
    :type fail_silently: bool
    :type as_object: bool
    :type fix_protocol: bool
    :type search_public: bool
    :type search_private: bool
    :return: String with top level domain (if ``as_object`` argument
        is set to False) or a ``tld.utils.Result`` object (if ``as_object``
        argument is set to True); returns None on failure.
    :rtype: str
    r   Nr   r    zNo hostname in URLr   )	subdomaindomaintldr   )r   r   r#   r   r   r   )r   r9   r   r   r   r   rX   r   r   r   r   r   _tlds                r/   r   r     s   J   #1 	 ,7#!#%!,(L*j  !!!>&&&xxZ[122A~	 "".D0DD.""HH\*:JN;<	,zA~
CDxxZ[12F r.   NNNc           	          |s|rt         nt        }	 t        | |d||||      }|y|j                  |j                  |j
                  fS # t        t        t        t        f$ r Y yw xY w)zParse TLD into parts.

    :param url:
    :param fail_silently:
    :param fix_protocol:
    :param search_public:
    :param search_private:
    :param parser_class:
    :return: Tuple (tld, domain, subdomain)
    :rtype: tuple
    T)r9   r   r   r   r   rX   r   )
r   r   r   r   r   r   r   r   r   r   )r   r9   r   r   r   rX   objs          r/   r!   r!   )  s    &   #1 	'%')%
 ;#ww

CMM11(*A:N s   A
 "A
 
A&%A&valuec                 N    |s|rt         nt        }t        | dd|||      }| |k(  S )ag  Check if given URL is tld.

    :param value: URL to get top level domain from.
    :param search_public: If set to True, search in public domains.
    :param search_private: If set to True, search in private domains.
    :param parser_class:
    :type value: str
    :type search_public: bool
    :type search_private: bool
    :return:
    :rtype: bool
    Tr   )r   r   r   )r   r   r   rX   r   s        r/   r    r    X  sC    $   #1 	 #%!D D=r.   c                 $    | rt        |        yi ay)zReset the ``tld_names`` to empty value.

    If ``tld_names_local_path`` is given, removes specified
    entry from ``tld_names`` instead.

    :param tld_names_local_path:
    :type tld_names_local_path: str
    :return:
    N)r"   r&   r5   s    r/   r%   r%   |  s      45 	r.   )FN)N)Fr   N)FFTTN)FFFTTN)TTN)>
__future__r   rN   rerR   codecsr   rd   	functoolsr   os.pathr   typingr   r   r	   r
   r   r   urllib.parser   r   baser   r   
exceptionsr   r   r   r   helpersr   resultr   ro   r   
__author____copyright____license____all__r&   ry   r   compiler$   r   r)   r"   rx   r'   rT   r(   r   r   r   r   r#   r   r   r!   r    r%   r-   r.   r/   <module>r      s   '  	 
 &   ; ; . /  !  
  
,<*  	4T	? bjj,-c4i //)-/	/.# .$ . 3d#37-0	 $6s :  .2 *+ 
#t)_	>P!4 Pf87 8"'A "$  .Ds0	sK	 s0s0 s0 	s0
 s0 *+s0 5cC,-uT45L/MMNs0p  .2B4	sK	 B4B4 B4 	B4
 B4 *+B4 c]B4N  .2Q	sK	 QQ Q 	Q
 Q Q *+Q eCK !Ql  .2,	sK	 ,, , 	,
 , *+, 5!"E#sC-$889,b .2	!k!"!! ! *+	!
 
!H#  r.   