o
    VÛë\I—  ã                   @   s  d Z 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	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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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. ddl/m0Z0m1Z1 zddl2m3Z3 W n e4y§   ddl5m3Z3 Y nw er¬dndZ6d-dd„Z7G dd„ de.ƒZ-G dd„ de-ƒZ8G dd„ de-ƒZ9dd„ Z:G d d!„ d!e-ƒZ;G d"d#„ d#e-ƒZ<G d$d%„ d%e=ƒZ>d&d'„ Z?d-d(d)„Z@g fd*d+„ZAeBd,krÿe?ƒ  dS dS ).zÕ
    babel.messages.frontend
    ~~~~~~~~~~~~~~~~~~~~~~~

    Frontends for the message extraction functionality.

    :copyright: (c) 2013-2019 by the Babel Team.
    :license: BSD, see LICENSE for more details.
é    )Úprint_functionN)ÚOrderedDict)Údatetime)Úgetpreferredencoding)Ú__version__)ÚLocaleÚ
localedata)ÚStringIOÚstring_typesÚ	text_typeÚPY2)ÚUnknownLocaleError)ÚCatalog)ÚDEFAULT_KEYWORDSÚDEFAULT_MAPPINGÚcheck_and_call_extract_fileÚextract_from_dir)Úwrite_mo)Úread_poÚwrite_po)ÚLOCALTZ)Úlog)ÚCommand)ÚDistutilsOptionErrorÚDistutilsSetupError)ÚRawConfigParserÚrUÚrc                 C   s†   g }t | ttfƒs| g} | D ]'}|du rqt |ttfƒr&| t||d�¡ q| dd„ t|ƒ |¡D ƒ¡ qtdd„ |D ƒƒsAJ ‚|S )a  
    Make a list out of an argument.

    Values from `distutils` argument parsing are always single strings;
    values from `optparse` parsing may be lists of strings that may need
    to be further split.

    No matter the input, this function returns a flat list of whitespace-trimmed
    strings, with `None` values filtered out.

    >>> listify_value("foo bar")
    ['foo', 'bar']
    >>> listify_value(["foo bar"])
    ['foo', 'bar']
    >>> listify_value([["foo"], "bar"])
    ['foo', 'bar']
    >>> listify_value([["foo"], ["bar", None, "foo"]])
    ['foo', 'bar', 'foo']
    >>> listify_value("foo, bar, quux", ",")
    ['foo', 'bar', 'quux']

    :param arg: A string or a list of strings
    :param split: The argument to pass to `str.split()`.
    :return:
    N©Úsplitc                 s   s   � | ]}|  ¡ V  qd S ©N©Ústrip)Ú.0Ús© r%   ú9/usr/lib/python3/dist-packages/babel/messages/frontend.pyÚ	<genexpr>S   s   € z listify_value.<locals>.<genexpr>c                 s   s   � | ]}t |tƒV  qd S r    )Ú
isinstancer
   )r#   Úvalr%   r%   r&   r'   T   s   € )r(   ÚlistÚtupleÚextendÚlistify_valuer   r   Úall)Úargr   Úoutr)   r%   r%   r&   r-   .   s    r-   c                   @   s.   e Zd ZdZdZdZi Zi ZeZ	ddd„Z
dS )r   Nr%   c                 C   s0   || _ |  ¡  d | _d| _d | _d| _d| _d S )NFr   )ÚdistributionÚinitialize_optionsÚ_dry_runÚverboseÚforceÚhelpÚ	finalized)ÚselfÚdistr%   r%   r&   Ú__init__v   s   
zCommand.__init__r    )Ú__name__Ú
__module__Ú__qualname__Úas_argsÚmultiple_value_optionsÚboolean_optionsÚoption_aliasesÚoption_choicesÚdistutils_logr   r:   r%   r%   r%   r&   r   X   s    r   c                   @   sD   e Zd ZdZdZg d¢ZddgZdd„ Zdd	„ Zd
d„ Z	dd„ Z
dS )Úcompile_catalogaÛ  Catalog compilation command for use in ``setup.py`` scripts.

    If correctly installed, this command is available to Setuptools-using
    setup scripts automatically. For projects using plain old ``distutils``,
    the command needs to be registered explicitly in ``setup.py``::

        from babel.messages.frontend import compile_catalog

        setup(
            ...
            cmdclass = {'compile_catalog': compile_catalog}
        )

    .. versionadded:: 0.9
    z+compile message catalogs to binary MO files))údomain=ÚDz>domains of PO files (space separated list, default 'messages'))z
