a
    VŸ$cî5  ã                   @   s>  d dl mZmZmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZ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„ Zi Zda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d„ Z(dd„ Z)dd„ Z*dd „ Z+d!d"„ Z,d#d$„ Z-d%d&„ Z.d'd(„ Z/ej0 1d)e/¡ dS )*é    )ÚdivisionÚabsolute_importÚwith_statementÚprint_functionÚunicode_literals)ÚPY2Ú
basestringÚbchrÚbordÚchrÚopenÚpystrÚrangeÚroundÚstrÚtobytesÚunicodeN)Úquote_unicode)Úelide_filenamec                 C   sö   g }| t jjvr|S g }d}t| ddd0}dd„ | ¡  dd¡ d	¡D ƒ}W d   ƒ n1 s`0    Y  t|ƒD ]~\}}|s†|d
 }t 	d|¡}|r¸| 
d
¡}	|r¬|	 ¡ }	| |	¡ qr|rrdd |¡ }
|
 d¡rÞ|
 d¡}
g }| ||
f¡ qr|S )Nr   Úrúutf-8©Úencodingc                 S   s   g | ]}|  ¡ ‘qS © )Úrstrip©Ú.0Úir   r   úrenpy/translation\generation.pyÚ
<listcomp>4   ó    z!scan_comments.<locals>.<listcomp>õ   ï»¿Ú Ú
é   z
\s*## (.*)z## ú ú#z# )ÚrenpyÚconfigÚtranslate_commentsr   ÚreadÚreplaceÚsplitÚ	enumerateÚreÚmatchÚgroupÚstripÚappendÚjoinÚendswithr   )ÚfilenameÚrvÚcommentÚstartÚfÚlinesr   ÚlÚmÚcÚsr   r   r   Úscan_comments)   s.    >


r?   Tc                 C   s¦   | t v rt |  S tj | ¡sdtj | ¡}zt |¡ W n tyH   Y n0 t| ddd}| d¡ nt| ddd}t	r| dd 
t d¡¡ ¡ | d¡ |t | < |S )	NÚar   r   r!   z# TOzDO: Translation updated at {}
z%Y-%m-%d %H:%Mr#   )Útl_file_cacheÚosÚpathÚexistsÚdirnameÚmakedirsÚ	Exceptionr   ÚwriteÚtodoÚformatÚtimeÚstrftime)ÚfnÚdnr9   r   r   r   Úopen_tl_fileX   s     
rO   c                  C   s"   t  ¡ D ]} |  ¡  qt  ¡  d S ©N)rA   ÚvaluesÚcloseÚclear)r   r   r   r   Úclose_tl_filesu   s    
rT   c                 C   st   t j tjj¡}t j tjj¡}|  |¡r>t j | |¡}d}n.|  |¡r\t j | |¡}d}nt j 	| ¡}d}||fS )z„
    Shortens a file name. Returns the shortened filename, and a flag that says
    if the filename is in the common directory.
    TF)
rB   rC   Únormpathr'   r(   Ú	commondirÚgamedirÚ
startswithÚrelpathÚbasename)r5   rV   rW   rM   Úcommonr   r   r   Úshorten_filename}   s    

r\   c              
   C   sD  t | ƒ\}}|rd S tj tjjtjj||¡}|d dkrF|d d… }|dkrRd }tjj	j
}|j|  D ]Ø\}}|j|f|jv r€qft|dƒrœ|j|f|jv rœqft|ƒ}	|d u r°d}|	 d |j|j¡¡ |	 d ||j dd	¡¡¡ |	 d
¡ |jD ]}
|	 d|
 ¡  d
 ¡ qò|jD ]}
|	 d|
 |¡ d
 ¡ q|	 d
¡ qfd S )Néÿÿÿÿr<   ÚNoneÚ	alternater"   z# {}:{}
ztranslate {} {}:
Ú.Ú_r#   z    # z    )r\   rB   rC   r3   r'   r(   rW   Útl_directoryÚgameÚscriptÚ
translatorÚfile_translatesÚ
identifierÚlanguage_translatesÚhasattrr_   rO   rH   rJ   r5   Ú
linenumberr+   ÚblockÚget_code)r5   ÚlanguageÚfilterrM   r[   Útl_filenamere   ÚlabelÚtr9   Únr   r   r   Úwrite_translates•   s4    




rs   c                 C   s:   t jjr| jS | jrdS | j}|d dkr6|d d… }|S )Nú
common.rpyr]   r<   )r'   r(   Útranslate_launcherÚlauncher_filer[   Úelided)r>   r5   r   r   r   Útranslation_filenameÂ   s    rx   c                 C   sF  | dkrt jjjjd }nt jjjj|  }t jj |||¡}t 	t
¡}|D ]F}t|ƒ}	|	du r`qJ|j|jv rnqJ| dkr‚|	dkr‚d}	||	  |¡ qJ| ¡ D ]¦\}	}
tj t jjt jj| |	¡}	t|	ƒ}| d | ¡¡ | d¡ |
D ]\}||jƒ}| d t|jƒ|j¡¡ | d t|jƒ¡¡ | d	 t|ƒ¡¡ | d¡ qâqšdS )
z%
    Writes strings to the file.
    r^   Nrt   zcommon.rpymztranslate {} strings:
r#   z    # {}:{}
z    old "{}"
z    new "{}"
)r'   rc   rd   re   ÚstringsÚtranslationÚscanstringsÚscanÚcollectionsÚdefaultdictÚlistrx   ÚtextÚtranslationsr2   ÚitemsrB   rC   r3   r(   rW   rb   rO   rH   rJ   r   r5   Úliner   )rm   rn   Úmin_priorityÚmax_priorityÚcommon_onlyÚstlry   Ústringfilesr>   ÚtlfnÚslr9   r€   r   r   r   Úwrite_stringsÒ   s2    


r‹   c                 C   s   | S rP   r   ©r>   r   r   r   Únull_filter  s    r   c                 C   s   dS )Nr"   r   rŒ   r   r   r   Úempty_filter
  s    rŽ   c                    s$   dd„ ‰‡ ‡fdd„}ˆ| dd|ƒS )a¹  
    :doc: text_utility

    Transforms `s`, while leaving text tags and interpolation the same.

    `function`
        A function that is called with strings corresponding to runs of
        text, and should return a second string that replaces that run
        of text.

    ::

        init python:
            def upper(s):
                return s.upper()

        $ upper_string = renpy.transform_text("{b}Not Upper{/b}", upper)

    c           	      S   s²   d}d}d}d}| D ]ˆ}||krX|r*d}n$|||ƒ7 }d}|dkrFd}|d7 }||7 }q||kr†d}|d8 }|dk r||d7 }||7 }q|r”||7 }q||7 }q|r®|||ƒ7 }|S )Nr   Fr"   Tr$   r   )	r>   r8   ÚendÚprocessÚspecialsÚfirstr6   Úbufr   r   r   r   Úremove_special#  s4    



