a
    ÷£BeÁª  ã                   @  sh  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
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 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&m'Z' ddl(m)Z) e *d¡Z+zNddl,m-Z. e+Z/zddl0m1Z1m2Z2m3Z3 W n  e4�yT   e5 Z2 Z3Z1Y n0 W nP e4�y¨   ddl6m+Z/ ddl7m-Z. ddl8m9Z1 ddl8m:Z2 ddl8m;Z3 Y n0 d8dd„Z<G dd„ de.ƒZ-G dd„ de-ƒZ=dd„ Z>G dd „ d e-ƒZ?d!d"„ Z@G d#d$„ d$e-ƒZAG d%d&„ d&e-ƒZBG d'd(„ d(ƒZCd)d*„ ZDd9d+d,„ZEd-d.d/œd0d1„ZFd:d3d4œd5d6„ZGeHd7k�rdeDƒ  dS );zÕ
    babel.messages.frontend
    ~~~~~~~~~~~~~~~~~~~~~~~

    Frontends for the message extraction functionality.

    :copyright: (c) 2013-2023 by the Babel Team.
    :license: BSD, see LICENSE for more details.
é    )ÚannotationsN)ÚOrderedDict)ÚRawConfigParser)ÚStringIO)ÚIterable)ÚLocaleÚ
localedata)Ú__version__)ÚUnknownLocaleError)ÚDEFAULT_HEADERÚCatalog)ÚDEFAULT_KEYWORDSÚDEFAULT_MAPPINGÚcheck_and_call_extract_fileÚextract_from_dir)Úwrite_mo)Úread_poÚwrite_po)ÚLOCALTZÚbabel)ÚCommand)Ú	BaseErrorÚOptionErrorÚ
SetupError)Úlog)ÚDistutilsError)ÚDistutilsOptionError)ÚDistutilsSetupErrorc                 C  s†   g }t | ttfƒs| g} | D ]N}|du r*qt |ttfƒrL| t||d�¡ q| dd„ t|ƒ |¡D ƒ¡ qtdd„ |D ƒƒs‚J ‚|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%   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/babel/messages/frontend.pyÚ	<genexpr>b   ó    z listify_value.<locals>.<genexpr>c                 s  s   | ]}t |tƒV  qd S r    )Ú
isinstanceÚstr)r#   Úvalr%   r%   r&   r'   c   r(   )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__…   s    zCommand.__init__)N)Ú__name__Ú
__module__Ú__qualname__Úas_argsÚmultiple_value_optionsÚboolean_optionsÚoption_aliasesÚoption_choicesÚdistutils_logr   r<   r%   r%   r%   r&   r   g   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 translationsrR   rT   c                 C  s.   d| _ d | _d | _d | _d | _d| _d| _d S ©NÚmessagesF)ÚdomainÚ	directoryÚ
input_fileÚoutput_fileÚlocaleÚ	use_fuzzyrT   ©r:   r%   r%   r&   r4   µ   s    z"compile_catalog.initialize_optionsc                 C  s8   t | jƒ| _| js | js tdƒ‚| js4| js4tdƒ‚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/   rW   rY   rX   r   rZ   r]   r%   r%   r&   Úfinalize_options¾   s
    z compile_catalog.finalize_optionsc                 C  sN   d}| j D ]$}|  |¡ ¡ D ]}|t|ƒ7 }qq
