
 X#e                 @   s  d  d l  m Z d  d l Z 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 m Z d  d l m Z m Z m Z d  d	 l m Z d  d
 l m Z 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 e(   Z+ Gd d   d e(  Z, d d   Z- d d   Z. Gd d   d e  Z/ d S)    )unicode_literalsN)total_ordering)	dropwhile)settings)NamedTemporaryFile)BaseCommandCommandError)find_commandhandle_extensionspopen_wrapper)upath)DEFAULT_LOCALE_ENCODING	force_str)cached_property)prepare_js_for_gettext)get_text_listz$^(?P<value>"Plural-Forms.+?\\n")\s*$c              G   s:   x3 |  D]+ } t  |  d  k r t d |   q 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   Z/home/ubuntu/projects/ifolica/build/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 t  j j |  j |  j g  S)Nz<TranslatableFile: %s>)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)	__name__
__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)	BuildFilezO
    Represents 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 Si d d 6d d 6j |  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.
        cr,   pyr/   z%s.%s)	r4   r    getr*   r+   r   r   r   r   )r   	extensionfilenamer   r   r   	work_pathR   s    	
zBuildFile.work_pathc             C   s   d d l  m } |  j s d St j |  j d d t j  } | j   } Wd QX|  j	 d k rn t
 |  } n. |  j	 d k r | | |  j d d   } n  t j |  j d	 d d
  } | j |  Wd QXd S)zz
        Preprocess (if necessary) a translatable file before passing it to
        xgettext GNU gettext utility.
        r   )
templatizeNrencodingr,   r/      wzutf-8)Zdjango.utils.translationr;   r4   ioopenr    r   ZFILE_CHARSETreadr*   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)	r4   r   namer:   r    resubescape	MULTILINE)r   msgsr   )rL   rM   r   postprocess_messagesv   s    		zBuildFile.postprocess_messagesc             C   s8   |  j  r4 t j j |  j  r4 t j |  j  q4 n  d S)zM
        Remove a preprocessed copy of a translatable file (if any).
        N)r4   r   r    existsr:   unlink)r   r   r   r   cleanup   s    	zBuildFile.cleanupN)r$   r%   r&   __doc__r   r   r4   r    r:   rF   rV   rY   r   r   r   r   r(   <   s   	r(   c             C   s9   |  j    } | r, | d r, | j d  n  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.
    r0    
)
splitlinesappendr   )Zraw_contentsZ
lines_listr   r   r   normalize_eols   s    r`   c          	   C   s   | j    } t j j |   r0 t t |  } np g  } d
 \ } } x[ | D]S } | rx | rx d } | j d d  } n  | r | r d } n  | j |  qI Wd j |  } t	 j
 |  d d d  } | j |  Wd	 QXd	 S)z|
    Write the :param potfile: POT file with the :param msgs: contents,
    previously making sure its format is valid.
    FTzcharset=CHARSETzcharset=UTF-8r\   ar=   zutf-8N)FF)r^   r   r    rW   r   lenrJ   r_   r   r@   rA   rC   )potfilerU   Z	pot_lineslinesfoundZheader_readlinerD   r   r   r   write_pot_file   s    	rg   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 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 d g  d d d d d d	 | j  d
 d d g  d d d d d d | j  d d d d d d d d | j  d d d d d d d d d d | j  d d d d d d d d | j  d d d d 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* d d+ | j  d, d d d d- d d d d. | j  d/ d d d d0 d d d d1 | j  d2 d d d d3 d d d d4 | j  d5 d d d d6 d d d d7 d  S)8Nz--localez-ldefaultdestlocaleactionr_   helpzfCreates or updates the message files for the given locale(s) (e.g. pt_BR). Can be used multiple times.z	--excludez-xexcludez@Locales to exclude. Default is none. Can be used multiple times.z--domainz-dr/   r*   z4The domain of the message files (default: "django").z--allz-a
store_trueallFz3Updates the message files for all existing locales.z--extensionz-e
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-ssymlinkszaFollows 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_patternsTzJDon'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--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   sJ    zCommand.add_argumentsc             O   s  | d } | d } | d |  _  | d |  _ | d } | d } | d |  _ t j rd d t _ n t j d	 d  | d
 } | d r | d d d d g 7} n  t t |   |  _	 | d r7|  j
 d  d   d g |  _
 |  j d  d   d g |  _ |  j d  d   d g |  _ |  j d  d   d g |  _ n  | d r|  j
 d  d   d g |  _
 |  j d  d   d g |  _ |  j d  d   d g |  _ |  j d  d   d g |  _ n  | d |  _ | d |  _ |  j  d* k rt d   n  |  j  d k r| r| n d g } n | r#| n d d d g } t |  |  _ | d  k r[| r[| sj|  j  d  k rt d t j j t j d  t j d f   n  |  j d k r|  j j d  t t |  j  d!   n  d" |  _ g  |  _ d  |  _ t j j t j j d# d   rTt j j  t j j d# d   g |  _ |  j d |  _ d |  _ n |  j j! t j"  t j j d  r|  j j# t j j  d   n  |  j r|  j d |  _ t j j$ |  j  st j% |  j  qn  t& t j j t' j' d$ |  j   }	 t( t j j |	  }
 | r%|
 } n" | p.|
 } t |  t |  } | r`t) d% d& d'  n  t) d(  zh |  j*   } xU | D]M } |  j d k r|  j j d) |  n  x | D] } |  j+ | |  qWqWWd  |  j s|  j,   n  Xd  S)+Nrk   rn   r*   	verbosityrp   rq   rr   TUSE_I18Nrs   rw   ZCVSz.*z*~z*.pycrx   z	--no-wrapry   z--no-locationrz   r{   r/   r,   zDcurrently makemessages only supports domains 'django' and 'djangojs'ZjshtmlZtxtr6   z(Type '%s help %s' for usage information.r   r0   z(examining files with the extensions: %s
