B
    \*                 @   s  d 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
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 ddlmZ ddlmZ ddlmZmZmZ eeZ eddZ!G dd de!Z"dd Z#dd Z$ddddeg fddZ%dddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d-ddd0d1d2d3d4Z&e'd5d6(e& Z)ej*j+fd7d8Z,d?d9d:Z+d;d< Z-d=d> Z.dS )@z
    sphinx.util.i18n
    ~~~~~~~~~~~~~~~~

    Builder superclass for all builders.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    N)
namedtuple)datetime)path)write_mo)read_po)RemovedInSphinx30Warning)SphinxError)__)logging)Matcher)SEPrelpathwalkCatalogInfozbase_dir,domain,charsetc               @   sL   e Zd Zedd Zedd Zedd Zedd Zd	d
 Zdd Z	dS )r   c             C   s
   | j d S )Nz.po)domain)self r   /lib/python3.7/site-packages/sphinx/util/i18n.pypo_file,   s    zCatalogInfo.po_filec             C   s
   | j d S )Nz.mo)r   )r   r   r   r   mo_file1   s    zCatalogInfo.mo_filec             C   s   t | j| jS )N)r   joinbase_dirr   )r   r   r   r   po_path6   s    zCatalogInfo.po_pathc             C   s   t | j| jS )N)r   r   r   r   )r   r   r   r   mo_path;   s    zCatalogInfo.mo_pathc             C   s&   t | j p$t | jt | jk S )N)r   existsr   getmtimer   )r   r   r   r   is_outdated@   s    zCatalogInfo.is_outdatedc             C   s   t j| jd| jdL}yt||}W n6 tk
rZ } zttd| j| d S d }~X Y nX W d Q R X t | j	dL}yt
|| W n6 tk
r } zttd| j	| W d d }~X Y nX W d Q R X d S )NZrt)encodingzreading error: %s, %swbzwriting error: %s, %s)ioopenr   charsetr   	Exceptionloggerwarningr	   r   r   )r   localeZfile_poZpoexcZfile_mor   r   r   r   F   s    zCatalogInfo.write_moN)
__name__
__module____qualname__propertyr   r   r   r   r   r   r   r   r   r   r   *   s   c             C   s   |r|  tdd }n| }|S )N   r   )splitr   )docname
compactionZretr   r   r   find_catalogV   s    r/   c                sB   r|sg S t | |  fdd|D }fdd|D }|S )Nc          	      s$   g | ]}t  t|gqS r   )gettextfindr   r   ).0Zdir_)r   langsrcdirr   r   
<listcomp>f   s   z&find_catalog_files.<locals>.<listcomp>c                s   g | ]}|rt | qS r   )r   )r2   f)r4   r   r   r5   h   s    )r/   )r-   r4   locale_dirsr3   r.   filesr   )r   r3   r4   r   find_catalog_files`   s    
r9   zutf-8Fc          	   C   s  |dk	rt jdtdd t }|s&|S x| D ]}|s6q,t||d}	t|	sPq,xt|	ddD ]\}
}}dd	 |D }x~|D ]v}|tt|
|	|rq|t	|d
 }tt|
||	
tjt}|r||krq|t|	||}|s| r||| q|W q^W q,W |S )a/  
    :param list locale_dirs:
       list of path as `['locale_dir1', 'locale_dir2', ...]` to find
       translation catalogs. Each path contains a structure such as
       `<locale>/LC_MESSAGES/domain.po`.
    :param str locale: a language as `'en'`
    :param list domains: list of domain names to get. If empty list or None
       is specified, get all domain names. default is None.
    :param boolean force_all:
       Set True if you want to get all catalogs rather than updated catalogs.
       default is False.
    :return: [CatalogInfo(), ...]
    NzGgettext_compact argument for find_catalog_source_files() is deprecated.   )
stacklevelLC_MESSAGEST)followlinksc             S   s   g | ]}| d r|qS )z.po)endswith)r2   r6   r   r   r   r5      s    z-find_catalog_source_files.<locals>.<listcomp>r   )warningswarnr   setr   r   r   r   r   splitextreplacesepr   r   r   add)r7   r%   ZdomainsZgettext_compactr!   Z	force_allZexcludedZcatalogsZ
locale_dirr   dirpathZdirnames	filenamesfilenamebaser   catr   r   r   find_catalog_source_filesl   s2    



rK   ZEEEZEEEEZMMMZMMMMZmediumdZddHZHHhZhhDZDDDMZMMmZmmasZssZWWeWZYYZYYYYZzzzz%)z%az%Az%bz%Bz%cz%-dz%dz%-Hz%Hz%-Iz%Iz%-jz%jz%-mz%mz%-Mz%Mz%pz%-Sz%Sz%Uz%wz%-Wz%Wz%xz%Xz%yz%Yz%Zz%%z(%s)|c          	   C   sz   |d krd}t | dstjj}y|| ||dS  ttjjfk
rR   || |ddS  tk
rt   t	t
d| |S X d S )NZenZtzinfo)r%   z[Invalid date format. Quote the string by single quote if you want to output it directly: %s)hasattrbabeldatesformat_date
ValueErrorZcoreZUnknownLocaleErrorAttributeErrorr#   r$   r	   )dateformatr%   	formatterr   r   r   babel_format_date   s    

ra   c       	   	   C   s   |d kr2t d}|d k	r*tt|}nt }g }t| }xn|D ]f}|tkrt	|d}|dkrpt
jj}n|dkrt
jj}nt
jj}|t||||d qF|| qFW d|S )NSOURCE_DATE_EPOCH z%xz%X)r%   r`   )osgetenvr   ZutcfromtimestampfloatZnowdate_format_rer,   date_format_mappingsgetrY   rZ   r[   Zformat_timeZformat_datetimeappendra   r   )	r_   r^   languageZsource_date_epochresulttokenstokenZbabel_formatZfunctionr   r   r   r[      s&    




r[   c          
   C   s   |j js| S |j j}t }t| \|d< |d< t|d }|rX|tjsX|tj7 }||d< t	|d |d< |j j|d< y|j
f |S  tk
r } ztd| W d d }~X Y nX d S )NrootZextr   basenamerk   z$Invalid figure_language_filename: %r)configrk   Zfigure_language_filenamedictr   rB   dirnamer>   rD   rp   r_   KeyErrorr   )rH   envZfilename_formatrL   rs   r&   r   r   r   get_image_filename_for_language  s    
rv   c             C   sD   |j js| S t| |}t|j}tt|j||r<|S | S d S )N)	rq   rk   rv   r   rs   r-   r   r   r4   )rH   ru   Z
translatedrs   r   r   r   search_image_for_language  s    
rw   )NN)/__doc__r0   r   rd   rer?   collectionsr   r   r   Zbabel.datesrY   Zbabel.messages.mofiler   Zbabel.messages.pofiler   Zsphinx.deprecationr   Zsphinx.errorsr   Zsphinx.localer	   Zsphinx.utilr
   Zsphinx.util.matchingr   Zsphinx.util.osutilr   r   r   Z	getLoggerr'   r#   ZLocaleFileInfoBaser   r/   r9   rK   rh   compiler   rg   rZ   r[   ra   rv   rw   r   r   r   r   <module>
   sx   

,
3
#