
    "htJ                     z   U d dl m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<   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      	 d7de-de(de-fd       Z.de/fd Z0	 	 	 d8de-d!e/d"ee   dee(e"f   fd#Z1 G d$ d%e      Z2 G d& d'e2      Z3 G d( d)e2      Z4dddde3fd*ee(ef   de-d+e-d,e-d-e-d"ee   deeee(   e/ef   eddef   f   fd.Z5	 	 	 	 	 d9d*ee(ef   de-d+e-d,e-d-e-d"ee   dee(   fd/Z6	 	 	 	 	 	 d:d*ee(ef   de-d0e-d+e-d,e-d-e-d"ee   deee(e f      fd1Z7	 	 	 	 	 d9d*ee(ef   de-d+e-d,e-d-e-d"ee   deed2   ee(e(e(f   f   fd3Z8	 	 	 d;d4ee(ef   d,e-d-e-d"ee   de-f
d5Z9d<de(ddfd6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-2023 Artur Barseghyanz,MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later)BaseMozillaTLDSourceParserget_fldget_tldget_tld_namesget_tld_names_containeris_tldMozillaTLDSourceParser MozillaPublicOnlyTLDSourceParser	parse_tldpop_tld_names_containerprocess_urlreset_tld_namesr   	tld_namesupdate_tld_namesupdate_tld_names_cliupdate_tld_names_containerr%   returnc                      t         S )zCGet container of all tld names.

    :return:
    :rtype dict:
    r%        F/var/www/html/sandstorm/venv/lib/python3.12/site-packages/tld/utils.pyr   r   3   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+   )r/   r0   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r/   s    r.   r"   r"   K   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r8   )appendr   get
source_urlr&   itemsall)r8   r9   resultsresults_append
parser_clss        r.   r&   r&   U   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                  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)descriptionr9   ?Nz*UID of the parser to update TLD names for.)nargsdefaulthelpz--fail-silentlyr8   F
store_truezFail silently)destrH   actionrI   r   )r9   r8   )
argparseArgumentParseradd_argument
parse_argssysargvr9   r8   intr&   )parserargsr9   r8   s       r.   r'   r'   q   s     $$1CDF
9	      SXXab\*D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`
    r8   rV   )r   r   )r8   rV   rW   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   r8   rV   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;   rY   )r   r%   
local_pathr   r   codecs_openr   addinclude_privatesplitstripr(   closeIOErrorr&   r   	Exception)clsr8   rV   
_tld_namesrb   
local_filetrietrie_addprivate_sectionre   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boolrS   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.txtrb   N)	rs   rt   ru   __doc__r|   rx   __annotations__r>   rb   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    rz   mozilla_public_onlyr|   z?https://publicsuffix.org/list/public_suffix_list.dat?publiconlyr>   z+res/effective_tld_names_public_only.dat.txtrb   Fre   N)rs   rt   ru   r}   r|   rx   r~   r>   rb   re   rw   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              s%|r| j	                  d      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t/        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.)r8   rW   )z//zhttp://https://r   N)r   .r   r   *)domain_name)r   r   
isinstancer   
startswithr   
ValueErrorhostnamer   lowerendswithrstriprf   rb   rootlenrangechildren	exceptionr=   leafra   r   max)r   r8   r   r   r   rW   rl   
parsed_urler   domain_partsr/   nodecurrent_length
tld_lengthmatchlen_domain_partsipartchild
non_zero_is                        r.   r#   r#     s:   $ ^%
 	
 #,J c;'/L M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
 
	G&G!G&G!!G&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   r8   r   r   r   rW   Nr   r   r   )r   r   r    r#   r   join)
r   r8   r   r   r   rW   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   r8   r   r   r   r   rW   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)r8   r   r   r   r   rW   r   )
r   r    r   r   r   r   r   r   r   r   )r   r8   r   r   r   rW   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   rW   r   s        r.   r   r   U  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%   r4   s    r.   r$   r$   y  s      45 	r-   )FN)Fr   N)FFTTN)FFFTTN)TTN)N);
__future__r   rM   rQ   codecsr   rc   	functoolsr   os.pathr   typingr   r   r	   r
   r   r   urllib.parser   r   baser   r   
exceptionsr   r   r   r   helpersr   resultr   rn   r   
__author____copyright____license____all__r%   rx   r~   r   r(   r"   rw   r&   rS   r'   r   r   r   r    r#   r   r   r!   r   r$   r,   r-   r.   <module>r      s   '  
 &   ; ; . /  !  
  
,<(  	4T	? c4i //)-/	/.# .$ . 3d#37-0	 $6c :  .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-   