3
tûœ]1  ã               @   s&   d dl Z d dlmZ G dd„ dƒZdS )é    N)Ú	lru_cachec               @   sZ   e Zd ZdZdZdd„ Zeed�dd„ ƒZdd	„ Zd
d„ Z	ddd„Z
ddd„Zdd„ ZdS )ÚTransformVisitorzþA visitor for handling transforms.

    The standard approach of using it is to call
    :meth:`~visit` with an *astroid* module and the class
    will take care of the rest, walking the tree and running the
    transforms for each encountered node.
    i'  c             C   s   t jtƒ| _d S )N)ÚcollectionsÚdefaultdictÚlistÚ
transforms)Úself© r	   ú5/tmp/pip-build-fn42xldr/astroid/astroid/transforms.pyÚ__init__   s    zTransformVisitor.__init__)Úmaxsizec             C   sd   |j }|| jkr|S | j| }x@|D ]8\}}|dks<||ƒr$||ƒ}|dk	rP|}|j |kr$P q$W |S )zdCall matching transforms for the given node if any and return the
        transformed node.
        N)Ú	__class__r   )r   ÚnodeÚclsr   Ztransform_funcÚ	predicateÚretr	   r	   r
   Ú
_transform   s    


zTransformVisitor._transformc             C   sL   t |dƒrBx6|jD ],}t||ƒ}| j|ƒ}||krt|||ƒ qW | j|ƒS )NÚ_astroid_fields)Úhasattrr   ÚgetattrÚ_visit_genericÚsetattrr   )r   r   ÚnameÚvalueÚvisitedr	   r	   r
   Ú_visit2   s    


zTransformVisitor._visitc                sZ   t |tƒr‡ fdd„|D ƒS t |tƒr<t‡ fdd„|D ƒƒS | sLt |tƒrP|S ˆ j|ƒS )Nc                s   g | ]}ˆ j |ƒ‘qS r	   )r   )Ú.0Úchild)r   r	   r
   ú
<listcomp>=   s    z3TransformVisitor._visit_generic.<locals>.<listcomp>c             3   s   | ]}ˆ j |ƒV  qd S )N)r   )r   r   )r   r	   r
   ú	<genexpr>?   s    z2TransformVisitor._visit_generic.<locals>.<genexpr>)Ú
isinstancer   ÚtupleÚstrr   )r   r   r	   )r   r
   r   ;   s    

zTransformVisitor._visit_genericNc             C   s   | j | j||fƒ dS )a7  Register `transform(node)` function to be applied on the given
        astroid's `node_class` if `predicate` is None or returns true
        when called with the node as argument.

        The transform function may return a value which is then used to
        substitute the original node in the tree.
        N)r   Úappend)r   Ú
node_classÚ	transformr   r	   r	   r
   Úregister_transformE   s    z#TransformVisitor.register_transformc             C   s   | j | j||fƒ dS )zUnregister the given transform.N)r   Úremove)r   r$   r%   r   r	   r	   r
   Úunregister_transformO   s    z%TransformVisitor.unregister_transformc                s    ‡ fdd„|j D ƒ|_ ˆ j|ƒS )z±Walk the given astroid *tree* and transform each encountered node

        Only the nodes which have transforms registered will actually
        be replaced or changed.
        c                s   g | ]}ˆ j |ƒ‘qS r	   )r   )r   r   )r   r	   r
   r   Y   s    z*TransformVisitor.visit.<locals>.<listcomp>)Úbodyr   )r   Úmoduler	   )r   r
   ÚvisitS   s    zTransformVisitor.visit)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZTRANSFORM_MAX_CACHE_SIZEr   r   r   r   r   r&   r(   r+   r	   r	   r	   r
   r      s   	



r   )r   Ú	functoolsr   r   r	   r	   r	   r
   Ú<module>	   s   