
    Æek5                     Z   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lm	Z	m
Z
mZ d dlmZ  G d d      Z G d de      Zd	 Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zd Z G d d      Zd Z G d d      Z G d d      Zy)    N)Union)
exceptions
taskhandleutils)FileContentc                   Z    e Zd ZdZddZddZd Zd Zee	j                  d               Zy)	ChangezThe base class for changes

    Rope refactorings return `Change` objects.  They can be previewed,
    committed or undone.
    Nc                      y)zaPerform the change

        .. note:: Do use this directly.  Use `Project.do()` instead.
        N selfjob_sets     2/usr/lib/python3/dist-packages/rope/base/change.pydoz	Change.do           c                      y)zcPerform the change

        .. note:: Do use this directly.  Use `History.undo()` instead.
        Nr   r   s     r   undozChange.undo   r   r   c                     t        |       S )zdReturn the description of this change

        This can be used for previewing the changes.
        )strr   s    r   get_descriptionzChange.get_description   s    
 4yr   c                     g S )z1Return the list of resources that will be changedr   r   s    r   get_changed_resourceszChange.get_changed_resources&   s    	r   c                 @    t        | j                  j                        S N)_ResourceOperationsresourceprojectr   s    r   _operationszChange._operations*   s     #4==#8#899r   r   )__name__
__module____qualname____doc__r   r   r   r   propertyr   saveitr    r   r   r   r	   r	      s;     
\\:  :r   r	   c                   l    e Zd ZdZd
dZej                  fdZej                  fdZd Z	d Z
d Zd	 Zy)	ChangeSetzA collection of `Change` objects

    This class holds a collection of changes.  This class provides
    these fields:

    * `changes`: the list of changes
    * `description`: the goal of these changes
    Nc                 .    g | _         || _        || _        y r   )changesdescriptiontime)r   r+   	timestamps      r   __init__zChangeSet.__init__:   s    &	r   c                     	 g }| j                   D ]$  }|j                  |       |j                  |       & t        j                         | _        y # t        $ r D ]  }|j                            w xY wr   )r*   r   appendr,   	Exceptionr   r   r   donechanges       r   r   zChangeSet.do?   sp    		D,, $		'"F#$ 		DI 	 	s   AA "A3c                     	 g }t        | j                        D ]$  }|j                  |       |j                  |       & y # t        $ r D ]  }|j                            w xY wr   )reversedr*   r   r0   r1   r   r2   s       r   r   zChangeSet.undoK   sh    	D"4<<0 $G$F#$  	 			s   >A "A#c                 :    | j                   j                  |       y r   )r*   r0   r   r4   s     r   
add_changezChangeSet.add_changeV   s    F#r   c                     t        |       dz   g}| j                  D ]2  }|j                  |j                                |j                  d       4 dj	                  |      S )Nz:



 )r   r*   r0   r   joinr   resultr4   s      r   r   zChangeSet.get_descriptionY   sW    d)i'(ll 	 FMM&0023MM$	  wwvr   c                    | j                   &t        j                  j                  | j                         }|j                         t        j                  j	                         k(  rd}n|j                         t        j                  j	                         t        j
                  d      z
  k(  rd}nX|j                  t        j                  j	                         j                  k(  r|j                  d      }n|j                  d      }|j                  d      }| d| d}| j                  dz   |z   S | j                  S )	Ntoday   	yesterdayz%b %dz	%d %b, %Yz%H:%M:%S z - )	r,   datetimefromtimestampdaterA   	timedeltayearstrftimer+   )r   rG   string_datestring_times       r   __str__zChangeSet.__str__`   s    99 $$22499=Dyy{hmm1133%!4!4!69K9KA9N!NO)hmm113888"mmG4"mmK8--
3K(M;-q9K##e+k99r   c                 z    t               }| j                  D ]!  }|j                  |j                                # |S r   )setr*   updater   r>   s      r   r   zChangeSet.get_changed_resourcesp   s6    ll 	:FMM&6689	:r   r   )r!   r"   r#   r$   r.   r   DEFAULT_JOB_SETr   r   r9   r   rM   r   r   r   r   r(   r(   0   s@    
 $33 
 &55 	$  r   r(   c                 2     t         j                  f fd	}|S )zA decorator for handling `taskhandle.JobSet`

    A decorator for handling `taskhandle.JobSet` for `do` and `undo`
    methods of `Change`.
    c                 j    |j                  t        |               |        |j                          y r   )started_jobr   finished_job)r   r   functions     r   callz_handle_job_set.<locals>.call~   s(    CI&r   )r   rQ   )rV   rW   s   ` r   _handle_job_setrX   w   s     &55 
 Kr   c                   J    e Zd ZdZd	dZed        Zed        Zd Zd Z	d Z