|rB| j d|¡ |rJdS dS )Nr   z%d errors encountered.é   )rW   Ú_run_domainÚvaluesÚlenr   Úerror)r:   Zn_errorsrW   Úerrorsr%   r%   r&   ÚrunÅ   s    
zcompile_catalog.runc              
   C  sš  g }g }| j sÈ| jr`| | jtj | j| jd|› d�¡f¡ | tj | j| jd|› d�¡¡ nft | j¡D ]X}tj | j|d|› d�¡}tj |¡rl| ||f¡ | tj | j|d|› d�¡¡ qlnH| | j| j f¡ | j	rî| | j	¡ n"| tj | j| jd|› d�¡¡ |�st
dƒ‚i }t|ƒD �]h\}\}}|| }t|dƒ�}	t|	|ƒ}
W d   ƒ n1 �sl0    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 �sˆ0    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)r\   )rY   r[   ÚappendÚosÚpathÚjoinrX   ÚlistdirÚexistsrZ   r   Ú	enumerateÚopenr   rT   r,   Ústringrb   r   ÚinfoÚfuzzyr\   Úcheckrc   Úlinenor   )r:   rW   Úpo_filesZmo_filesr[   Úpo_fileZcatalogs_and_errorsÚidxZmo_fileÚinfileÚcatalogZ
translatedÚmessageÚ
percentageZcatalog_errorsrd   rc   Úoutfiler%   r%   r&   r`   Î   s~    þÿþ
ÿþ
þ*
þ
ÿ4zcompile_catalog._run_domainN)r=   r>   r?   Ú__doc__ÚdescriptionÚuser_optionsrB   r4   r^   re   r`   r%   r%   r%   r&   rF   �   s   		rF   c                   s   ‡ fdd„}|S )zO
    Build a directory_filter function based on a list of ignore patterns.
    c                   s$   t j | ¡‰ t‡ fdd„ˆD ƒƒ S )Nc                 3  s   | ]}t   ˆ |¡V  qd S r    )Úfnmatch)r#   Zignore_pattern©Úbasenamer%   r&   r'     s   ÿzG_make_directory_filter.<locals>.cli_directory_filter.<locals>.<genexpr>)rm   rn   r†   Úany)Údirname©Úignore_patternsr…   r&   Úcli_directory_filter  s    ýz4_make_directory_filter.<locals>.cli_directory_filterr%   )rŠ   r‹   r%   r‰   r&   Ú_make_directory_filter  s    rŒ   c                   @  sp   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œ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)rM   rN   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).)zignore-dirs=NzwPatterns for directories to ignore when scanning for messages. Separate multiple patterns with spaces (default ".* ._"))zheader-comment=Nzheader comment for the catalog)r�   r‘   r’   r–   r—   r˜   rš   zinput-paths)zadd-commentsÚkeywordszignore-dirs)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 | _d | _d S )Nzutf-8FT)Úcharsetr›   Úno_default_keywordsÚmapping_fileÚno_locationÚadd_locationÚomit_headerrZ   Ú
input_dirsÚinput_pathsÚwidthÚno_wrapÚsort_outputÚsort_by_fileÚmsgid_bugs_addressÚcopyright_holderÚprojectÚversionÚadd_commentsÚstrip_commentsÚinclude_linenoÚignore_dirsÚheader_commentr]   r%   r%   r&   r4     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	sXtdƒ‚| j
sftdƒ‚| jrz| jrztdƒ‚| jsŽ| jsŽd| _n| jd ur¤t| jƒ| _| jr¸| jr¸tdƒ‚| jrÜt| jtƒrÚt d| j¡| _n6| 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|› d�ƒ‚�q(t| j�pZd
dƒ| _| j�r’| j�s~| j ¡ | _| j�s’| j  ¡ | _| j!dk�r¦d| _"n| j!dk�r¸d| _#t| j$ƒ}|�rÖt%| j$ƒ| _&nd | _&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: z does not existú,rž   Tr�   F)'r¥   r¦   r   r    r   ÚcopyÚupdateÚparse_keywordsr/   r›   rZ   r¨   r§   Úintr©   rª   r)   r*   Úrer   r3   ÚdictÚfromkeysÚpackagesÚkeysrm   rn   rq   r¯   r­   Úget_namer®   Úget_versionr£   r¢   r±   r²   rŒ   Údirectory_filter)r:   r›   rn   r²   r%   r%   r&   r^   ˜  sl    
ÿÿÿ
ÿ

þ

z!extract_messages.finalize_optionsr*   )rn   c                   s   ddddœ‡ ‡fdd„}|S )Nr*   r¾   )ÚfilenameÚmethodÚoptionsc                   st   |dkrd S t j ˆ ¡rˆ }nt j t j ˆ | ¡¡}d}|r`d dd„ | ¡ D ƒ¡}d|› d�}ˆj d||¡ d S )	NÚignoreÚ z, c                 s  s"   | ]\}}|› d |› d�V  qdS )z="ú"Nr%   )r#   rŽ   Úvr%   r%   r&   r'   ñ  r(   zEextract_messages._build_callback.<locals>.callback.<locals>.<genexpr>z (ú)zextracting messages from %s%s)rm   rn   ÚisfileÚnormpathro   Úitemsr   ru   )rÅ   rÆ   rÇ   ÚfilepathZoptstrZ
opt_values©rn   r:   r%   r&   Úcallbackâ  s    z2extract_messages._build_callback.<locals>.callbackr%   )r:   rn   rÒ   r%   rÑ   r&   Ú_build_callbacká  s    z extract_messages._build_callbackc                 C  sV  |   ¡ }t| jdƒ��*}t| j| j| j| j| j| j	p6t
d�}|D ]¼\}}}|  |¡}tj |¡r†t ¡ }t||||| j| j| j|ƒ}	n t|||| j| j|| j| jd�}	|	D ]P\}
}}}}tj |¡rÊ|
}ntj tj ||
¡¡}|j|d ||fg||d� qªq@| j d| j¡ t||| j| j| j| j| j | j!d� W d   ƒ n1 �sH0    Y  d S )Nrk   )r­   r®   r«   r¬   rŸ   r³   )r›   Zcomment_tagsrÒ   Zstrip_comment_tagsrÄ   )Zauto_commentsÚcontextzwriting PO template file to %s)r§   r¢   r¤   r©   rª   r±   )"Ú_get_mappingsrs   rZ   r   r­   r®   r«   r¬   rŸ   r³   r   rÓ   rm   rn   rÍ   Úgetcwdr   r›   r¯   r°   r   rÄ   rÎ   ro   Úaddr   ru   r   r§   r¢   r¤   r©   rª   r±   )r:   Úmappingsr€   r}   rn   Ú
method_mapÚoptions_maprÒ   Zcurrent_dirZ	extractedrÅ   rx   r~   ÚcommentsrÔ   rÐ   r%   r%   r&   re   ö  sR    û

