a
    Lcòf¤r  ã                   @   sP  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
Z
d dlmZ d dlmZ d dlmZ d dlmZmZ d d	l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$ e dej%ej&B ƒZ'd Z(e)ƒ Z*dd„ Z+dd„ Z,eG dd„ dƒƒZ-G dd„ dƒZ.dd„ Z/dd„ Z0G dd„ deƒZ1dS )é    N)Útotal_ordering)Ú	dropwhile)ÚPath)Úsettings)ÚImproperlyConfigured)ÚNamedTemporaryFile)ÚBaseCommandÚCommandError)Úfind_commandÚhandle_extensionsÚis_ignored_pathÚpopen_wrapper)ÚDEFAULT_LOCALE_ENCODING)Úcached_property)Úprepare_js_for_gettext)Ú_lazy_re_compile)Úget_text_list)Ú
templatizez$^(?P<value>"Plural-Forms.+?\\n")\s*$c                  G   s&   | D ]}t |ƒd u rtd| ƒ‚qd S )NzLCan't find %s. Make sure you have GNU gettext tools 0.15 or newer installed.)r
   r	   )ZprogramsÚprogram© r   úi/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/core/management/commands/makemessages.pyÚcheck_programs"   s    ÿÿr   c                 C   s   t  d| ¡pt  d| ¡S )Nz^[a-z]+$z^[a-z]+_[A-Z].*$)ÚreÚmatch)Úlocaler   r   r   Úis_valid_locale+   s    r   c                   @   s8   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zed	d
„ ƒZdS )ÚTranslatableFilec                 C   s   || _ || _|| _d S ©N)ÚfileÚdirpathÚ
locale_dir)Úselfr   Ú	file_namer    r   r   r   Ú__init__1   s    zTranslatableFile.__init__c                 C   s    d| j jtj | j| jg¡f S )Nz<%s: %s>)Ú	__class__Ú__name__ÚosÚsepÚjoinr   r   ©r!   r   r   r   Ú__repr__6   s    þzTranslatableFile.__repr__c                 C   s   | j |j kS r   ©Úpath©r!   Úotherr   r   r   Ú__eq__<   s    zTranslatableFile.__eq__c                 C   s   | j |j k S r   r+   r-   r   r   r   Ú__lt__?   s    zTranslatableFile.__lt__c                 C   s   t j | j| j¡S r   )r&   r,   r(   r   r   r)   r   r   r   r,   B   s    zTranslatableFile.pathN)	r%   Ú
__module__Ú__qualname__r#   r*   r/   r0   Úpropertyr,   r   r   r   r   r   /   s   r   c                   @   sT   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZd
d„ Z	dd„ Z
dd„ ZdS )Ú	BuildFilezN
    Represent the state of a translatable file during the build process.
    c                 C   s   || _ || _|| _d S r   )ÚcommandÚdomainÚtranslatable)r!   r5   r6   r7   r   r   r   r#   L   s    zBuildFile.__init__c                 C   s@   | j dkr| jjdk S | j dkr<tj | jj¡d }|dkS dS )NÚdjangojs)r   é   é   Údjangoé   z.pyF)r6   r5   Úgettext_versionr&   r,   Úsplitextr7   r   )r!   Úfile_extr   r   r   Úis_templatizedQ   s    

zBuildFile.is_templatizedc                 C   s   | j jS r   )r7   r,   r)   r   r   r   r,   Z   s    zBuildFile.pathc                 C   s@   | j s| jS dddœ | j¡}d| jj|f }tj | jj|¡S )z•
        Path to a file which is being fed into GNU gettext pipeline. This may
        be either a translatable or its preprocessed version.
        ÚcÚpy©r8   r;   z%s.%s)	r@   r,   Úgetr6   r7   r   r&   r(   r   )r!   Ú	extensionÚfilenamer   r   r   Ú	work_path^   s    þýzBuildFile.work_pathc                 C   s²   | j s
