
v^f                 @   s  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 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 j! d e j" e j# B Z$ d  Z% e&   Z' d d   Z( e Gd d   d   Z) Gd d   d  Z* d d   Z+ d d   Z, Gd d   d e  Z- d S)    N)total_ordering)	dropwhile)settings)ImproperlyConfigured)NamedTemporaryFile)BaseCommandCommandError)find_commandhandle_extensionsis_ignored_pathpopen_wrapper)DEFAULT_LOCALE_ENCODING)cached_property)prepare_js_for_gettext)get_text_list)
templatizez$^(?P<value>"Plural-Forms.+?\\n")\s*$c              G   s7   x0 |  D]( } t  |  d  k r t d |   q Wd  S)NzLCan't find %s. Make sure you have GNU gettext tools 0.15 or newer installed.)r	   r   )ZprogramsZprogram r   N/tmp/pip-build-8lau8j11/django/django/core/management/commands/makemessages.pycheck_programs   s
    r   c               @   sR   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 |  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__1   s    zTranslatableFile.__eq__c             C   s   |  j  | j  k  S)N)r"   )r   r#   r   r   r   __lt__4   s    zTranslatableFile.__lt__c             C   s   t  j j |  j |  j  S)N)r   r"   r    r   r   )r   r   r   r   r"   7   s    zTranslatableFile.pathN)	r   
__module____qualname__r   r!   r$   r%   propertyr"   r   r   r   r   r   $   s
   r   c               @   s|   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   @   s    		zBuildFile.__init__c             C   sX   |  j  d k r |  j j d	 k  S|  j  d k rT t j j |  j j  d } | d k Sd S)
Ndjangojsr         django   z.pyF)r   r.   r/   )r+   r*   gettext_versionr   r"   splitextr,   r   )r   file_extr   r   r   is_templatizedE   s    
zBuildFile.is_templatizedc             C   s
   |  j  j S)N)r,   r"   )r   r   r   r   r"   N   s    zBuildFile.pathc             C   s]   |  j  s |  j Sd d d d i j |  j  } d |  j j | f } t j 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.
        r-   cr0   pyz%s.%s)	r5   r"   getr+   r,   r   r   r    r   )r   	extensionfilenamer   r   r   	work_pathR   s    	zBuildFile.work_pathc             C   s   |  j  s d S|  j j r" t j n d } t |  j d |  } | j   } Wd QRX|  j d k rq t	 |  } n. |  j d k r t
 | d |  j d d  } t |  j d d d  } | j |  Wd QRXd S)	zz
        Preprocess (if necessary) a translatable file before passing it to
        xgettext GNU gettext utility.
        Nzutf-8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   
preprocessa   s    	zBuildFile.preprocessc                s   |  j  s | St j d k r1 |  j  |  j   n& |  j d d   |  j d d    t j d t j   d    f d d   | d t j 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   |  j    j     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   )rL   rM   r   postprocess_messagesu   s    		zBuildFile.postprocess_messagesc             C   s2   |  j  r. t j j |  j  r. t j |  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;   rF   rV   rY   r   r   r   r   r)   <   s   	r)   c             C   s6   |  j    } | r) | d r) | j d  d j |  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.
    r1    
)
splitlinesappendr    )Zraw_contentsZ
lines_listr   r   r   normalize_eols   s    r`   c          	   C   s   | j    } t j j |   r0 t t |  } nv g  } d \ } } xa | D]Y } | r | r d | k r d } | j d d  } | r | r d } | j |  qI Wd j |  } t	 |  d d d d	 d  } | j
 |  Wd
 QRXd
 S)z\
    Write the `potfile` with the `msgs` contents, making sure its format is
    valid.
    Fzcharset=CHARSETTzcharset=UTF-8r\   ar<   zutf-8newlineN)FF)r^   r   r"   rW   r   lenrJ   r_   r    rA   rC   )potfilerU   Z	pot_lineslinesfoundZheader_readlinerD   r   r   r   write_pot_file   s     rh   c               @   s   e  Z d  Z d Z e Z e 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.Fz-qz
--previousz--to-code=utf-8z--no-obsoletez--from-code=UTF-8z--add-comments=Translatorsc             C   s  | j  d d d g  d d d d | j  d 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 d d | j  d d d d d d | j  d d d d d d d g  d d d d  | j  d! d d" d d# d d$ | j  d% d d d d& | j  d' d d d d( | j  d) d* d6 d. d+ d/ d0 d d1 | j  d2 d d d d3 | j  d4 d d d d5 d  S)7Nz--localez-ldefaultactionr_   helpzfCreates or updates the message files for the given locale(s) (e.g. pt_BR). Can be used multiple times.z	--excludez-xz@Locales to exclude. Default is none. Can be used multiple times.z--domainz-dr0   z4The domain of the message files (default: "django").z--allz-a
store_truez3Updates the message files for all existing locales.z--extensionz-edest
extensionszThe 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.z
--symlinksz-szaFollows symlinks to directories when examining source code and templates for translation strings.z--ignorez-iignore_patternsmetavarPATTERNz`Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.z--no-default-ignorestore_falseuse_default_ignore_patternszJDon't ignore the common glob-style patterns 'CVS', '.*', '*~' and '*.pyc'.z	--no-wrapz2Don't break long message lines into several lines.z--no-locationz%Don't write '#: filename:line' lines.z--add-locationchoicesfullr   neverconstnargs?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.z--no-obsoletez Remove obsolete message strings.z
--keep-potz<Keep .pot file after making messages. Useful when debugging.)zfullzfilerw   )add_argument)r   parserr   r   r   add_arguments   sR    zCommand.add_argumentsc                sz  | d } | d } | d |  _  | d |  _ | d } | d } | d |  _ | d } | d	 ry | 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 rN|  j
 d4 k  rt d d j 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  d5 k rt d   |  j  d k r| pd g }	 n | pd  d! d" g }	 t |	  |  _ | d  k r| r| s|  j  d  k r!t d# t j j t j d  t j d$ f   |  j d$ k rV|  j j d% t t |  j  d&   d' |  _ g  |  _ d  |  _ t j j t j j d( d   rt j j t j j d( d   g |  _ |  j d |  _ d) |  _ ny |  j r|  j j t  j!  t j j d  r|  j j" t j j d   |  j rN|  j d |  _ t j# |  j d* d) t$ j% d+    t& t j j t' j' d, |  j   }
   f d- d.   t( t j j |
  D } | r| } n! | p| } t |  j) |  } | rt* d/ d0 d1  t* d2  ze |  j+   } xR | D]J } |  j d k r9|  j j d3 |  x | D] } |  j, | |  q@WqWWd  |  j su|  j-   Xd  S)6Nlocaleexcluder+   	verbosityallro   symlinksrp   rt   ZCVSz.*z*~z*.pycZno_wrapz	--no-wrapZno_locationz--no-locationZadd_locationr      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>8  s    z!Command.handle.<locals>.<genexpr>z--add-location=%sno_obsoletekeep_potr0   r-   zDcurrently makemessages only supports domains 'django' and 'djangojs'ZjshtmlZtxtr7   z(Type '%s help %s' for usage information.r1   z(examining files with the extensions: %s
andFconfTexist_okz[a-z]{2}z%s/*c                s%   g  |  ] }   j  |  r |  q Sr   )rK   )r   Z	lang_code)looks_like_localer   r   
<listcomp>m  s   	 z"Command.handle.<locals>.<listcomp>msguniqmsgmerge	msgattribxgettextzprocessing locale %s
)r   r   )zdjangozdjangojs).r+   r   r   listsetrp   msgmerge_optionsmsguniq_optionsmsgattrib_optionsxgettext_optionsr2   r   r    r   r   r
   ro   r   r"   basenamesysargvstdoutrC   r   invoked_for_djangolocale_pathsdefault_locale_pathisdirabspathr@   extendr   LOCALE_PATHSr_   makedirsrQ   compilefilterglobmap
differencer   build_potfileswrite_po_fileremove_potfiles)r   argsoptionsr~   r   Zprocess_allro   rp   Zarg_add_locationextsZlocale_dirsZall_localesZlocalespotfilesrd   r   )r   r   handle  s    








#)*				!'		%(	
	zCommand.handlec             C   sf   t  d d g d t \ } } } t j d |  } | rV t d d   | j   D  St d   d  S)Nr   z	--versionZ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   rQ   searchtuplegroupsr   )r   outerrstatusmr   r   r   r2     s    zCommand.gettext_versionc             C   sG   y t  j Wn5 t k
 rB |  j d k r: |  j j d  d SYn Xd S)Nr1   z$Running without configured settings.FT)r   r   r   r   stderrrC   )r   r   r   r   r@     s    	zCommand.settings_availablec       
   
   C   s*  |  j  d  } |  j   |  j |  g  } x |  j D] } t j j | d |  j  } t j j |  sm q6 d g |  j	 | g } t
 |  \ } } } | r | t k r t d |   n |  j d k r |  j j |  t |  } t | d d d  }	 |	 j |  Wd	 QRX| j |  q6 W| S)
z<
        Build pot files and apply msguniq to them.
        r   z%s.potr   z(errors happened while running msguniq
%sr   r?   r<   zutf-8N)
find_filesr   process_filesr   r   r"   r    r+   rW   r   r   	STATUS_OKr   r   r   rC   r`   rA   r_   )
r   	file_listr   r"   rd   r   rU   errorsr   rD   r   r   r   r     s*    
zCommand.build_potfilesc             C   sS   xL |  j  D]A } t j j | d |  j  } t j j |  r
 t j |  q
 Wd  S)Nz%s.pot)r   r   r"   r    r+   rW   rX   )r   r"   Zpot_pathr   r   r   r     s    zCommand.remove_potfilesc             C   sg  g  } g  } |  j  r4 d d   t j t j f D } x&t j | d d d |  j D]\ } } } x | d d  D] } t t j j	 t j j
 | |   |  j  s t j j
 t j j |  |  | k r | j |  |  j d k rG|  j j d |  qs | d	 k rs | j |  |  j j d
 t j j
 t j j |  |   qs Wx| D]} t j j	 t j j
 | |   }	 t j j |  d }
 |
 |  j k st |	 |  j  r|  j d k rU|  j j d | | f  qRd } x? |  j D]4 } t j j |  j t j j |   r| } PqW| p6|  j p6t } | j |  j | | |   qRWqS Wt |  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 j |   q Sr   )r   r"   normpath)r   pr   r   r   r     s   	 z&Command.find_files.<locals>.<listcomp>topdownTfollowlinksNr1   zignoring directory %s
r~   r   zignoring file %s in %s
)r@   r   Z
MEDIA_ROOTZSTATIC_ROOTr   walkr   r   r"   r   r    rp   r   remover   r   rC   r   insertr3   ro   
startswithdirnamer   NO_LOCALE_DIRr_   translatable_file_classsorted)r   rootZ	all_filesZignored_rootsr   dirnames	filenamesr   r:   	file_pathr4   r   r"   r   r   r   r     s8    	.-'2!!'$zCommand.find_filesc             C   sj   i  } x0 | D]( } | j  | j g   } | j |  q Wx* | j   D] \ } } |  j | |  qF Wd 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   s  g  } x | D] } |  j  d k rB |  j j d | j | j f  |  j d" k rT q |  j |  |  j |  } y | j   WnK t k
 r } z+ |  j j d | j | j | f  w WYd d } ~ Xn X| j	 |  q W|  j d k r.| j
 } d d |  j d	 | rd
 n d f d d d d d d g
 } nL |  j d k rvd d |  j d d d d d d d d d d d d g } n d Sd d   | D }	 t d d  b }
 |
 j d j |	   |
 j   | j d |
 j g  | j |  j  t |  \ } } } Wd QRX| ro| t k rPx | D] } | j   qWt d d j |	  | f   n |  j  d k ro|  j j |  | r| t k rt j j | d j  } t d  |   x | D] } | j |  } qWt j j | d! |  j  } t | |  x | D] } | j   qWd 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   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=-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 Sr   )r;   )r   Zbfr   r   r   r   -  s   	 z.Command.process_locale_dir.<locals>.<listcomp>modezw+r\   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)zdjangojszdjango)r   r   rC   r   r   r+   build_file_classrF   UnicodeDecodeErrorr_   r5   r   r    flushr   rP   r   r   r   rY   r   r   r   r"   r   rV   rh   )r   r   r   Zbuild_filesr,   Z
build_fileer5   r   Zinput_filesZinput_files_listrU   r   r   r   rd   r   r   r   r     s    				

zCommand.process_locale_dirc       
      C   s  t  j j t  j j |  | d  } t  j | d d t  j j | d |  j  } t  j j |  r d g |  j | | g } t |  \ } } } | r| t	 k r t
 d |   q|  j d k r|  j j |  nC t | d d	  }	 |	 j   } Wd
 QRX|  j s|  j | |  } t |  } | j d |  j d  } t | d d d	  }	 |	 j |  Wd
 QRX|  j rd g |  j d | | g } t |  \ } } } | r| t	 k rt
 d |   n |  j d k r|  j 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_MESSAGESr   Tz%s.por   z)errors happened while running msgmerge
%sr   r<   zutf-8Nz2#. #-#-#-#-#  %s.pot (PACKAGE VERSION)  #-#-#-#-#
r[   r?   r   z-oz*errors happened while running msgattrib
%s)r   r"   r    r   r   r+   rW   r   r   r   r   r   r   rC   rA   rB   r   copy_plural_formsr`   rJ   r   r   )
r   rd   r~   basedirZpofiler   rU   r   r   rD   r   r   r   r   P  s<    $		zCommand.write_po_filec             C   si  t  j j t  j j t  j j t j    } |  j d k rE d } n d } x| D]} t  j j | d d | d d |  } t  j j |  rR t	 | d d  } t
 j | j    } Wd	 QRX| rR | j d
  }	 |  j d k r |  j j d |	  g  }
 d } xJ | j   D]< } | r@| s4t
 j |  r@|	 } d } |
 j |  qWd j |
  } PqR W| 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-   r0   r   r~   r   z%s.por<   zutf-8Nvaluer1   zcopying plural forms: %s
FTr\   )zdjangojsdjango)r   )r   r"   r   r    r   r0   __file__r+   rW   rA   plural_forms_rer   rB   rI   r   r   rC   r^   r_   )r   rU   r~   Z
django_dirdomainsr+   Z	django_porD   r   Zplural_form_linere   rf   rg   r   r   r   r   y  s.    -	%zCommand.copy_plural_formsN)r   r&   r'   rl   r   r   r)   r   Zrequires_system_checksr   r   r   r   r}   r   r   r2   r@   r   r   r   r   r   r   r   r   r   r   r   ri      s&   		Co
#_)ri   ).r   r   rQ   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   r   Zdjango.utils.encodingr   Zdjango.utils.functionalr   Zdjango.utils.jslexr   Zdjango.utils.textr   Zdjango.utils.translationr   r   rT   DOTALLr   r   objectr   r   r   r)   r`   rh   ri   r   r   r   r   <module>   s2   "		_