a
    F^i)                     @   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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   ^C:\Program Files (x86)\Steam\steamapps\common\Selene ~Apoptosis~\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   ltZt_textZ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   )r1   Zsquaresr3   r)   bufr   r4   r   r   square_passk   s4    



z"notags_filter.<locals>.square_passr   )r1   r:   r   r9   r   notags_filterH   s    !,r;   c                 C   s   dS )Nz[what]r   )r1   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)	selfr@   outputrG   rJ   rH   rI   r(   rE   r   r   r   __init__   s    
zDialogueFile.__init__c              
   C   s  g }t jjj}|j| j D ]\}}}|du r2d}|jdd}d}| jdurb|j	
|| jfd}|du rr|j}n|j}|D ]}	t|	t jjr||	jsd}
n|	j}
|	j}| jrt|}| jrt|}n*| jr|dd}|dd}|d	d
}| jr(|rq||	t}||j|
||	jt|	j|g q|||g q|q| jrf||   | jrf|jdd d |D ]}| j d!|d	  qjd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#   r@   r'   replacer(   r%   r&   r   r   r   r   whor   rH   r;   rI   r   rG   get_coder<   appendr   
linenumberrJ   extendget_stringssortrK   writejoin)rN   linesr"   labelr   hr'   tlr   nr`   r   Z
niggercodeliner   r   r   rL      sZ    




zDialogueFile.write_dialoguec              	   C   s   g }t j| j}t jj| jD ]}|j}|j}|j	}t j
jjjd }||jv rTq"||j|< t j|| j}| jr|t|}| jrt|}n*| jr|dd}|dd}|dd}| jr|rq"|dd||t|g q"||g q"|S )	z0
        Finds the strings in the file.
        NrR   rS   rT   rU   rV   rW   r.   )r   parserelide_filenamer@   translationscanstringsscan_stringsrn   texthider    r!   r"   rJ   translationstranslate_stringr(   rH   r;   rI   r   rG   r_   rb   r   )rN   ri   r@   ssrn   r1   rk   stlr   r   r   re     s2    


zDialogueFile.get_strings)TFTTN)__name__
__module____qualname__rP   rL   re   r   r   r   r   r=      s   
&Lr=   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}|jdvrd|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 }|jt d |D ]~\}}	|du r:q$tj	||	}	|	dsd|	d sdq$tj|	}	|j}
|
dv rd}
t|	|||j|j|j|
d! q$d"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.rJ   z--notagsz"Strip text tags from the dialogue.rH   z--escapez+Escape quotes and other special characters.rI   dialogue)Noner.   zdialogue_%sz%s.tabz%s.txtw)Z
Identifier	CharacterZDialogueZFilenamezLine NumberzRen'Py ScriptrT   rV   Nr]   z.rpyz.rpym)rG   rJ   rH   rI   r(   F)r   	argumentsArgumentParseradd_argument
parse_argsrt   r(   rA   rB   rh   rD   basedirr   rg   loaderlistdirfilesrf   get_alphanum_keyendswithrC   r=   rJ   rH   rI   )apargsrG   ZdialfilenamerO   rK   rn   filesdirnamer@   r(   r   r   r   dialogue_commandB  sD    

	2




r   c                     s"   dd l dd   fdd} | S )Nr   c                 S   s   |   rt| S |  S r   )isdigitrY   lower)rt   r   r   r   r[   |  r\   z"get_alphanum_key.<locals>.<lambda>c                    s    fdd d| d D S )Nc                    s   g | ]} |qS r   r   ).0c)convertr   r   
<listcomp>}  r\   z6get_alphanum_key.<locals>.<lambda>.<locals>.<listcomp>z([0-9]+)r6   )splitr]   r   rer   r   r[   }  r\   )r   )Zalphanum_keyr   r   r   r   z  s    r   r   ) 
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   r   rA   renpy.translationr   r-   r;   r<   objectr=   r   r   r   register_commandr   r   r   r   <module>   s   8&R %8