U
    —ùi¤.  ã                   @  sÚ   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m	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ dddddœdd„ZG dd„ dƒZddddœdd„ZedkrÖeƒ  dS )é    )ÚannotationsN)Údumps)ÚabspathÚbasenameÚdirnameÚjoinÚrealpath)Úpython_version)Úunidata_version)Úfrom_fp)ÚCliDetectionResult)Ú__version__ÚyesÚstrÚbool)ÚquestionÚdefaultÚreturnc                 C  sV   |dkrdnd}t | | ƒ ¡  ¡ }|s0|dkS |dkr<dS |dkrHdS tdƒ qd	S )
zBAsk a yes/no question via input() and return the answer as a bool.r   z [Y/n] z [y/N] )Úyr   T)ÚnÚnoFzPlease respond with 'y' or 'n'.N)ÚinputÚstripÚlowerÚprint)r   r   ÚpromptÚchoice© r   úC/tmp/pip-unpacked-wheel-mwc9rwws/charset_normalizer/cli/__main__.pyÚquery_yes_no   s    r   c                   @  sD   e Zd ZdZddddddœd	d
„Zdddœdd„Zddœdd„ZdS )ÚFileTypeaè  Factory for creating file object types

    Instances of FileType are typically passed as type= arguments to the
    ArgumentParser add_argument() method.

    Keyword Arguments:
        - mode -- A string indicating how the file is to be opened. Accepts the
            same values as the builtin open() function.
        - bufsize -- The file's desired buffer size. Accepts the same values as
            the builtin open() function.
        - encoding -- The file's encoding. Accepts the same values as the
            builtin open() function.
        - errors -- A string indicating how encoding and decoding errors are to
            be handled. Accepts the same value as the builtin open() function.

    Backported from CPython 3.12
    ÚréÿÿÿÿNr   Úintz
str | None)ÚmodeÚbufsizeÚencodingÚerrorsc                 C  s   || _ || _|| _|| _d S ©N)Ú_modeÚ_bufsizeÚ	_encodingÚ_errors)Úselfr$   r%   r&   r'   r   r   r   Ú__init__3   s    zFileType.__init__z	typing.IO)Ústringr   c              
     sÈ   |dkrldˆ j kr*dˆ j kr$tjjS tjS t‡ fdd„dD ƒƒrXdˆ j krRtjjS tjS dˆ j › �}t|ƒ‚zt|ˆ j ˆ jˆ j	ˆ j
ƒW S  tk
rÂ } zd|› d	|› �}t |¡‚W 5 d }~X Y nX d S )
Nú-r!   Úbc                 3  s   | ]}|ˆ j kV  qd S r(   )r)   )Ú.0Úc©r-   r   r   Ú	<genexpr>D   s     z$FileType.__call__.<locals>.<genexpr>Zwaxzargument "-" with mode zcan't open 'z': )r)   ÚsysÚstdinÚbufferÚanyÚstdoutÚ
ValueErrorÚopenr*   r+   r,   ÚOSErrorÚargparseÚArgumentTypeError)r-   r/   ÚmsgÚeÚmessager   r4   r   Ú__call__?   s    
zFileType.__call__)r   c                 C  sX   | j | jf}d| jfd| jfg}d dd„ |D ƒdd„ |D ƒ ¡}t| ƒj› d|› d�S )	Nr&   r'   ú, c                 S  s   g | ]}|d krt |ƒ‘qS )r"   )Úrepr)r2   Úargr   r   r   Ú
<listcomp>U   s      z%FileType.__repr__.<locals>.<listcomp>c                 S  s&   g | ]\}}|d k	r|› d|›�‘qS )Nú=r   )r2   ÚkwrF   r   r   r   rG   V   s      ú(ú))r)   r*   r+   r,   r   ÚtypeÚ__name__)r-   ÚargsÚkwargsÚargs_strr   r   r   Ú__repr__Q   s    ÿÿzFileType.__repr__)r!   r"   NN)rM   Ú
__module__Ú__qualname__Ú__doc__r.   rC   rQ   r   r   r   r   r        s       ûr    zlist[str] | Noner#   )Úargvr   c                   s,  t jdd�}|jdtdƒddd� |jdd	d
dddd� |jddd
dddd� |jddd
dddd� |jddd
dddd� |jddd
dddd� |jdd d
dd!d"d� |jd#d$d
dd%d&d� |jd'd(d)d*td+d,d-� |jd.d/d0 ttƒ tt	j
 ¡  d1¡rüd2nd3¡d4d5� | | ¡}|jd6k�rZ|jdk�rZ|j�rH|jD ]‰ˆ ¡  �q8td7tjd8� d9S |jd6k�r¢|jdk�r¢|j�r�|jD ]‰ˆ ¡  �q€td:tjd8� d9S |jd;k �sº|jd<k�rê|j�rØ|jD ]‰ˆ ¡  �qÈtd=tjd8� d9S g }|jD �]Ä‰tˆ|j|j|jdkd>�}| ¡ ‰ ˆ d?k�rztd@ ˆj|jd<k �r@dAndB¡tjd8� | ttˆjƒd?g g dCg dd<d;d?d6ƒ¡ �n*ttˆjƒˆ jˆ j ‡ fdDdE„ˆ j!D ƒˆ j"ˆ j#ˆ j$ˆ j%ˆ j&d?d6ƒ}| |¡ t'|ƒd9k�r6|j(�r6|D ]V‰ˆˆ k�rÞ| ttˆjƒˆjˆj ‡fdFdE„ˆj!D ƒˆj"ˆj#ˆj$ˆj%ˆj&d?dƒ¡ �qÞ|jd6k�r¤ˆ j )dG¡d6k�r‚tdH ˆj¡tjd8� ˆj*dk�rôˆ ¡  �qôt+t,ˆjƒƒ}t-t,ˆjƒƒ}| .dI¡}|jdk�rØ| /dJˆ j¡ ˆj*dk�rˆ ¡  n<|jdk�rt0dK ˆj¡dLƒdk�rˆj*dk�rôˆ ¡  �qôz<t1|dI 1|¡ƒ|_2t3|j2dMƒ�}	|	 4ˆ  5¡ ¡ W 5 Q R X W nR t6k
