
    j4                        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
 ddlmZmZ ddlmZmZmZ  G d d	          Z G d
 d          Ze
eef         Ze	e         Z G d d          ZdS )    )annotations)aliases)sub)AnyIteratorListTuple   )RE_POSSIBLE_ENCODING_INDICATIONTOO_BIG_SEQUENCE)	iana_nameis_multi_byte_encodingunicode_rangec                     e Zd Z	 	 d1d2dZd3dZd3dZed4d            Zd5dZd5dZ	d6dZ
ed5d            Zed7d            Zed8d            Zed8d            Zed7d            Zed5d             Zed4d!            Zed4d"            Zed4d#            Zed4d$            Zed9d%            Zed:d'            Zed8d(            Zed7d)            Zed7d*            Zd;d<d.Zed=d0            ZdS )>CharsetMatchNpayloadbytes | bytearrayguessed_encodingstrmean_mess_ratiofloathas_sig_or_bombool	languagesCoherenceMatchesdecoded_payload
str | Nonepreemptive_declarationc                    || _         || _        || _        || _        || _        d | _        g | _        d| _        d | _        d | _	        || _
        || _        d S )N        )_payload	_encoding_mean_mess_ratio
_languages_has_sig_or_bom_unicode_ranges_leaves_mean_coherence_ratio_output_payload_output_encoding_string_preemptive_declaration)selfr   r   r   r   r   r   r   s           c/workspace/show-city-briefing-agent/.venv/lib/python3.11/site-packages/charset_normalizer/models.py__init__zCharsetMatch.__init__   se     ,3.'6,5%315+-,/"-1,0#23I$$$    otherobjectreturnc                    t          |t                    s0t          |t                    rt          |d          | j        k    S dS | j        |j        k    o| j        |j        k    S )NF)
isinstancer   r   r   encodingfingerprintr-   r1   s     r.   __eq__zCharsetMatch.__eq__(   sd    %.. 	%%% @
 !..$-??5}.X43CuGX3XXr0   c                   t          |t                    st          t          | j        |j        z
            }t          | j        |j        z
            }|dk     r|dk    r| j        |j        k    S |dk     rC|dk    r=t          | j                  t          k    r| j        |j        k     S | j	        |j	        k    S | j        |j        k     S )zQ
        Implemented to make sorted available upon CharsetMatches items.
        g{Gzt?g{Gz?)
r5   r   
ValueErrorabschaos	coherencelenr!   r   multi_byte_usage)r-   r1   chaos_differencecoherence_differences       r.   __lt__zCharsetMatch.__lt__3   s     %.. 	"%dj5;&>"?"?&)$.5?*J&K&K e##(<t(C(C>EO33%%*>$*F*F 4=!!%555zEK//(5+AAAzEK''r0   c                ~    t          | j                  }|dk    rdS dt          t          |                     |z  z
  S )Nr   r    g      ?)r?   rawr   )r-   raw_lens     r.   r@   zCharsetMatch.multi_byte_usageI   s;     dh--a<<3c#d))nnw.//r0   c                    | j         ^t          | j        | j        d          | _         | j        r7| j        dk    r,| j         r%| j         d         dk    r| j         dd          | _         | j         S )Nstrictutf_7r   u   ﻿r
   )r+   r   r!   r"   r%   r-   s    r.   __str__zCharsetMatch.__str__Q   sp    <t}dnhGGDL
 $0Ng--L .LOx//#|ABB/|r0   c                (    d| j          d| j         dS )Nz<CharsetMatch 'z' fp(z)>)r6   r7   rJ   s    r.   __repr__zCharsetMatch.__repr__a   s     IIIT5EIIIIr0   Nonec                    t          |t                    r|| k    r't          d                    |j                            d |_        | j                            |           d S )Nz;Unable to add instance <{}> as a submatch of a CharsetMatch)r5   r   r;   format	__class__r+   r'   appendr8   s     r.   add_submatchzCharsetMatch.add_submatchd   sk    %.. 	%4--MTTO    E"""""r0   c                    | j         S N)r"   rJ   s    r.   r6   zCharsetMatch.encodingo   s
    ~r0   	list[str]c                    g }t          j                    D ]F\  }}| j        |k    r|                    |           &| j        |k    r|                    |           G|S )z
        Encoding name are known by many name, using this could help when searching for IBM855 when it's listed as CP855.
        )r   itemsr6   rR   )r-   also_known_asups       r.   encoding_aliaseszCharsetMatch.encoding_aliasess   sn    
 $&MOO 	( 	(DAq}!!$$Q''''!##$$Q'''r0   c                    | j         S rU   r%   rJ   s    r.   bomzCharsetMatch.bom       ##r0   c                    | j         S rU   r^   rJ   s    r.   byte_order_markzCharsetMatch.byte_order_mark   r`   r0   c                $    d | j         D             S )z
        Return the complete list of possible languages found in decoded sequence.
        Usually not really useful. Returned list may be empty even if 'language' property return something != 'Unknown'.
        c                    g | ]
}|d          S )r    ).0es     r.   
<listcomp>z*CharsetMatch.languages.<locals>.<listcomp>   s    ...!...r0   r$   rJ   s    r.   r   zCharsetMatch.languages   s     /.do....r0   c                   | j         shd| j        v rdS ddlm}m} t          | j                  r || j                  n || j                  }t          |          dk    sd|v rdS |d         S | j         d         d         S )z
        Most probable language found in decoded sequence. If none were detected or inferred, the property will return
        "Unknown".
        asciiEnglishr   )encoding_languagesmb_encoding_languageszLatin BasedUnknown)r$   could_be_from_charsetcharset_normalizer.cdrm   rn   r   r6   r?   )r-   rm   rn   r   s       r.   languagezCharsetMatch.language   s      	  $444 y XWWWWWWW *$-887%%dm444''66  9~~""my&@&@ yQ<q!!$$r0   c                    | j         S rU   )r#   rJ   s    r.   r=   zCharsetMatch.chaos   s    $$r0   c                :    | j         sdS | j         d         d         S )Nr    r   r
   ri   rJ   s    r.   r>   zCharsetMatch.coherence   s#     	3q!!$$r0   c                4    t          | j        dz  d          S Nd      )ndigits)roundr=   rJ   s    r.   percent_chaoszCharsetMatch.percent_chaos   s    TZ#%q1111r0   c                4    t          | j        dz  d          S rv   )rz   r>   rJ   s    r.   percent_coherencezCharsetMatch.percent_coherence   s    T^c)15555r0   c                    | j         S )z+
        Original untouched bytes.
        )r!   rJ   s    r.   rE   zCharsetMatch.raw   s    
 }r0   list[CharsetMatch]c                    | j         S rU   )r'   rJ   s    r.   submatchzCharsetMatch.submatch   s
    |r0   c                2    t          | j                  dk    S Nr   )r?   r'   rJ   s    r.   has_submatchzCharsetMatch.has_submatch   s    4<  1$$r0   c                    | j         | j         S d t          |           D             }t          t          d |D                                 | _         | j         S )Nc                ,    g | ]}t          |          S re   )r   )rf   chars     r.   rh   z*CharsetMatch.alphabets.<locals>.<listcomp>   s     ,W,W,WT]4-@-@,W,W,Wr0   c                    h | ]}||S re   re   )rf   rs     r.   	<setcomp>z)CharsetMatch.alphabets.<locals>.<setcomp>   s    +L+L+L!!+LA+L+L+Lr0   )r&   r   sortedlist)r-   detected_rangess     r.   	alphabetszCharsetMatch.alphabets   s^    +'',W,WSQUYY,W,W,W%d+L+L+L+L+L&M&MNN##r0   c                6    | j         gd | j        D             z   S )z
        The complete list of encoding that output the exact SAME str result and therefore could be the originating
        encoding.
        This list does include the encoding available in property 'encoding'.
        c                    g | ]	}|j         
