B
    :Ra\                 @   s|   d Z ddlZddlZddlmZ ddlmZ ddlm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S )a  Files and folders in a project are represented as resource objects.

Files and folders are access through `Resource` objects. `Resource` has
two subclasses: `File` and `Folder`. What we care about is that
refactorings and `rope.base.change.Change`s use resources.

There are two options to create a `Resource` for a path in a project.
Note that in these examples `path` is the path to a file or folder
relative to the project's root. A project's root folder is represented
by an empty string.

  1) Use the `rope.base.Project.get_resource()` method. E.g.:

       myresource = myproject.get_resource(path)


  2) Use the `rope.base.libutils` module. `libutils` has a function
     named `path_to_resource()`. It takes a project and a path:

       from rope.base import libutils

       myresource = libutils.path_to_resource(myproject, path)

Once we have a `Resource`, we can retrieve information from it, like
getting the path relative to the project's root (via `path`), reading
from and writing to the resource, moving the resource, etc.
    N)change)
exceptions)
fscommandsc               @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dd Ze
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 )Resourcez)Represents files and folders in a projectc             C   s   || _ || _d S )N)project_path)selfr   path r
   2lib/python3.7/site-packages/rope/base/resources.py__init__(   s    zResource.__init__c             C   s"   |  t| |d| j|f  dS )zMove resource to `new_location`zMoving <%s> to <%s>N)_perform_changer   ZMoveResourcer	   )r   Znew_locationr
   r
   r   move,   s    zResource.movec             C   s   |  t| d| j  dS )z Remove resource from the projectzRemoving <%s>N)r   r   ZRemoveResourcer	   )r   r
   r
   r   remove1   s    zResource.removec             C   s   dS )z'Return true if the resource is a folderNr
   )r   r
   r
   r   	is_folder6   s    zResource.is_folderc             C   s   dS )zCreate this resourceNr
   )r   r
   r
   r   create9   s    zResource.createc             C   s   t j| jS )N)osr	   exists	real_path)r   r
   r
   r   r   <   s    zResource.existsc             C   s&   d | jddd }| j|S )N/r   )joinr	   splitr   Z
get_folder)r   parentr
   r
   r   r   ?   s    zResource.parentc             C   s   | j S )zReturn the path of this resource relative to the project root

        The path is the list of parent directories separated by '/' followed
        by the resource name.
        )r   )r   r
   r
   r   r	   D   s    zResource.pathc             C   s   | j dd S )z Return the name of this resourcer   r   )r	   r   )r   r
   r
   r   nameM   s    zResource.namec             C   s   | j | jS )z,Return the file system path of this resource)r   Z_get_resource_pathr	   )r   r
   r
   r   r   R   s    zResource.real_pathc             C   s   | j |j ko| j|jkS )N)	__class__r	   )r   objr
   r
   r   __eq__W   s    zResource.__eq__c             C   s   |  | S )N)r   )r   r   r
   r
   r   __ne__Z   s    zResource.__ne__c             C   s
   t | jS )N)hashr	   )r   r
   r
   r   __hash__]   s    zResource.__hash__c             C   s$   t |}|| | j| d S )N)r   Z	ChangeSetZ
