ó
†Å•Zc           @   s´   d  d l  m Z m Z m Z m Z m Z m Z m Z d  d l  m Z d  d l	 m
 Z
 m Z m Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d	 „  Z d
 e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   changet
   taskhandlet   evaluatet
   exceptionst	   pyobjectst   pynamest   ast(   t   libutils(   t   restructuret   sourceutilst   similarfindert   UseFunctionc           B   s˜   e  Z d  Z d „  Z d „  Z d e j ƒ  d „ Z d „  Z	 e
 d „ Z d „  Z d „  Z d „  Z d	 „  Z e d
 „ Z d „  Z d „  Z d Z d Z RS(   s'   Try to use a function wherever possiblec         C   sÇ   | |  _  | |  _ | j | ƒ } t j | | ƒ } | d  k rQ t j d ƒ ‚ n  | j ƒ  |  _	 t
 |  j	 t j ƒ s t
 |  j	 j t j ƒ r¡ t j d ƒ ‚ n  |  j	 j ƒ  j ƒ  |  _ |  j ƒ  d  S(   Ns   Unresolvable name selecteds.   Use function works for global functions, only.(   t   projectt   offsett   get_pymoduleR   t   eval_locationt   NoneR   t   RefactoringErrort
   get_objectt
   pyfunctiont
   isinstanceR   t
   PyFunctiont   parentt   PyModulet
   get_modulet   get_resourcet   resourcet   _check_returns(   t   selfR   R   R   t   this_pymodulet   pyname(    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyt   __init__
   s    		c         C   s†   |  j  j ƒ  } t | ƒ r- t j d ƒ ‚ n  t | ƒ } | d k rW t j d ƒ ‚ n  | d k r‚ t | ƒ r‚ t j d ƒ ‚ n  d  S(   Ns.   Use function should not be used on generators.i   s9   usefunction: Function has more than one return statement.s1   usefunction: return should be the last statement.(   R   t   get_astt   _yield_countR   R   t   _return_countt   _returns_last(   R   t   nodet   returns(    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR      s    c         C   sä   | d  k r |  j j ƒ  } n  t j d |  j j ƒ  ƒ } |  j | k rh t | ƒ } | j	 |  j ƒ n  x* |  j
 | | ƒ j D] } | j | ƒ q~ W|  j | k rà x9 |  j
 |  j g | d t ƒj D] } | j | ƒ qÆ Wn  | S(   Ns   Using function <%s>t   others(   R   R   t   get_python_filesR    t	   ChangeSetR   t   get_nameR   t   listt   removet   _restructuret   changest
   add_changet   False(   R   t	   resourcest   task_handleR-   t   newresourcest   c(    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyt   get_changes&   s    	c         C   s   |  j  j ƒ  S(   N(   R   R)   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyt   get_function_name7   s    c         C   s¯   |  j  ƒ  } |  j d | ƒ } d  } | r@ d |  j ƒ  g } n  t j |  j ƒ } i |  j | f d 6} i | d 6}	 t j	 |  j
 | | d |	 d | ƒ}
 |
 j d | d | ƒ S(	   Nt   import_s	   import %st   skipt    t   argst   importsR0   R1   (   t   _make_patternt
   _make_goalR   t   _module_nameR	   t   get_body_regionR   R   R   t   RestructureR   R4   (   R   R0   R1   R&   t   patternt   goalR:   t   body_regiont
   args_valueR9   t   restructuring(    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR,   :   s    c         C   s   t  |  j |  j ƒ  ƒ S(   N(   t
   find_tempsR   t	   _get_body(   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyt   _find_tempsJ   s    c         C   s   t  j |  j ƒ S(   N(   R   t   modnameR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR=   M   s    c         C   sÌ   |  j  j ƒ  } |  j ƒ  } t j | d d ƒ } t | ƒ } | j |  j ƒ  ƒ |  j ƒ  r¼ |  j	 ƒ  rw d |  j
 } n d |  j |  j
 f } t j | d |  j
 | ƒ } | j |  j ƒ n  t j | | ƒ S(   Nt   returnt   passs   ${%s}s
   %s = ${%s}s   return ${%s}(   R   t   get_param_namesRF   R   t   replaceR*   t   extendRG   t   _does_returnt   _is_expressiont   _rope_returnedt   _rope_resultt   appendR
   t   make_pattern(   R   t   paramst   bodyt	   wildcardst   replacement(    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR;   P   s    		c         C   s   t  j |  j ƒ S(   N(   R	   t   get_bodyR   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyRF   b   s    c         C   s‘   |  j  j ƒ  } |  j  j ƒ  } | r; |  j ƒ  d | } n  d | d j d „  | Dƒ ƒ f } |  j ƒ  r |  j ƒ  r d |  j | f } n  | S(   Nt   .s   %s(%s)s   , c         s   s   |  ] } d  | Vq d S(   s   ${%s}N(    (   t   .0t   p(    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pys	   <genexpr>k   s    s
   ${%s} = %s(   R   RK   R)   R=   t   joinRN   RO   RQ   (   R   R6   RT   t   function_nameRA   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR<   e   s    c         C   s+   |  j  ƒ  } t j | d d ƒ } | | k S(   Ns   return ${result}R8   (   RF   R   RL   (   R   RU   t   removed_return(    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyRN   p   s    c         C   s   t  |  j j ƒ  j ƒ d k S(   Ni   (   t   lenR   R    RU   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyRO   u   s    t   _rope__resultt   _rope__returnedN(   t   __name__t
   __module__t   __doc__R   R   R   R   t   NullTaskHandleR4   R5   t   TrueR,   RG   R=   R;   RF   R/   R<   RN   RO   RQ   RP   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR      s    									c         C   s   d t  j | d ƒ } t j |  | ƒ } g  } | j ƒ  j ƒ  d } xB | j ƒ  j ƒ  D]. \ } } t | t	 j
 ƒ rW | j | ƒ qW qW W| S(   Ns	   def f():
i   i    (   R	   t   indent_linesR   t   get_string_modulet	   get_scopet
   get_scopest	   get_namest   itemsR   R   t   AssignedNameRR   (   R   t   codet   pymodulet   resultt   function_scopet   nameR   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyRE   |   s    c         C   s    |  j  o t |  j  d t j ƒ S(   Niÿÿÿÿ(   RU   R   R   t   Return(   R$   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR#   ‡   s    c         C   s   t  ƒ  } | j |  ƒ | j S(   N(   t   _ReturnOrYieldFindert   start_walkingt   yields(   R$   t   visitor(    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR!   ‹   s    	c         C   s   t  ƒ  } | j |  ƒ | j S(   N(   Rt   Ru   R%   (   R$   Rw   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR"   ‘   s    	Rt   c           B   s>   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   d |  _  d |  _ d  S(   Ni    (   R%   Rv   (   R   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyR   ™   s    	c         C   s   |  j  d 7_  d  S(   Ni   (   R%   (   R   R$   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyt   _Return   s    c         C   s   |  j  d 7_  d  S(   Ni   (   Rv   (   R   R$   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyt   _Yield    s    c         C   s   d  S(   N(    (   R   R$   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyt   _FunctionDef£   s    c         C   s   d  S(   N(    (   R   R$   (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyt	   _ClassDef¦   s    c         C   sR   | g } t  | t j ƒ r- t j | ƒ } n  x | D] } t j | |  ƒ q4 Wd  S(   N(   R   R   t   FunctionDeft   get_child_nodest   walk(   R   R$   t   nodest   child(    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyRu   ©   s
    	(   Rb   Rc   R   Rx   Ry   Rz   R{   Ru   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyRt   —   s   					N(   t	   rope.baseR    R   R   R   R   R   R   R   t   rope.refactorR   R	   R
   t   objectR   RE   R#   R!   R"   Rt   (    (    (    s8   lib/python2.7/site-packages/rope/refactor/usefunction.pyt   <module>   s   4u				