o
    ßý°j­.  ã                   @  sÄ   d dl mZ d dlZd dlZd dl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„ZG dd„ dƒZdddd„Zedkr`eƒ  dS dS )é    )ÚannotationsN)ÚabspathÚbasenameÚdirnameÚjoinÚrealpath)Úpython_version)Úunidata_version)Úfrom_fp)ÚCliDetectionResult)Ú__version__ÚyesÚquestionÚstrÚdefaultÚreturnÚboolc                 C  sT   |dkrdnd}	 t | | ƒ ¡  ¡ }|s|dkS |dv rdS |dv r%dS tdƒ q	)	zBAsk a yes/no question via input() and return the answer as a bool.r   z [Y/n] z [y/N] T)Úyr   )ÚnÚnoFzPlease respond with 'y' or 'n'.)ÚinputÚstripÚlowerÚprint)r   r   ÚpromptÚchoice© r   ú’/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/charset_normalizer/cli/__main__.pyÚquery_yes_no   s   ør   c                   @  s8   e Zd ZdZ				dddd„Zddd„Z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éÿÿÿÿNÚmoder   ÚbufsizeÚintÚencodingú
str | NoneÚerrorsc                 C  s   || _ || _|| _|| _d S ©N)Ú_modeÚ_bufsizeÚ	_encodingÚ_errors)Úselfr"   r#   r%   r'   r   r   r   Ú__init__2   s   
zFileType.__init__Ústringr   ú	typing.IOc              
     sº   |dkr6dˆ j v rdˆ j v rtjjS tjS t‡ fdd„dD ƒƒr,dˆ j v r)tjjS tjS dˆ j › �}t|ƒ‚zt|ˆ j ˆ jˆ j	ˆ j
ƒW S  ty\ } zd|› d	|› �}t |¡‚d }~ww )
Nú-r    Úbc                 3  s   � | ]}|ˆ j v V  qd S r(   )r)   )Ú.0Úc©r-   r   r   Ú	<genexpr>C   s   € z$FileType.__call__.<locals>.<genexpr>Ú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   r5   r   Ú__call__>   s   

€þzFileType.__call__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)r3   Úargr   r   r   Ú
<listcomp>T   s    z%FileType.__repr__.<locals>.<listcomp>c                 S  s&   g | ]\}}|d ur|› d|›�‘qS )Nú=r   )r3   ÚkwrH   r   r   r   rI   U   s   & ú(ú))r)   r*   r+   r,   r   ÚtypeÚ__name__)r-   ÚargsÚkwargsÚargs_strr   r   r   Ú__repr__P   s   ÿÿzFileType.__repr__)r    r!   NN)r"   r   r#   r$   r%   r&   r'   r&   )r/   r   r   r0   )r   r   )rO   Ú
__module__Ú__qualname__Ú__doc__r.   rE   rS   r   r   r   r   r      s    û
r   Úargvúlist[str] | Noner$   c                   s0  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d6u r©|jdu r©|jr |jD ]‰ˆ ¡  q™td7tjd8� d9S |jd6u rÉ|jdu 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 �]i‰tˆ|j|j|jdu d>�}| ¡ ‰ ˆ d?u �r1t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�r�|j(�r�|D ]+‰ˆˆ k�r�| ttˆjƒˆjˆj ‡fdFdE„ˆj!D ƒˆj"ˆj#ˆj$ˆj%ˆj&d?dƒ¡ �qc|jd6u �rNˆ j )dG¡d6u �r´tdH ˆj¡tjd8� ˆj*du �r³ˆ ¡  qît+t,ˆjƒƒ}t-t,ˆjƒƒ}| .dI¡}|jdu �rß| /dJˆ j¡ ˆj*du �rÞˆ ¡  n|jdu �rüt0dK ˆj¡dLƒdu �rüˆj*du �rûˆ ¡  qîz)t1|dI 1|¡ƒ|_2t3|j2dMƒ�}	|	 4ˆ  5¡ ¡ W d?  ƒ n	1 �sw   Y  W n( t6�yM }
 ztt7|
ƒtjd8� ˆj*du �rAˆ ¡  W Y d?}
~
 dNS d?}
~
ww ˆj*du �rXˆ ¡  qî|j8du �r�dOdPl9m:} t|t'|ƒd9k�rudQdE„ |D ƒn|dO j;d6dRdS�ƒ dOS |jD ]‰tdT 1‡fdUdE„|D ƒ¡ƒ �q„dOS )Vzˆ
    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)rN   Ú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.)ra   r   rN   rb   r^   z	--versionÚversionz;Charset-Normalizer {} - Python {} - Unicode {} - SpeedUp {}z.pyÚOFFÚONz"Show version information and exit.)ra   rk   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.)rj   ÚexplainÚpreemptive_behaviourNz4Unable to identify originating encoding for "{}". {}z-Maybe try increasing maximum amount of chaos.Ú ÚUnknownc                   ó   g | ]	}|ˆ j kr|‘qS r   ©r%   ©r3   Úcp)Ú
best_guessr   r   rI   ú   ó
    
þzcli_detect.<locals>.<listcomp>c                   rt   r   ru   rv   )Úelr   r   rI     ry   Ú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é   r   )Údumpsc                 S  s   g | ]}|j ‘qS r   )Ú__dict__©r3   rz   r   r   r   rI   V  s    é   )Úensure_asciiÚindentrF   c                   s&   g | ]}|j tˆ jƒkr|jpd ‘qS )Ú	undefined)Úpathr   Únamer%   r�   )Úmy_filer   r   rI   _  s
    þ)<r@   ÚArgumentParserÚadd_argumentr   ÚfloatÚformatr   r   r	   Ú	md_moduleÚ__file__r   ÚendswithÚ
parse_argsrf   rd   rZ   Úcloser   r8   Ústderrrg   rj   r
   r`   rh   Úbestr‡   Úappendr   r   r%   Úencoding_aliasesÚcould_be_from_charsetÚlanguageÚ	alphabetsÚbomÚpercent_chaosÚpercent_coherenceÚlenrc   Ú
startswithÚclosedr   r   r   ÚsplitÚinsertr   r   Úunicode_pathr>   ÚwriteÚoutputr?   r   re   Újsonr   r€   )rW   ÚparserrP   Úx_ÚmatchesÚ
cli_resultÚdir_pathÚ	file_nameÚo_ÚfprC   r   r   )rx   rz   rˆ   r   Ú
cli_detectZ   sÜ  ÿÿú	úúúúúúù	ü÷






ü
ÿû÷õÿ
þñ


þñÿ€ÿü
€ÿüúÿ€€ü€$ýÿ
õ
þÿÿ
r­   Ú__main__)r   )r   r   r   r   r   r   r(   )rW   rX   r   r$   )Ú
__future__r   r@   r8   ÚtypingÚos.pathr   r   r   r   r   Úplatformr   Úunicodedatar	   Úcharset_normalizer.mdÚmdr�   Úcharset_normalizerr
   Úcharset_normalizer.modelsr   Úcharset_normalizer.versionr   r   r   r­   rO   r   r   r   r   Ú<module>   s&    ;  
ÿ