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mZ d dlZd dlZd dlZejjZejjZdd Zddd	Zdd
dZdddZdd Zejdd Z dd Z!dS )    )divisionabsolute_importwith_statementprint_functionunicode_literals)PY2
basestringbchrbordchropenpystrrangeroundstrtobytesunicode)AnyNc                 C   s@   |  d\}}}|sdS tjj|d}|du r4dS t||S )z
    Given `d`, a string given a displayable, returns the displayable it
    corresponds to or None if it does not correspond to one.
    :N)	partitionrenpyconfigdisplayable_prefixgetdisplayable)dprefixcolonargfn r    renpy/easy.pylookup_displayable_prefix&   s    r"   Tc                 C   s  t | tjjjr| S | d u r | S t | tr| s8tdn&d| v r^tjjr^|r^tjj	j
| |dS t| }|d urr|S | d dkrtj| S d| v rtj| S tjt|  S t | trtj| S t | trtjj	j
| |dS t| dr| S | du s| d	u r
| S td
| f d S )N0An empty string cannot be used as a displayable.[scoper   #.
_duplicateTFNot a displayable: %r
isinstancer   displaycoreDisplayabler   	Exceptionr   dynamic_imagesimageDynamicImager"   storeSolidImageImageReferencetuplesplitColorlisthasattr)r   r&   dynamicrvr    r    r!   displayable_or_none8   s4    




r?   c                 C   s  t | tjjjr| S t | tr| s,tdn"d| v rNtjjrNtjj	j
| |dS t| }|durb|S | d dkrztj| S d| v rtj| S tjt|  S t | trtj| S t | trtjj	j
| |dS t| dr| S | d	u s| d
u r| S td| f dS )z
    :doc: udd_utility
    :name: renpy.displayable

    This takes `d`, which may be a displayable object or a string. If it's
    a string, it converts that string into a displayable using the usual
    rules.
    r#   r$   r%   Nr   r'   r(   r)   TFr*   r+   )r   r&   r>   r    r    r!   r   a   s0    





r   c                    s  t  ts g  fdd} D ]}t |ts0q |durd|v r|rNt|}ni }tjjj| D ]N}||d< tjj	||dddd	 }||rt
|    S |dur`|| q`q tjj	||dddd	 }||rt
|  S |dur || q  d
 }||rt
|ddS dS )z
    Substitutes a scope into `d`, then returns a displayable.

    If `prefix` is given, and a prefix has been given a prefix search is
    performed until a file is found. (Only a file can be used in this case.)
    c                    sX   t j| rdS t j| r dS t| r,dS t dkrTt jjd urTt j| rTdS d S )NT   )	r   exportsimage_existsloaderloadabler"   lenr   missing_image_callback)namer   r    r!   find   s    zdynamic_image.<locals>.findNz[prefix_prefix_TF)r&   force	translater   )r=   )r,   r;   r   dictr   	styledata	stylesetsprefix_searchsubstitutions
substituter?   append)r   r&   r   searchrI   ipr>   r    rH   r!   dynamic_image   s4    



rX   c                 C   s&   t j| } | d ur"t jj|  d S )N)r   easyr?   r-   predictr   rH   r    r    r!   rZ      s    rZ   c                 c   s.   t   }d V  td| t   | d  d S )Nz{0}: {1:.2f} msg     @@)timeprintformat)rG   startr    r    r!   timed   s    r_   c           	      G   s   g }|D ]}| i  q| s |S tt||}|  D ]F\}}|D ]*\}}||rB|||t|d <  q6qBtd|q6|S )a  
    :doc: other

    Splits up `properties` into multiple dictionaries, one per `prefix`. This
    function checks each key in properties against each prefix, in turn.
    When a prefix matches, the prefix is stripped from the key, and the
    resulting key is mapped to the value in the corresponding dictionary.

    If no prefix matches, an exception is thrown. (The empty string, "",
    can be used as the last prefix to create a catch-all dictionary.)

    For example, this splits properties beginning with text from
    those that do not::

        text_properties, button_properties = renpy.split_properties(properties, "text_", "")
    Nz*Property {} begins with an unknown prefix.)rT   r;   zipitems
startswithrE   r0   r]   )	
propertiesprefixesr>   _iprefix_dkvr   r   r    r    r!   split_properties   s    
ri   )NT)N)NNN)"
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   typingr   
contextlibr[   r   colorr:   r"   r?   r   rX   rZ   contextmanagerr_   ri   r    r    r    r!   <module>   s   8
)
.
C