�r¢ }
 z2tt7|
ƒtjd8� ˆj*dk�rŠˆ ¡  W Y ¢ dNS d?}
~
X Y nX ˆj*dk�rôˆ ¡  �qô|j8dk�rþtt9t'|ƒd9k�rèdOdE„ |D ƒn|dP j:d6dQdR�ƒ n*|jD ]"‰tdS 1‡fdTdE„|D ƒ¡ƒ �qdPS )Uzˆ
    CLI assistant using ARGV and ArgumentParser
    :param argv:
    :return: 0 if everything is fine, anything else equal trouble
    zvThe Real First Universal Charset Detector. Discover originating encoding used on text file. Normalize text to unicode.)ÚdescriptionÚfilesÚrbú+zFile(s) to be analysed)rL   ÚnargsÚhelpz-vz	--verboseÚ
store_trueFÚverbosezjDisplay complementary information about file if any. Stdout will contain logs about the detection process.)Úactionr   Údestr[   z-az--with-alternativeÚalternativeszIOutput complementary possibilities if any. Top-level JSON WILL be a list.z-nz--normalizeÚ	normalizezLPermit to normalize input file. If not set, program does not write anything.z-mz	--minimalÚminimalzBOnly output the charset detected to STDOUT. Disabling JSON output.z-rz	--replaceÚreplacezGReplace file when trying to normalize it instead of creating a new one.z-fz--forceÚforcezHReplace file without asking if you are sure, use this flag with caution.z-iz--no-preemptiveÚno_preemptivez>Disable looking at a charset declaration to hint the detector.z-tz--thresholdÚstoregš™™™™™É?Ú	thresholdzUDefine a custom maximum amount of noise allowed in decoded content. 0. <= noise <= 1.)r^   r   rL   r_   r[   z	--versionÚversionz;Charset-Normalizer {} - Python {} - Unicode {} - SpeedUp {}z.pyZOFFÚONz"Show version information and exit.)r^   rh   r[   Tz.Use --replace in addition of --normalize only.)Úfileé   z*Use --force in addition of --replace only.g        g      ð?z---threshold VALUE should be between 0. AND 1.)rg   ÚexplainZpreemptive_behaviourNz4Unable to identify originating encoding for "{}". {}z-Maybe try increasing maximum amount of chaos.Ú ÚUnknownc                   s   g | ]}|ˆ j kr|‘qS r   ©r&   ©r2   Úcp)Ú
best_guessr   r   rG   û   s   
þzcli_detect.<locals>.<listcomp>c                   s   g | ]}|ˆ j kr|‘qS r   ro   rp   )Úelr   r   rG     s   
þÚutfzJ"{}" file does not need to be normalized, as it already came from unicode.Ú.r"   z0Are you sure to normalize "{}" by replacing it ?r   Úwbé   c                 S  s   g | ]
}|j ‘qS r   )Ú__dict__©r2   rs   r   r   r   rG   U  s     r   é   )Úensure_asciiÚindentrD   c                   s&   g | ]}|j tˆ jƒkr|jp d ‘qS )Z	undefined)Úpathr   Únamer&   ry   )Úmy_filer   r   rG   ^  s   þ);r>   ÚArgumentParserÚadd_argumentr    ÚfloatÚformatr   r	   r
   Ú	md_moduleÚ__file__r   ÚendswithÚ
parse_argsrc   ra   rW   Úcloser   r6   Ústderrrd   rg   r   r]   re   Úbestr~   Úappendr   r   r&   Zencoding_aliasesZcould_be_from_charsetÚlanguageZ	alphabetsÚbomZpercent_chaosZpercent_coherenceÚlenr`   Ú
startswithÚclosedr   r   r   ÚsplitÚinsertr   r   Zunicode_pathr<   ÚwriteÚoutputr=   r   rb   r   rx   )rU   ÚparserrN   Zx_ÚmatchesZ
cli_resultZdir_pathÚ	file_nameZo_ÚfprA   r   )rr   rs   r   r   Ú
cli_detect[   sÔ   ÿ   ÿú	úúúúúúù	ü÷



ü
ÿû÷õÿ
þñ


þñÿÿü

ÿÿüúþ
$ýÿ

þÿÿ
r™   Ú__main__)r   )N)Ú
__future__r   r>   r6   ÚtypingÚjsonr   Úos.pathr   r   r   r   r   Úplatformr	   Úunicodedatar
   Zcharset_normalizer.mdZmdr„   Zcharset_normalizerr   Zcharset_normalizer.modelsr   Zcharset_normalizer.versionr   r   r    r™   rM   r   r   r   r   Ú<module>   s$   ;  