3
՛gY*                 @   s   d dl Z d dlZd dlZd dlZd dlmZ d dl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 jde jZdd Zdd Zdd Zd"ddZdd  ZdS )#    N)compatc             C   s   || _ |j| _| S )N)__wrapped____name__)Z	decoratedfn r   +/usr/lib/python3/dist-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 dd l}xH|j| j|D ]}|j| j |< |j S W ddlm} |jd| j|f d S )Nr   )
exceptionszCan't load plugin %s %s)r   pkg_resourcesZ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$ j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j| sJy|d7 }t j| tj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)dirZtriesr   r   r   verify_directory4   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_listB   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   O   s    zmemoized_property.__init__c             C   s&   |d kr| S | j | |j| j< }|S )N)r.   __dict__r   )r   objclsresultr   r   r   __get__T   s    zmemoized_property.__get__)N)r   r   r   r/   r   r5   r   r   r   r   r-   K   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   e   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   <lambda>p   s    zBmemoized_instancemethod.__get__.<locals>.oneshot.<locals>.<lambda>)r.   r   r/   r1   )argsr8   memo)r2   r   )r4   r   oneshotn   s    z0memoized_instancemethod.__get__.<locals>.oneshot)r   r/   )r   r2   r3   r<   r   )r2   r   r   r5   j   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 | }|j| |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=   z   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 j | _|| _|r"tjd| _nd| _|| _|| _| jj	| _
d S )N )collectionsdequedataencodingr   udelim
as_unicodeerrorsappendwrite)r   rG   rK   rJ   r   r   r   r      s    
zFastEncodingBuffer.__init__c             C   s   t j | _| jj| _d S )N)rD   rE   rF   rL   rM   )r   r   r   r   truncate   s    
zFastEncodingBuffer.truncatec             C   s2   | j r | jj| jj| j | jS | jj| jS d S )N)rG   rI   joinrF   encoderK   )r   r   r   r   getvalue   s    zFastEncodingBuffer.getvalue)NrB   F)r   r   r   r/   r   rN   rQ   r   r   r   r   rA      s   
rA   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j | _d S )N)keyvaluer   	time_func	timestamp)r   rS   rT   r   r   r   r      s    zLRUCache._Item.__init__c             C   s
   t | jS )N)reprrT   )r   r   r   r   __repr__   s    zLRUCache._Item.__repr__N)r   r   r   r   rX   r   r   r   r   _Item   s   rY         ?c             C   s   || _ || _d S )N)capacity	threshold)r   r[   r\   r   r   r   r      s    zLRUCache.__init__c             C   s   t j| |}tj |_|jS )N)dict__getitem__r   rU   rV   rT   )r   rS   itemr   r   r   r^      s    
zLRUCache.__getitem__c             C   s   dd t j| D S )Nc             S   s   g | ]
}|j qS r   )rT   ).0ir   r   r   
<listcomp>   s    z#LRUCache.values.<locals>.<listcomp>)r]   values)r   r   r   r   rc      s    zLRUCache.valuesc             C   s    || kr| | S || |< |S d S )Nr   )r   rS   rT   r   r   r   
setdefault   s    zLRUCache.setdefaultc             C   sB   t j| |}|d kr0| j||}t j| || n||_| j  d S )N)r]   getrY   __setitem__rT   _manage_size)r   rS   rT   r_   r   r   r   rf      s    zLRUCache.__setitem__c             C   sz   xtt | | j| j| j  krtttj| tjddd}x:|| jd  D ](}y| |j= W qF t	k
rl   P Y qFX qFW qW d S )NrV   T)rS   reverse)
lenr[   r\   sortedr]   rc   operator
attrgetterrS   KeyError)r   Zbytimer_   r   r   r   rg      s    
zLRUCache._manage_sizeN)rZ   )r   r   r   r/   objectrY   r   r^   rc   rd   rf   rg   r   r   r   r   rR      s   

	rR   z([ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)c             C   s   | j  }| jd z| j }|jtj}|r>|ttjd }tj|j	dd}|syddl
}|j|j	dd W n ttfk
r   Y nX | j }tj|j	dd}|r|rtddS |r|jdS dS W d| j| 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_UTF8ri   _PYTHON_MAGIC_COMMENT_rematchdecodeparserZsuiteImportErrorSyntaxErrorr
   )fpposZline1Zhas_bommr{   Zline2r   r   r   parse_encoding   s4    


r   c                s4   t  j }|j  ddj 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   )r`   k)dr   r   rb     s    z$sorted_dict_repr.<locals>.<listcomp>})r(   keyssortrO   )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
    ASTN   	   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             
         i   )4hasattrZPyCF_ONLY_ASTcompiletypeZModule__mro__r   r   r   ZbodyZFunctionDefZClassDefZIfZtargetsNameZctxZStorerT   ZStropZSubleftZAddrightZDivZMultZRShiftZLShiftZModZFloorDivZBitOrZBitXorZBitAndZOrrc   ZAndZInvertZNotZoperandZUAddZUSubZopsZIsNotZNotEqZIsZEqZGtZLtZGtEZLtEZInZNotIn)Z_astr   r3   r   r   r   restore__ast   s\    




r   rbc          
   C   s&   t | |}z|j }|S |j  X d S )N)openreadclose)r"   moder~   rF   r   r   r   	read_filel  s
    
r   c          
   C   s<   t | d}z"t|}|j }|r*|j|}|S |j  X d S )Nr   )r   r   r   rz   r   )r"   r~   rG   rF   r   r   r   read_python_fileu  s    

r   )N)r   )rerD   rv   r!   r   r   rk   r   rn   r	   r&   r,   r-   r6   r]   r=   rA   rR   r   VERBOSErx   r   r   r   r   r   r   r   r   r   <module>   s,   
	=0L
	