y)
ChangeContentszA class to change the contents of a file

    Fields:

    * `resource`: The `rope.base.resources.File` to change
    * `new_contents`: What to write in the file
    Nc                 .    || _         || _        || _        y r   )r   new_contentsold_contents)r   r   r\   r]   s       r   r.   zChangeContents.__init__   s     ((r   c                     | j                   | j                  j                         | _         | j                  j	                  | j                  | j
                         y r   )r]   r   readr    
write_filer\   r   s    r   r   zChangeContents.do   sC    $ $ 2 2 4D##DMM43D3DEr   c                     | j                   t        j                  d      | j                  j	                  | j
                  | j                          y )Nz+Undoing a change that is not performed yet!)r]   r   HistoryErrorr    r`   r   r   s    r   r   zChangeContents.undo   s@    $))*WXX##DMM43D3DEr   c                 4    d| j                   j                  z  S )NzChange <%s>r   pathr   s    r   rM   zChangeContents.__str__   s    t}}1111r   c                    | j                   }| j                  }|7| j                  j                         r| j                  j	                         }nd}t        j                  |j                  d      |j                  d      d| j                  j                  z   d| j                  j                  z         }dj                  t        |            S )Nr<   Tza/zb/)r\   r]   r   existsr_   difflibunified_diff
splitlinesre   r=   list)r   newoldr?   s       r   r   zChangeContents.get_description   s    ;}}##%mm((*%%NN4 NN4 4==%%%4==%%%	
 wwtF|$$r   c                     | j                   gS r   r   r   s    r   r   z$ChangeContents.get_changed_resources       r   r   r!   r"   r#   r$   r.   rX   r   r   rM   r   r   r   r   r   rZ   rZ      sG    ) F F
 F F
2% r   rZ   c                   J    e Zd ZdZd	dZed        Zed        Zd Zd Z	d Z
y)
MoveResourcezMove a resource to a new location

    Fields:

    * `resource`: The `rope.base.resources.Resource` to move
    * `new_resource`: The destination for move; It is the moved
      resource not the folder containing that resource.
    c                     |j                   | _         || _        |st        ||      }|j                         r!| j                   j	                  |      | _        y | j                   j                  |      | _        y r   )r   r   _get_destination_for_move	is_folder
get_foldernew_resourceget_file)r   r   new_locationexacts       r   r.   zMoveResource.__init__   s_    '' 4X|LL $ 7 7 ED $ 5 5l CDr   c                 d    | j                   j                  | j                  | j                         y r   )r    mover   rx   r   s    r   r   zMoveResource.do   s"    dmmT->->?r   c                 d    | j                   j                  | j                  | j                         y r   )r    r}   rx   r   r   s    r   r   zMoveResource.undo   s"    d//?r   c                 4    d| j                   j                  z  S )Nz	Move <%s>rd   r   s    r   rM   zMoveResource.__str__   s    T]]////r   c                 v    dj                  | j                  j                  | j                  j                        S )Nzrename from {}
rename to {})formatr   re   rx   r   s    r   r   zMoveResource.get_description   s1    -44MM""
 	
r   c                 2    | j                   | j                  gS r   )r   rx   r   s    r   r   z"MoveResource.get_changed_resources   s    t0011r   N)Frq   r   r   r   rs   rs      sH    D @ @ @ @0
2r   rs   c                   N    e Zd ZdZd Zed        Zed        Zd Zd Z	d Z
d Zy	)
CreateResourcezXA class to create a resource

    Fields:

    * `resource`: The resource to create
    c                     || _         y r   ro   r   r   s     r   r.   zCreateResource.__init__   	     r   c                 N    | j                   j                  | j                         y r   )r    creater   r   s    r   r   zCreateResource.do       .r   c                 N    | j                   j                  | j                         y r   r    remover   r   s    r   r   zCreateResource.undo   r   r   c                 4    d| j                   j                  z  S )NzCreate Resource <%s>rd   r   s    r   rM   zCreateResource.__str__   s    %););<<r   c                 4    d| j                   j                  z  S )Nznew file %srd   r   s    r   r   zCreateResource.get_description        2 233r   c                     | j                   gS r   ro   r   s    r   r   z$CreateResource.get_changed_resources   rp   r   c                 H    |j                   dk(  r|S |j                   dz   |z   S Nr<   /)re   )r   parentnames      r   _get_child_pathzCreateResource._get_child_path   s'    ;;"K;;$t++r   N)r!   r"   r#   r$   r.   rX   r   r   rM   r   r   r   r   r   r   r   r      sH    ! / / / /=4,r   r   c                   "     e Zd ZdZ fdZ xZS )CreateFolderzCA class to create a folder

    See docs for `CreateResource`.
    c                 z    |j                   j                  | j                  ||            }t        |   |       y r   )r   rw   r   superr.   r   r   r   r   	__class__s       r   r.   zCreateFolder.__init__
  s1    >>,,T-A-A&$-OP"r   r!   r"   r#   r$   r.   __classcell__r   s   @r   r   r         
# #r   r   c                   "     e Zd ZdZ fdZ xZS )
CreateFilezAA class to create a file

    See docs for `CreateResource`.
    c                 z    |j                   j                  | j                  ||            }t        |   |       y r   )r   ry   r   r   r.   r   s       r   r.   zCreateFile.__init__  s1    >>**4+?+?+MN"r   r   r   s   @r   r   r     r   r   r   c                   B    e Zd ZdZd Zed        Zed        Zd Zd Z	y)RemoveResourcez\A class to remove a resource

    Fields:

    * `resource`: The resource to be removed
    c                     || _         y r   ro   r   s     r   r.   zRemoveResource.__init__"  r   r   c                 N    | j                   j                  | j                         y r   r   r   s    r   r   zRemoveResource.do%  r   r   c                     t        d      )Nz0Undoing `RemoveResource` is not implemented yet.)NotImplementedErrorr   s    r   r   zRemoveResource.undo*  s    !"TUUr   c                 4    d| j                   j                  z  S )NzRemove <%s>rd   r   s    r   rM   zRemoveResource.__str__.  r   r   c                     | j                   gS r   ro   r   s    r   r   z$RemoveResource.get_changed_resources1  rp   r   N)
r!   r"   r#   r$   r.   rX   r   r   rM   r   r   r   r   r   r     s@    ! / / V V4r   r   c                 j    t        | t              r#d}| j                  D ]  }|t        |      z  } |S y)z7Counts the number of basic changes a `Change` will maker   rB   )
isinstancer(   r*   count_changes)r4   r?   childs      r   r   r   5  s;    &)$^^ 	+EmE**F	+r   c                 J    | j                  t        |      t        |            S r   )create_jobsetr   r   )task_handler4   s     r   create_job_setr   ?  s    $$S[-2GHHr   c                   H    e Zd Zd Zd Zdeeef   fdZd Z	d Z
d Zd
dZy	)r   c                     || _         |j                  | _        t        j                  j                  j	                         | _        y r   )r   
fscommandsropebaseFileSystemCommandsdirect_commandsr   r   s     r   r.   z_ResourceOperations.__init__D  s1    !,,#yy33FFHr   c                 h    | j                   j                  |      r| j                  S | j                  S r   )r   
is_ignoredr   r   r   s     r   _get_fscommandsz#_ResourceOperations._get_fscommandsI  s*    <<""8,'''r   contentsc                 X   t        |t              s6t        j                  j                  j                  ||j                        }n|}| j                  |      }|j                  |j                  |       t        | j                  j                        D ]  }|j                  |        y )N)newlines)r   bytesr   r   r   unicode_to_file_datar   r   write	real_pathrk   r   	observersresource_changed)r   r   r   datar   observers         r   r`   z_ResourceOperations.write_fileN  s    (E*99''<<!** = D
 D))(3
++T2T\\334 	0H%%h/	0r   c                     | j                  |      }|j                  |j                  |j                         t        | j                  j
                        D ]  }|j                  ||        y r   )r   r}   r   rk   r   r   resource_moved)r   r   rx   r   r   s        r   r}   z_ResourceOperations.move\  sZ    ))(3
**L,B,BCT\\334 	<H##Hl;	<r   c                     |j                         r| j                  |j                  d       n| j                  |j                         t        | j                  j
                        D ]  }|j                  |        y )Nfolder)kind)rv   _create_resourcere   rk   r   r   resource_created)r   r   r   s      r   r   z_ResourceOperations.createb  sc    !!(--h!?!!(--0T\\334 	0H%%h/	0r   c                     | j                  |      }|j                  |j                         t        | j                  j
                        D ]  }|j                  |        y r   )r   r   r   rk   r   r   resource_removed)r   r   r   r   s       r   r   z_ResourceOperations.removej  sR    ))(3
(,,-T\\334 	0H%%h/	0r   c                    | j                   j                  |      }t        j                  j	                  |      rt        j                  d|z        | j                   j                  |      }|j                  j	                         s"t        j                  d|j                  z        | j                  |      }	 |dk(  r|j                  |       y |j                  |       y # t        $ r}t        j                  |      d }~ww xY w)NzResource <%s> already existsz$Parent folder of <%s> does not existfile)r   _get_resource_pathosre   rg   r   	RopeErrorry   r   ResourceNotFoundErrorr   create_filecreate_folderOSError)r   	file_namer   resource_pathr   r   es          r   r   z$_ResourceOperations._create_resourcep  s    77	B77>>-(&&'E'UVV<<((3%%'226F  ))(3
	*v~&&}5((7 	*&&q))	*s   <C% C% %	D.DDN)r   )r!   r"   r#   r.   r   r   r   r   r`   r}   r   r   r   r   r   r   r   r   C  s7    I

