B
    ©0H[Lk  ã               @   s6  d dl Z 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	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 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(eG dd„ dƒƒZ)G dd„ dƒZ*dd„ Z+dd„ Z,G dd„ deƒZ-dS )é    N)Útotal_ordering)Ú	dropwhile)Úsettings)ÚImproperlyConfigured)ÚNamedTemporaryFile)ÚBaseCommandÚCommandError)Úfind_commandÚhandle_extensionsÚpopen_wrapper)ÚDEFAULT_LOCALE_ENCODING)Úcached_property)Úprepare_js_for_gettext)Úget_text_list)Ú
templatizez$^(?P<value>"Plural-Forms.+?\\n")\s*$c              G   s*   x$| D ]}t |ƒd krtd| ƒ‚qW d S )NzLCan't find %s. Make sure you have GNU gettext tools 0.15 or newer installed.)r	   r   )ZprogramsÚprogram© r   úpC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\management\commands\makemessages.pyÚcheck_programs   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__'   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__,   s    zTranslatableFile.__repr__c             C   s   | j |j kS )N)Úpath)r   Úotherr   r   r   Ú__eq__2   s    zTranslatableFile.__eq__c             C   s   | j |j k S )N)r"   )r   r#   r   r   r   Ú__lt__5   s    zTranslatableFile.__lt__c             C   s   t j | j| j¡S )N)r   r"   r    r   r   )r   r   r   r   r"   8   s    zTranslatableFile.pathN)	r   Ú
__module__Ú__qualname__r   r!   r$   r%   Ú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 )N)ÚcommandÚdomainÚtranslatable)r   r*   r+   r,   r   r   r   r   A   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)r+   r*   Úgettext_versionr   r"   Úsplitextr,   r   )r   Úfile_extr   r   r   Úis_templatizedF   s    

zBuildFile.is_templatizedc             C   s   | j jS )N)r,   r"   )r   r   r   r   r"   O   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)r-   r0   z%s.%s)	r5   r"   Úgetr+   r,   r   r   r    r   )r   Ú	extensionÚfilenamer   r   r   Ú	work_pathS   s    zBuildFile.work_pathc          	   C   sž   | j s
dS | jjrtjnd}t| jd|d��}| ¡ }W dQ R X | jdkrTt	|ƒ}n | jdkrtt
|| jdd… d�}t| jd	dd��}| |¡ W dQ R X dS )
zz
        Preprocess (if necessary) a translatable file before passing it to
        xgettext GNU gettext utility.
        Nzutf-8Úr)Úencodingr-   r0   é   )ÚoriginÚw)r5   r*   Úsettings_availabler   ZFILE_CHARSETÚopenr"   Úreadr+   r   r   r;   Úwrite)r   r=   ÚfpZsrc_dataÚcontentr   r   r   Ú
preprocessb   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.
        Úntr>   Nz	^(#: .*)(ú)c                s   |   ¡  ˆˆ ¡S )N)ÚgroupÚreplace)Úmatch)Únew_pathÚold_pathr   r   Ú<lambda>‹   s    z0BuildFile.postprocess_messages.<locals>.<lambda>)Úflags)	r5   r   Únamer;   r"   ÚreÚsubÚescapeÚ	MULTILINE)r   Úmsgsr   )rM   rN   r   Úpostprocess_messagesv   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)r5   r   r"   Úexistsr;   Úunlink)r   r   r   r   Úcleanup�   s    zBuildFile.cleanupN)r   r&   r'   Ú__doc__r   r   r5   r"   r;   rG   rW   rZ   r   r   r   r   r)   =   s   	r)   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    
ra   c          	   C   sœ   |  ¡ }tj | ¡r tt|ƒ}nHg }d\}}x:|D ]2}|sN|sNd}| dd¡}|sZ|sZd}| |¡ q2W d |¡}t	| dddd��}| 
|¡ W d	Q R X d	S )
z\
    Write the `potfile` with the `msgs` contents, making sure its format is
    valid.
    )FFTzcharset=CHARSETzcharset=UTF-8r^   Úazutf-8)r=   ÚnewlineN)r_   r   r"   rX   r   ÚlenrK   r`   r    rB   rD   )ÚpotfilerV   Z	pot_linesÚlinesÚfoundZheader_readÚlinerE   r   r   r   Úwrite_pot_file«   s    

