a
    V$c&                     @   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mZ dd Zdd Zd	d
 ZG dd deZdd Zejd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_unicodec           
      C   sv   i }dd }t jjj}|j D ]P}|D ]F\}}|j|j| fd}|du rNq(||}||}	|r(|	r(|	||< q(q |S )z
    :undocumented:

    Creates a map from a dialogue string to a potential translation of the
    the dialogue. This is meant for the Ren'Py tutorial, as a way of translating
    strings found in the examples.
    c                 S   s(   | j D ]}t|tjjr|j  S qd S )N)block
isinstancerenpyastSaywhat)ti r   renpy/translation\dialogue.pyget_text-   s    
z%create_dialogue_map.<locals>.get_textN)	r   gamescript
translatorfile_translatesvalueslanguage_translatesget
identifier)
languagervr   r!   v_r   ltt_textlt_textr   r   r   create_dialogue_map"   s    	
r.   c                    s   dd   fdd}|| S )Nc                 S   s^   d}d}d}| D ]H}|dkr0|r&d}qXd}d}q|dkrFd}|rXd}qd}|rPq||7 }q|S )NF {T}r   )sbracefirstr(   r   r   r   r   tag_passJ   s$    
znotags_filter.<locals>.tag_passc                    s   d}d}d}d}| D ]}|dkrX|r*d}n$| |7 }d}|dkrFd}|d7 }|d7 }q|dkrd}|d8 }|dk r||d7 }|d7 }q|r||7 }q||7 }q|r| |7 }|S )Nr   Fr/   [T   ]r   )r2   squaresr4   r(   bufr   r5   r   r   square_passk   s4    



z"notags_filter.<locals>.square_passr   )r2   r<   r   r;   r   notags_filterH   s    !,r=   c                 C   s   dS )Nz[what]r   )r2   r   r   r   what_filter   s    r>   c                   @   s&   e Zd Zd
ddZdd Zdd	 ZdS )DialogueFileTFNc           	      C   sh   || _ tjtjj}||r$dS || _|| _	|| _
|| _|| _t|ddd| _|   | j  dS )a  
        `filename`
            The file we're extracting dialogue from.

        `tdf`
            If true, dialogue is extracted in tab-delimited format. If false,
            dialogue is extracted by itself.

        `strings`
            If true, extract all translatable strings, not just dialogue.

        `notags`
            If true, strip text tags from the extracted dialogue.

        `escape`
            If true, escape special characters in the dialogue.
        Nazutf-8)encoding)filenameospathnormpathr   config	commondir
startswithtdfnotagsescapestringsr'   r   fwrite_dialogueclose)	selfrB   outputrI   rL   rJ   rK   r'   rG   r   r   r   __init__   s    
zDialogueFile.__init__c                 C   s  g }t jjj}|j| j D ]\}}|du r0d}|jdd}d}| jdur`|j	
|| jfd}|du rp|j}n|j}|D ]}t|t jjrz|jsd}	n|j}	|j}
| jrt|
}
| jrt|
}
n*| jr|
dd}
|
dd}
|
d	d
}
| jr||j|	|
|jt|j|tg qz||
g qzq| jrZ||   | jrZ|jdd d |D ]}| j d!|d	  q^dS )z2
        Writes the dialogue to the file.
        Nr/   .r*   \\\	\t
\nc                 S   s   t | d S )N   )int)xr   r   r   <lambda>      z-DialogueFile.write_dialogue.<locals>.<lambda>)key)"r   r   r    r!   r"   rB   r&   replacer'   r$   r%   r   r   r   r   whor   rJ   r=   rK   r   rI   appendr   
linenumberget_coder>   rL   extendget_stringssortrM   writejoin)rP   linesr!   labelr   r&   tlr   nra   r   liner   r   r   rN      sT    



zDialogueFile.write_dialoguec              	   C   s   g }t j| j}t jj| jD ]}|j}|j}t j	j
jjd }||jv rNq"||j|< t j|| j}| jrvt|}| jrt|}n*| jr|dd}|dd}|dd}| jr|dd||t|g q"||g q"|S )	z0
        Finds the strings in the file.
        NrT   rU   rV   rW   rX   rY   r/   )r   parserelide_filenamerB   translationscanstringsscan_stringsrn   textr   r    r!   rL   translationstranslate_stringr'   rJ   r=   rK   r   rI   r`   rb   r   )rP   rj   rB   ssrn   r2   stlr   r   r   rf     s,    


zDialogueFile.get_strings)TFTTN)__name__
__module____qualname__rR   rN   rf   r   r   r   r   r?      s   
&Fr?   c            	   
   C   sp  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 }|rtjt j	j
d}ntjt j	j
d}t|d0}|rg d}|d|d  W d   n1 s0    Y  t j D ]x\}}|du rqtj||}|ds.|ds.qtj|}|j}|dv rNd}t||||j|j|j|d qdS ) zj
    The dialogue command. This updates dialogue.txt, a file giving all the dialogue
    in the game.
    z"Generates or updates translations.)descriptionr'   z%The language to extract dialogue for.)helpz--textzCOutput the dialogue as plain text, instead of a tab-delimited file.rt   
store_true)r}   destactionz	--stringsz3Output all translatable strings, not just dialogue.rL   z--notagsz"Strip text tags from the dialogue.rJ   z--escapez+Escape quotes and other special characters.rK   zdialogue.tabzdialogue.txtw)
Identifier	CharacterDialogueFilenamezLine NumberzRen'Py ScriptrV   rX   Nz.rpyz.rpym)Noner/   )rI   rL   rJ   rK   r'   F)r   	argumentsArgumentParseradd_argument
parse_argsrt   rC   rD   ri   rF   basedirr   rh   loaderlistdirfilesendswithrE   r'   r?   rL   rJ   rK   )	apargsrI   rQ   rM   rn   dirnamerB   r'   r   r   r   dialogue_command9  s:    	2


r   dialogue)
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   r   rC   renpy.translationr   r.   r=   r>   objectr?   r   r   register_commandr   r   r   r   <module>   s   8&R 4