0U33C-D 0<00*r   r   c                     | j                   j                  |      }t        j                  j	                  |      r#|dk7  r|dz   | j
                  z   S | j
                  S |S r   )r   r   r   re   isdirr   )r   destination	dest_paths      r   ru   ru     sT      33K@I	ww}}Y"$x}}44== r   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)ChangeToDatac                     |j                   }|j                  D cg c]
  } | |       }}|||j                  fS c c}w r   )r+   r*   r,   )r   r4   r+   r   r*   s        r   convertChangeSetzChangeToData.convertChangeSet  s>    ((,2NN;54;;;Wfkk22 <s   ;c                 \    |j                   j                  |j                  |j                  fS r   )r   re   r\   r]   r8   s     r   convertChangeContentsz"ChangeToData.convertChangeContents  s%    $$f&9&96;N;NOOr   c                 Z    |j                   j                  |j                  j                  fS r   )r   re   rx   r8   s     r   convertMoveResourcez ChangeToData.convertMoveResource  s#    $$f&9&9&>&>??r   c                 b    |j                   j                  |j                   j                         fS r   r   re   rv   r8   s     r   convertCreateResourcez"ChangeToData.convertCreateResource  $    $$foo&?&?&ABBr   c                 b    |j                   j                  |j                   j                         fS r   r   r8   s     r   convertRemoveResourcez"ChangeToData.convertRemoveResource  r   r   c                     t        |      }|t        t        fv rt        }t	        | d|j
                  z         }|j
                   ||      fS )Nconvert)typer   r   r   getattrr!   )r   r4   change_typemethods       r   __call__zChangeToData.__call__  sI    6l<44(Ky;+?+??@$$fVn55r   N)	r!   r"   r#   r   r   r   r   r   r   r   r   r   r   r     s$    3
