a
    Bù’if1  ã                   @  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  sš   ddddddœ}|du rd}n(|dkr,d}n|dkr:d	}nt d
| ƒ‚tj | | ¡ tƒ  ¡ }|durx|dkrx|| S ||v rˆ|| S tj d¡ qFdS )aÝ  Ask a yes/no question via input() and return their answer.

    "question" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
        It must be "yes" (the default), "no" or None (meaning
        an answer is required of the user).

    The "answer" return value is True for "yes" or False for "no".

    Credit goes to (c) https://stackoverflow.com/questions/3041986/apt-command-line-interface-like-yes-no-input
    TF)r   ÚyZyeÚnoÚnNz [y/n] r   z [Y/n] r   z [y/N] zinvalid default answer: '%s'Ú z3Please respond with 'yes' or 'no' (or 'y' or 'n').
)Ú
ValueErrorÚsysÚstdoutÚwriteÚinputÚlower)r   r   ZvalidÚpromptÚchoice© r    ú\/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/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__E   s    zFileType.__init__z	typing.IO)Ústringr   c              
     sÊ   |dkrldˆ j v r*dˆ j v r$tjjS tjS t‡ fdd„dD ƒƒrXdˆ j v rRtjjS tjS dˆ j › �}t|ƒ‚zt|ˆ j ˆ jˆ j	ˆ j
ƒW S  tyÄ } z&d|› d	|› �}t |¡‚W Y d }~n
d }~0 0 d S )
Nú-r$   Úbc                 3  s   | ]}|ˆ j v V  qd S r+   )r,   )Ú.0Úc©r0   r    r!   Ú	<genexpr>V   ó    z$FileType.__call__.<locals>.<genexpr>Úwaxzargument "-" with mode zcan't open 'z': )r,   r   ÚstdinÚbufferÚanyr   r   Úopenr-   r.   r/   ÚOSErrorÚargparseÚArgumentTypeError)r0   r2   ÚmsgÚeÚmessager    r7   r!   Ú__call__Q   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)r5   Úargr    r    r!   Ú
<listcomp>g   r9   z%FileType.__repr__.<locals>.<listcomp>c                 S  s&   g | ]\}}|d ur|› d|›�‘qS )Nú=r    )r5   ÚkwrH   r    r    r!   rI   h   r9   ú(ú))r,   r-   r.   r/   r   ÚtypeÚ__name__)r0   ÚargsÚkwargsÚargs_strr    r    r!   Ú__repr__c   s    ÿÿzFileType.__repr__)r$   r%   NN)rO   Ú
__module__Ú__qualname__Ú__doc__r1   rE   rS   r    r    r    r!   r#   2   s       ûr#   zlist[str] | Noner&   )Úargvr   c           
        sD  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 �rZ|jdu �rZ|j�rH|jD ]‰ˆ ¡  �q8t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 �]Ü‰tˆ|j|j|jdu d>�}| ¡ ‰ ˆ d?u �rztd@ ˆj|jd<k �r@dAndB¡tjd8� | ttˆjƒd?g g dCg dd<d;d?d6ƒ¡ �nB| ttˆjƒˆ jˆ j ‡ fdDdE„ˆ j!D ƒˆ j"ˆ j#ˆ j$ˆ j%ˆ j&d?d6ƒ¡ t'|ƒd9k�r2|j(�r2|D ]V‰ˆˆ k�rÚ| ttˆjƒˆjˆj ‡fdFdE„ˆj!D ƒˆj"ˆj#ˆj$ˆj%ˆj&d?dƒ¡ �qÚ|jd6u �r¼ˆ 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ôzZt1|dI 1|¡ƒ|dM _2t3|dM j2dNƒ�}| 4ˆ  5¡ ¡ W d?  ƒ n1 �s^0    Y  W nP t6�yº }	 z6tt7|	ƒtjd8� ˆj*du �r¢ˆ ¡  W Y d?}	~	 dOS d?}	~	0 0 ˆj*du �rôˆ ¡  �qô|j8du �rtt9t'|ƒd9k�r dPdE„ |D ƒn|dM j:d6dQdR�ƒ n*|jD ]"‰tdS 1‡fdTdE„|D ƒ¡ƒ �qdMS )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)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.)r`   r   rN   ra   r]   z	--versionÚversionz;Charset-Normalizer {} - Python {} - Unicode {} - SpeedUp {}z.pyZOFFÚONz"Show version information and exit.)r`   rj   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.)ri   ÚexplainZpreemptive_behaviourNz4Unable to identify originating encoding for "{}". {}z-Maybe try increasing maximum amount of chaos.r   ÚUnknownc                   s   g | ]}|ˆ j kr|‘qS r    ©r)   ©r5   Úcp)Ú
best_guessr    r!   rI     s   
þzcli_detect.<locals>.<listcomp>c                   s   g | ]}|ˆ j kr|‘qS r    rp   rq   )Úelr    r!   rI   %  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   r   Úwbé   c                 S  s   g | ]
}|j ‘qS r    )Ú__dict__©r5   rt   r    r    r!   rI   h  r9   é   )Úensure_asciiÚindentrF   c                   s&   g | ]}|j tˆ jƒkr|jp d ‘qS )Ú	undefined)Úpathr   Únamer)   rz   )Úmy_filer    r!   rI   q  s   þ);r@   ÚArgumentParserÚadd_argumentr#   ÚfloatÚformatr   r	   r
   Ú	md_moduleÚ__file__r   ÚendswithÚ
parse_argsre   rc   rY   ÚcloseÚprintr   Ústderrrf   ri   r   r_   rg   Úbestr€   Úappendr   r   r)   Zencoding_aliasesZcould_be_from_charsetÚlanguageZ	alphabetsÚbomZpercent_chaosZpercent_coherenceÚlenrb   Ú
startswithÚclosedr   r   r   ÚsplitÚinsertr"   r   Zunicode_pathr>   r   Úoutputr?   r   rd   r   ry   )
rW   ÚparserrP   Zx_ÚmatchesZdir_pathÚ	file_nameZo_ÚfprC   r    )rs   rt   r�   r!   Ú
cli_detectm   sÐ   ÿÿú	úúúúúúù	ü÷



ü
ÿû÷õÿ
þñÿ

þñÿÿü

ÿÿüúþ
2$ýÿ

þÿÿ
r›   Ú__main__)r   )N)Ú
__future__r   r@   r   ÚtypingÚjsonr   Úos.pathr   r   r   r   r   Úplatformr	   Úunicodedatar
   Zcharset_normalizer.mdÚmdr†   Zcharset_normalizerr   Zcharset_normalizer.modelsr   Zcharset_normalizer.versionr   r"   r#   r›   rO   r    r    r    r!   Ú<module>   s$   !;  