B
    V5                 @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZmZm	Z	 G dd de
ZG dd deZdd ZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZdd Zdd ZG dd de
Zdd ZG dd de
ZG dd  d e
ZdS )!    N)
taskhandle
exceptionsutilsc               @   sF   e Zd ZdZdddZdddZdd Zd	d
 Zee	j
dd ZdS )ChangezThe base class for changes

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

        .. note:: Do use this directly.  Use `Project.do()` instead.
        N )selfjob_setr   r   /lib/python3.7/site-packages/rope/base/change.pydo   s    z	Change.doc             C   s   dS )zcPerform the change

        .. note:: Do use this directly.  Use `History.undo()` instead.
        Nr   )r   r   r   r   r	   undo   s    zChange.undoc             C   s   t | S )zdReturn the description of this change

        This can be used for previewing the changes.
        )str)r   r   r   r	   get_description   s    zChange.get_descriptionc             C   s   g S )z1Return the list of resources that will be changedr   )r   r   r   r	   get_changed_resources$   s    zChange.get_changed_resourcesc             C   s   t | jjS )N)_ResourceOperationsresourceproject)r   r   r   r	   _operations(   s    zChange._operations)N)N)__name__
__module____qualname____doc__r
   r   r   r   propertyr   Zsaveitr   r   r   r   r	   r   
   s   

r   c               @   sZ   e Zd ZdZdddZe fddZe fddZd	d
 Z	dd Z
dd Zdd ZdS )	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             C   s   g | _ || _|| _d S )N)changesdescriptiontime)r   r   Z	timestampr   r   r	   __init__8   s    zChangeSet.__init__c             C   sh   y6g }x"| j D ]}|| || qW t | _W n, tk
rb   x|D ]}|  qLW  Y nX d S )N)r   r
   appendr   	Exceptionr   )r   r   donechanger   r   r	   r
   =   s    

zChangeSet.doc             C   sb   y0g }x&t | jD ]}|| || qW W n, tk
r\   x|D ]}|  qFW  Y nX d S )N)reversedr   r   r   r   r
   )r   r   r   r    r   r   r	   r   I   s    

zChangeSet.undoc             C   s   | j | d S )N)r   r   )r   r    r   r   r	   
add_changeT   s    zChangeSet.add_changec             C   s@   t | d g}x&| jD ]}||  |d qW d|S )Nz:



 )r   r   r   r   join)r   resultr    r   r   r	   r   W   s
    zChangeSet.get_descriptionc             C   s   | j d k	rtj| j }| tj kr0d}nJ| tj td krRd}n(|jtj jkrp|d}n
|d}|d}d||f }| jd | S | jS )	Ntoday   Z	yesterdayz%b %dz	%d %b, %Yz%H:%M:%Sz%s %s z - )	r   datetimeZfromtimestampdater'   Z	timedeltaZyearZstrftimer   )r   r*   Zstring_dateZstring_timer   r   r	   __str__^   s    


zChangeSet.__str__c             C   s(   t  }x| jD ]}||  qW |S )N)setr   updater   )r   r&   r    r   r   r	   r   o   s    zChangeSet.get_changed_resources)N)r   r   r   r   r   r   
NullJobSetr
   r   r"   r   r+   r   r   r   r   r	   r   .   s   
r   c                s   t  f fdd	}|S )zA decorator for handling `taskhandle.JobSet`\s

    A decorator for handling `taskhandle.JobSet`\s for `do` and `undo`
    methods of `Change`\s.
    c                s"   | t|   |  |  d S )N)Zstarted_jobr   Zfinished_job)r   r   )functionr   r	   call|   s    z_handle_job_set.<locals>.call)r   r.   )r/   r0   r   )r/   r	   _handle_job_setv   s    r1   c               @   sJ   e Zd ZdZdddZedd Zedd Zd	d
 Zdd Z	dd Z
dS )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             C   s   || _ || _|| _d S )N)r   new_contentsold_contents)r   r   r3   r4   r   r   r	   r      s    zChangeContents.__init__c             C   s,   | j d kr| j | _ | j| j| j d S )N)r4   r   readr   
write_filer3   )r   r   r   r	   r
      s    
zChangeContents.doc             C   s*   | j d krtd| j| j| j  d S )Nz+Undoing a change that is not performed yet!)r4   r   ZHistoryErrorr   r6   r   )r   r   r   r	   r      s    
zChangeContents.undoc             C   s   d| j j S )NzChange <%s>)r   path)r   r   r   r	   r+      s    zChangeContents.__str__c             C   sh   | j }| j}|d kr.| j r*| j }nd}t|d|dd| jj d| jj }d	t
|S )Nr$   Tza/zb/)r3   r4   r   existsr5   difflibZunified_diff
splitlinesr7   r%   list)r   newoldr&   r   r   r	   r      s    
zChangeContents.get_descriptionc             C   s   | j gS )N)r   )r   r   r   r	   r      s    z$ChangeContents.get_changed_resources)N)r   r   r   r   r   r1   r
   r   r+   r   r   r   r   r   r	   r2      s   
