B
    ra+                 @   s
  d dl mZ d dl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 ZG dd deZd	d
 Zd#ddZG dd deZG dd deZG dd deZG dd deZG dd deZedejZdd Zdd Zdd Zd$dd Zd!d" ZdS )%    )absolute_import)parseN)compatc             C   s   || _ |j| _| S )N)__wrapped____name__)Z	decoratedfn r   +/tmp/pip-install-0_xzzbcd/Mako/mako/util.pyupdate_wrapper   s    r
   c               @   s$   e Zd Zdd Zdd Zdd ZdS )PluginLoaderc             C   s   || _ i | _d S )N)groupimpls)selfr   r   r   r	   __init__   s    zPluginLoader.__init__c             C   sl   || j kr| j |  S dd l}xH|| j|D ]}|j| j |< | S W ddlm} |d| j|f d S )Nr   )
exceptionszCan't load plugin %s %s)r   pkg_resourcesiter_entry_pointsr   loadmakor   ZRuntimeException)r   namer   implr   r   r   r	   r      s    

zPluginLoader.loadc                s    fdd}|| j |< d S )Nc                 s8   t  } x$ ddd  D ]}t| |} qW t| S )N.   )
__import__splitgetattr)modtoken)
modulepathobjnamer   r	   r   /   s    z#PluginLoader.register.<locals>.load)r   )r   r   r   r   r   r   )r   r   r	   register.   s    zPluginLoader.registerN)r   
__module____qualname__r   r   r    r   r   r   r	   r      s   r   c             C   sP   d}xFt j| sJy|d7 }t | td W q   |dkrB Y qX qW dS )z,create and/or verify a filesystem directory.r   r   Z0775   N)ospathexistsmakedirsr   Zoctal)Zdir_triesr   r   r	   verify_directory8   s    r)   c             C   s(   | d kr|S t | ttfs | gS | S d S )N)
isinstancelisttuple)xdefaultr   r   r	   to_listF   s
    r/   c               @   s"   e Zd ZdZdddZdd ZdS )memoized_propertyz2A read-only @property that is only evaluated once.Nc             C   s   || _ |p|j| _|j| _d S )N)fget__doc__r   )r   r1   docr   r   r	   r   S   s    zmemoized_property.__init__c             C   s&   |d kr| S |  | |j| j< }|S )N)r1   __dict__r   )r   objclsresultr   r   r	   __get__X   s    zmemoized_property.__get__)N)r   r!   r"   r2   r   r8   r   r   r   r	   r0   O   s   
r0   c               @   s"   e Zd ZdZdddZdd ZdS )memoized_instancemethodzDecorate a method memoize its return value.

    Best applied to no-arg methods: memoization is not sensitive to
    argument values, and will always return the same value even when
    called with different arguments.

    Nc             C   s   || _ |p|j| _|j| _d S )N)r1   r2   r   )r   r1   r3   r   r   r	   r   i   s    z memoized_instancemethod.__init__c                s.    d krS  fdd}j |_ j|_|S )Nc                 s>   j f| |  fdd}j|_j|_|jj<  S )Nc                 s    S )Nr   )akw)r7   r   r	   memou   s    z>memoized_instancemethod.__get__.<locals>.oneshot.<locals>.memo)r1   r   r2   r4   )argsr;   r<   )r5   r   )r7   r	   oneshotr   s    z0memoized_instancemethod.__get__.<locals>.oneshot)r   r2   )r   r5   r6   r>   r   )r5   r   r	   r8   n   s    zmemoized_instancemethod.__get__)N)r   r!   r"   r2   r   r8   r   r   r   r	   r9   _   s   
r9   c               @   s   e Zd ZdZdd ZdS )SetLikeDictz0a dictionary that has some setlike methods on itc             C   s   t f | }|| |S )zproduce a 'union' of this dict and another (at the key level).

        values in the second dict take precedence over that of the first)r?   update)r   otherr-   r   r   r	   union   s    

zSetLikeDict.unionN)r   r!   r"   r2   rB   r   r   r   r	   r?      s   r?   c               @   s*   e Zd ZdZdddZdd Zd	d
 ZdS )FastEncodingBufferzma very rudimentary buffer that is faster than StringIO,
    but doesn't crash on unicode data like cStringIO.NstrictFc             C   sB   t  | _|| _|r"td| _nd| _|| _|| _| jj	| _
d S )N )collectionsdequedataencodingr   udelim
as_unicodeerrorsappendwrite)r   rI   rM   rL   r   r   r	   r      s    
zFastEncodingBuffer.__init__c             C   s   t  | _| jj| _d S )N)rF   rG   rH   rN   rO   )r   r   r   r	   truncate   s    
zFastEncodingBuffer.truncatec             C   s2   | j r | j| j| j | jS | j| jS d S )N)rI   rK   joinrH   encoderM   )r   r   r   r	   getvalue   s    zFastEncodingBuffer.getvalue)NrD   F)r   r!   r"   r2   r   rP   rS   r   r   r   r	   rC      s   
rC   c               @   sR   e Zd ZdZG dd deZdddZdd Zd	d
 Zdd Z	dd Z
dd ZdS )LRUCachea$  A dictionary-like object that stores a limited number of items,
    discarding lesser used items periodically.

    this is a rewrite of LRUCache from Myghty to use a periodic timestamp-based
    paradigm so that synchronization is not really needed.  the size management
    is inexact.
    c               @   s   e Zd Zdd Zdd ZdS )zLRUCache._Itemc             C   s   || _ || _t | _d S )N)keyvaluetimeitdefault_timer	timestamp)r   rU   rV   r   r   r	   r      s    zLRUCache._Item.__init__c             C   s
   t | jS )N)reprrV   )r   r   r   r	   __repr__   s    zLRUCache._Item.__repr__N)r   r!   r"   r   r[   r   r   r   r	   _Item   s   r\         ?c             C   s   || _ || _d S )N)capacity	threshold)r   r^   r_   r   r   r	   r      s    zLRUCache.__init__c             C   s   t | |}t |_|jS )N)dict__getitem__rW   rX   rY   rV   )r   rU   itemr   r   r	   ra      s    