ri   c               @   sœ   e Zd ZdZeZeZdZdZ	ddg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.FTz-qz
--previousz--to-code=utf-8z--no-obsoletez--from-code=UTF-8z--add-comments=Translatorsc          	   C   s  |j ddg dddd� |j ddg d	dd
d� |j dddddd� |j dddddd� |j dddddd� |j d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,d� |j d-dd.d/d� |j d0d1d2d3d4d5d6� |j d7dd8d9d� |j d:dd;d<d� d S )=Nz--localez-lÚlocaler`   zfCreates or updates the message files for the given locale(s) (e.g. pt_BR). Can be used multiple times.)ÚdefaultÚdestÚactionÚhelpz	--excludez-xÚexcludez@Locales to exclude. Default is none. Can be used multiple times.z--domainz-dr0   r+   z4The domain of the message files (default: "django").)rl   rm   ro   z--allz-aÚ
store_trueÚallz3Updates the message files for all existing locales.)rn   rm   ro   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.)rm   rn   ro   z
--symlinksz-sÚsymlinkszaFollows 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.)rn   rm   rl   Úmetavarro   z--no-default-ignoreÚstore_falseÚuse_default_ignore_patternszJDon't ignore the common glob-style patterns 'CVS', '.*', '*~' and '*.pyc'.z	--no-wrapÚno_wrapz2Don't break long message lines into several lines.z--no-locationÚno_locationz%Don't write '#: filename:line' lines.z--add-locationÚ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.)rm   ÚchoicesÚconstÚnargsro   z--no-obsoleteÚno_obsoletez Remove obsolete message strings.z
--keep-potÚkeep_potz<Keep .pot file after making messages. Useful when debugging.)Úadd_argument)r   Úparserr   r   r   Úadd_argumentsÙ   sR    

zCommand.add_argumentsc                sB  |d }|d }|d | _ |d | _|d }|d }|d | _|d }|d	 r^|d
dddg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 �rbt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k�rîtdƒ‚| j dk�r|�r|ndg}	n|�r|ndd d!g}	t|	ƒ| _|d k�r@|�s@|�rL| j d k�rpt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)| _nh| j�r| j t j!¡ tj d¡�r(| j "tj d¡¡ | j�rX| jd# | _tj #| j¡�sXt $| j¡ 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/d0ƒ t+d1ƒ zT|  ,¡ }xF|D ]>}| jd#k�r| j d2| ¡ x|D ]}|  -||¡ �qW �qæW W d | j�s<|  .¡  X d S )3Nrk   rp   r+   Ú	verbosityrr   rs   rt   ru   rx   ZCVSz.*z*~z*.pycry   z	--no-wraprz   z--no-locationr{   )r   é   zFThe --add-location option requires gettext 0.19 or later. You have %s.Ú.c             s   s   | ]}t |ƒV  qd S )N)Ústr)Ú.0Úxr   r   r   ú	<genexpr>9  s    z!Command.handle.<locals>.<genexpr>z--add-location=%sr‚   rƒ   )r0   r-   zDcurrently makemessages only supports domains 'django' and 'djangojs'r-   ZjsÚhtmlÚtxtr7   z(Type '%s help %s' for usage information.r   r1   z(examining files with the extensions: %s
ÚandFÚconfTz[a-z]{2}z%s/*c                s   g | ]}ˆ   |¡r|‘qS r   )rL   )r‹   Z	lang_code)Úlooks_like_localer   r   ú
<listcomp>o  s    z"Command.handle.<locals>.<listcomp>ÚmsguniqÚmsgmergeÚ	msgattribÚxgettextzprocessing locale %s
)/r+   r‡   rt   ÚlistÚsetru   Úmsgmerge_optionsÚmsguniq_optionsÚmsgattrib_optionsÚxgettext_optionsr2   r   r    r‚   rƒ   r
   rs   r   r"   ÚbasenameÚsysÚargvÚstdoutrD   r   Úinvoked_for_djangoÚlocale_pathsÚdefault_locale_pathÚisdirÚabspathrA   Úextendr   ÚLOCALE_PATHSr`   rX   ÚmakedirsrR   ÚcompileÚfilterÚglobÚmapÚ
