ó
†Å•Zc           @   s–   d  d l  Z d  d l Z d  d l m Z d  d l m Z m Z d  d l m Z m	 Z	 d  d l
 m Z m Z m Z m Z d e f d „  ƒ  YZ e Z d S(   iÿÿÿÿN(   t   libutils(   t
   taskhandlet   evaluate(   t	   ChangeSett   ChangeContents(   t   renamet   occurrencest   sourceutilst   importutilst   IntroduceFactoryc           B   sq   e  Z d  „  Z e d
 e j ƒ  d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z RS(   c         C   sÈ   | |  _  | |  _ |  j  j | ƒ } t j | | ƒ |  _ |  j d  k sj t |  j j ƒ  t	 j
 j j ƒ r‚ t	 j
 j j d ƒ ‚ n  |  j j ƒ  j ƒ  |  _ |  j j ƒ  j ƒ  |  _ |  j j ƒ  |  _ d  S(   Ns1   Introduce factory should be performed on a class.(   t   projectt   offsett   get_pymoduleR   t   eval_locationt
   old_pynamet   Nonet
   isinstancet
   get_objectt   ropet   baset	   pyobjectst   PyClasst
   exceptionst   RefactoringErrort   get_namet   old_namet
   get_modulet   pymodulet   get_resourcet   resource(   t   selfR
   R   R   t   this_pymodule(    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyt   __init__   s    		c         C   sc   | d k r |  j j ƒ  } n  t d | ƒ } | j d t | ƒ ƒ } |  j | | | | | ƒ | S(   s±  Get the changes this refactoring makes

        `factory_name` indicates the name of the factory function to
        be added.  If `global_factory` is `True` the factory will be
        global otherwise a static method is added to the class.

        `resources` can be a list of `rope.base.resource.File`\s that
        this refactoring should be applied on; if `None` all python
        files in the project are searched.

        s   Introduce factory method <%s>s   Collecting ChangesN(   R   R
   t   get_python_filesR   t   create_jobsett   lent   _change_module(   R   t   factory_namet   global_factoryt	   resourcest   task_handlet   changest   job_set(    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyt   get_changes   s    	
c         C   s   |  j  S(   s   Return the name of the class(   R   (   R   (    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyR   0   s    c         C   s)  | r d | } n |  j  | | ƒ } xý | D]õ } | j | j ƒ | |  j k rt |  j | | | ƒ | j ƒ  q, n  |  j | | | ƒ } | d  k	 r| rþ t j	 |  j
 | |  j ƒ }	 t j |  j ƒ }
 t j |  j
 |	 |
 | ƒ \ } } | j | | ƒ } n  | j t | | ƒ ƒ n  | j ƒ  q, Wd  S(   Ns   __rope_factory_%s_(   t   _new_function_namet   started_jobt   pathR   t   _change_resourcet   finished_jobt   _rename_occurrencesR   R    t   get_string_moduleR
   t   modnameR   t
   add_importt   replacet
   add_changeR   (   R   R'   R)   R%   t   global_R*   t   replacementt   file_t   changed_codet   new_pymoduleR3   t   imported(    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyR$   4   s*    
	c   	      C   sä   |  j  j ƒ  j ƒ  } |  j |  j |  j | | ƒ | ƒ } | d  k rT |  j j } n! t	 j
 |  j | d |  j ƒ|  _ |  j j } |  j | | ƒ } | |  } | |  j | | | | ƒ 7} | | | 7} | j t |  j | ƒ ƒ d  S(   NR   (   R   R   t	   get_scopeR1   R   R,   R   R   t   source_codeR    R2   R
   t   linest   _get_insertion_offsett   _get_factory_methodR6   R   (	   R   R)   R%   R7   t   class_scopeR>   R?   t   startt   result(    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyR/   N   s    
c         C   sH   | j  ƒ  } | j ƒ  r1 | j ƒ  d j  ƒ  } n  | j | ƒ d } | S(   Niÿÿÿÿi   (   t   get_endt
   get_scopest   get_line_end(   R   RB   R?   t
   start_lineRC   (    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyR@   `   s
    c         C   s±   d t  j |  j ƒ } | r` |  j | | ƒ d k rL t j j j d ƒ ‚ n  d | | |  j f Sd | d | |  j f } |  j | | ƒ t  j |  j ƒ } d t  j	 | | ƒ S(   Nt    i    s5   Cannot make global factory method for nested classes.s3   
def %s(*args, **kwds):
%sreturn %s(*args, **kwds)
s%   @staticmethod
def %s(*args, **kwds):
s   %sreturn %s(*args, **kwds)
s   
(
   R   t
   get_indentR
   t   _get_scope_indentsR   R   R   R   R   t   indent_lines(   R   R?   RB   R%   R7   t   unit_indentst   unindented_factoryt   indents(    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyRA   g   s    c         C   s   t  j | | j ƒ  ƒ S(   N(   R   t   get_indentst	   get_start(   R   R?   t   scope(    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyRK   w   s    c         C   s   | r
 | S|  j  d | Sd  S(   Nt   .(   R   (   R   R%   R7   (    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyR,   z   s    c         C   sF   t  j |  j |  j |  j d t ƒ} t j | | d | d | ƒ} | S(   Nt
   only_callsR   t   replace_primary(   R   t   create_finderR
   R   R   t   TrueR   t   rename_in_module(   R   R9   t   changed_nameR&   t   finderRD   (    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyR1   €   s
    	N(   t   __name__t
   __module__R    t   FalseR   R   t   NullTaskHandleR+   R   R$   R/   R@   RA   RK   R,   R1   (    (    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyR	   	   s   								(   t   rope.base.exceptionsR   t   rope.base.pyobjectst	   rope.baseR    R   R   t   rope.base.changeR   R   t   rope.refactorR   R   R   R   t   objectR	   t   IntroduceFactoryRefactoring(    (    (    s>   lib/python2.7/site-packages/rope/refactor/introduce_factory.pyt   <module>   s   "~