B
    –ìZ/  ã               @   sð   d Z ddlZddlZddlZddlm  m  mZ ye W n e	k
rT   e
ZY nX 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ddd„Zddd„Zddd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) a  Project file system commands.

This modules implements file system operations used by rope.  Different
version control systems can be supported by implementing the interface
provided by `FileSystemCommands` class.  See `SubversionCommands` and
`MercurialCommands` for example.

é    Nc          
   C   s\   t  | ¡}tttttdœ}x:|D ]2}||kr y|| | ƒS  ttfk
rP   Y q X q W tƒ S )N)z.hgz.svnz.gitZ_svnZ_darcs)	ÚosÚlistdirÚMercurialCommandsÚSubversionCommandsÚGITCommandsÚDarcsCommandsÚImportErrorÚOSErrorÚFileSystemCommands)ÚrootÚdirlistÚcommandsÚkey© r   ú3lib/python3.7/site-packages/rope/base/fscommands.pyÚcreate_fscommands   s    


r   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )r
   c             C   s   t |dƒ ¡  d S )NÚw)ÚopenÚclose)ÚselfÚpathr   r   r   Úcreate_file&   s    zFileSystemCommands.create_filec             C   s   t  |¡ d S )N)r   Úmkdir)r   r   r   r   r   Úcreate_folder)   s    z FileSystemCommands.create_folderc             C   s   t  ||¡ d S )N)ÚshutilÚmove)r   r   Únew_locationr   r   r   r   ,   s    zFileSystemCommands.movec             C   s&   t j |¡rt  |¡ n
t |¡ d S )N)r   r   ÚisfileÚremover   Zrmtree)r   r   r   r   r   r   /   s    zFileSystemCommands.removec             C   s(   t |dƒ}z| |¡ W d | ¡  X d S )NÚwb)r   Úwriter   )r   r   ÚdataZfile_r   r   r   r    5   s    
zFileSystemCommands.writeN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r    r   r   r   r   r
   $   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 )r   c             G   s   t ƒ | _dd l}| ¡ | _d S )Nr   )r
   Únormal_actionsÚpysvnZClientÚclient)r   Úargsr&   r   r   r   Ú__init__?   s    zSubversionCommands.__init__c             C   s    | j  |¡ | jj|dd d S )NT)Úforce)r%   r   r'   Úadd)r   r   r   r   r   r   D   s    zSubversionCommands.create_filec             C   s    | j  |¡ | jj|dd d S )NT)r*   )r%   r   r'   r+   )r   r   r   r   r   r   H   s    z SubversionCommands.create_folderc             C   s   | j j||dd d S )NT)r*   )r'   r   )r   r   r   r   r   r   r   L   s    zSubversionCommands.movec             C   s   | j j|dd d S )NT)r*   )r'   r   )r   r   r   r   r   r   O   s    zSubversionCommands.removec             C   s   | j  ||¡ d S )N)r%   r    )r   r   r!   r   r   r   r    R   s    zSubversionCommands.writeN)	r"   r#   r$   r)   r   r   r   r   r    r   r   r   r   r   =   s   r   c               @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )r   c             C   sÆ   |   ¡ | _tƒ | _y | jjjddddddd| _W nz   | jj ¡ | _| j ddd¡ | j ddd¡ | j ddd¡ | j dd	d¡ | j dd
d¡ | j ddd¡ Y nX | jj | j|¡| _d S )NFT)ÚverboseÚdebugÚquietÚinteractiveÚ	tracebackÚreport_untrustedÚuir/   Znor-   r0   r,   r1   r.   Zyes)Ú_import_mercurialÚhgr
   r%   r2   Z	setconfigZ