dS t| jdd��}| ¡ }W d  ƒ n1 s60    Y  | jdkrTt|ƒ}n | jdkrtt|| jdd… d�}t| jddd��}| |¡ W d  ƒ n1 s¤0    Y  dS )	zz
        Preprocess (if necessary) a translatable file before passing it to
        xgettext GNU gettext utility.
        Núutf-8©Úencodingr8   r;   é   )ÚoriginÚw)	r@   Úopenr,   Úreadr6   r   r   rG   Úwrite)r!   ÚfpZsrc_dataÚcontentr   r   r   Ú
preprocessm   s    &


zBuildFile.preprocessc                    sj   | j s
|S tjdkr"| j‰| j‰ n| jdd… ‰| jdd… ‰ tjdt ˆ¡ d ‡ ‡fdd„|tjd�S )	zß
        Postprocess messages generated by xgettext GNU gettext utility.

        Transform paths as if these messages were generated from original
        translatable files rather than from preprocessed versions.
        ÚntrK   Nz	^(#: .*)(ú)c                    s   | d   ˆˆ ¡S )Nr   )Úreplace©r   ©Únew_pathÚold_pathr   r   Ú<lambda>•   ó    z0BuildFile.postprocess_messages.<locals>.<lambda>)Úflags)	r@   r&   ÚnamerG   r,   r   ÚsubÚescapeÚ	MULTILINE)r!   Úmsgsr   rX   r   Úpostprocess_messages€   s    
üzBuildFile.postprocess_messagesc                 C   s$   | j r tj | j¡r t | j¡ dS )zM
        Remove a preprocessed copy of a translatable file (if any).
        N)r@   r&   r,   ÚexistsrG   Úunlinkr)   r   r   r   Úcleanupš   s    zBuildFile.cleanupN)r%   r1   r2   Ú__doc__r#   r   r@   r,   rG   rS   rc   rf   r   r   r   r   r4   G   s   


r4   c                 C   s(   |   ¡ }|r|d r| d¡ d |¡S )a  
    Take a block of raw text that will be passed through str.splitlines() to
    get universal newlines treatment.

    Return the resulting block of text with normalized `
` EOL sequences ready
    to be written to disk using current platform's native EOLs.
    éÿÿÿÿÚ Ú
)Ú
splitlinesÚappendr(   )Zraw_contentsZ
lines_listr   r   r   Únormalize_eols¦   s    
rm   c                 C   s´   |  ¡ }tj | ¡r tt|ƒ}nLg }d\}}|D ]:}|sT|sTd|v rTd}| dd¡}|s`|s`d}| |¡ q0d |¡}t	| dddd��}| 
|¡ W d	  ƒ n1 s¦0    Y  d	S )
z\
    Write the `potfile` with the `msgs` contents, making sure its format is
    valid.
    )FFzcharset=CHARSETTzcharset=UTF-8rj   ÚarH   )rJ   ÚnewlineN)rk   r&   r,   rd   r   ÚlenrV   rl   r(   rN   rP   )Úpotfilerb   Z	pot_linesÚlinesÚfoundZheader_readÚlinerQ   r   r   r   Úwrite_pot_fileµ   s     
ru   c                   @   s˜   e Zd ZdZeZeZg Zg d¢Z	dgZ
dgZddgZdd„ Zd	d
„ Zedd„ ƒZe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 )ÚCommandaH  Runs over the entire source tree of the current directory and pulls out all strings marked for translation. It creates (or updates) a message file in the conf/locale (in the django tree) or locale (for projects and applications) directory.