z&generic_filter.<locals>.remove_specialc                    s   ˆ| ddˆ ƒS )NÚ{Ú}r   rŒ   ©Úfunctionr”   r   r   Úremove_bracesO  s    z%generic_filter.<locals>.remove_bracesú[ú]r   )r>   r˜   r™   r   r—   r   Úgeneric_filter  s    ,rœ   c                    s`   i ‰ t ddƒD ]8\}}|ˆ |< |ˆ |< | ¡ }| ¡ }|ˆ |< |ˆ |< qd ‡ fdd„| D ƒ¡S )NÚABCDEFGHIJKLMÚNOPQRSTUVWXYZr"   c                 3   s   | ]}ˆ   ||¡V  qd S rP   )Úgetr   ©ÚROT13r   r   Ú	<genexpr>c  r    z"rot13_transform.<locals>.<genexpr>)ÚzipÚlowerr3   )r>   r   Újr   r    r   Úrot13_transformU  s    
r¦   c                 C   s
   t | tƒS rP   )rœ   r¦   rŒ   r   r   r   Úrot13_filterf  s    r§   c                    s"   g d¢‰ ‡ fdd„}t  d|| ¡S )N)ÚshÚglÚchÚphÚtrÚbrÚfrÚblÚgrÚstrŠ   ÚclÚplÚflc                    s~   |   d¡}|d dv r |d }nF|d d… ˆ v rN|dd … |d d…  d }n|dd … |d  d }|d  ¡ rz| ¡ }|S )Nr   )r@   Úer   ÚoÚuÚayé   r$   )r0   ÚisupperÚ
capitalize)r<   r   r6   ©Úlstr   r   r+   o  s    

z#piglatin_transform.<locals>.replacez\w+)r.   Úsub)r>   r+   r   r¼   r   Úpiglatin_transformj  s    r¿   c                 C   s   | dkrdS t | tƒS )Nz{#language name and font}zIgpay Atinlay)rœ   r¿   rŒ   r   r   r   Úpiglatin_filter  s    rÀ   c                  C   s‚   t tjjƒ} tj ¡ D ]f\}}|du r(q| d¡r4qtj 	||¡}| 
d¡sX| 
d¡sXqtj |¡}tj |¡srq|  |¡ q| S )zT
    Returns a list of files that exist and should be scanned for translations.
    Nztl/z.rpyz.rpym)r   r'   r(   Útranslate_filesÚloaderÚlistdirfilesrX   rB   rC   r3   r4   rU   rD   r2   )Ú	filenamesrE   r5   r   r   r   Útranslate_list_filesˆ  s    
rÅ   c                 C   s´   t jjj}d}tƒ D ]0}|j| D ] \}}|j| f|jvr"|d7 }q"qd}	t jjjj|  }
t j	j
 |||¡}|D ],}t|ƒ}|du r†qp|j|
jv r”qp|	d7 }	qptd | ||	¡ƒ dS )z@
    Prints a count of missing translations for `language`.
    r   r$   NzE{}: {} missing dialogue translations, {} missing string translations.)r'   rc   rd   re   rÅ   rf   rg   rh   ry   rz   r{   r|   rx   r€   r   ÚprintrJ   )rm   r„   r…   r†   re   Úmissing_translatesr5   ra   rq   Úmissing_stringsr‡   ry   r>   r‰   r   r   r   Úcount_missing¥  s*    


ýrÉ   c                  C   sî  t jjdd} | jddd | jdddd	d
 | jdddd	d
 | jdddd	d
 | jdddd	d
 | jddddtd | jddddtd | jddddd	d | jd d!d"dd	d | jd#d$d%d&d'd |  ¡ }|jat jjrê|j	pæd(}n
|j	pòd)}|j
rt|j|j||jƒ dS |jr"t}n |jr0t}n|jr>t}nt}|jsftƒ D ]}t||j|ƒ qPt|j||j||jƒ tƒ  t jjrê|jsêtj t jjd*d+d,¡}tj t jjd-|jd.¡}tj  |¡rêtj  |¡sêt! "||¡ dS )/zh
    The translate command. When called from the command line, this generates
    the translations.
    z"Generates or updates translations.)Údescriptionrm   z*The language to generate translations for.)Úhelpz--rot13z*Apply rot13 while generating translations.Úrot13Ú
store_true)rË   ÚdestÚactionz
--piglatinz.Apply pig latin while generating translations.Úpiglatinz--emptyz4Produce empty strings while generating translations.Úemptyz--countzCInstead of generating files, print a count of missing translations.Úcountz--min-priorityz/Translate strings with more than this priority.r„   r   )rË   rÎ   ÚdefaultÚtypez--max-priorityr…   z--strings-onlyz&Only translate strings (not dialogue).Ústrings_onlyF)rË   rÎ   rÓ   rÏ   z--common-onlyz+Only translate string from the common code.r†   z	--no-todozDo not include the TODO flag.rI   TÚstore_falseió  i+  Úguirc   z
script.rpyÚtlzscript.rpym)#r'   Ú	argumentsÚArgumentParserÚadd_argumentÚintÚ
parse_argsrI   r(   ru   r…   rÒ   rÉ   rm   r„   r†   rÌ   r§   rÐ   rÀ   rÑ   rŽ   r   rÕ   rÅ   rs   r‹   rT   rB   rC   r3   Ú
renpy_baserW   rD   ÚshutilÚcopy)ÚapÚargsr…   rn   r5   ÚsrcÚdstr   r   r   Útranslate_commandÌ  sJ    

rå   Ú	translate)2Ú
__future__r   r   r   r   r   Úrenpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   r.   rB   rK   r}   rß   r'   Úrenpy.translationr   Úrenpy.parserr   r?   rA   rI   rO   rT   r\   rs   rx   r‹   r   rŽ   rœ   r¦   r§   r¿   rÀ   rÅ   rÉ   rå   rÙ   Úregister_commandr   r   r   r   Ú<module>   s:   8)-4G';