repositoryÚrepo)r   r   r   r   r   r)   X   s    
zMercurialCommands.__init__c             C   s   dd l }dd l}dd l}|S )Nr   )Zmercurial.commandsZmercurial.hgZmercurial.ui)r   Z	mercurialr   r   r   r3   j   s    z#MercurialCommands._import_mercurialc             C   s&   | j  |¡ | jj | j| j|¡ d S )N)r%   r   r4   r   r+   r2   r5   )r   r   r   r   r   r   p   s    zMercurialCommands.create_filec             C   s   | j  |¡ d S )N)r%   r   )r   r   r   r   r   r   t   s    zMercurialCommands.create_folderc             C   s    | j jj| j| j||dd d S )NF)Zafter)r4   r   Úrenamer2   r5   )r   r   r   r   r   r   r   w   s    zMercurialCommands.movec             C   s   | j j | j| j|¡ d S )N)r4   r   r   r2   r5   )r   r   r   r   r   r   {   s    zMercurialCommands.removec             C   s   | j  ||¡ d S )N)r%   r    )r   r   r!   r   r   r   r    ~   s    zMercurialCommands.writeN)
r"   r#   r$   r)   r3   r   r   r   r   r    r   r   r   r   r   V   s   r   c               @   sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )r   c             C   s   || _ |  dg¡ tƒ | _d S )NÚversion)r   Ú_dor
   r%   )r   r   r   r   r   r)   „   s    zGITCommands.__init__c             C   s$   | j  |¡ |  d|  |¡g¡ d S )Nr+   )r%   r   r8   Ú_in_dir)r   r   r   r   r   r   ‰   s    zGITCommands.create_filec             C   s   | j  |¡ d S )N)r%   r   )r   r   r   r   r   r      s    zGITCommands.create_folderc             C   s    |   d|  |¡|  |¡g¡ d S )NÚmv)r8   r9   )r   r   r   r   r   r   r      s    zGITCommands.movec             C   s   |   d|  |¡g¡ d S )NZrm)r8   r9   )r   r   r   r   r   r   “   s    zGITCommands.removec             C   s   | j  ||¡ d S )N)r%   r    )r   r   r!   r   r   r   r    –   s    zGITCommands.writec             C   s   t dg| | jd d S )NZgit)Úcwd)Ú_executer   )r   r(   r   r   r   r8   š   s    zGITCommands._doc             C   s(   |  | j¡r"|t| jƒd d … S | jS )Né   )Ú
startswithr   Úlen)r   r   r   r   r   r9      s    zGITCommands._in_dirN)r"   r#   r$   r)   r   r   r   r   r    r8   r9   r   r   r   r   r   ‚   s   r   c               @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )r   c             C   s   || _ tƒ | _d S )N)r   r
   r%   )r   r   r   r   r   r)   ¥   s    zDarcsCommands.__init__c             C   s   | j  |¡ |  d|g¡ d S )Nr+   )r%   r   r8   )r   r   r   r   r   r   ©   s    zDarcsCommands.create_filec             C   s   | j  |¡ |  d|g¡ d S )Nr+   )r%   r   r8   )r   r   r   r   r   r   ­   s    zDarcsCommands.create_folderc             C   s   |   d||g¡ d S )Nr:   )r8   )r   r   r   r   r   r   r   ±   s    zDarcsCommands.movec             C   s   | j  |¡ d S )N)r%   r   )r   r   r   r   r   r   ´   s    zDarcsCommands.removec             C   s   | j  ||¡ d S )N)r%   r    )r   r   r!   r   r   r   r    ·   s    zDarcsCommands.writec             C   s   t dg| | jd d S )NZdarcs)r;   )r<   r   )r   r(   r   r   r   r8   º   s    zDarcsCommands._doN)
r"   r#   r$   r)   r   r   r   r   r    r8   r   r   r   r   r   £   s   r   c             C   s    t j| |t jd}| ¡  |jS )N)r;   Ústdout)Ú
subprocessÚPopenÚPIPEÚwaitÚ
returncode)r(   r;   Zprocessr   r   r   r<   ¾   s    r<   c             C   sX   t | tƒs| S |d krt| ƒ}|d k	r0|  |¡S y|  ¡ S  tk
rR   |  d¡S X d S )Nzutf-8)Ú
isinstanceÚunicodeÚread_str_codingÚencodeÚUnicodeEncodeError)ÚcontentsÚencodingr   r   r   Úunicode_to_file_dataÄ   s    

rM   c             C   s*   t | |ƒ}d|kr&| dd¡ dd¡}|S )Núz
Ú
)Ú_decode_dataÚreplace)r!   rL   Úresultr   r   r   Úfile_data_to_unicodeÑ   s    
rS   c          	   C   sX   t | tƒr| S |d krt| ƒ}|d kr*d}y
|  |¡S  ttfk
rR   |  d¡S X d S )Nzutf-8Úlatin1)rF   rG   rH   ÚdecodeÚUnicodeErrorÚLookupError)r!   rL   r   r   r   rP   Ø   s    

rP   c             C   sV   t | dƒ}d}g }x,| d¡}|s$P || d¡7 }| |¡ qW | ¡  td |¡ƒS )NÚbr   é
   rO   Ú )r   ÚreadÚcountÚappendr   Ú_find_codingÚjoin)r   Úfiler\   rR   Zcurrentr   r   r   Úread_file_codingê   s    

ra   c             C   sf   t | ƒtkrd}nd}y"|  |¡d }|  ||¡d }W n tk
rT   t| ƒ}Y nX t| d |… ƒS )Nó   
rO   r=   )ÚtypeÚbytesÚindexÚ
ValueErrorr?   r^   )ÚsourceÚnewlineÚfirstÚsecondr   r   r   rH   ø   s    rH   c             C   sú   t | tjƒr|  d¡} d}tjr$tndd„ }y´|  |¡t|ƒ }| | dkrPd S |d7 }x(|t| ƒk r€|| | ƒ ¡ r€|d7 }qZW |}x6|t| ƒk r¼| | }||ƒ 	¡ s²|dkr²P |d7 }qˆW | ||… }t |t
ƒrÞ| d¡}|S  tk
rô   Y nX d S )Nzutf-8s   codingc             S   s   | S )Nr   )Úxr   r   r   Ú<lambda>  s    z_find_coding.<locals>.<lambda>s   =:r=   s   -_)rF   ÚpycompatÚstrrI   ZPY3Úchrre   r?   ÚisspaceÚisalnumrd   rU   rf   )ÚtextZcodingZto_chrÚstartÚendÚcrR   r   r   r   r^   	  s.    


r^   )N)N)N)Ú__doc__r   r   rA   Zrope.base.utils.pycompatÚbaseZutilsrm   rG   Ú	NameErrorrn   r   Úobjectr
   r   r   r   r   r<   rM   rS   rP   ra   rH   r^   r   r   r   r   Ú<module>   s*   
,!