S re   )r6   )rf   ms     r.   rh   z6CharsetMatch.could_be_from_charset.<locals>.<listcomp>   s    "D"D"D!1:"D"D"Dr0   )r"   r'   rJ   s    r.   rp   z"CharsetMatch.could_be_from_charset   s%     "D"Dt|"D"D"DDDr0   utf_8r6   bytesc                <     j          j         |k    r| _         t                     } j        K j                                        dvr0t	          t
           fd|dd         d          }||dd         z   }|                    |d           _         j        S )z
        Method to get re-encoded bytes payload using given target encoding. Default to UTF-8.
        Any errors will be simply ignored by the encoder NOT replaced.
        N)zutf-8utf8r   c                &   | j         |                                 d         |                                 d                                      |                                 d         t	          j                                      dd                    S )Nr   r
   _-)stringspanreplacegroupsr   r*   )r   r-   s    r.   <lambda>z%CharsetMatch.output.<locals>.<lambda>   sh    ahqvvxx{QVVXXa['@AII

1!$"788@@cJJ  r0   i    r
   )countr   )r*   r   r,   lowerr   r   encoder)   )r-   r6   decoded_stringpatched_headers   `   r.   outputzCharsetMatch.output   s    
  (D,AX,M,M$,D! YYN,80668812 2 "%3    #5D5)" " " "0.2G!G#1#8#89#M#MD ##r0   intc                :    t          t          |                     S )z]
        Retrieve a hash fingerprint of the decoded payload, used for deduplication.
        )hashr   rJ   s    r.   r7   zCharsetMatch.fingerprint   s    
 CIIr0   )NN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r1   r2   r3   r   )r3   r   r3   r   )r1   r   r3   rN   )r3   rV   r3   r   )r3   r   )r3   r   )r   )r6   r   r3   r   r3   r   )__name__
__module____qualname__r/   r9   rC   propertyr@   rK   rM   rS   r6   r\   r_   rb   r   rr   r=   r>   r{   r}   rE   r   r   r   rp   r   r7   re   r0   r.   r   r      s        '+-1J J J J J8	Y 	Y 	Y 	Y( ( ( (, 0 0 0 X0    J J J J	# 	# 	# 	#    X 
 
 
 X
 $ $ $ X$ $ $ $ X$ / / / X/ % % % X%6 % % % X% % % % X%
 2 2 2 X2 6 6 6 X6    X    X % % % X% $ $ $ X$ E E E XE$ $ $ $ $:    X  r0   r   c                  \    e Zd ZdZdddZddZdd
ZddZddZddZ	ddZ
ddZddZdS ) CharsetMatchesz
    Container with every CharsetMatch items ordered by default from most probable to the less one.
    Act like a list(iterable) but does not implements all related methods.
    Nresultslist[CharsetMatch] | Nonec                D    |rt          |          ng | _        d| _        d S NT)r   _results
_is_sorted)r-   r   s     r.   r/   zCharsetMatches.__init__  s#    ?F,NF7OOOB $r0   r3   rN   c                X    | j         s"| j                                         d| _         d S d S r   )r   r   sortrJ   s    r.   _ensure_sortedzCharsetMatches._ensure_sorted  s5     	#M   "DOOO	# 	#r0   Iterator[CharsetMatch]c              #  L   K   |                                   | j        E d {V  d S rU   )r   r   rJ   s    r.   __iter__zCharsetMatches.__iter__  s8      =         r0   item	int | strr   c                    t          |t                    r!|                                  | j        |         S t          |t                    r't          |d          }| j        D ]}||j        v r|c S t          )z
        Retrieve a single item either by its position or encoding name (alias may be used here).
        Raise KeyError upon invalid index or encoding not present in results.
        F)r5   r   r   r   r   r   rp   KeyError)r-   r   results      r.   __getitem__zCharsetMatches.__getitem__  s    
 dC   	'!!!=&&dC   	"T5))D- " "6777!MMM 8r0   r   c                *    t          | j                  S rU   r?   r   rJ   s    r.   __len__zCharsetMatches.__len__'  s    4=!!!r0   r   c                2    t          | j                  dk    S r   r   rJ   s    r.   __bool__zCharsetMatches.__bool__*  s    4=!!A%%r0   c                   t          |t                    s4t          d                    t	          |j                                      t          |j                  t          k     rB| j	        D ]:}|j
        |j
        k    r(|j        |j        k    r|                    |            dS ;| j	                            |           d| _        dS )z~
        Insert a single match. Will be inserted accordingly to preserve sort.
        Can be inserted as a submatch.
        z-Cannot append instance '{}' to CharsetMatchesNF)r5   r   r;   rP   r   rQ   r?   rE   r   r   r7   r=   rS   rR   r   )r-   r   matchs      r.   rR   zCharsetMatches.append-  s    
 $-- 	?FF''    tx==+++  $(888U[DJ=V=V&&t,,,FFT"""r0   CharsetMatch | Nonec                V    | j         sdS |                                  | j         d         S )zQ
        Simply return the first match. Strict equivalent to matches[0].
        Nr   )r   r   rJ   s    r.   bestzCharsetMatches.bestA  s2     } 	4}Qr0   c                *    |                                  S )zP
        Redundant method, call the method best(). Kept for BC reasons.
        )r   rJ   s    r.   firstzCharsetMatches.firstJ  s     yy{{r0   rU   )r   r   )r3   rN   )r3   r   )r   r   r3   r   r   r   )r   r   r3   rN   )r3   r   )r   r   r   __doc__r/   r   r   r   r   r   rR   r   r   re   r0   r.   r   r     s         
% % % % %# # # #
! ! ! !   " " " "& & & &       (            r0   r   c                  6    e Zd ZddZedd            ZddZdS )CliDetectionResultpathr   r6   r   r\   rV   alternative_encodingsrr   r   r   r   r=   r   r>   unicode_pathis_preferredc                    || _         |
| _        || _        || _        || _        || _        || _        || _        || _        |	| _	        || _
        d S rU   )r   r   r6   r\   r   rr   r   r   r=   r>   r   )r-   r   r6   r\   r   rr   r   r   r=   r>   r   r   s               r.   r/   zCliDetectionResult.__init__V  s\     	(4$,+;0E"%$-$2!
 )".r0   r3   dict[str, Any]c                    | j         | j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
        dS )Nr   r6   r\   r   rr   r   r   r=   r>   r   r   r   rJ   s    r.   __dict__zCliDetectionResult.__dict__p  sO     I $ 5%)%?"1Z - -
 
 	
r0   c                4    ddl m}  || j        dd          S )Nr   )dumpsT   )ensure_asciiindent)jsonr   r   )r-   r   s     r.   to_jsonzCliDetectionResult.to_json  s-    uT]a@@@@r0   N)r   r   r6   r   r\   rV   r   rV   rr   r   r   rV   r   r   r=   r   r>   r   r   r   r   r   )r3   r   r   )r   r   r   r/   r   r   r   re   r0   r.   r   r   U  sb        / / / /4 
 
 
 X
A A A A A Ar0   r   N)
__future__r   encodings.aliasesr   rer   typingr   r   r   r	   constantr   r   utilsr   r   r   r   r   r   r   CoherenceMatchr   r   re   r0   r.   <module>r      sN   " " " " " " % % % % % %       - - - - - - - - - - - - G G G G G G G G C C C C C C C C C Cw w w w w w w wtI I I I I I I IX sEz"' .A .A .A .A .A .A .A .A .A .Ar0   