
    Æe                         d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	  G d dej                        Z G d de      Z G d	 d
e      Z G d d      Zy)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Path)change
exceptions
fscommandsc                       e Zd ZdZd Zd ZdefdZd Zd Z	d Z
d	 Zd
 Zd Zed        Zedefd       Zedefd       Zedefd       Zedefd       Zd Zd Zd Zd Zy)Resourcez)Represents files and folders in a projectc                      || _         || _        y N)project_path)selfr   paths      5/usr/lib/python3/dist-packages/rope/base/resources.py__init__zResource.__init__(   s    
    c           
          dj                  | j                  j                  | j                  j                  | j                  t        t        |                   S )Nz<{}.{} "{}" at {}>)format	__class__
__module____name__r   hexidr   s    r   __repr__zResource.__repr__,   sA    #**NN%%NN##II4M	
 	
r   returnc                 L    | j                   j                  | j                        S )z,Return the file system path of this resource)r   _get_resource_pathr   r   s    r   
__fspath__zResource.__fspath__4   s    ||..tyy99r   c                 r    | j                  t        j                  | |      d| j                   d| d       y)zMove resource to `new_location`zMoving <z> to <>N)_perform_changer   MoveResourcer   )r   new_locations     r   movezResource.move8   s6    l3tyyk~Q7	
r   c                 h    | j                  t        j                  |       d| j                  z         y)z Remove resource from the projectzRemoving <%s>N)r"   r   RemoveResourcer   r   s    r   removezResource.remove?   s&    V2248/DII:UVr   c                      y)zAlias for `is_folder()`N r   s    r   is_dirzResource.is_dirC       r   c                      y)z'Return True if the resource is a FolderNr*   r   s    r   	is_folderzResource.is_folderF   r,   r   c                      y)zCreate this resourceNr*   r   s    r   createzResource.createI   r,   r   c                 @    t         j                  j                  |       S r   )osr   existsr   s    r   r3   zResource.existsL   s    ww~~d##r   c                     dj                  | j                  j                  d      dd       }| j                  j	                  |      S )N/r   )joinr   splitr   
get_folder)r   parents     r   r:   zResource.parentO   s9    $))//#.q45||&&v..r   c                     | 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   s    r   r   zResource.pathT   s     zzr   c                 >    | j                   j                  d      d   S )z Return the name of this resourcer5   r6   )r   r8   r   s    r   namezResource.name]   s     yys#B''r   c                 ,    t        j                  |       S r   )r2   fspathr   s    r   	real_pathzResource.real_pathb   s    yyr   c                     t        |       S )z"Return the file as a pathlib path.r   r   s    r   pathlibzResource.pathlibf   s     Dzr   c                 j    | j                   |j                   k(  xr | j                  |j                  k(  S r   )r   r   r   objs     r   __eq__zResource.__eq__k   s'    ~~.H4993HHr   c                 &    | j                  |       S r   )rF   rD   s     r   __ne__zResource.__ne__n   s    ;;s###r   c                 ,    t        | j                        S r   )hashr   r   s    r   __hash__zResource.__hash__q   s    DIIr   c                     t        j                  |      }|j                  |       | j                  j	                  |       y r   )r   	ChangeSet
add_changer   do)r   change_descriptionchangess       r   r"   zResource._perform_changet   s0    "";/7# r   N)r   r   __qualname____doc__r   r   strr   r%   r(   r+   r.   r0   r3   propertyr:   r   r=   r@   r   rB   rF   rH   rK   r"   r*   r   r   r	   r	   %   s    3
:C :
W&6#$ / / c   (c ( ( 3     I$!r   r	   c                   @     e Zd ZdZ fdZd Zd Zd Zd Zd Z	 xZ
S )FilezRepresents a filec                 4    d | _         t        | 	  ||       y r   )newlinessuperr   )r   r   r=   r   s      r   r   zFile.__init__}   s    $'r   c                     | j                         }	 t        j                  |      \  }| _        |S # t        $ r/}t        j                  | j                  |j                        d }~ww xY wr   )	
read_bytesr   file_data_to_unicoderZ   UnicodeDecodeErrorr   ModuleDecodeErrorr   reason)r   datacontentes       r   readz	File.read   s]     	D%/%D%DT%J"GT]N! 	D..tyy!((CC	Ds   1 	A)*A$$A)c                 <   t        | j                  j                  d      sBt        j                  dt
        d       t        | d      5 }|j                         cd d d        S | j                  j                  j                  | j                        S # 1 sw Y   8xY w)Nre   z1FileSystemCommands should implement read() method   )