differencer   Úbuild_potfilesÚwrite_po_fileÚremove_potfiles)r   ÚargsÚoptionsrk   rp   Zprocess_allrs   ru   Zarg_add_locationÚextsZlocale_dirsZall_localesZlocalesÚpotfilesre   r   )r’   r   Úhandle  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 k	rt |ƒV  qd S )N)Úint)r‹   Údr   r   r   r�   –  s    z*Command.gettext_version.<locals>.<genexpr>z/Unable to get gettext version. Is it installed?)r   r   rR   ÚsearchÚtupleÚgroupsr   )r   ÚoutÚerrÚstatusÚmr   r   r   r2   Œ  s    zCommand.gettext_versionc             C   s:   y
t j W n* tk
r4   | jdkr0| j d¡ dS X dS )Nr1   z$Running without configured settings.FT)r   r¨   r   r‡   ÚstderrrD   )r   r   r   r   rA   š  s    

zCommand.settings_availablec       
   
   C   sÜ   |   d¡}|  ¡  |  |¡ g }x¶| jD ]¬}tj |d| j ¡}tj |¡sNq(dg| j	 |g }t
|ƒ\}}}|rž|tkrˆtd| ƒ‚n| jdkrž| j |¡ t|ƒ}t|ddd��}	|	 |¡ W d	Q R X | |¡ q(W |S )
z<
        Build pot files and apply msguniq to them.
        r‰   z%s.potr”   z(errors happened while running msguniq
%sr   r@   zutf-8)r=   N)Ú
find_filesr±   Úprocess_filesr£   r   r"   r    r+   rX   r›   r   Ú	STATUS_OKr   r‡   r¡   rD   ra   rB   r`   )
r   Ú	file_listrµ   r"   re   r²   rV   Úerrorsr¾   rE   r   r   r   r¯   ¤  s*    


zCommand.build_potfilesc             C   s>   x8| j D ].}tj |d| j ¡}tj |¡rt |¡ qW d S )Nz%s.pot)r£   r   r"   r    r+   rX   rY   )r   r"   Zpot_pathr   r   r   r±   ¾  s    zCommand.remove_potfilesc          
   C   sJ  dd„ }dd„ | j D ƒ}dd„ dtjhD ƒ}g }xH|D ]@}x:|D ](}| |¡r@| |dt|ƒ … ¡ P q@W | |¡ q6W g }g }	| jržd	d„ tjtj	fD ƒ}	�x tj
|d
| jd�D �]ˆ\}
}}x¨|dd… D ]˜}|tj tj |
|¡¡|ƒ�stj tj |
¡|¡|	k�r4| |¡ | jdk�rf| j d| ¡ qÎ|dkrÎ| |¡ | j dtj tj |
¡|¡¡ qÎW xÒ|D ]Ê}tj tj |
|¡¡}tj |¡d }|| jk�s´||| j ƒ�rÖ| jdk�r8| j d||
f ¡ nbd}x2| jD ](}tj |
¡ tj |¡¡�râ|}P �qâW |�s| j}|�s$t}| |  |
||¡¡ �qpW q´W t|ƒS )zx
        Get all files in the given root. Also check that there is a matching
        locale dir for each file.
        c                s0   t j ˆ¡‰ ‡ ‡fdd„‰t‡fdd„|D ƒƒS )zK
            Check if the given path should be ignored or not.
            c                s   t  ˆ | ¡pt  ˆ| ¡S )N)ÚfnmatchÚfnmatchcase)Úpattern)r:   r"   r   r   ÚignoreÏ  s    z6Command.find_files.<locals>.is_ignored.<locals>.ignorec             3   s   | ]}ˆ |ƒV  qd S )Nr   )r‹   rÈ   )rÉ   r   r   r�   Ò  s    z9Command.find_files.<locals>.is_ignored.<locals>.<genexpr>)r   r"   rž   Úany)r"   ru   r   )r:   rÉ   r"   r   Ú
is_ignoredÉ  s    z&Command.find_files.<locals>.is_ignoredc             S   s   g | ]}t j |¡‘qS r   )r   r"   Únormcase)r‹   Úpr   r   r   r“   Ô  s    z&Command.find_files.<locals>.<listcomp>c             S   s   h | ]}d | ’qS )z%s*r   )r‹   Úpath_sepr   r   r   ú	<setcomp>Õ  s    z%Command.find_files.<locals>.<setcomp>ú/Nc             S   s   g | ]}|rt j |¡‘qS r   )r   r"   Únormpath)r‹   rÍ   r   r   r   r“   â  s    T)ÚtopdownÚfollowlinksr1   zignoring directory %s
rk   r   zignoring file %s in %s
)ru   r   r   Úendswithr`   rd   rA   r   Z
MEDIA_ROOTZSTATIC_ROOTÚwalkrt   r"   rÑ   r    r¦   Úremover‡   r¡   rD   r£   Úinsertr3   rs   Ú
startswithÚdirnamer¤   ÚNO_LOCALE_DIRÚtranslatable_file_classÚsorted)r   ÚrootrË   ru   Zdir_suffixesZnorm_patternsrÍ   Z
dir_suffixZ	all_filesZignored_rootsr   ÚdirnamesÚ	filenamesrÙ   r:   Ú	file_pathr4   r   r"   r   r   r   rÁ   Ä  sR    


"

$
zCommand.find_filesc             C   sP   i }x$|D ]}|  |jg ¡}| |¡ q
W x | ¡ D ]\}}|  ||¡ q4W dS )zu
        Group translatable files by locale directory and run pot file build
        process for each group.
        N)Ú
setdefaultr   r`   ÚitemsÚprocess_locale_dir)r   rÄ   Zfile_groupsr,   Z
file_groupr   Úfilesr   r   r   rÂ      s    
zCommand.process_filesc             C   sV  g }x |D ]˜}| j dkr0| j d|j|jf ¡ | jdkr<q
|  | | j|¡}y| ¡  W n> tk
r– } z | j d|j|j|f ¡ w
W dd}~X Y nX | 	|¡ q
W | jdkrà|j
}dd| jd	|rÈd
ndf ddddddg
}n6| jdk�rdd| jddddddddddddg}ndS dd„ |D ƒ}	tdd��H}
|
 d |	¡¡ |
 ¡  | d|
jg¡ | | j¡ t|ƒ\}}}W dQ R X |�rÔ|tk�r¼x|D ]}| ¡  �q’W tdd |	¡|f ƒ‚n| j d k�rÔ| j |¡ |�r:|tk�rtj |d  j¡}td!| ƒ‚x|D ]}| |¡}�qW tj |d"| j ¡}t||ƒ x|D ]}| ¡  �q@W 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.
        r1   zprocessing file %s in %s
)r-   r0   z6UnicodeDecodeError: skipped file %s in %s (reason: %s)Nr-   r—   z-dz--language=%sÚCZ
JavaScriptz--keyword=gettext_noopz--keyword=gettext_lazyz--keyword=ngettext_lazy:1,2z--keyword=pgettext:1c,2z--keyword=npgettext:1c,2,3z
--output=-r0   z--language=Pythonz--keyword=ugettext_noopz--keyword=ugettext_lazyz--keyword=ungettext_lazy:1,2z--keyword=pgettext_lazy:1c,2z--keyword=npgettext_lazy:1c,2,3c             S   s   g | ]
}|j ‘qS r   )r;   )r‹   Zbfr   r   r   r“   H  s    z.Command.process_locale_dir.<locals>.<listcomp>zw+)Úmoder^   z--files-fromz/errors happened while running xgettext on %s
%sr   z>Unable to find a locale path to store translations for file %sz%s.pot)r‡   r¡   rD   r   r   r+   Úbuild_file_classrG   ÚUnicodeDecodeErrorr`   r5   r   r    Úflushr§   rQ   r�   r   rÃ   rZ   r   rÚ   r   r"   rÑ   rW   ri   )r   r   rä   Zbuild_filesr,   Z
build_fileÚer5   r²   Zinput_filesZinput_files_listrV   rÅ   r¾   rà   re   r   r   r   rã     sŽ    









zCommand.process_locale_dirc       
   	   C   sz  t j t j |¡|d¡}t j |¡s.t  |¡ t j |d| j ¡}t j |¡r¢dg| j ||g }t	|ƒ\}}}|rÖ|t
krŠtd| ƒ‚qÖ| jdkrÖ| j |¡ n4t|ddd��}	|	 ¡ }W d	Q R X | jsÖ|  ||¡}t|ƒ}| d
| j d¡}t|ddd��}	|	 |¡ W d	Q R X | j�rvdg| j d||g }t	|ƒ\}}}|�rv|t
k�r^td| ƒ‚n| jdk�rv| 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_MESSAGESz%s.por•   z)errors happened while running msgmerge
%sr   r<   zutf-8)r=   Nz2#. #-#-#-#-#  %s.pot (PACKAGE VERSION)  #-#-#-#-#
r]   r@   r–   z-oz*errors happened while running msgattrib
%s)r   r"   r    rÙ   r¥   r©   r+   rX   rš   r   rÃ   r   r‡   r¡   rD   rB   rC   r¢   Úcopy_plural_formsra   rK   r‚   rœ   )
r   re   rk   ÚbasedirZpofiler²   rV   rÅ   r¾   rE   r   r   r   r°   k  s>    


zCommand.write_po_filec          
   C   s  t j t j t j tj¡¡¡}| jdkr.d}nd}xÌ|D ]Ä}t j |dd|dd| ¡}t j |¡r8t	|dd	d
��}t
 | ¡ ¡}W dQ R X |r8| d¡}	| jdkr²| j d|	 ¡ g }
d}x4| ¡ D ](}|sâ|rÚt
 |¡râ|	}d}|
 |¡ qÄW d |
¡}P q8W |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.
        r-   )r-   r0   )r0   r‘   rk   rë   z%s.por<   zutf-8)r=   NÚvaluer1   zcopying plural forms: %s
FTr^   )r   r"   rÑ   r    rÙ   r0   Ú__file__r+   rX   rB   Úplural_forms_rer¹   rC   rJ   r‡   r¡   rD   r_   r`   )r   rV   rk   Z
django_dirÚdomainsr+   Z	django_porE   r¿   Zplural_form_linerf   rg   rh   r   r   r   rì   •  s.    




zCommand.copy_plural_formsN)r   r&   r'   ro   r   rÛ   r)   rç   Zrequires_system_checksZleave_locale_alonerš   r›   rœ   r�   r†   r¶   r   r2   rA   r¯   r±   rÁ   rÂ   rã   r°   rì   r   r   r   r   rj   Å   s(   Cp
<_*rj   ).rÆ   r¬   r   rR   rŸ   Ú	functoolsr   Ú	itertoolsr   r0   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   Zdjango.utils.encodingr   Zdjango.utils.functionalr   Zdjango.utils.jslexr   Zdjango.utils.textr   Zdjango.utils.translationr   rª   rU   ÚDOTALLrð   rÃ   ÚobjectrÚ   r   r   r)   ra   ri   rj   r   r   r   r   Ú<module>   s4   	_