ó
î%![c           @   s2   d  d l  Z  d  d l Z d e f d „  ƒ  YZ d S(   iÿÿÿÿNt   TransformVisitorc           B   sS   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d d „ Z	 d „  Z
 RS(	   sþ   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.
    c         C   s   t  j t ƒ |  _ d  S(   N(   t   collectionst   defaultdictt   listt
   transforms(   t   self(    (    s1   lib/python2.7/site-packages/astroid/transforms.pyt   __init__   s    c         C   s¦   | j  } | |  j k r | S|  j | } | } xp | D]h \ } } | d k sZ | | ƒ r6 | | ƒ } | d k	 rž | | k	 r’ t j d | ƒ n  | } qž q6 q6 W| S(   sd   Call matching transforms for the given node if any and return the
        transformed node.
        s"   node %s substituted multiple timesN(   t	   __class__R   t   Nonet   warningst   warn(   R   t   nodet   clsR   t	   orig_nodet   transform_funct	   predicatet   ret(    (    s1   lib/python2.7/site-packages/astroid/transforms.pyt
   _transform   s    	c         C   sa   t  | d ƒ rT xB | j D]4 } t | | ƒ } |  j | ƒ } t | | | ƒ q Wn  |  j | ƒ S(   Nt   _astroid_fields(   t   hasattrR   t   getattrt   _visit_generict   setattrR   (   R   R   t   fieldt   valuet   visited(    (    s1   lib/python2.7/site-packages/astroid/transforms.pyt   _visit/   s    c            se   t  | t ƒ r/ g  | D] } ˆ  j | ƒ ^ q St  | t ƒ rX t ‡  f d †  | Dƒ ƒ Sˆ  j | ƒ S(   Nc         3   s   |  ] } ˆ  j  | ƒ Vq d  S(   N(   R   (   t   .0t   child(   R   (    s1   lib/python2.7/site-packages/astroid/transforms.pys	   <genexpr>;   s    (   t
   isinstanceR   R   t   tupleR   (   R   R   R   (    (   R   s1   lib/python2.7/site-packages/astroid/transforms.pyR   7   s
     c         C   s   |  j  | j | | f ƒ d S(   s7  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   t   append(   R   t
   node_classt	   transformR   (    (    s1   lib/python2.7/site-packages/astroid/transforms.pyt   register_transform?   s    c         C   s   |  j  | j | | f ƒ d S(   s   Unregister the given transform.N(   R   t   remove(   R   R    R!   R   (    (    s1   lib/python2.7/site-packages/astroid/transforms.pyt   unregister_transformI   s    c         C   s5   g  | j  D] } |  j | ƒ ^ q
 | _  |  j | ƒ S(   s±   Walk the given astroid *tree* and transform each encountered node

        Only the nodes which have transforms registered will actually
        be replaced or changed.
        (   t   bodyR   R   (   R   t   moduleR   (    (    s1   lib/python2.7/site-packages/astroid/transforms.pyt   visitM   s    (N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R"   R$   R'   (    (    (    s1   lib/python2.7/site-packages/astroid/transforms.pyR       s   				
(   R   R	   t   objectR    (    (    (    s1   lib/python2.7/site-packages/astroid/transforms.pyt   <module>   s   