directory=Údú.path to base directory containing the catalogs©zinput-file=Úizname of the input file)úoutput-file=ÚozQname of the output file (default '<output_dir>/<locale>/LC_MESSAGES/<domain>.mo')©úlocale=Úlz locale of the catalog to compile)ú	use-fuzzyÚfzalso include fuzzy translations)Ú
statisticsNz#print statistics about translationsrP   rR   c                 C   s.   d| _ d | _d | _d | _d | _d| _d| _d S ©NÚmessagesF)ÚdomainÚ	directoryÚ
input_fileÚoutput_fileÚlocaleÚ	use_fuzzyrR   ©r8   r%   r%   r&   r2   ¦   ó   
z"compile_catalog.initialize_optionsc                 C   s<   t | jƒ| _| js| jstdƒ‚| js| jstdƒ‚d S d S )Nz<you must specify either the input file or the base directoryz=you must specify either the output file or the base directory)r-   rU   rW   rV   r   rX   r[   r%   r%   r&   Úfinalize_options¯   s   ÿz compile_catalog.finalize_optionsc                 C   sT   d}| j D ]}|  |¡ ¡ D ]
\}}|t|ƒ7 }qq|r$| j d| ¡ |r(dS dS )Nr   z%d errors encountered.é   )rU   Ú_run_domainÚitemsÚlenr   Úerror)r8   Ún_errorsrU   ÚcatalogÚerrorsr%   r%   r&   Úrun¸   s   
ÿzcompile_catalog.runc              
   C   sx  g }g }| j s`| jr.| | jtj | j| jd|d ¡f¡ | tj | j| jd|d ¡¡ nUt | j¡D ]*}tj | j|d|d ¡}tj |¡r^| ||f¡ | tj | j|d|d ¡¡ q4n#| | j| j f¡ | j	rs| | j	¡ n| tj | j| jd|d ¡¡ |s‰t
dƒ‚i }t|ƒD ]ª\}\}}|| }t|dƒ�}	t|	|ƒ}
W d   ƒ n1 s®w   Y  | jräd}t|
ƒdd … D ]	}|jrÉ|d7 }qÀd}t|
ƒrØ|d t|
ƒ }| j d	|t|
ƒ||¡ |
jrò| jsò| j d
|¡ q�t|
 ¡ ƒ ||
< }|D ]\}}|D ]}| j d||j|¡ �qqþ| j d||¡ t|dƒ�}t||
| jd� W d   ƒ n	1 �s4w   Y  q�|S )NÚLC_MESSAGESú.poz.moúno message catalogs foundÚrbr   r^   éd   z)%d of %d messages (%d%%) translated in %sz'catalog %s is marked as fuzzy, skippingzerror: %s:%d: %szcompiling catalog %s to %sÚwb)rZ   )rW   rY   ÚappendÚosÚpathÚjoinrV   ÚlistdirÚexistsrX   r   Ú	enumerateÚopenr   rR   r*   Ústringra   r   ÚinfoÚfuzzyrZ   Úcheckrb   Úlinenor   )r8   rU   Úpo_filesÚmo_filesrY   Úpo_fileÚcatalogs_and_errorsÚidxÚmo_fileÚinfilerd   Ú
translatedÚmessageÚ
percentageÚcatalog_errorsre   rb   Úoutfiler%   r%   r&   r_   Á   sŒ   þÿþÿþ€û	þÿ€þ
ÿÿÿ€zcompile_catalog._run_domainN)r;   r<   r=   Ú__doc__ÚdescriptionÚuser_optionsr@   r2   r]   rf   r_   r%   r%   r%   r&   rD   �   s    			rD   c                   @   sb   e Zd ZdZdZg d¢Zg d¢ZdZdZddd	d
dœZ	ddiZ
dd„ Zdd„ Zdd„ Zdd„ ZdS )Úextract_messagesaÂ  Message extraction command for use in ``setup.py`` scripts.

    If correctly installed, this command is available to Setuptools-using
    setup scripts automatically. For projects using plain old ``distutils``,
    the command needs to be registered explicitly in ``setup.py``::

        from babel.messages.frontend import extract_messages

        setup(
            ...
            cmdclass = {'extract_messages': extract_messages}
        )
    z1extract localizable strings from the project code))zcharset=Nz3charset to use in the output file (default "utf-8"))z	keywords=Úkzispace-separated list of keywords to look for in addition to the defaults (may be repeated multiple times))úno-default-keywordsNz#do not include the default keywords)zmapping-file=ÚFz&path to the mapping configuration file)úno-locationNz>do not include location comments with filename and line number)zadd-location=Nzðlocation lines format. If it is not given or "full", it generates the lines with both file name and line number. If it is "file", the line number part is omitted. If it is "never", it completely suppresses the lines (same as --no-location).)úomit-headerNz'do not include msgid "" entry in header)rK   rL   zname of the output file©zwidth=Úwz"set output line width (default 76)©úno-wrapNzVdo not break long message lines, longer than the output line width, into several lines)úsort-outputNz&generate sorted output (default False))úsort-by-fileNz,sort output by file location (default False))zmsgid-bugs-address=Nzset report address for msgid)zcopyright-holder=Nzset copyright holder in output)zproject=Nzset project name in output)zversion=Nzset project version in output)zadd-comments=Úczuplace comment block with TAG (or those preceding keyword lines) in output file. Separate multiple TAGs with commas(,))ústrip-commentsr$   z)strip the comment TAGs from the comments.)zinput-paths=Nzofiles or directories that should be scanned for messages. Separate multiple files or directories with commas(,))zinput-dirs=Nz@alias for input-paths (does allow files as well as directories).)r‹   r�   rŽ   r’   r“   r”   r–   zinput-paths)zadd-commentsÚkeywords)z	--keyword)z	--mapping)z--output)z--strip-comment-tags)r—   zmapping-filezoutput-filer–   zadd-location)ÚfullÚfileÚneverc                 C   s‚   d| _ d | _d| _d | _d| _d | _d| _d | _d | _d | _	d | _
d| _d| _d| _d | _d | _d | _d | _d | _d| _d| _d S )Nzutf-8FT)Úcharsetr—   Úno_default_keywordsÚmapping_fileÚno_locationÚadd_locationÚomit_headerrX   Ú
input_dirsÚinput_pathsÚwidthÚno_wrapÚsort_outputÚsort_by_fileÚmsgid_bugs_addressÚcopyright_holderÚprojectÚversionÚadd_commentsÚstrip_commentsÚinclude_linenor[   r%   r%   r&   r2   [  s*   
z#extract_messages.initialize_optionsc                 C   s¬  | j r| js| j | _ntdƒ‚| jri }nt ¡ }| tt| j	ƒƒ¡ || _	| j	s-tdƒ‚| j
s4tdƒ‚| jr>| jr>tdƒ‚| jsH| jsHd| _n| jd urSt| jƒ| _| jr]| jr]tdƒ‚| jrot| jtƒrnt d| j¡| _n| jd ur†t dd	„ | jjp~d
D ƒ¡ ¡ | _ng | _| js�tdƒ‚| jD ]}tj |¡s¡td| ƒ‚q“t| jp§d
dƒ| _| jrÀ| js·| j ¡ | _| jsÀ| j  ¡ | _| j!dkrÊd| _"d S | j!dkrÔd| _#d S d S )Nz1input-dirs and input-paths are mutually exclusivez=you must specify new keywords if you disable the default oneszno output file specifiedú0'--no-wrap' and '--width' are mutually exclusiveéL   z;'--sort-output' and '--sort-by-file' are mutually exclusivez,\s*c                 S   s   g | ]
}|  d d¡d ‘qS )Ú.r^   r   r   )r#   rŠ   r%   r%   r&   Ú
<listcomp>š  s    ÿÿz5extract_messages.finalize_options.<locals>.<listcomp>r%   z'no input files or directories specifiedzInput path: %s does not existú,rš   Tr™   F)$r¡   r¢   r   rœ   r   ÚcopyÚupdateÚparse_keywordsr-   r—   rX   r¤   r£   Úintr¥   r¦   r(   r
   Úrer   r1   ÚdictÚfromkeysÚpackagesÚkeysrn   ro   rr   r«   r©   Úget_namerª   Úget_versionrŸ   rž   r­   )r8   r—   ro   r%   r%   r&   r]   r  sf   
ÿ
€


þý
ÿ



ÿz!extract_messages.finalize_optionsc                    sL  ˆ  ¡ }tˆjdƒ�’}tˆjˆjˆjˆjˆjd�}|D ]^\‰ }}‡ ‡fdd„}t	j
 ˆ ¡r@t	 ¡ }tˆ |||ˆjˆjˆj|ƒ}ntˆ ||ˆjˆj|ˆjd�}|D ](\}	}
}}}t	j
 ˆ ¡r`|	}nt	j
 t	j
 ˆ |	¡¡}|j|d ||
fg||d� qPqˆj dˆj¡ t||ˆjˆjˆjˆjˆjˆjd� W d   ƒ d S 1 sŸw   Y  d S )	Nrl   )r©   rª   r§   r¨   r›   c                    sl   |dkrd S t j ˆ ¡rˆ }nt j t j ˆ | ¡¡}d}|r,dd dd„ | ¡ D ƒ¡ }ˆj d||¡ d S )NÚignoreÚ z (%s)z, c                 S   s   g | ]
\}}d ||f ‘qS )z%s="%s"r%   )r#   rŠ   Úvr%   r%   r&   r±   Î  s    ÿz:extract_messages.run.<locals>.callback.<locals>.<listcomp>zextracting messages from %s%s)rn   ro   ÚisfileÚnormpathrp   r`   r   rv   )ÚfilenameÚmethodÚoptionsÚfilepathÚoptstr©ro   r8   r%   r&   Úcallback¿  s   
ÿz&extract_messages.run.<locals>.callback)r—   Úcomment_tagsrÉ   Ústrip_comment_tags)Úauto_commentsÚcontextzwriting PO template file to %s)r£   rž   r    r¥   r¦   r­   )Ú_get_mappingsrt   rX   r   r©   rª   r§   r¨   r›   rn   ro   rÁ   Úgetcwdr   r—   r«   r¬   r   rÂ   rp   Úaddr   rv   r   r£   rž   r    r¥   r¦   r­   )r8   Úmappingsr…   rd   Ú
method_mapÚoptions_maprÉ   Úcurrent_dirÚ	extractedrÃ   ry   r‚   ÚcommentsrÍ   rÆ   r%   rÈ   r&   rf   µ  sR   ü
ýûÿú	
û"Ìzextract_messages.runc                 C   s  g }| j r1t| j tƒ�}t|ƒ\}}W d   ƒ n1 sw   Y  | jD ]
}| |||f¡ q$|S t| jdd ƒrw| jj}| 	¡ D ]4\}}t
|tƒrRtt|ƒƒ\}}ng i }}|D ]\}}	}
| ||	f¡ |
phi ||< qY| |||f¡ q@|S | jD ]
}| |ti f¡ qz|S )NÚmessage_extractors)r�   rt   Úpo_file_read_modeÚparse_mappingr¢   rm   Úgetattrr1   r×   r`   r(   r
   r	   r   )r8   rÑ   ÚfileobjrÒ   rÓ   ro   r×   ÚmappingÚpatternrÄ   rÅ   r%   r%   r&   rÎ   ò  s,   ÿ
ð


ýzextract_messages._get_mappingsN)r;   r<   r=   r†   r‡   rˆ   r@   r>   r?   rA   rB   r2   r]   rf   rÎ   r%   r%   r%   r&   r‰   
  s$    1üÿC=r‰   c                 C   s"   |dksJ ‚t |tƒstdƒ‚dS )ag  Validate the ``message_extractors`` keyword argument to ``setup()``.

    :param dist: the distutils/setuptools ``Distribution`` object
    :param name: the name of the keyword argument (should always be
                 "message_extractors")
    :param value: the value of the keyword argument
    :raise `DistutilsSetupError`: if the value is not valid
    r×   zDthe value of the "message_extractors" parameter must be a dictionaryN)r(   r¸   r   )r9   ÚnameÚvaluer%   r%   r&   Úcheck_message_extractors  s   	
ÿrà   c                   @   s:   e Zd ZdZdZg d¢ZdgZdd„ Zdd„ Zd	d
„ Z	dS )Úinit_cataloga¾  New catalog initialization command for use in ``setup.py`` scripts.

    If correctly installed, this command is available to Setuptools-using
    setup scripts automatically. For projects using plain old ``distutils``,
    the command needs to be registered explicitly in ``setup.py``::

        from babel.messages.frontend import init_catalog

        setup(
            ...
            cmdclass = {'init_catalog': init_catalog}
        )
    z(create a new catalog based on a POT file)©rE   rF   z&domain of PO file (default 'messages')rI   )úoutput-dir=rG   zpath to output directory©rK   rL   zQname of the output file (default '<output_dir>/<locale>/LC_MESSAGES/<domain>.po'))rN   rO   z$locale for the new localized catalogr�   r‘   r’   c                 C   s.   d | _ d | _d | _d | _d| _d| _d | _d S rS   )Ú
output_dirrX   rW   rY   rU   r¤   r£   r[   r%   r%   r&   r2   A  r\   zinit_catalog.initialize_optionsc              
   C   sú   | j stdƒ‚| jstdƒ‚z	t | j¡| _W n ty' } zt|ƒ‚d }~ww | js2| js2tdƒ‚| jsDt	j
 | j| jd| jd ¡| _t	j
 t	j
 | j¡¡sYt	 t	j
 | j¡¡ | jrc| jrctdƒ‚| jsn| jsnd| _d S | jd ur{t| jƒ| _d S d S )Núyou must specify the input filez-you must provide a locale for the new catalogz%you must specify the output directoryrg   rh   r®   r¯   )rW   r   rY   r   ÚparseÚ_localer   rX   rå   rn   ro   rp   rU   rr   ÚdirnameÚmakedirsr¤   r£   r¶   )r8   Úer%   r%   r&   r]   J  s2   €ÿ
ÿ

ÿzinit_catalog.finalize_optionsc                 C   sª   | j  d| j| j¡ t| jdƒ�}t|| jd�}W d   ƒ n1 s"w   Y  | j|_t 	t
¡|_d|_t| jdƒ�}t||| jd� W d   ƒ d S 1 sNw   Y  d S )Nzcreating catalog %s based on %srj   )rY   Frl   )r£   )r   rv   rX   rW   rt   r   rY   rè   r   Únowr   Úrevision_daterw   r   r£   )r8   r€   rd   r…   r%   r%   r&   rf   f  s   
ÿý"ÿzinit_catalog.runN©
r;   r<   r=   r†   r‡   rˆ   r@   r2   r]   rf   r%   r%   r%   r&   rá     s    	rá   c                   @   s<   e Zd ZdZdZg d¢Zg d¢Zdd„ Zdd„ Zd	d
„ Z	dS )Úupdate_catalogaÔ  Catalog merging command for use in ``setup.py`` scripts.

    If correctly installed, this command is available to Setuptools-using
    setup scripts automatically. For projects using plain old ``distutils``,
    the command needs to be registered explicitly in ``setup.py``::

        from babel.messages.frontend import update_catalog

        setup(
            ...
            cmdclass = {'update_catalog': update_catalog}
        )

    .. versionadded:: 0.9
    z'update message catalogs from a POT file)râ   rI   )rã   rG   rH   rä   )rŽ   Nz%do not include msgid  entry in headerrM   r�   r‘   )zignore-obsolete=Nz1whether to omit obsolete messages from the output)úno-fuzzy-matchingÚNzdo not use fuzzy matching)úupdate-header-commentNzupdate target header comment)ÚpreviousNz+keep previous msgids of translated messages)rŽ   r’   zignore-obsoleterð   ró   rò   c                 C   sL   d| _ d | _d | _d | _d| _d | _d | _d| _d| _d| _	d| _
d| _d S rS   )rU   rW   rå   rX   r    rY   r£   r¤   Úignore_obsoleteÚno_fuzzy_matchingÚupdate_header_commentró   r[   r%   r%   r&   r2   «  s   
z!update_catalog.initialize_optionsc                 C   s’   | j stdƒ‚| js| jstdƒ‚| jr| jstdƒ‚| jr%| jr%tdƒ‚| js/| js/d| _n| jd ur:t| jƒ| _| jrE| j	rGd| _	d S d S d S )Nræ   z-you must specify the output file or directoryzyou must specify the localer®   r¯   F)
rW   r   rX   rå   rY   r¤   r£   r¶   rõ   ró   r[   r%   r%   r&   r]   ¹  s   

ÿzupdate_catalog.finalize_optionsc              
   C   s0  g }| j sA| jr| | jtj | j| jd| jd ¡f¡ n-t | j¡D ]}tj | j|d| jd ¡}tj 	|¡r?| ||f¡ q#n	| | j| j f¡ |sPt
dƒ‚| j}|sbtj tj | j¡¡d }t| jdƒ�}t|ƒ}W d   ƒ n1 sww   Y  |D ]—\}}| j d|| j¡ t|dƒ�}t|||d�}W d   ƒ n1 s¢w   Y  |j|| j| jd� tj tj |¡t ¡ tj |¡ ¡}	z%t|	d	ƒ�}
t|
|| j| j| j| jd
� W d   ƒ n1 sãw   Y  W n	   t |	¡ ‚ zt |	|¡ W q~ t�y   t |¡ t  !|	|¡ t |	¡ Y q~w d S )Nrg   rh   ri   r   rj   zupdating catalog %s based on %s)rY   rU   )rö   rl   )r    rô   Úinclude_previousr£   )"rX   rY   rm   rn   ro   rp   rå   rU   rq   rr   r   ÚsplitextÚbasenamerW   rt   r   r   rv   r´   rõ   rö   ré   ÚtempfileÚgettempprefixr   r    rô   ró   r£   ÚremoveÚrenameÚOSErrorÚshutilr³   )r8   rz   rY   r|   rU   r€   ÚtemplaterÃ   rd   ÚtmpnameÚtmpfiler%   r%   r&   rf   Ë  sz   þÿþ€û
ÿÿþ
ÿÿýÿ€

øçzupdate_catalog.runNrî   r%   r%   r%   r&   rï   x  s    rï   c                   @   s^   e Zd ZdZdZde ZdddddœZee	e
edœZd	Zdd
d„Zdd„ Zdd„ Zdd„ Zd	S )ÚCommandLineInterfacez™Command-line interface.

    This class provides a simple command-line interface to the message
    extraction and PO file generation functionality.
    z%%prog %s [options] %sz	%%prog %sz$compile message catalogs to MO filesz:extract messages from source files and generate a POT filez+create new message catalogs from a POT filez0update existing message catalogs from a POT file)ÚcompileÚextractÚinitr´   Nc                 C   s~  |du rt j}tj| jd | jd�| _| j ¡  | j| j_	| jj
ddddd� | jj
d	d
ddtjdd� | jj
ddddtjdd� | jjdtjd� | j |dd… ¡\}}|  |j¡ |jr˜t ¡ }tdd„ |D ƒƒ}| ¡  d|d  }|D ]}t |¡}|||jf }	t|	 t jjp‘tƒ p‘dd¡ƒ qxdS |s | j  d¡ |d }
|
| j!vr±| j  d|
 ¡ |  "|
|dd… ¡}| #¡ S )z{Main entry point of the command-line interface.

        :param argv: list of arguments passed on the command-line
        N)Úcommandz[args])Úusagerª   z--list-localesÚlist_localesÚ
store_truez print all known locales and exit)ÚdestÚactionr6   z-vz	--verboseÚstore_constÚloglevelzprint as much as possible)r  r  Úconstr6   z-qz--quietzprint as little as possibleF)r	  r  r^   c                 S   ó   g | ]}t |ƒ‘qS r%   ©ra   )r#   Ú
identifierr%   r%   r&   r±   A  ó    z,CommandLineInterface.run.<locals>.<listcomp>z
%%-%ds %%sÚasciiÚreplacer   zQno valid command or option passed. Try the -h/--help option for more information.zunknown command "%s")$ÚsysÚargvÚoptparseÚOptionParserr  rª   ÚparserÚdisable_interspersed_argsÚ_helpÚ
print_helpÚ
add_optionÚloggingÚDEBUGÚERRORÚset_defaultsÚINFOÚ
parse_argsÚ_configure_loggingr  r	  r   Úlocale_identifiersÚmaxÚsortr   rç   Úenglish_nameÚprintÚencodeÚstdoutÚencodingr   rb   ÚcommandsÚ_configure_commandrf   )r8   r  rÅ   ÚargsÚidentifiersÚlongestÚformatr  rY   ÚoutputÚcmdnameÚcmdinstr%   r%   r&   rf   $  sV   ÿ


þþþ
ÿþ
zCommandLineInterface.runc                 C   sd   t  d¡| _| j |¡ | jjr| jjd }n
t  ¡ }| j |¡ | |¡ t  d¡}| |¡ d S )NÚbabelr   z%(message)s)	r  Ú	getLoggerr   ÚsetLevelÚhandlersÚStreamHandlerÚ
addHandlerÚ	FormatterÚsetFormatter)r8   r  ÚhandlerÚ	formatterr%   r%   r&   r%  W  s   

z'CommandLineInterface._configure_loggingc                 C   sl   t | j ¡ ƒ t dƒ tdd„ | jD ƒƒ}dtd|d ƒ }t| j ¡ ƒ}|D ]\}}t |||f ƒ q'd S )Nz	commands:c                 S   r  r%   r  )r#   r  r%   r%   r&   r±   i  r  z.CommandLineInterface._help.<locals>.<listcomp>z  %%-%ds %%sé   r^   )r*  r  Úformat_helpr'  r.  Úsortedr`   )r8   r2  r3  r.  rÞ   r‡   r%   r%   r&   r  f  s   ÿzCommandLineInterface._helpc              
   C   sÄ  | j | }|ƒ }| jr| j|_t|tƒsJ ‚| ¡  tj| j|df | j| d�}t	|ddƒ}|j
D ]i\}}}	| d¡}
t	||
 dd¡ƒ}d|
 g}|rS| d	| ¡ | |j |
d¡¡ |j |
d
¡}|
|krr| jd|
 7  _q2|
|jv r�|j|d|	dœŽ q2|
|jv r‘|j|d|	|dœŽ q2|j||	||dœŽ q2| |¡\}}|r¯t|| dd¡|ƒ t|ƒ ¡ D ]
\}}t|||ƒ qµz| ¡  W |S  tyá } z| t|ƒ¡ W Y d
}~|S d
}~ww )zB
        :type cmdname: str
        :type argv: list[str]
        r¿   )r  r‡   r>   r%   ú=ú-Ú_z--%sz-%sNz<%s>r
  )r  r6   rm   )r  r6   Úchoices)r6   ÚdefaultrG  )Úcommand_classesr   r(   r   r2   r  r  r  r.  rÚ   rˆ   r"   r  rm   r,   rA   ÚgetrB   r@   r  r?   r$  ÚsetattrÚvarsr`   Úensure_finalizedr   rb   Ústr)r8   r5  r  Úcmdclassr6  r  r>   ÚlongÚshortr6   rÞ   rH  ÚstrsrG  rÅ   r0  Úkeyrß   Úerrr%   r%   r&   r/  o  sN   
þ




ý€ýz'CommandLineInterface._configure_commandr    )r;   r<   r=   r†   r  ÚVERSIONrª   r.  rD   r‰   rá   rï   rI  r   rf   r%  r  r/  r%   r%   r%   r&   r    s&    üü
3	r  c                   C   s   t ƒ  tj¡S r    )r  rf   r  r  r%   r%   r%   r&   Úmain   s   rV  c           
      C   sÚ   i }g }i }t ƒ }t|jƒ|_tr| | |¡ n| | |¡ | ¡ D ]+}|dkr0t| |¡ƒ}q"dd„ | 	dd¡D ƒ\}}| 
||f¡ t| |¡ƒ||< q"|rit|ƒD ]\}	\}}||v rb|| }||f||	< qT||fS )a  Parse an extraction method mapping from a file-like object.

    >>> buf = StringIO('''
    ... [extractors]
    ... custom = mypackage.module:myfunc
    ...
    ... # Python source files
    ... [python: **.py]
    ...
    ... # Genshi templates
    ... [genshi: **/templates/**.html]
    ... include_attrs =
    ... [genshi: **/templates/**.txt]
    ... template_class = genshi.template:TextTemplate
    ... encoding = latin-1
    ...
    ... # Some custom extractor
    ... [custom: **/custom/*.*]
    ... ''')

    >>> method_map, options_map = parse_mapping(buf)
    >>> len(method_map)
    4

    >>> method_map[0]
    ('**.py', 'python')
    >>> options_map['**.py']
    {}
    >>> method_map[1]
    ('**/templates/**.html', 'genshi')
    >>> options_map['**/templates/**.html']['include_attrs']
    ''
    >>> method_map[2]
    ('**/templates/**.txt', 'genshi')
    >>> options_map['**/templates/**.txt']['template_class']
    'genshi.template:TextTemplate'
    >>> options_map['**/templates/**.txt']['encoding']
    'latin-1'

    >>> method_map[3]
    ('**/custom/*.*', 'mypackage.module:myfunc')
    >>> options_map['**/custom/*.*']
    {}

    :param fileobj: a readable file-like object containing the configuration
                    text to parse
    :see: `extract_from_directory`
    Ú
extractorsc                 S   s   g | ]}|  ¡ ‘qS r%   r!   )r#   Úpartr%   r%   r&   r±   å  r  z!parse_mapping.<locals>.<listcomp>ú:r^   )r   r   Ú	_sectionsr   ÚreadfpÚ	read_fileÚsectionsr¸   r`   r   rm   rs   )
rÛ   rÃ   rW  rÒ   rÓ   r  ÚsectionrÄ   rÝ   r~   r%   r%   r&   rÙ   ¤  s(   1rÙ   c                 C   sš   i }| D ]F}d|v r|  d¡\}}n|d}}||vrJ|rFg }|  d¡D ]}|d dkr:| t|dd… ƒdf¡ q$| t|ƒ¡ q$t|ƒ}|||< q|S )aQ  Parse keywords specifications from the given list of strings.

    >>> kw = sorted(parse_keywords(['_', 'dgettext:2', 'dngettext:2,3', 'pgettext:1c,2']).items())
    >>> for keyword, indices in kw:
    ...     print((keyword, indices))
    ('_', None)
    ('dgettext', (2,))
    ('dngettext', (2, 3))
    ('pgettext', ((1, 'c'), 2))
    rY  Nr²   éÿÿÿÿr•   )r   rm   r¶   r+   )Ústringsr—   ru   ÚfuncnameÚindicesÚindsÚxr%   r%   r&   rµ   ò  s    
€rµ   Ú__main__r    )Cr†   Ú
__future__r   r  r  rn   r·   rÿ   r  rú   Úcollectionsr   r   rY   r   r7  r   rU  r   r   Úbabel._compatr	   r
   r   r   Ú
babel.corer   Úbabel.messages.catalogr   Úbabel.messages.extractr   r   r   r   Úbabel.messages.mofiler   Úbabel.messages.pofiler   r   Ú
babel.utilr   Ú	distutilsr   rC   Údistutils.cmdr   Ú_CommandÚdistutils.errorsr   r   ÚConfigParserr   ÚImportErrorÚconfigparserrØ   r-   rD   r‰   rà   rá   rï   Úobjectr  rV  rÙ   rµ   r;   r%   r%   r%   r&   Ú<module>   sb   	ÿ
*) 
  [  
N
ÿ