P@CC6r   r   c                   8    e Zd Zd Zd	dZd Zd Zd Zd Zd Z	y)
DataToChangec                     || _         y r   )r   r   s     r   r.   zDataToChange.__init__  s	    r   Nc                 Z    t        ||      }|D ]  }|j                   | |              |S r   )r(   r9   )r   r+   r*   r,   r?   r   s         r   makeChangeSetzDataToChange.makeChangeSet  s4    ;- 	+Ed5k*	+r   c                 R    | j                   j                  |      }t        |||      S r   )r   ry   rZ   )r   re   r\   r]   r   s        r   makeChangeContentszDataToChange.makeChangeContents  s%    <<((.hlCCr   c                 T    | j                   j                  |      }t        ||d      S )NT)r{   )r   ry   rs   )r   old_pathnew_pathr   s       r   makeMoveResourcezDataToChange.makeMoveResource  s%    <<((2Hhd;;r   c                     |r&| j                   j                  |      }t        |      S | j                   j                  |      }t        |      S r   )r   rw   ry   r   r   re   rv   r   s       r   makeCreateResourcezDataToChange.makeCreateResource  G    ||..t4H h'' ||,,T2Hh''r   c                     |r&| j                   j                  |      }t        |      S | j                   j                  |      }t        |      S r   )r   rw   ry   r   r  s       r   makeRemoveResourcezDataToChange.makeRemoveResource  r  r   c                 6    t        | d|d   z         } ||d    S )Nmaker   rB   )r   )r   r   r   s      r   r   zDataToChange.__call__  s&    vQ/0tAwr   r   )
r!   r"   r#   r.   r   r   r  r  r	  r   r   r   r   r   r     s&    D<(( r   r   )rE   rh   r   r,   typingr   rope.base.fscommandsr   	rope.baser   r   r   r   r	   r(   rX   rZ   rs   r   r   r   r   r   r   r   ru   r   r   r   r   r   <module>r     s      	    3 3 ,!: !:HD DN/V /d&26 &2R ,V  ,F#> ## #V 6I=* =*@6 64"  " r   