ýúÿ

ûzextract_messages.runc                 C  s  g }| j r^t| j ƒ�}t|ƒ\}}W d   ƒ n1 s60    Y  | jD ]}| |||f¡ qFn¦t| jdd ƒrè| jj}| ¡ D ]h\}}t	|t
ƒr tt|ƒƒ\}}n4g i  }}|D ]$\}}	}
| ||	f¡ |
pÌi ||< q®| |||f¡ q|n| jD ]}| |ti f¡ qî|S )NÚmessage_extractors)r¡   rs   Úparse_mappingr¦   rl   Úgetattrr3   rÜ   rÏ   r)   r*   r   r   )r:   rØ   ÚfileobjrÙ   rÚ   rn   rÜ   ÚmappingÚpatternrÆ   rÇ   r%   r%   r&   rÕ   #  s&    *



zextract_messages._get_mappingsN)r=   r>   r?   r�   r‚   rƒ   rB   r@   rA   rC   rD   r4   r^   rÓ   re   rÕ   r%   r%   r%   r&   r�   %  s$   6üÿI-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   )r;   Ú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)©rG   rH   z&domain of PO file (default 'messages')rK   )úoutput-dir=rI   zpath to output directory©rM   rN   zQname of the output file (default '<output_dir>/<locale>/LC_MESSAGES/<domain>.po'))rP   rQ   z$locale for the new localized catalogr“   r•   r–   c                 C  s.   d | _ d | _d | _d | _d| _d| _d | _d S rU   )Ú
output_dirrZ   rY   r[   rW   r¨   r§   r]   r%   r%   r&   r4   t  s    zinit_catalog.initialize_optionsc              
   C  s  | j stdƒ‚| jstdƒ‚zt | j¡| _W n. ty\ } zt|ƒ|‚W Y d }~n
d }~0 0 | jsr| jsrtdƒ‚| js˜t	j
 | j| jd| j› d�¡| _t	j
 t	j
 | j¡¡sÂt	 t	j
 | j¡¡ | jrÖ| jrÖtdƒ‚| jsê| jsêd| _n| jd u�rt| jƒ| _d S )Núyou must specify the input filez-you must provide a locale for the new catalogz%you must specify the output directoryrf   rg   r´   rµ   )rY   r   r[   r   ÚparseÚ_localer
   rZ   ré   rm   rn   ro   rW   rq   rˆ   Úmakedirsr¨   r§   r¼   ©r:   Úer%   r%   r&   r^   }  s,     ÿzinit_catalog.finalize_optionsc                 C  sª   | j  d| j| j¡ t| jdƒ�}t|| jd�}W d   ƒ n1 sD0    Y  | j|_tj 	t