stacklevelrb)	hasattrr   r   warningswarnDeprecationWarningopenre   r@   )r   handles     r   r]   zFile.read_bytes   s{    t||..7MMC"
 dD! %V{{}% %||&&++DNN;;% %s   	BBc                     	 || j                         k(  ry 	 | j                  t        j                  | |      d| j
                  z         y # t        $ r Y ?w xY w)NzWriting file <%s>)re   OSErrorr"   r   ChangeContentsr   )r   contentss     r   writez
File.write   s]    	499;& ' 	!!$13F3R	
  		s   A 	AAc                      y)NFr*   r   s    r   r.   zFile.is_folder   s    r   c                 N    | j                   j                  | j                         y r   )r:   create_filer=   r   s    r   r0   zFile.create   s    		*r   )r   r   rS   rT   r   re   r]   rt   r.   r0   __classcell__)r   s   @r   rX   rX   z   s%    (D	<
+r   rX   c                   R    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)FolderzRepresents a folderc                      y)NTr*   r   s    r   r.   zFolder.is_folder   s    r   c                 4   	 t        j                  |       }g }|D ]P  }	 | j                  |      }| j                  j                  |      r1|j                  | j                  |             R |S # t        $ r g cY S w xY w# t        j
                  $ r Y {w xY w)z"Return the children of this folder)	r2   listdirrq   	get_childr   ResourceNotFoundErrorr   
is_ignoredappend)r   childrenresultr=   childs        r   get_childrenzFolder.get_children   s    	zz$'H  	4Dt, <<**51dnnT23	4   	I	 33 s"   A0 B0A>=A>BBc                     | j                  t        j                  | |      d| j                  |      z         | j	                  |      S )NzCreating file <%s>)r"   r   
CreateFile_get_child_pathr~   )r   	file_names     r   rw   zFolder.create_file   sD    dI. 4#7#7	#BB	
 ~~i((r   c                     | j                  t        j                  | |      d| j                  |      z         | j	                  |      S )NzCreating folder <%s>)r"   r   CreateFolderr   r~   )r   folder_names     r   create_folderzFolder.create_folder   sD    k2"T%9%9+%FF	
 ~~k**r   c                 B    | j                   r| j                   dz   |z   S |S )Nr5   )r   r   r=   s     r   r   zFolder._get_child_path   s!    9999s?T))Kr   c                 V    | j                   j                  | j                  |            S r   )r   get_resourcer   r   s     r   r~   zFolder.get_child   s"    ||(()=)=d)CDDr   c                 Z    	 | j                  |       y# t        j                  $ r Y yw xY w)NTF)r~   r   r   r   s     r   	has_childzFolder.has_child   s.    	NN4 // 		s    **c                 j    | j                         D cg c]  }|j                         r| c}S c c}w r   r   r.   r   resources     r   	get_fileszFolder.get_files   s3    %)%6%6%8
!@R@R@TH
 	
 
   00c                 j    | j                         D cg c]  }|j                         s| c}S c c}w r   r   r   s     r   get_folderszFolder.get_folders   s*    )-):):)<UX@R@R@TUUUr   c                     | |k(  ry| j                   dk(  xs( |j                   j                  | j                   dz         S )NF r5   )r   
startswithr   s     r   containszFolder.contains   s7    8yyBK(--":":499s?"KKr   c                 N    | j                   j                  | j                         y r   )r:   r   r=   r   s    r   r0   zFolder.create   s    !!$)),r   N)r   r   rS   rT   r.   r   rw   r   r   r~   r   r   r   r   r0   r*   r   r   rz   rz      s?     )+E

VL
-r   rz   c                   4    e Zd Zd Zd Zd Zd Zed        Zy)_ResourceMatcherc                      g | _         g | _        y r   )patterns_compiled_patternsr   s    r   r   z_ResourceMatcher.__init__   s    "$r   c                      d| _         || _        y)zSpecify which resources to match

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

        N)r   r   )r   r   s     r   set_patternsz_ResourceMatcher.set_patterns   s     #' r   c                     |j                  dd      j                  dd      j                  dd      j                  dd      }d	|z   d
z   }| j                  j                  t        j                  |             y )N.z\.*z[^/]*?z[^/]z//z/(.*/)?z^(.*/)?z(/.*)?$)replacecompiled_patternsr   recompile)r   pattern
re_patterns      r   _add_patternz_ResourceMatcher._add_pattern   sg    OOC'WS'"WS&!WT9%	 	 +i7
%%bjj&<=r   c                 6   | j                   D ]  }|j                  |j                        s y t        j                  j                  |j
                  j                  g|j                  j                  d       }t        j                  j                  |      S )NTr5   )	r   matchr   r2   r7   r   addressr8   islink)r   r   r   r   s       r   
does_matchz_ResourceMatcher.does_match  sq    -- 	G}}X]]+	 ww||H,,44Px}}7J7J37OPww~~d##r   c                     | j                   )g | _         | j                  D ]  }| j                  |        | j                   S r   )r   r   r   )r   r   s     r   r   z"_ResourceMatcher.compiled_patterns
  sC    ""*&(D#== +!!'*+&&&r   N)	r   r   rS   r   r   r   r   rV   r   r*   r   r   r   r      s*    %!>$ ' 'r   r   )rT   r2   r   rk   rB   r   	rope.baser   r   r   PathLiker	   rX   rz   r   r*   r   r   <module>r      sW   8 
 	   4 4R!r{{ R!j(+8 (+VB-X B-J&' &'r   