r2   c               @   sJ   e Zd ZdZdddZedd Zedd Zd	d
 Zdd Z	dd Z
dS )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.
    Fc             C   sF   |j | _ || _|st||}| r4| j || _n| j || _d S )N)r   r   _get_destination_for_move	is_folder
get_foldernew_resourceget_file)r   r   Znew_locationexactr   r   r	   r      s    
zMoveResource.__init__c             C   s   | j | j| j d S )N)r   mover   rB   )r   r   r   r	   r
      s    zMoveResource.doc             C   s   | j | j| j d S )N)r   rE   rB   r   )r   r   r   r	   r      s    zMoveResource.undoc             C   s   d| j j S )Nz	Move <%s>)r   r7   )r   r   r   r	   r+      s    zMoveResource.__str__c             C   s   d| j j| jjf S )Nzrename from %s
rename to %s)r   r7   rB   )r   r   r   r	   r      s    zMoveResource.get_descriptionc             C   s   | j | jgS )N)r   rB   )r   r   r   r	   r      s    z"MoveResource.get_changed_resourcesN)F)r   r   r   r   r   r1   r
   r   r+   r   r   r   r   r   r	   r>      s   

r>   c               @   sP   e Zd ZdZdd Zedd Zedd Zdd	 Zd
d Z	dd Z
dd ZdS )CreateResourcezXA class to create a resource

    Fields:

    * `resource`: The resource to create
    c             C   s
   || _ d S )N)r   )r   r   r   r   r	   r      s    zCreateResource.__init__c             C   s   | j | j d S )N)r   creater   )r   r   r   r	   r
      s    zCreateResource.doc             C   s   | j | j d S )N)r   remover   )r   r   r   r	   r      s    zCreateResource.undoc             C   s   d| j j S )NzCreate Resource <%s>)r   r7   )r   r   r   r	   r+      s    zCreateResource.__str__c             C   s   d| j j S )Nznew file %s)r   r7   )r   r   r   r	   r      s    zCreateResource.get_descriptionc             C   s   | j gS )N)r   )r   r   r   r	   r      s    z$CreateResource.get_changed_resourcesc             C   s    |j dkr|S |j d | S d S )Nr$   /)r7   )r   parentnamer   r   r	   _get_child_path   s    
zCreateResource._get_child_pathN)r   r   r   r   r   r1   r
   r   r+   r   r   rL   r   r   r   r	   rF      s   rF   c                   s    e Zd ZdZ fddZ  ZS )CreateFolderzCA class to create a folder

    See docs for `CreateResource`.
    c                s(   |j | ||}tt| | d S )N)r   rA   rL   superrM   r   )r   rJ   rK   r   )	__class__r   r	   r     s    zCreateFolder.__init__)r   r   r   r   r   __classcell__r   r   )rO   r	   rM      s   rM   c                   s    e Zd ZdZ fddZ  ZS )
CreateFilezAA class to create a file

    See docs for `CreateResource`.
    c                s(   |j | ||}tt| | d S )N)r   rC   rL   rN   rQ   r   )r   rJ   rK   r   )rO   r   r	   r     s    zCreateFile.__init__)r   r   r   r   r   rP   r   r   )rO   r	   rQ   	  s   rQ   c               @   s@   e Zd ZdZdd Zedd Zedd Zdd	 Zd
d Z	dS )RemoveResourcez\A class to remove a resource

    Fields:

    * `resource`: The resource to be removed
    c             C   s
   || _ d S )N)r   )r   r   r   r   r	   r     s    zRemoveResource.__init__c             C   s   | j | j d S )N)r   rH   r   )r   r   r   r	   r
     s    zRemoveResource.doc             C   s   t dd S )Nz0Undoing `RemoveResource` is not implemented yet.)NotImplementedError)r   r   r   r	   r   $  s    zRemoveResource.undoc             C   s   d| j j S )NzRemove <%s>)r   r7   )r   r   r   r	   r+   )  s    zRemoveResource.__str__c             C   s   | j gS )N)r   )r   r   r   r	   r   ,  s    z$RemoveResource.get_changed_resourcesN)
r   r   r   r   r   r1   r
   r   r+   r   r   r   r   r	   rR     s   rR   c             C   s2   t | tr.d}x| jD ]}|t|7 }qW |S dS )z7Counts the number of basic changes a `Change` will maker   r(   )
isinstancer   r   count_changes)r    r&   childr   r   r	   rU   0  s    
rU   c             C   s   |  t|t|S )N)Zcreate_jobsetr   rU   )Ztask_handler    r   r   r	   create_job_set:  s    rW   c               @   sF   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdddZ	dS )r   c             C   s    || _ |j| _tjj | _d S )N)r   
fscommandsropebaseZFileSystemCommandsdirect_commands)r   r   r   r   r	   r   @  s    z_ResourceOperations.__init__c             C   s   | j |r| jS | jS )N)r   Z
is_ignoredr[   rX   )r   r   r   r   r	   _get_fscommandsE  s    z#_ResourceOperations._get_fscommandsc             C   sJ   t jj|}| |}||j| xt| jj	D ]}|
| q4W d S )N)rY   rZ   rX   Zunicode_to_file_datar\   write	real_pathr;   r   	observersZresource_changed)r   r   contentsdatarX   observerr   r   r	   r6   J  s
    