andFconfz%s/*msguniqmsgmerge	msgattribxgettextzprocessing locale %s
)zdjangozdjangojs)-r*   r   rr   r   Z
configuredr   	configurelistsetrs   msgmerge_optionsmsguniq_optionsmsgattrib_optionsxgettext_optionsrz   r{   r   r
   rq   r   r    basenamesysargvstdoutrC   r   invoked_for_djangolocale_pathsdefault_locale_pathisdirr   abspathextendZLOCALE_PATHSr_   rW   makedirsfilterglobmapr   build_potfileswrite_po_fileremove_potfiles)r   argsoptionsrk   rn   Zprocess_allrq   rs   extsZlocale_dirsZall_localesZlocalespotfilesrc   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).0dr   r   r   	<genexpr>  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   r1   u  s    zCommand.gettext_versionc       
   
   C   s;  |  j  d  } |  j   |  j |  g  } x|  j D] } t j j | d t |  j   } t j j	 |  sv q6 n  d g |  j
 | g } t |  \ } } } | r | t k r t d |   q |  j d k r |  j j |  q n  t |  } t j | d d d  }	 |	 j |  Wd	 QX| j |  q6 W| S)
z<
        Build pot files and apply msguniq to them.
        .z%s.potr   z(errors happened while running msguniq
%sr   r?   r=   zutf-8N)
find_filesr   process_filesr   r   r    r   strr*   rW   r   r   	STATUS_OKr   r   r   rC   r`   r@   rA   r_   )
r   	file_listr   r    rc   r   rU   errorsr   rD   r   r   r   r     s*    
"zCommand.build_potfilesc             C   s\   xU |  j  D]J } t j j | d t |  j   } t j j |  r
 t j |  q
 q
 Wd  S)Nz%s.pot)r   r   r    r   r   r*   rW   rX   )r   r    Zpot_pathr   r   r   r     s    "zCommand.remove_potfilesc             C   s  d d   } d d   |  j  D } d d   d t j h D } g  } x] | D]U } xL | D]7 } | j |  rX | j | d t |    PqX qX W| j |  qK Wg  } d	 d   t j t j f D }	 x;t j	 | d
 d d |  j
 D]\ }
 } } x | d d  D] } | t j j t j j |
 |   |  s_t j j t j j |
  |  |	 k r| j |  |  j d k r|  j j d |  qq| d k r| j |  |  j j d t j j t j j |
  |   qqWx| D]} t j j t j j |
 |   } t j j |  d } | |  j k sH| | |  j   rw|  j d k r|  j j d | |
 f  qqd } xB |  j D]7 } t j j |
  j t j j |   r| } PqqW| s|  j } n  | st } n  | j |  j |
 | |   qWq Wt |  S)z
        Helper method to get all files in the given root. Also check that there
        is a matching locale dir for each file.
        c                sD   t  j j        f d d    t  f d d   | D  S)zK
            Check if the given path should be ignored or not.
            c                s"   t  j   |   p! t  j  |   S)N)fnmatchfnmatchcase)pattern)r9   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    rs   r   )r9   r   r    r   
is_ignored  s    z&Command.find_files.<locals>.is_ignoredc             S   s"   g  |  ] } t  j j |   q Sr   )r   r    normcase)r   pr   r   r   
<listcomp>  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 j |   q Sr   )r   r    normpath)r   r   r   r   r   r     s   	 topdownTfollowlinksr0   zignoring directory %s
rk   r   zignoring file %s in %s
)rs   r   r   endswithr_   rb   r   Z
MEDIA_ROOTZSTATIC_ROOTwalkrr   r    r   r   r   remover   r   rC   r   insertr2   rq   
startswithdirnamer   NO_LOCALE_DIRtranslatable_file_classsorted)r   rootr   rs   Zdir_suffixesZnorm_patternsr   Z
dir_suffixZ	all_filesZignored_rootsr   dirnames	filenamesr   r9   	file_pathr3   r   r    r   r   r   r     sN    .*'5!! '	$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   s5  g  } x | D] } |  j  d k rE |  j j d | j | j f  n  |  j d# k rZ q n  |  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 r4| j
 } d d |  j d	 | rd
 n d f d d d d d d g
 } nL |  j d k r|d d |  j d d d d d d d d d d d d g } n d Sd d   | D }	 t d d  n }
 |
 j t d j |	  d t  |
 j   | j d |
 j g  | j |  j  t |  \ } } } Wd QX| r| t k rax | D] } | j   q+Wt d d j |	  | f   q|  j  d  k r|  j j |  qn  | r| t k rt j j | d  j  } t d! |   n  x | D] } | j |  } qWt j j | d" t |  j   } t | |  n  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.

        Uses the xgettext GNU gettext utility.
        r0   zprocessing file %s in %s
r,   r/   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\   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_   r4   r   r   r   r   flushr   rP   r   r   r   rY   r   r   r   r    r   rV   r   rg   )r   r   r   Zbuild_filesr+   Z
build_fileer4   r   Zinput_filesZinput_files_listrU   r   r   r   rc   r   r   r   r     s    				"
"zCommand.process_locale_dirc       
      C   s   t  j j t  j j |  | d  } t  j j |  sF t  j |  n  t  j j | d t |  j   } t  j j |  r d g |  j	 | | g } t
 |  \ } } } | rA| t k r t d |   q |  j d k r |  j j |  q qAnK t j | d d d  }	 |	 j   } Wd	 QX|  j sA|  j | |  } n  t |  } | j d
 |  j d  } t j | d d d  }	 |	 j |  Wd	 QX|  j rd g |  j d | | g } t
 |  \ } } } | r| t k rt d |   q|  j d k r|  j j |  qqn  d	 S)z
        Creates or updates the PO file for self.domain and :param locale:.
        Uses contents of the existing :param potfile:.

        Uses msgmerge, and msgattrib GNU gettext utilities.
        LC_MESSAGESz%s.por   z)errors happened while running msgmerge
%sr   r<   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   r   r*   rW   r   r   r   r   r   r   rC   r@   rA   rB   r   copy_plural_formsr`   rJ   rz   r   )
r   rc   rk   basedirZpofiler   rU   r   r   rD   r   r   r   r   I  s>    $"		zCommand.write_po_filec          
   C   s  t  j j t  j j t  j j t t j     } |  j d k rK d } n d } x.| D]&} t  j j | d d | d d |  } t  j j	 |  rX t
 j | d d d	  } t j | j    } Wd
 QX| r~t | j d   }	 |  j d k r|  j j d |	  n  g  }
 d } xM | j   D]? } | rW| sHt j |  rW|	 } d } n  |
 j |  q%Wd j |
  } Pq~qX qX W| S)z
        Copies 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/   r   rk   r   z%s.por<   r=   zutf-8Nvaluer0   zcopying plural forms: %s
FTr\   )zdjangojsdjango)r   )r   r    r   r   r   r   r/   __file__r*   rW   r@   rA   plural_forms_rer   rB   r   rI   r   r   rC   r^   r_   )r   rU   rk   Z
django_dirdomainsr*   Z	django_porD   r   Zplural_form_linerd   re   rf   r   r   r   r   s  s.    3	%	zCommand.copy_plural_formsN)r$   r%   r&   rm   r   r   r(   r   Zrequires_system_checksZleave_locale_aloner   r   r   r   r~   r   r   r1   r   r   r   r   r   r   r   r   r   r   r   rh      s&   		8f;_*rh   )0
__future__r   r   r   r@   r   rQ   r   	functoolsr   	itertoolsr   r/   Zdjango.confr   Zdjango.core.files.tempr   Zdjango.core.management.baser   r   Zdjango.core.management.utilsr	   r
   r   Zdjango.utils._osr   Zdjango.utils.encodingr   r   Zdjango.utils.functionalr   Zdjango.utils.jslexr   Zdjango.utils.textr   compilerT   DOTALLr   r   objectr   r   r   r(   r`   rg   rh   r   r   r   r   <module>   s8   		`