add_changer   Zdo)r   Zchange_ZdescriptionZchangesr
   r
   r   r   `   s    

zResource._perform_changeN)__name__
__module____qualname____doc__r   r   r   r   r   r   propertyr   r	   r   r   r   r   r    r   r
   r
   r
   r   r   %   s   	r   c                   sH   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	  Z
S )FilezRepresents a filec                s   t t| || d S )N)superr&   r   )r   r   r   )r   r
   r   r   i   s    zFile.__init__c          
   C   sJ   |   }y
t|S  tk
rD } zt| j|jW d d }~X Y nX d S )N)
read_bytesr   Zfile_data_to_unicodeUnicodeDecodeErrorr   ZModuleDecodeErrorr	   reason)r   dataer
   r
   r   readl   s
    
z	File.readc             C   s$   t | jd}z| S |  X d S )Nrb)openr   r-   close)r   Zhandler
   r
   r   r(   s   s    zFile.read_bytesc             C   sH   y||   krd S W n tk
r(   Y nX | t| |d| j  d S )NzWriting file <%s>)r-   IOErrorr   r   ZChangeContentsr	   )r   contentsr
   r
   r   writez   s    z
File.writec             C   s   dS )NFr
   )r   r
   r
   r   r      s    zFile.is_folderc             C   s   | j | j d S )N)r   create_filer   )r   r
   r
   r   r      s    zFile.create)r!   r"   r#   r$   r   r-   r(   r3   r   r   __classcell__r
   r
   )r   r   r&   f   s   	r&   c                   sx   e Zd ZdZ f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d Zdd Zdd Zdd Z  ZS )FolderzRepresents a folderc                s   t t| || d S )N)r'   r6   r   )r   r   r   )r   r
   r   r      s    zFolder.__init__c             C   s   dS )NTr
   )r   r
   r
   r   r      s    zFolder.is_folderc          	   C   s   yt | j}W n tk
r$   g S X g }xP|D ]H}y| |}W n tjk
rZ   w0Y nX | j|s0|	| | q0W |S )z"Return the children of this folder)
r   listdirr   OSError	get_childr   ResourceNotFoundErrorr   Z
is_ignoredappend)r   Zchildrenresultr   Zchildr
   r
   r   get_children   s    
zFolder.get_childrenc             C   s(   |  t| |d| |  | |S )NzCreating file <%s>)r   r   Z
CreateFile_get_child_pathr9   )r   	file_namer
   r
   r   r4      s    
zFolder.create_filec             C   s(   |  t| |d| |  | |S )NzCreating folder <%s>)r   r   ZCreateFolderr>   r9   )r   Zfolder_namer
   r
   r   create_folder   s    
zFolder.create_folderc             C   s   | j r| j d | S |S d S )Nr   )r	   )r   r   r
   r
   r   r>      s    zFolder._get_child_pathc             C   s   | j | |S )N)r   Zget_resourcer>   )r   r   r
   r
   r   r9      s    zFolder.get_childc             C   s*   y|  | dS  tjk
r$   dS X d S )NTF)r9   r   r:   )r   r   r
   r
   r   	has_child   s
    
zFolder.has_childc             C   s   dd |   D S )Nc             S   s   g | ]}|  s|qS r
   )r   ).0resourcer
   r
   r   
<listcomp>   s    z$Folder.get_files.<locals>.<listcomp>)r=   )r   r
   r
   r   	get_files   s    zFolder.get_filesc             C   s   dd |   D S )Nc             S   s   g | ]}|  r|qS r
   )r   )rB   rC   r
   r
   r   rD      s    z&Folder.get_folders.<locals>.<listcomp>)r=   )r   r
   r
   r   get_folders   s    zFolder.get_foldersc             C   s(   | |krdS | j dkp&|j | j d S )NF r   )r	   
startswith)r   rC   r
   r
   r   contains   s    zFolder.containsc             C   s   | j | j d S )N)r   r@   r   )r   r
   r
   r   r      s    zFolder.create)r!   r"   r#   r$   r   r   r=   r4   r@   r>   r9   rA   rE   rF   rI   r   r5   r
   r
   )r   r   r6      s   r6   c               @   s8   e Zd Zdd Zdd Zdd Zdd Zed	d
 ZdS )_ResourceMatcherc             C   s   g | _ g | _d S )N)patterns_compiled_patterns)r   r
   r
   r   r      s    z_ResourceMatcher.__init__c             C   s   d| _ || _dS )zSpecify which resources to match

        `patterns` is a `list` of `str`\s that can contain ``*`` and
        ``?`` signs for matching resource names.

        N)rL   rK   )r   rK   r
   r
   r   set_patterns   s    z_ResourceMatcher.set_patternsc             C   sF   | dd dd dd dd}d	| d
 }| jt| d S )N.z\.*z[^/]*?z[^/]z//z/(.*/)?z^(.*/)?z(/.*)?$)replacecompiled_patternsr;   recompile)r   patternZ
re_patternr
   r
   r   _add_pattern   s
    z_ResourceMatcher._add_patternc             C   sR   x| j D ]}||jrdS qW tjj|jjf|jd }tj|rNdS dS )NTr   F)	rR   matchr	   r   r   r   Zaddressr   islink)r   rC   rU   r	   r
   r
   r   
does_match   s    z_ResourceMatcher.does_matchc             C   s0   | j d kr*g | _ x| jD ]}| | qW | j S )N)rL   rK   rV   )r   rU   r
   r
   r   rR      s
    
z"_ResourceMatcher.compiled_patternsN)	r!   r"   r#   r   rM   rV   rY   r%   rR   r
   r
   r
   r   rJ      s
   

rJ   )r$   r   rS   Z	rope.baser   r   r   objectr   r&   r6   rJ   r
   r
   r
   r   <module>   s   A$F