zLRUCache.__getitem__c             C   s   dd t | D S )Nc             S   s   g | ]
}|j qS r   )rV   ).0ir   r   r	   
<listcomp>   s    z#LRUCache.values.<locals>.<listcomp>)r`   values)r   r   r   r	   rf      s    zLRUCache.valuesc             C   s    || kr| | S || |< |S d S )Nr   )r   rU   rV   r   r   r	   
setdefault   s    zLRUCache.setdefaultc             C   sB   t | |}|d kr0| ||}t | || n||_|   d S )N)r`   getr\   __setitem__rV   _manage_size)r   rU   rV   rb   r   r   r	   ri      s    zLRUCache.__setitem__c          	   C   sz   xtt | | j| j| j  krttt| tddd}x:|| jd  D ](}y| |j= W qF t	k
rl   P Y qFX qFW qW d S )NrY   T)rU   reverse)
lenr^   r_   sortedr`   rf   operator
attrgetterrU   KeyError)r   Zbytimerb   r   r   r	   rj      s    zLRUCache._manage_sizeN)r]   )r   r!   r"   r2   objectr\   r   ra   rf   rg   ri   rj   r   r   r   r	   rT      s   	
	rT   z([ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)c          	   C   s   |   }| d z|  }|tj}|r>|ttjd }t|	dd}|syt
|	dd W n ttfk
r   Y nX |  }t|	dd}|r|rtddS |r|dS dS W d| | X dS )a  Deduce the encoding of a Python source file (binary mode) from magic
    comment.

    It does this in the same way as the `Python interpreter`__

    .. __: http://docs.python.org/ref/encodings.html

    The ``fp`` argument should be a seekable file object in binary mode.
    r   Nasciiignorez\python refuses to compile code with both a UTF8 byte-order-mark and a magic encoding commentutf_8r   )tellseekreadline
startswithcodecsBOM_UTF8rl   _PYTHON_MAGIC_COMMENT_rematchdecoder   ImportErrorSyntaxErrorr   )fpposZline1Zhas_bommZline2r   r   r	   parse_encoding   s2    


r   c                s4   t   }|  dd fdd|D  d S )z{repr() a dictionary with the keys in order.

    Used by the lexer unit test to compare parse trees based on strings.

    {z, c                s   g | ]}d | | f qS )z%r: %rr   )rc   k)dr   r	   re   *  s    z$sorted_dict_repr.<locals>.<listcomp>})r+   keyssortrQ   )r   r   r   )r   r	   sorted_dict_repr"  s    r   c             C   s|  t | drdS d| _tddd| j}t|| _x2| jjD ]&}|jdkrN|| _q8|jdkr8|| _q8W t|j	d | _
t|j	d	 | _t|j	d
 | _t|j	d jd | _t|j	d jd j| _t|j	d j| _t|j	d jj| _t|j	d jjj| _t|j	d jjj| _t|j	d jjjj| _t|j	d jj| _t|j	d jjj| _t|j	d jjjj| _t|j	d jjjjj| _t|j	d jj| _t|j	d jjj| _t|j	d jjjj| _ t|j	d jj| _!t|j	d jj"d j| _#t|j	d jjj| _$t|j	d jjjj| _%t|j	d jjjj&j| _'t|j	d jjjj| _(t|j	d jj| _!t|j	d jj"d j| _#t|j	d jj)d | _*t|j	d jj)d	 | _+t|j	d jjj)d | _,t|j	d jjj)d	 | _-t|j	d jj)d | _.t|j	d jj)d	 | _/t|j	d jj)d
 | _0t|j	d jj)d | _1t|j	d jj)d | _2t|j	d jj)d	 | _3dS )zdAttempt to restore the required classes to the _ast module if it
    appears to be missing them
    ASTNi   a  def foo(): pass
class Bar(object): pass
if False: pass
baz = 'mako'
1 + 2 - 3 * 4 / 5
6 // 7 % 8 << 9 >> 10
11 & 12 ^ 13 | 14
15 and 16 or 17
-baz + (not +18) - ~17
baz and 'foo' or 'bar'
(mako is baz == baz) is not baz != mako
mako > baz < mako >= baz <= mako
mako in baz not in makoz	<unknown>execr   r   r            r#            	   
         )4hasattrZPyCF_ONLY_ASTcompiletypeModule__mro__r   r   r   bodyZFunctionDefZClassDefZIftargetsNamectxZStorerV   StropZSubleftZAddrightZDivZMultZRShiftZLShiftZModZFloorDivZBitOrZBitXorZBitAndOrrf   AndZInvertZNotZoperandZUAddZUSubopsZIsNotZNotEqZIsZEqZGtZLtZGtEZLtEZInZNotIn)_astr   r6   r   r   r	   restore__ast-  sb    




r   rbc             C   s&   t | |}z| }|S |  X d S )N)openreadclose)r%   moder   rH   r   r   r	   	read_file~  s
    
r   c             C   s<   t | d}z"t|}| }|r*||}|S |  X d S )Nr   )r   r   r   r}   r   )r%   r   rI   rH   r   r   r	   read_python_file  s    

r   )N)r   )
__future__r   astr   ry   rF   rn   r$   rerW   r   r   r
   rq   r   r)   r/   r0   r9   r`   r?   rC   rT   r   VERBOSEr{   r   r   r   r   r   r   r   r   r	   <module>   s0   
	#@
1Q
	