B
    d)                 @   s   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Zd	d
 Zd#ddZG dd dZG dd dZG dd deZG dd d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 )%    )parseN   )importlib_metadata_getc             C   s   || _ |j| _| S )N)__wrapped____name__)Z	decoratedfn r   V/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/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 x0t| jD ]"}|j|kr"|j| j |< | S q"W ddlm} |d| j|f d S )Nr   )
exceptionszCan't load plugin %s %s)r   r   r   nameloadmakor   ZRuntimeException)r   r   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.r   )
__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   sJ   d}x@t j| sDy|d7 }t | d W q   |dkr< Y qX qW dS )z,create and/or verify a filesystem directory.r   r   i     N)ospathexistsmakedirs)Zdir_triesr   r   r	   verify_directory5   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_listC   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   r.   docr   r   r	   r   P   s    zmemoized_property.__init__c             C   s&   |d kr| S |  | |j| j< }|S )N)r.   __dict__r   )r   objclsresultr   r   r	   __get__U   s    zmemoized_property.__get__)N)r   r   r   r/   r   r5   r   r   r   r	   r-   L   s   
r-   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)r.   r/   r   )r   r.   r0   r   r   r	   r   f   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)r4   r   r	   memor   s    z>memoized_instancemethod.__get__.<locals>.oneshot.<locals>.memo)r.   r   r/   r1   )argsr8   r9   )r2   r   )r4   r	   oneshoto   s    z0memoized_instancemethod.__get__.<locals>.oneshot)r   r/   )r   r2   r3   r;   r   )r2   r   r	   r5   k   s    zmemoized_instancemethod.__get__)N)r   r   r   r/   r   r5   r   r   r   r	   r6   \   s   
r6   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   r/   r?   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 )FastEncodingBufferzVa very rudimentary buffer that is faster than StringIO,
    and supports unicode data.Nstrictc             C   s*   t  | _|| _d| _|| _| jj| _d S )N )collectionsdequedataencodingdelimerrorsappendwrite)r   rF   rH   r   r   r	   r      s
    
zFastEncodingBuffer.__init__c             C   s   t  | _| jj| _d S )N)rC   rD   rE   rI   rJ   )r   r   r   r	   truncate   s    
zFastEncodingBuffer.truncatec             C   s2   | j r | j| j| j | jS | j| jS d S )N)rF   rG   joinrE   encoderH   )r   r   r   r	   getvalue   s    zFastEncodingBuffer.getvalue)NrA   )r   r   r   r/   r   rK   rN   r   r   r   r	   r@      s   
r@   c               @   sP   e Zd ZdZG dd d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   rP   rQ   r   r   r	   r      s    zLRUCache._Item.__init__c             C   s
   t | jS )N)reprrQ   )r   r   r   r	   __repr__   s    zLRUCache._Item.__repr__N)r   r   r   r   rV   r   r   r   r	   _Item   s   rW         ?c             C   s   || _ || _d S )N)capacity	threshold)r   rY   rZ   r   r   r	   r      s    zLRUCache.__init__c             C   s   t | |}t |_|jS )N)dict__getitem__rR   rS   rT   rQ   )r   rP   itemr   r   r	   r\      s    
zLRUCache.__getitem__c             C   s   dd t | D S )Nc             S   s   g | ]
}|j qS r   )rQ   ).0ir   r   r	   
<listcomp>   s    z#LRUCache.values.<locals>.<listcomp>)r[   values)r   r   r   r	   ra      s    zLRUCache.valuesc             C   s   || kr| | S || |< |S )Nr   )r   rP   rQ   r   r   r	   
setdefault   s    zLRUCache.setdefaultc             C   sB   t | |}|d kr0| ||}t | || n||_|   d S )N)r[   getrW   __setitem__rQ   _manage_size)r   rP   rQ   r]   r   r   r	   rd      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 )NrT   T)rP   reverse)
lenrY   rZ   sortedr[   ra   operator
attrgetterrP   KeyError)r   Zbytimer]   r   r   r	   re      s    zLRUCache._manage_sizeN)rX   )r   r   r   r/   rW   r   r\   ra   rb   rd   re   r   r   r   r	   rO      s   	
	rO   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_UTF8rg   _PYTHON_MAGIC_COMMENT_rematchdecoder   ImportErrorSyntaxErrorr   )fpposline1Zhas_bomm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             3   s   | ]}d | | f V  qdS )z%r: %rNr   )r^   k)dr   r	   	<genexpr>"  s    z#sorted_dict_repr.<locals>.<genexpr>})r(   keyssortrL   )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: 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hasattrPyCF_ONLY_ASTcompiletypeModule__mro__r   r   r   bodyFunctionDefClassDefIftargetsNamectxStorerQ   StropSubleftAddrightDivMultRShiftLShiftModFloorDivBitOrBitXorBitAndOrra   AndInvertNotoperandUAddUSubZopsIsNotNotEqIsEqGtLtGtELtEInNotIn)_astr}   r3   r   r   r	   restore__ast%  sb    




r   rbc          	   C   s   t | |
}| S Q R X d S )N)openread)r"   moderz   r   r   r	   	read_filev  s    r   c             C   s<   t | d}z"t|}| }|r*||}|S |  X d S )Nr   )r   r   r   rw   close)r"   rz   rF   rE   r   r   r	   read_python_file{  s    

r   )N)r   )astr   rs   rC   ri   r!   rerR   compatr   r
   r   r&   r,   r-   r6   r[   r<   r@   rO   r   VERBOSEru   r   r   r   r   r   r   r   r   r	   <module>   s.   
	#?
1Q
