
    IZjJ                     T   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j+        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=dS )>    )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&        U/Users/jameslopez/projects/MentorCore/.venv/lib/python3.11/site-packages/tld/utils.pyr   r   6   s
     r.   tld_names_local_pathtrie_objc                     |t           | <   dS )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                 <    t                               | d           dS )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                 4   g }|j         }|r>t          j        |d          }|r&|j        r ||                    |                      nAt          j                    D ]-\  }}|r&|j        r ||                    |                      .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3
 	*/ 	N++-+HH   '/n&6&6 	 	"J
 j3 //m/LL   w<<r.   c                 B   t          j        d          }|                    dddd           |                    dd	d
dd           |                    | | 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DDDF
9	          T%5TT38ABB<HHDJ&M
-PPPP  r.   retry_countparser_classc                 B    |st           }|                    | |          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      s3    $  .-%%# &   r.   c                   R    e Zd Ze	 ddededeeee	f                  fd            Z
dS )	r   Fr   r9   rW   r*   c                    |dk    r|rdS t           t          }| j        |v r|| j                 |S 	 t          | j                  r| j        }nt	          | j                  }t          |dd          }t                      }|j        }d}| j        }	|D ]W}
d|
v rd}|r|	s nJd	|
v r|
	                                d         }
|
d
         dv r7 ||

                                 |           Xt          | j        |           |                                 n# t          $ rZ |                     |           |dz  }|                     ||          cY 	 |                                 S # t           $ r Y S w xY wt           $ r5}|r,Y d}~	 |                                 dS # t           $ r Y dS w xY w|d}~ww xY w	 	 |                                 n:# t           $ r Y n.w xY w# 	 |                                 w # t           $ r Y w w xY wxY w|S )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    ?? !t   

 Nj((3>*62	S^$$ 9 ^

(88
$ZvFFFJ66DxH#O!1O" E E0D88&*O" ? E $$::<<?D7k))DJJLL*ODDDDD&s~t<<< 	 	 	  } ===1K$$+ %      """"     	 	 	 ttt  """""   	 			    """"     """"    s   C#D G! <F7G! E))
E65E69	F7F2G! 	F 
F-,F-0F22F77G! <G 
GG!H#G87H8
HHHHN)Fr   )__name__
__module____qualname__classmethodboolrT   r	   r   strr   r   r-   r.   r/   r   r      si        =>N N N7:N	$sDy/	"N N N [N 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<   dS )	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     sH         CLJLLL7J77777r.   r   c                   L    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	<   d
S )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   
  sd         $C$$$I     DJCCC!OT!!!!!r.   r   urlfix_protocolsearch_publicsearch_privatec                     |s|st          d          t          ||          }t          | t                    sd|r1t                              |                                           sd|  } 	 t          |           }n"# t          $ r}|r
dd| fcY d}~S |d}~ww xY w| }|j	        }	|	s|rdd|fS t          |           |	                                }	|	                    d          r|	                    d          }	|	                    d          }
|j        }||         j        }d}d}d}t!          |
          }t#          |dz
  d	d	          D ]l}|
|         }|j         nZ||j        k    r nM|j                            |          }||j                            d
          }| n|dz  }|}|j        r|}|}m||j        r|s|j        r	|s|j        r|rdd|fS t/          |	          ||k    rd	}nt1          d||z
            }|
||fS )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#     s   $  
^ 
%
 
 	
 #,  J c;''  	# 1 1#))++ > > 	#"S""C	!#JJ 	 	 	 T3&&&&&&		 
 %K % 	%z))$$$$##%%K C   .!((--$$S))L'2 *+0DNJE<((#a'R00  A = E 4>!!E!!$'' =M%%c**E =E 	!9 	'JE 

 
 
&+m 
  
 $)= 

  	=z))#<<<<:%%

,z9::
Z//s$   .A> >
BBBBBc                     d|v rt          d          |s|rt          nt          }t          | |||||          \  }}}	|dS |J |dk     r|	j        S d                    ||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%
 
 	

  
 2""1 	 ,7#!#%!, , ,(L*j t !!!A~~""88La!1!12333r.   r   c                    |s|rt           nt          }t          | |||||          \  }}}	|dS |J |s*|dk     r|	j        S d                    ||d                   S |dk     rd}
d}|	j        
J d            |	j        }n]d                    |d|dz
                     }
d                    ||dz
  |                   }d                    ||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     sT   J  
 2""1 	 ,7#!#%!, , ,(L*j t !!! 3>>&&xxZ[[1222A~~	 "..0D..."HH\*:JN*:;<<	,zA~
'BCDDxxZ[[122F   r.   NNNc           	          |s|rt           nt          }	 t          | |d||||          }|dS |j        |j        |j        fS # t          t          t          t          f$ r Y nw xY wdS )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   Nr   )
r   r   r   r   r   r   r   r   r   r   )r   r9   r   r   r   rX   objs          r/   r!   r!   )  s    &  
 2""1 	'%')%
 
 
 ;##w
CM11(*A:N    s   A A A"!A"valuec                 \    |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  sX    $  
 2""1 	 #%!  D D=r.   c                 0    | rt          |            dS i adS )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555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      sQ   ' ' ' ' ' ' '  				 



 & & & & & &             ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; . . . . . . . . / / / / / / / /            !                      
  
,<*  	4T	?   bj,--c4i    //)-/	/ / / /.# .$ . . . . 3d###37 -0	   $#6 s    :  .2  *+ 
#t)_	   >P P P P P!4 P P Pf8 8 8 8 87 8 8 8" " " " "'A " " "$  .Ds0 s0	sK	 s0s0 s0 	s0
 s0 *+s0 5cC,-uT45L/MMNs0 s0 s0 s0p  .2B4 B4	sK	 B4B4 B4 	B4
 B4 *+B4 c]B4 B4 B4 B4N  .2Q Q	sK	 QQ Q 	Q
 Q Q *+Q eCK !Q Q Q Ql  .2, ,	sK	 ,, , 	,
 , *+, 5!"E#sC-$889, , , ,b .2	! !k!"!! ! *+	!
 
! ! ! !H #       r.   