¡|_d|_t| jdƒ� }t||| jd� W d   ƒ n1 sœ0    Y  d S )Núcreating catalog %s based on %sri   ©r[   Frk   )r§   )r   ru   rZ   rY   rs   r   r[   rì   ÚdatetimeÚnowr   Úrevision_daterv   r   r§   )r:   r|   r}   r€   r%   r%   r&   re   —  s    
ÿ,zinit_catalog.runN©
r=   r>   r?   r�   r‚   rƒ   rB   r4   r^   re   r%   r%   r%   r&   rå   P  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æ   rK   )rç   rI   rJ   rè   )r’   Nz%do not include msgid  entry in headerrO   r“   r•   )zignore-obsolete=Nz1whether to omit obsolete messages from the output)zinit-missing=Nz6if any output files are missing, initialize them first)úno-fuzzy-matchingÚNzdo not use fuzzy matching)úupdate-header-commentNzupdate target header comment)ÚpreviousNz+keep previous msgids of translated messages)zcheck=Nz‘don't update the catalog, just return the status. Return code 0 means nothing would change. Return code 1 means that the catalog would be updated)zignore-pot-creation-date=Nz=ignore changes to POT-Creation-Date when updating or checking)	r’   r–   zignore-obsoletezinit-missingr÷   rú   rù   rw   zignore-pot-creation-datec                 C  s^   d| _ d | _d | _d | _d| _d | _d | _d| _d| _d| _	d| _
d| _d| _d| _d| _d S rU   )rW   rY   ré   rZ   r¤   r[   r§   r¨   Úignore_obsoleteÚinit_missingÚno_fuzzy_matchingÚupdate_header_commentrú   rw   Úignore_pot_creation_dater]   r%   r%   r&   r4   å  s    z!update_catalog.initialize_optionsc              
   C  sè   | j stdƒ‚| js"| js"tdƒ‚| jr6| js6tdƒ‚| jrŽ| jsJtdƒ‚zt | j¡| _W q” t	yŠ } zt|ƒ|‚W Y d }~q”d }~0 0 nd | _| j
r¨| jr¨tdƒ‚| j
s¼| js¼d| _n| jd urÒt| jƒ| _| jrä| jräd| _d S )Nrê   z-you must specify the output file or directoryzyou must specify the localez?you must specify the locale for the init-missing option to workr´   rµ   F)rY   r   rZ   ré   r[   rü   r   rë   rì   r
   r¨   r§   r¼   rý   rú   rî   r%   r%   r&   r^   ö  s0    ÿ"
zupdate_catalog.finalize_optionsc              
   C  s  i }g }| j sŠ| jr@| | jtj | j| jd| j› d�¡f¡ qœt | j¡D ]:}tj | j|d| j› d�¡}tj 	|¡rL| ||f¡ qLn| | j| j f¡ |s¨t