z_ResourceOperations.write_filec             C   s@   |  |}||j|j x t| jjD ]}||| q(W d S )N)r\   rE   r^   r;   r   r_   Zresource_moved)r   r   rB   rX   rb   r   r   r	   rE   Q  s    
z_ResourceOperations.movec             C   sJ   |  r| j|jdd n| |j xt| jjD ]}|| q4W d S )NZfolder)kind)r@   _create_resourcer7   r;   r   r_   Zresource_created)r   r   rb   r   r   r	   rG   W  s
    z_ResourceOperations.createc             C   s:   |  |}||j xt| jjD ]}|| q$W d S )N)r\   rH   r^   r;   r   r_   Zresource_removed)r   r   rX   rb   r   r   r	   rH   _  s    
z_ResourceOperations.removefilec          
   C   s   | j |}tj|r&td| | j |}|j sLt	d|j | 
|}y"|dkrl|| n
|| W n, tk
r } zt|W d d }~X Y nX d S )NzResource <%s> already existsz$Parent folder of <%s> does not existre   )r   _get_resource_pathosr7   r8   r   Z	RopeErrorrC   rJ   ZResourceNotFoundErrorr\   Zcreate_fileZcreate_folderIOError)r   	file_namerc   resource_pathr   rX   er   r   r	   rd   e  s    

z$_ResourceOperations._create_resourceN)re   )
r   r   r   r   r\   r6   rE   rG   rH   rd   r   r   r   r	   r   >  s   r   c             C   s8   | j |}tj|r4|dkr.|d | j S | jS |S )Nr$   rI   )r   rf   rg   r7   isdirrK   )r   ZdestinationZ	dest_pathr   r   r	   r?   x  s    r?   c               @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )ChangeToDatac             C   s4   |j }g }x|jD ]}|| | qW |||jfS )N)r   r   r   r   )r   r    r   r   rV   r   r   r	   convertChangeSet  s
    zChangeToData.convertChangeSetc             C   s   |j j|j|jfS )N)r   r7   r3   r4   )r   r    r   r   r	   convertChangeContents  s    z"ChangeToData.convertChangeContentsc             C   s   |j j|jjfS )N)r   r7   rB   )r   r    r   r   r	   convertMoveResource  s    z ChangeToData.convertMoveResourcec             C   s   |j j|j  fS )N)r   r7   r@   )r   r    r   r   r	   convertCreateResource  s    z"ChangeToData.convertCreateResourcec             C   s   |j j|j  fS )N)r   r7   r@   )r   r    r   r   r	   convertRemoveResource  s    z"ChangeToData.convertRemoveResourcec             C   s6   t |}|ttfkrt}t| d|j }|j||fS )NZconvert)typerM   rQ   rF   getattrr   )r   r    Zchange_typemethodr   r   r	   __call__  s
    zChangeToData.__call__N)	r   r   r   rn   ro   rp   rq   rr   rv   r   r   r   r	   rm     s   rm   c               @   sF   e Zd Zdd ZdddZdd Zdd	 Zd
d Zdd Zdd Z	dS )DataToChangec             C   s
   || _ d S )N)r   )r   r   r   r   r	   r     s    zDataToChange.__init__Nc             C   s*   t ||}x|D ]}|| | qW |S )N)r   r"   )r   r   r   r   r&   rV   r   r   r	   makeChangeSet  s    

zDataToChange.makeChangeSetc             C   s   | j |}t|||S )N)r   rC   r2   )r   r7   r3   r4   r   r   r   r	   makeChangeContents  s    zDataToChange.makeChangeContentsc             C   s   | j |}t||ddS )NT)rD   )r   rC   r>   )r   Zold_pathnew_pathr   r   r   r	   makeMoveResource  s    zDataToChange.makeMoveResourcec             C   s&   |r| j |}n| j |}t|S )N)r   rA   rC   rF   )r   r7   r@   r   r   r   r	   makeCreateResource  s    zDataToChange.makeCreateResourcec             C   s&   |r| j |}n| j |}t|S )N)r   rA   rC   rR   )r   r7   r@   r   r   r   r	   makeRemoveResource  s    zDataToChange.makeRemoveResourcec             C   s   t | d|d  }||d  S )NZmaker   r(   )rt   )r   ra   ru   r   r   r	   rv     s    zDataToChange.__call__)N)
r   r   r   r   rx   ry   r{   r|   r}   rv   r   r   r   r	   rw     s   
rw   )r)   r9   rg   r   Zrope.base.fscommandsrY   Z	rope.baser   r   r   objectr   r   r1   r2   r>   rF   rM   rQ   rR   rU   rW   r   r?   rm   rw   r   r   r   r	   <module>   s(   $H0'#
:
