
    ;OOf7;                         d Z ddlmZ ddlmZ ddlZddlmZ ddlZ	 ddlm	Z	m
Z
 dZ ej                  e      Z ej"                  ed      Z ej"                  ed	      Z G d
 de      ZdaddZddZddZd Zy# e$ r ddlm	Z	m
Z
 Y fw xY w)z'
Public Suffix List module for Python.
    )absolute_import)unicode_literalsN)path)urlopenRequestz4https://publicsuffix.org/list/public_suffix_list.datzpublic_suffix_list.datzpublic_suffix_list.ABOUTc                   J    e Zd ZddZd Zd Zd Zd Zd ZddZ	dd	Z
dd
Zy)PublicSuffixListNc                    |t        |t              r:t        j                  |xs t        dd      5 }|j                         }ddd       n|}g | _        | j                  |      }| j                  |      | _	        y# 1 sw Y   9xY w)a/  
        Read and parse a public suffix list. `psl_file` is either a file
        location string, or a file-like object, or an iterable of lines from a
        public suffix data file.

        If psl_file is None, the vendored file named "public_suffix_list.dat" is
        loaded. It is stored side by side with this Python package.

        The Mozilla public suffix list is no longer IDNA-encoded, it is UTF-8.
        For use cases with domains that are IDNA encoded, choose idna=True and
        the list will be converted upon loading. The wrong encoding will provide
        incorrect answers in either use case.

        The file format is described at http://publicsuffix.org/

        :param psl_file: string or None
        :param idna: boolean, whether to convert file to IDNA-encoded strings
        Nrutf8)encoding)

isinstancestrcodecsopenPSL_FILE	readlinestlds_build_structure	_simplifyroot)selfpsl_fileidnapslr   s        W/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/publicsuffix2/__init__.py__init__zPublicSuffixList.__init__A   s    ( z(C8X13H &Cmmo& & C 	$$S$/NN4(	& &s   A??Bc                     |s|S t        |      dk(  r|j                  i        t        |      dk(  sJ |\  }}|j                         }|j                  |d      }|sdgx||<   }| j	                  ||      S )a  
        Processing each line of the public suffix list recursively to build the
        Trie.  Each line is processed into a dictionary, which may contain sub-
        Trie, and nodes terminate in node of either 0 or 1 (negate).

        This method takes the current parent Trie, and searches it for the next
        part in the line (child). If not found, it adds a node to the Trie,
        creating a new branch with the [0]. If found, the existing sub-Trie is
        passed for the next part.

        :param parent: current Trie, form is Tuple (negate, dict of Trie)
        :param parts: list of strings
        :return: recursive search for remaining domain parts
              Nr   )lenappendpopget
_find_node)r   parentparts_negatechildrenchild
child_nodes          r   r%   zPublicSuffixList._find_nodea   s     M v;!MM"6{a"		 \\%.
 ,-3.HUOjz511    c                     |j                  d      rd}|dd }nd}|j                  d      }|| j                  ||      d<   y)aG  
        Initial setup for a line of the public suffix list. If it starts with !
        that is a negation operation. this calls the find_node() method
        recursively to build out the Trie for this rule.

        :param root: root Trie
        :param rule: string, line of public suffixlist
        :return: None
        !r   Nr   .)
startswithsplitr%   )r   r   rulenegater'   s        r   	_add_rulezPublicSuffixList._add_rule   sG     ??3F8DF

3*0e$Q'r,   c                 ~     t        |      dk(  r|d   S |d   t         fd|d   j                         D              fS )z
        Condense the lines of the Trie in place.

        :param node: node in the Trie, either 0/1 or a subTrie
        :return: simplified Trie, form Tuple
        r   r   c              3   J   K   | ]  \  }}|j                  |      f  y w)N)r   ).0kvr   s      r   	<genexpr>z-PublicSuffixList._simplify.<locals>.<genexpr>   s#     S!Qq$.."34Ss    #)r!   dictitems)r   nodes   ` r   r   zPublicSuffixList._simplify   s=     t9>7NQS47==?SSTTr,   c                 B   dg}| j                   }|D ]  }|j                         }|r|j                  d      r'|r|j                  d      j	                         }|j                  |       | j                  ||j                         d   j                  d              |S )a  
        Build a Trie from the public suffix list. If idna==True, idna-encode
        each line before building.

        The Trie is comprised of tuples that encode whether the line is a
        negation line (0 or 1), and terminate with 0. Each node is represented
        with two-tuple of the form (negate, dict of children / sub-Trie). A
        partial subTrie therefore looks like: (0, {'ac': 0, 'co': (0,
        {'blogspot': 0}), 'gv': 0,....}) where each tuple starts with the
        negation encoding, and each leaf in the Trie as a dictionary element
        returns 0.

        Also creates an instance attribute, tlds, which simply contains the
        publicsuffix list, with the modifiers such as wildcards, as a list. This
        can be accessed for post-processing by the application.

        :param fp: pointer for the public suffix list
        :param idna: boolean, convert lines to idna-encoded strings
        :return: Trie
        r   z//r   r/   )	r   stripr0   encodedecoder"   r4   r1   lstrip)r   fpr   r   r   lines         r   r   z!PublicSuffixList._build_structure   s    * syy 	>D::<D4??40{{6*113KKNN4a!7!7!<=	> r,   c                     |r|dk(  rd|| <   |dv ry|d   }|t        |      k  rX|rUd||    fD ]I  }|j                  |d      }||s|dk7  s |dv r|}	n|d   }	|	|| <   | j                  ||dz   |||       K yyy)al  
        Traverses the Trie recursively to find the parts. By default, the
        traverse follows wildcards, as appropriate for the public suffix list,
        but if wildcard is set to False, it will stop at wildcard leaves. This
        can be useful for summarizing complex wildcard domains like those under
        amazonaws.com.

        The lookup is tracked via a list, initially set to all None, that marks
        the negation flags of nodes it matches. each match will be marked for
        later composition of the eTLD.

        :param matches: list, parts long, None (initial), 0, or 1
        :param depth: int, how far in the Trie this run is
        :param parent: Tuple, the current subTrie
        :param parts: list of domain parts, strings
        :param wildcard: boolean, whether to process wildcard nodes
        :return: None, recursive call
        r   r   r   r   N*)r!   r$   _lookup_node)
r   matchesdepthr&   r'   wildcardr)   namer*   r3   s
             r   rH   zPublicSuffixList._lookup_node   s    & 
  GUFOV!9CJ8eUFm, 	V T40$43; F?%*F%*1XF*0))'519eUHU	V $,r,   c                    |sy| j                  ||d      }|r|y|j                         j                  d      j                  d      }|dn|j	                  d      dz   }t        |      |k  r|S dj                  ||dz    d       S )a  
        Return the second-level-domain (SLD) or private suffix of a given domain
        according to the public suffix list. The public suffix list includes
        wildcards, so if wildcard is set to True, this will follow the wildcard
        on traversal, otherwise it will stop at wildcard nodes.

        The logic does not check by default whether the TLD is in the Trie, so
        for example, 'www.this.local' will return 'this.local'. If you want to
        ensure the TLD is in the public suffix list, use strict=True.

        If domain is already an eTLD, it returns domain as-is instead of None
        value.

        :param domain: string, needs to match the encoding of the PSL (idna or UTF8)
        :param wildcard: boolean, follow wildcard patterns
        :param strict: boolean, check the TLD is valid, return None if not
        :return: string, the SLD for the domain
        NTr/   r   r   )get_tldlowerr?   r1   countr!   join)r   domainrK   stricttldr'   num_of_tld_partss          r   get_sldzPublicSuffixList.get_sld   s    &  ll68T2ck$$S)//4 #13!1Cu:))J88E$4q$8"9":;<<r,   c                 (    | j                  |||      S )z(
        Use get_sld() instead.
        )rV   )r   rR   rK   rS   s       r   get_public_suffixz"PublicSuffixList.get_public_suffix  s     ||FHf55r,   c                    |sy|j                         j                  d      j                  d      }dgt        |      z  }|r1| j                  dv s"|d   | j                  d   j                         vry| j                  |d| j                  ||       t        |      D ]#  \  }}|	|dk(  sdj                  ||d       c S  y)a  
        Return the TLD, or public suffix, of a domain using the public suffix
        list. uses wildcards if set, and checks for valid top TLD is
        strict=True.

        This will return the domain itself when it is an ICANN TLD, e.g., 'com'
        returns 'com', for follow on processing, while 'co.uk' return 'uk'. On
        the other hand, more complicated domains will return their public
        suffix, e.g.,
        'google.co.uk' will return 'co.uk'.  Root ('.') will return empty string.

        :param domain: string
        :param wildcard: boolean, follow wildcards in Trie
        :param strict: boolean, check that top TLD is valid in Trie
        :return: string, the TLD for the domain
        Nr/   rF   r   r   )	rO   r?   r1   r!   r   keysrH   	enumeraterQ   )r   rR   rK   rS   r'   hitsiwhats           r   rN   zPublicSuffixList.get_tld  s    " $$S)//4vE