dƒ‚| j}|sÌtj tj | j¡¡d }t| jdƒ�}t|ƒ}W d   ƒ n1 sö0    Y  |D �]¨\}}| j�rØtj 	|¡�sØ| j�r8d||< �q| j d|| j¡ t| jdƒ�}t|| jd�}	W d   ƒ n1 �s|0    Y  | j|	_tj t¡|	_d|	_t|d	ƒ�}
t|
|	ƒ W d   ƒ n1 �sÎ0    Y  | j d
|| j¡ t|dƒ�}t|||d�}	W d   ƒ n1 �s0    Y  |	j|| j| j| j d� tj tj |¡t  !¡ tj |¡ ¡}zLt|d	ƒ�,}t||	| j"| j#| j$| j%d� W d   ƒ n1 �s¤0    Y  W n  t&�yÐ   t '|¡ ‚ Y n0 | j�rft|dƒ�}t|ƒ}W d   ƒ n1 �s0    Y  t|dƒ�}t|ƒ}W d   ƒ n1 �s80    Y  |j|_| (|¡||< t '|¡ �qzt )||¡ W n4 t*�yª   t '|¡ t+ ,||¡ t '|¡ Y n0 �q| j�r| -¡ D ].\}}|�rÞ| j d|¡ n| j .d|¡ �qÀt/| 0¡ ƒ�st1dƒ‚n| j d¡ d S d S )Nrf   rg   rh   r   ri   Frð   rñ   rk   zupdating catalog %s based on %s)r[   rW   )rþ   Zupdate_creation_date)r¤   rû   Zinclude_previousr§   zCatalog %s is up to date.zCatalog %s is out of date.zSome catalogs are out of date.z All the catalogs are up-to-date.)2rZ   r[   rl   rm   rn   ro   ré   rW   rp   rq   r   Úsplitextr†   rY   rs   r   rü   rw   r   ru   rì   rò   ró   r   rô   rv   r   rº   rý   rþ   rÿ   rˆ   ÚtempfileÚgettempprefixr¤   rû   rú   r§   Ú	ExceptionÚremoveZis_identicalÚrenameÚOSErrorÚshutilr¹   rÏ   Úwarningr0   ra   r   )r:   Zcheck_statusry   r[   rz   rW   r|   ÚtemplaterÅ   r}   r€   ZtmpnameZtmpfileZorigfileZoriginal_catalogÚnewfileZupdated_catalogZ
up_to_dater%   r%   r&   re     s®    
þÿ
þ&ÿ.*.ý
ÿÿý*
((


zupdate_catalog.runNrõ   r%   r%   r%   r&   rö   ©  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 z$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  sl  |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 }t|ƒD ]*}t |¡}t|d|› �›d|j› �ƒ qædS |�s(| j d¡ |d }|| jv�rP| j d|› 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Úactionr8   z-vz	--verboseÚstore_constÚloglevelzprint as much as possible)r  r  Úconstr8   z-qz--quietzprint as little as possibleF)r  r  r_   c                 s  s   | ]}t |ƒV  qd S r    ©rb   )r#   Ú
identifierr%   r%   r&   r'   ·  r(   z+CommandLineInterface.run.<locals>.<genexpr>ú<ú r   zQno valid command or option passed. Try the -h/--help option for more information.zunknown command "rÊ   ) Ú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   Zlocale_identifiersÚmaxÚsortedr   rë   ÚprintZenglish_namerc   ÚcommandsÚ_configure_commandre   )
r:   r  rÇ   ÚargsZidentifiersZid_widthr  r[   ÚcmdnameÚcmdinstr%   r%   r&   re   š  sH    ÿ


þþþ
zCommandLineInterface.runc                 C  s^   t | _ | j  |¡ | j jr(| j jd }nt ¡ }| j  |¡ | |¡ t d¡}| |¡ d S )Nr   z%(message)s)r   ÚsetLevelÚhandlersr%  ÚStreamHandlerÚ
addHandlerÚ	FormatterÚsetFormatter)r:   r  ÚhandlerÚ	formatterr%   r%   r&   r+  È  s    

z'CommandLineInterface._configure_loggingc                 C  sl   t | j ¡ ƒ t dƒ tdtdd„ | jD ƒƒd ƒ}t| j ¡ ƒD ]$\}}t d|d|› �›d|› �ƒ qBd S )	Nz	commands:é   c                 s  s   | ]}t |ƒV  qd S r    r  )r#   r  r%   r%   r&   r'   Ú  r(   z-CommandLineInterface._help.<locals>.<genexpr>r_   z  r  r  )r.  r   Úformat_helpr,  r/  r-  rÏ   )r:   Z	cmd_widthrâ   r‚   r%   r%   r&   r"  ×  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 ]Þ\}}}	| d¡}
t	||
 dd¡ƒ}d|
› �g}|rª| d|› �¡ | |j |
d¡¡ |j |
d	¡}|
|krì| jd
|
› d�7  _qd|
|jv �r|j|d|	dœŽ qd|
|jv �r.|j|d|	|dœŽ qd|j||	||dœŽ qd| |¡\}}|�rlt|| dd¡|ƒ t|ƒ ¡ D ]\}}t|||ƒ �qxz| ¡  W n4 t�yÐ } z| t|ƒ¡ W Y d	}~n
d	}~0 0 |S )zB
        :type cmdname: str
        :type argv: list[str]
        rÉ   )r  r‚   r@   r%   ú=ú-Ú_z--Nr  ú>r  )r  r8   rl   )r  r8   Úchoices)r8   ÚdefaultrB  )Úcommand_classesr   r)   r   r4   r  r  r  r/  rÞ   rƒ   r"   Úreplacerl   r.   rC   ÚgetrD   rB   r$  rA   r*  ÚsetattrÚvarsrÏ   Úensure_finalizedr   rc   r*   )r:   r2  r  Úcmdclassr3  r   r@   ÚlongÚshortr8   râ   rC  ÚstrsrB  rÇ   r1  Úkeyrã   Úerrr%   r%   r&   r0  Þ  sH    