You must run this command with one of either the --locale, --exclude, or --all options.)z-qz--backup=nonez
--previousz--updatez--to-code=utf-8ú--no-obsoletez--from-code=UTF-8z--add-comments=Translatorsc              	   C   sô   |j ddg ddd� |j ddg ddd� |j d	d
ddd� |j ddddd� |j dddddd� |j ddddd� |j ddddg ddd � |j d!d"d#d$d%� |j d&dd'd� |j d(dd)d� |j d*d+d,d-d.d/� |j d0dd1d� |j d2dd3d� d S )4Nz--localez-lrl   zfCreates or updates the message files for the given locale(s) (e.g. pt_BR). Can be used multiple times.)ÚdefaultÚactionÚhelpz	--excludez-xz@Locales to exclude. Default is none. Can be used multiple times.z--domainú-dr;   z4The domain of the message files (default: "django").)rx   rz   z--allz-aÚ
store_truez3Updates the message files for all existing locales.)ry   rz   z--extensionz-eÚ
extensionsz£The file extension(s) to examine (default: "html,txt,py", or "js" if the domain is "djangojs"). Separate multiple extensions with commas, or use -e multiple times.)Údestry   rz   z
--symlinksz-szaFollows symlinks to directories when examining source code and templates for translation strings.z--ignorez-iÚignore_patternsZPATTERNz`Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.)ry   r~   rx   Úmetavarrz   z--no-default-ignoreÚstore_falseÚuse_default_ignore_patternszJDon't ignore the common glob-style patterns 'CVS', '.*', '*~' and '*.pyc'.)ry   r~   rz   ú	--no-wrapz2Don't break long message lines into several lines.ú--no-locationz%Don't write '#: filename:line' lines.z--add-location)Úfullr   Úneverr…   ú?a'  Controls '#: filename:line' lines. If the option is 'full' (the default if not given), the lines  include both file name and line number. If it's 'file', the line number is omitted. If it's 'never', the lines are suppressed (same as --no-location). --add-location requires gettext 0.19 or newer.)ÚchoicesÚconstÚnargsrz   rw   z Remove obsolete message strings.z
--keep-potz<Keep .pot file after making messages. Useful when debugging.)Úadd_argument)r!   Úparserr   r   r   Úadd_argumentsã   s¢    ú
ûüüû	üù
û	ýýúýýzCommand.add_argumentsc                    sÌ  |d }|d }|d | _ |d | _|d }|d }|d | _|d }|d	 rZ|g d
¢7 }tt|ƒƒ| _|d rÈ| jd d … dg | _| jd d … dg | _| jd d … dg | _| j	d d … dg | _	|d �r*| jd d … dg | _| jd d … dg | _| jd d … dg | _| j	d d … dg | _	|d �rÂ| j
dk �r^tdd dd„ | j
D ƒ¡ ƒ‚d|d  }| jd d … |g | _| jd d … |g | _| jd d … |g | _| j	d d … |g | _	|d | _|d | _| j dv�rêtdƒ‚| j dk�r|�p dg}	n|�pg d¢}	t|	ƒ| _|�s.|�s.|�r:| j d u �r^tdtj tjd ¡tjd f ƒ‚| jdk�r†| j d tt| jƒd!ƒ ¡ d"| _g | _d | _tj tj d#d¡¡�rÞtj tj d#d¡¡g| _| jd | _d$| _n\| j�rô| j t j!¡ tj d¡�r| j "tj d¡¡ | j�r:| jd | _tj#| jd$d%� t$ %d&¡‰ t&tjjt' 'd'| j ¡ƒ}
‡ fd(d)„t(tjj|
ƒD ƒ}|�r†|}n|�pŽ|}t|ƒ )|¡}|�r°t*d*d+d,ƒ t*d-ƒ zü|  +¡ }|D ]Ú}t,|ƒ�sjt$ -d.|¡}|�r.| .¡ }|d/  /¡ }|d0 d d1…  0¡ |d0 d1d …  }|› d2|› �}n| /¡ }t,|ƒ�rV| j d3||f ¡ n| j d4| ¡ �qÆ| jdk�r†| j d5| ¡ |D ]}|  1||¡ �qŠ�qÆW | j�sÈ|  2¡  n| j�sÆ|  2¡  0 d S )6Nr   Úexcluder6   Ú	verbosityÚallr}   Úsymlinksr   r‚   )ÚCVSz.*z*~z*.pycÚno_wraprƒ   Zno_locationr„   Zadd_location)r   é   zFThe --add-location option requires gettext 0.19 or later. You have %s.Ú.c                 s   s   | ]}t |ƒV  qd S r   )Ústr)Ú.0Úxr   r   r   Ú	<genexpr>c  r\   z!Command.handle.<locals>.<genexpr>z--add-location=%sÚno_obsoleteÚkeep_pot)r;   r8   zDcurrently makemessages only supports domains 'django' and 'djangojs'r8   Újs)ÚhtmlÚtxtrB   z(Type '%s help %s' for usage information.r   r<   z'examining files with the extensions: %sÚandFÚconfT©Úexist_okz[a-z]{2}z%s/*c                    s   g | ]}ˆ   |¡r|‘qS r   rW   )r—   Z	lang_code©Zlooks_like_localer   r   Ú
<listcomp>›  s   
þz"Command.handle.<locals>.<listcomp>ÚmsguniqÚmsgmergeÚ	msgattribÚxgettextzB^(?P<language>[a-zA-Z]+)(?P<separator>[^a-zA-Z])(?P<territory>.+)$ÚlanguageÚ	territoryrK   Ú_z#invalid locale %s, did you mean %s?zinvalid locale %szprocessing locale %s)3r6   r�   r‘   ÚlistÚsetr   Úmsgmerge_optionsÚmsguniq_optionsÚmsgattrib_optionsÚxgettext_optionsr=   r	   r(   rš   r›   r   r}   r&   r,   ÚbasenameÚsysÚargvÚstdoutrP   r   Úinvoked_for_djangoÚlocale_pathsÚdefault_locale_pathÚisdirÚabspathÚsettings_availableÚextendr   ÚLOCALE_PATHSrl   Úmakedirsr   ÚcompileÚfilterÚglobÚmapÚ
differencer   Úbuild_potfilesr   r   Ú	groupdictÚlowerÚupperÚwrite_po_fileÚremove_potfiles)r!   ÚargsÚoptionsr   rŽ   Zprocess_allr}   r   Zarg_add_locationÚextsZlocale_dirsZall_localesZlocalesÚpotfilesr   Zlocale_partsr©   rª   Zproposed_localerq   r   r£   r   ÚhandleF  sì    




ÿÿ

ÿ
ÿÿÿÿ
ÿ
þ

üÿÿ
þÿÿ
ÿzCommand.handlec                 C   sH   t ddgtd�\}}}t d|¡}|r<tdd„ | ¡ D ƒƒS tdƒ‚d S )Nr¨   z	--version)Zstdout_encodingz(\d+)\.(\d+)\.?(\d+)?c                 s   s   | ]}|d urt |ƒV  qd S r   )Úint)r—   Údr   r   r   r™   è  r\   z*Command.gettext_version.<locals>.<genexpr>z/Unable to get gettext version. Is it installed?)r   r   r   ÚsearchÚtupleÚgroupsr	   )r!   ÚoutÚerrÚstatusÚmr   r   r   r=   Þ  s    þzCommand.gettext_versionc                 C   s:   z
t j W n* ty4   | jdkr.| j d¡ Y dS 0 dS )Nr<   z$Running without configured settings.FT)r   r½   r   r�   ÚstderrrP   r)   r   r   r   r»   ì  s    

zCommand.settings_availablec           
   	   C   sì   |   d¡}|  ¡  |  |¡ g }| jD ]À}tj |d| j ¡}tj |¡sLq&dg| j	 |g }t
|ƒ\}}}|rœ|tkr†td| ƒ‚n| jdkrœ| j |¡ t|ƒ}t|ddd��}	|	 |¡ W d	  ƒ n1 sÒ0    Y  | |¡ q&|S )
z<
        Build pot files and apply msguniq to them.
        r•   ú%s.potr¥   z(errors happened while running msguniq
%sr   rM   rH   rI   N)Ú
find_filesrÉ   Úprocess_filesr·   r&   r,   r(   r6   rd   r¯   r   Ú	STATUS_OKr	   r�   rµ   rP   rm   rN   rl   )
r!   Ú	file_listrÍ   r,   rq   rÊ   rb   ÚerrorsrÖ   rQ   r   r   r   rÄ   ö  s,    


ÿ
(zCommand.build_potfilesc                 C   s:   | j D ].}tj |d| j ¡}tj |¡rt |¡ qd S )NrÙ   )r·   r&   r,   r(   r6   rd   re   )r!   r,   Zpot_pathr   r   r   rÉ     s    
zCommand.remove_potfilesc              
   C   s¸  g }g }| j r$dd„ tjtjfD ƒ}tj|d| jd�D �]v\}}}|dd… D ]”}ttj 	tj 
||¡¡| jƒsŠtj 
tj |¡|¡|v r°| |¡ | jdkrâ| j d| ¡ qN|dkrN| |¡ | j d	tj 
tj |¡|¡¡ qN|D ]Ä}tj 	tj 
||¡¡}	tj |¡d }
|
| jv�s,t|	| jƒ�rN| jdk�r¬| j d
||f ¡ qèd}| jD ],}tj |¡ tj |¡¡�rX|} �q†�qX|�p–| j�p–t}| |  |||¡¡ qèq6t|ƒS )zx
        Get all files in the given root. Also check that there is a matching
        locale dir for each file.
        c                 S   s   g | ]}|rt j |¡‘qS r   )r&   r,   Únormpath)r—   Úpr   r   r   r¤     s   þz&Command.find_files.<locals>.<listcomp>T)ÚtopdownÚfollowlinksNr<   zignoring directory %sr   r   zignoring file %s in %s)r»   r   Z
MEDIA_ROOTZSTATIC_ROOTr&   Úwalkr‘   r   r,   rß   r(   r   rº   Úremover�   rµ   rP   r·   Úinsertr>   r}   Ú
startswithÚdirnamer¸   ÚNO_LOCALE_DIRrl   Útranslatable_file_classÚsorted)r!   ÚrootÚ	all_filesZignored_rootsr   ÚdirnamesÚ	filenamesrç   rF   Ú	file_pathr?   r    r,   r   r   r   rÚ     sZ    
þÿþÿû


ÿÿ
ÿ

ÿzCommand.find_filesc                 C   sH   i }|D ]}|  |jg ¡}| |¡ q| ¡ D ]\}}|  ||¡ q.dS )zu
        Group translatable files by locale directory and run pot file build
        process for each group.
        N)Ú
setdefaultr    rl   ÚitemsÚprocess_locale_dir)r!   rÝ   Zfile_groupsr7   Z
file_groupr    Úfilesr   r   r   rÛ   M  s    zCommand.process_filesc                 C   sœ  g }|D ]È}| j dkr.| j d|j|jf ¡ | jdvr:q|  | | j|¡}z| ¡  W nn ty  } z2| j d|j|j|f ¡ W Y d}~qW Y d}~n.d}~0  t	yÄ   |D ]}| 
¡  q°‚ Y n0 | |¡ q| jdk�r|j}dd| jd	|röd
ndf ddddddg
}n0| jdk�r:dd| jdddddddddg}ndS dd„ |D ƒ}	tdd��R}
|
 d |	¡¡ |
 ¡  | d|
jg¡ | | j¡ t|ƒ\}}}W d  ƒ n1 �s°0    Y  |�r|tk�rö|D ]}| 
¡  �qÎtdd |	¡|f ƒ‚n| j dk�r| j |¡ |�r„|tu �rP|D ]}| 
¡  �q"tj |d j¡}td| ƒ‚|D ]}| |¡}�qTtj |d| j ¡}t||ƒ |D ]}| 
¡  �qˆdS ) zÀ
        Extract translatable literals from the specified files, creating or
        updating the POT file for a given locale directory.

        Use the xgettext GNU gettext utility.
        r<   zprocessing file %s in %srC   z6UnicodeDecodeError: skipped file %s in %s (reason: %s)Nr8   r¨   r{   z--language=%sÚCÚ
JavaScriptz--keyword=gettext_noopz--keyword=gettext_lazyz--keyword=ngettext_lazy:1,2z--keyword=pgettext:1c,2z--keyword=npgettext:1c,2,3z
--output=-r;   z--language=Pythonz--keyword=pgettext_lazy:1c,2z--keyword=npgettext_lazy:1c,2,3c                 S   s   g | ]
}|j ‘qS r   )rG   )r—   Úbfr   r   r   r¤   �  r\   z.Command.process_locale_dir.<locals>.<listcomp>zw+)Úmoderj   z--files-fromz/errors happened while running xgettext on %s
%sr   z‘Unable to find a locale path to store translations for file %s. Make sure the 'locale' directory exists in an app or LOCALE_PATHS setting is set.rÙ   )r�   rµ   rP   r   r   r6   Úbuild_file_classrS   ÚUnicodeDecodeErrorÚBaseExceptionrf   rl   r@   r   r(   Úflushr¼   r^   r±   r   rÜ   r	   rè   r&   r,   rß   rc   ru   )r!   r    ró   Zbuild_filesr7   Z
build_fileÚer@   rÊ   Zinput_filesZinput_files_listrb   rÞ   rÖ   rï   rq   r   r   r   rò   Y  s´    

ÿÿ
ýÿÿ 
öô.
ÿÿ
þÿ
zCommand.process_locale_dirc                 C   sª  t j t j |¡|d¡}t j|dd� t j |d| j ¡}t j |¡rªdg| j ||g }t|ƒ\}}}|r˜|t	kr‚t
d| ƒ‚n| jdkr˜| j |¡ t|ƒjdd	�}	nFt|dd	��}
|
 ¡ }	W d
  ƒ n1 sÔ0    Y  | jsð|  |	|¡}	t|	ƒ}	|	 d| j d¡}	t|ddd	��}
|
 |	¡ W d
  ƒ n1 �s:0    Y  | j�r¦dg| j d||g }t|ƒ\}	}}|�r¦|t	k�rŽt
d| ƒ‚n| jdk�r¦| j |¡ d
S )z·
        Create or update the PO file for self.domain and `locale`.
        Use contents of the existing `potfile`.

        Use msgmerge and msgattrib GNU gettext utilities.
        ÚLC_MESSAGESTr¡   ú%s.por¦   z)errors happened while running msgmerge
%sr   rH   rI   Nz2#. #-#-#-#-#  %s.pot (PACKAGE VERSION)  #-#-#-#-#
ri   rM   r§   z-oz*errors happened while running msgattrib
%s)r&   r,   r(   rç   r¾   r6   rd   r®   r   rÜ   r	   r�   rµ   rP   r   Ú	read_textrN   rO   r¶   Úcopy_plural_formsrm   rV   rš   r°   )r!   rq   r   ZbasedirZpofilerÊ   r«   rÞ   rÖ   rb   rQ   r   r   r   rÈ   Ã  sD    ÿ
&
ÿ*
ÿzCommand.write_po_filec              
   C   s  t j t j t j tj¡¡¡}| jdkr.d}nd}|D ]Ô}t j |dd|dd| ¡}t j |¡r6t	|dd	��}t
 | ¡ ¡}W d
  ƒ n1 s�0    Y  |r6|d }	| jdkrÀ| j d|	 ¡ g }
d}| ¡ D ](}|sî|ræt
 |¡rî|	}d}|
 |¡ qÐd |
¡} �qq6|S )zÑ
        Copy plural forms header contents from a Django catalog of locale to
        the msgs string, inserting it at the right place. msgs should be the
        contents of a newly created .po file.
        r8   rC   )r;   r    r   rý   rþ   rH   rI   NÚvaluer<   zcopying plural forms: %sFTrj   )r&   r,   rß   r(   rç   r;   Ú__file__r6   rd   rN   Úplural_forms_rerÑ   rO   r�   rµ   rP   rk   rl   )r!   rb   r   Z
django_dirÚdomainsr6   Z	django_porQ   r×   Zplural_form_linerr   rs   rt   r   r   r   r   ð  s2    
ÿ,

zCommand.copy_plural_formsN)r%   r1   r2   rz   r   ré   r4   rø   Zrequires_system_checksr®   r¯   r°   r±   r�   rÎ   r   r=   r»   rÄ   rÉ   rÚ   rÛ   rò   rÈ   r   r   r   r   r   rv   Ð   s.   ÿc 

	6j-rv   )2rÁ   r&   r   r³   Ú	functoolsr   Ú	itertoolsr   Úpathlibr   r;   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.files.tempr   Zdjango.core.management.baser   r	   Zdjango.core.management.utilsr
   r   r   r   Zdjango.utils.encodingr   Zdjango.utils.functionalr   Zdjango.utils.jslexr   Zdjango.utils.regex_helperr   Zdjango.utils.textr   Zdjango.utils.translationr   ra   ÚDOTALLr  rÜ   Úobjectrè   r   r   r   r4   rm   ru   rv   r   r   r   r   Ú<module>   s>   ÿ	_