"II59DIIaL4E4E4G#G$499eX>  	+GAtDAIxxab	**	+r,   )NT)TF)__name__
__module____qualname__r   r%   r4   r   r   rH   rV   rX   rN    r,   r   r	   r	   ?   s6    )@"2H1&
U#J'VR!=F6+r,   r	   c                 \    t         xs t        ||      a t         j                  | ||      S )a  
    Return the private suffix or SLD for a `domain` DNS name string. The
    original publicsuffix2 library used the method get_public_suffix() for this
    purpose, but get_private_suffix() is more proper. Convenience function that
    builds and caches a PublicSuffixList object.

    Optionally read, and parse a public suffix list. `psl_file` is either a file
    location string, or a file-like object, or an iterable of lines from a
    public suffix data file.

    If psl_file is None, the vendored file named "public_suffix_list.dat" is
    loaded. It is stored side by side with this Python package.

    The file format is described at http://publicsuffix.org/
    r   rK   rS   )_PSLr	   rV   rR   r   rK   r   rS   s        r   rV   rV   ?  s+    " 8#H48D<<&<AAr,   c                 \    t         xs t        ||      a t         j                  | ||      S )aU  
    Return the TLD or public suffix for a `domain` DNS name string. (this is
    actually the private suffix that is returned) Convenience function that
    builds and caches a PublicSuffixList object.

    Optionally read, and parse a public suffix list. `psl_file` is either a file
    location string, or a file-like object, or an iterable of lines from a
    public suffix data file.

    If psl_file is None, the vendored file named "public_suffix_list.dat" is
    loaded. It is stored side by side with this Python package.

    The file format is described at http://publicsuffix.org/
    re   rf   )rg   r	   rN   rh   s        r   rN   rN   T  s+      8#H48D<<&<AAr,   c                 T    t        j                  dt               t        | ||||      S )a  
    Included for compatibility with the original publicsuffix2 library -- this
    function returns the private suffix or SLD of the domain. To get the public
    suffix, use get_tld(). Convenience function that builds and caches a
    PublicSuffixList object.

    Optionally read, and parse a public suffix list. `psl_file` is either a file
    location string, or a file-like object, or an iterable of lines from a
    public suffix data file.

    If psl_file is None, the vendored file named "public_suffix_list.dat" is
    loaded. It is stored side by side with this Python package.

    The file format is described at http://publicsuffix.org/
    zThis function returns the private suffix, SLD, or registrable domain. This equivalent to function get_sld(). To get the public suffix itself, use get_tld().)warningswarnUserWarningrV   rh   s        r   rX   rX   h  s/      MM	: 		 68XtV<<r,   c                     t        t        ddi      } t        |       }	 |j                  j	                         } t        j                  |      |      }|S # t
        $ r |j                  j                  d      }Y Cw xY w)zj
    Return a file-like object for the latest public suffix list downloaded from
    publicsuffix.org
    z
User-Agentzpython-publicsuffix2)headerscharset)	r   PSL_URLr   ro   get_content_charsetAttributeErrorgetparamr   	getreader)reqresr   fs       r   fetchry     sy    
 'L2H#I
JC
#,C3;;224 	#"3'AH  3;;''	23s   A $A>=A>)NTTF)__doc__
__future__r   r   r   osr   rk   urllib.requestr   r   ImportErrorurllib2rq   dirname__file__BASE_DIRrQ   r   ABOUT_PSL_FILEobjectr	   rg   rV   rN   rX   ry   rc   r,   r   <module>r      s   L ' '   )/
 A4<<!499X788%?@z+v z+z B*B(=2]
  )(()s   A= =BB