þ
$z'CommandLineInterface._configure_command)N)r=   r>   r?   r�   r  ÚVERSIONr®   r/  rF   r�   rå   rö   rD  r   re   r+  r"  r0  r%   r%   r%   r&   r  �  s$   
üü
.r  c                   C  s   t ƒ  tj¡S r    )r  re   r  r  r%   r%   r%   r&   Úmain  s    rQ  c           
      C  sÈ   i }g }i }t ƒ }t|jƒ|_| | |¡ | ¡ D ]V}|dkrNt| |¡ƒ}q2dd„ | dd¡D ƒ\}}| ||f¡ t| |¡ƒ||< q2|rÀt	|ƒD ](\}	\}}||v r²|| }||f||	< q–||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   | ]}|  ¡ V  qd S r    r!   )r#   Úpartr%   r%   r&   r'   P  r(   z parse_mapping.<locals>.<genexpr>ú:r_   )
r   r   Ú	_sectionsÚ	read_fileÚsectionsr¾   rÏ   r   rl   rr   )
rß   rÅ   rR  rÙ   rÚ   r   ÚsectionrÆ   rá   r{   r%   r%   r&   rÝ     s$    1rÝ   r*   z4tuple[int | None, tuple[int | tuple[int, str], ...]])r$   Úreturnc                 C  sx   g }d }|   d¡D ]X}|d dkr4t|d d… ƒ}q|d dkr\| t|d d… ƒdf¡ q| t|ƒ¡ q|t|ƒfS )Nr¸   éÿÿÿÿÚtr™   )r   r¼   rl   r-   )r$   ZindsÚnumberÚxr%   r%   r&   Ú_parse_spec\  s    r^  r%   zIterable[str])Ústringsc           	      C  s~   i }| D ]D}d|v r0|  d¡\}}t|ƒ\}}n|}d}d}|| |i ¡|< q| ¡ D ]"\}}t|ƒdhkrV|d ||< qV|S )a¿  Parse keywords specifications from the given list of strings.

    >>> import pprint
    >>> keywords = ['_', 'dgettext:2', 'dngettext:2,3', 'pgettext:1c,2',
    ...             'polymorphic:1', 'polymorphic:2,2t', 'polymorphic:3c,3t']
    >>> pprint.pprint(parse_keywords(keywords))
    {'_': None,
     'dgettext': (2,),
     'dngettext': (2, 3),
     'pgettext': ((1, 'c'), 2),
     'polymorphic': {None: (1,), 2: (2,), 3: ((3, 'c'),)}}

    The input keywords are in GNU Gettext style; see :doc:`cmdline` for details.

    The output is a dictionary mapping keyword names to a dictionary of specifications.
    Keys in this dictionary are numbers of arguments, where ``None`` means that all numbers
    of arguments are matched, and a number means only calls with that number of arguments
    are matched (which happens when using the "t" specifier). However, as a special
    case for backwards compatibility, if the dictionary of specifications would
    be ``{None: x}``, i.e., there is only one specification and it matches all argument
    counts, then it is collapsed into just ``x``.

    A specification is either a tuple or None. If a tuple, each element can be either a number
    ``n``, meaning that the nth argument should be extracted as a message, or the tuple
    ``(n, 'c')``, meaning that the nth argument should be extracted as context for the
    messages. A ``None`` specification is equivalent to ``(1,)``, extracting the first
    argument.
    rT  N)r   r^  Ú
setdefaultrÏ   Úset)	r_  r›   rt   ÚfuncnameÚspec_strr\  ÚspecrŽ   rË   r%   r%   r&   r»   h  s    r»   Ú__main__)N)N)r%   )Ir�   Ú
__future__r   rò   r„   r%  r  rm   r½   r  r  r  Úcollectionsr   Úconfigparserr   Úior   Útypingr   r   r   r   r	   rP  Z
babel.corer
   Zbabel.messages.catalogr   r   Zbabel.messages.extractr   r   r   r   Zbabel.messages.mofiler   Zbabel.messages.pofiler   r   Z
babel.utilr   Ú	getLoggerr   Ú
setuptoolsr   Z_CommandrE   Zsetuptools.errorsr   r   r   ÚImportErrorr  Ú	distutilsÚdistutils.cmdÚdistutils.errorsr   r   r   r/   rF   rŒ   r�   rä   rå   rö   r  rQ  rÝ   r^  r»   r=   r%   r%   r%   r&   Ú<module>   sp   


*)   Y Y 
I0
