
    c                         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          Z
 G d de
          Z G d	 d
e
          Z G d d          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)Path)change
exceptions
fscommandsc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
ed	             Zed
             Zed             Zed             Zed             Zd Zd Zd Zd ZdS )Resourcez)Represents files and folders in a projectc                 "    || _         || _        d S N)project_path)selfr   paths      3lib/python3.11/site-packages/rope/base/resources.py__init__zResource.__init__(   s    


    c           
          d                     | j        j        | j        j        | j        t          t          |                               S )Nz<{}.{} "{}" at {}>)format	__class__
__module____name__r   hexidr   s    r   __repr__zResource.__repr__,   s>    #**N%N#I4MM	
 
 	
r   c                 p    |                      t          j        | |          d| j         d| d           dS )zMove resource to `new_location`zMoving <z> to <>N)_perform_changer   MoveResourcer   )r   new_locations     r   movezResource.move4   sL    l337ty77777	
 	
 	
 	
 	
r   c                 f    |                      t          j        |           d| j        z             dS )z Remove resource from the projectzRemoving <%s>N)r   r   RemoveResourcer   r   s    r   removezResource.remove;   s0    V2488/DI:UVVVVVr   c                     dS )z'Return true if the resource is a folderN r   s    r   	is_folderzResource.is_folder?         r   c                     dS )zCreate this resourceNr%   r   s    r   createzResource.createB   r'   r   c                 J    t           j                            | j                  S r
   )osr   exists	real_pathr   s    r   r,   zResource.existsE   s    w~~dn---r   c                     d                     | j                            d          dd                   }| j                            |          S )N/r   )joinr   splitr   
get_folder)r   parents     r   r4   zResource.parentH   s@    $)//#..qt45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.pathM   s     zr   c                 B    | j                             d          d         S )z Return the name of this resourcer/   r0   )r   r2   r   s    r   namezResource.nameV   s     ys##B''r   c                 @    | j                             | j                  S )z,Return the file system path of this resource)r   _get_resource_pathr   r   s    r   r-   zResource.real_path[   s     |..ty999r   c                 *    t          | j                  S )z"Return the file as a pathlib path.)r   r-   r   s    r   pathlibzResource.pathlib`   s     DN###r   c                 B    | j         |j         k    o| j        |j        k    S r
   )r   r   r   objs     r   __eq__zResource.__eq__e   s    ~.H493HHr   c                 .    |                      |           S r
   )r?   r=   s     r   __ne__zResource.__ne__h   s    ;;s####r   c                 *    t          | j                  S r
   )hashr   r   s    r   __hash__zResource.__hash__k   s    DIr   c                     t          j        |          }|                    |           | j                            |           d S r
   )r   	ChangeSet
add_changer   do)r   change_descriptionchangess       r   r   zResource._perform_changen   s@    ";//7###     r   N)r   r   __qualname____doc__r   r   r    r#   r&   r)   r,   propertyr4   r   r7   r-   r;   r?   rA   rD   r   r%   r   r   r   r   %   sL       33  
 
 

 
 
W W W6 6 6# # #. . . / / X/   X ( ( X( : : X: $ $ X$I I 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                 Z    d | _         t                                          ||           d S r
   )newlinessuperr   r   r   r7   r   s      r   r   zFile.__init__w   s*    $'''''r   c                     |                                  }	 t          j        |          \  }| _        |S # t          $ r$}t          j        | j        |j                  d }~ww xY wr
   )	
read_bytesr   file_data_to_unicoderR   UnicodeDecodeErrorr   ModuleDecodeErrorr   reason)r   datacontentes       r   readz	File.read{   sm      	D%/%DT%J%J"GT]N! 	D 	D 	D.ty!(CCC	Ds   4 
A"AA"c                 8   t          | j        j        d          s]t          j        dt
          d           t          | j        d          5 }|                                cd d d            S # 1 swxY w Y   | j        j                            | j                  S )Nr^   z1FileSystemCommands should implement read() method   )
stacklevelrb)	hasattrr   r   warningswarnDeprecationWarningopenr-   r^   )r   handles     r   rV   zFile.read_bytes   s    t|.77 	%MC"   
 dnd++ %v{{}}% % % % % % % % % % % % % % % %|&++DN;;;s   A--A14A1c                     	 ||                                  k    rd S n# t          $ r Y nw xY w|                     t          j        | |          d| j        z             d S )NzWriting file <%s>)r^   OSErrorr   r   ChangeContentsr   )r   contentss     r   writez
File.write   s    	499;;&  	 	 	D	!$113F3R	
 	
 	
 	
 	
s    
**c                     dS )NFr%   r   s    r   r&   zFile.is_folder   s    ur   c                 D    | j                             | j                   d S r
   )r4   create_filer7   r   s    r   r)   zFile.create   s     	*****r   )r   r   rL   rM   r   r^   rV   rm   r&   r)   __classcell__r   s   @r   rP   rP   t   s        ( ( ( ( (D D D	< 	< 	<
 
 
  + + + + + + +r   rP   c                   d     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )FolderzRepresents a folderc                 L    t                                          ||           d S r
   )rS   r   rT   s      r   r   zFolder.__init__   s#    $'''''r   c                     dS )NTr%   r   s    r   r&   zFolder.is_folder   s    tr   c                 J   	 t          j        | j                  }n# t          $ r g cY S w xY wg }|D ]p}	 |                     |          }n# t
          j        $ r Y *w xY w| j                            |          s(|	                    |                     |                     q|S )z"Return the children of this folder)
r+   listdirr-   rj   	get_childr   ResourceNotFoundErrorr   
is_ignoredappend)r   childrenresultr7   childs        r   get_childrenzFolder.get_children   s    	z$.11HH 	 	 	III	 	4 	4Dt,,3   <**511 4dnnT22333s    ++AAAc                     |                      t          j        | |          d|                     |          z             |                     |          S )NzCreating file <%s>)r   r   
CreateFile_get_child_pathry   )r   	file_names     r   rp   zFolder.create_file   sT    dI.. 4#7#7	#B#BB	
 	
 	
 ~~i(((r   c                     |                      t          j        | |          d|                     |          z             |                     |          S )NzCreating folder <%s>)r   r   CreateFolderr   ry   )r   folder_names     r   create_folderzFolder.create_folder   sT    k22"T%9%9+%F%FF	
 	
 	
 ~~k***r   c                 .    | j         r| j         dz   |z   S |S )Nr/   )r   r   r7   s     r   r   zFolder._get_child_path   s"    9 	9s?T))Kr   c                 \    | j                             |                     |                    S r
   )r   get_resourcer   r   s     r   ry   zFolder.get_child   s&    |(()=)=d)C)CDDDr   c                 ^    	 |                      |           dS # t          j        $ r Y dS w xY w)NTF)ry   r   rz   r   s     r   	has_childzFolder.has_child   sC    	NN4   4/ 	 	 	55	s    ,,c                 >    d |                                  D             S )Nc                 :    g | ]}|                                 |S r%   r&   .0resources     r   
<listcomp>z$Folder.get_files.<locals>.<listcomp>   s9     
 
 
!@R@R@T@T

 
 
r   r   r   s    r   	get_fileszFolder.get_files   s/    
 
%)%6%6%8%8
 
 
 	
r   c                 >    d |                                  D             S )Nc                 :    g | ]}|                                 |S r%   r   r   s     r   r   z&Folder.get_folders.<locals>.<listcomp>   s)    UUUX@R@R@T@TUUUUr   r   r   s    r   get_folderszFolder.get_folders   s"    UU):):)<)<UUUUr   c                 l    | |k    rdS | j         dk    p!|j                             | j         dz             S )NF r/   )r   
startswith)r   r   s     r   containszFolder.contains   s;    8 	5yBK(-":":49s?"K"KKr   c                 D    | j                             | j                   d S r
   )r4   r   r7   r   s    r   r)   zFolder.create   s     !!$),,,,,r   )r   r   rL   rM   r   r&   r   rp   r   r   ry   r   r   r   r   r)   rq   rr   s   @r   rt   rt      s        ( ( ( ( (     ) ) )+ + +  E E E  
 
 

V V VL L L
- - - - - - -r   rt   c                   <    e Zd Zd Zd Zd Zd Zed             ZdS )_ResourceMatcherc                 "    g | _         g | _        d S r
   )patterns_compiled_patternsr   s    r   r   z_ResourceMatcher.__init__   s    "$r   c                 "    d| _         || _        dS )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                    |                     dd                               dd                               dd                               dd          }d	|z   d
z   }| j                            t          j        |                     d S )N.z\.*z[^/]*?z[^/]z//z/(.*/)?z^(.*/)?z(/.*)?$)replacecompiled_patternsr|   recompile)r   pattern
re_patterns      r   _add_patternz_ResourceMatcher._add_pattern   s}    OOC''WS'""WS&!!WT9%%	 	 +i7
%%bj&<&<=====r   c                     | j         D ]}|                    |j                  r dS  t          j        j        |j        j        g|j                            d          R  }t          j                            |          S )NTr/   )	r   matchr   r+   r1   r   addressr2   islink)r   r   r   r   s       r   
does_matchz_ResourceMatcher.does_match   sy    - 	 	G}}X]++ ttw|H,4Px}7J7J37O7OPPPw~~d###r   c                 j    | j         &g | _         | j        D ]}|                     |           | j         S r
   )r   r   r   )r   r   s     r   r   z"_ResourceMatcher.compiled_patterns  sF    " 	+&(D#= + +!!'****&&r   N)	r   r   rL   r   r   r   r   rN   r   r%   r   r   r   r      sk        % % %! ! !> > >$ $ $ ' ' X' ' 'r   r   )rM   r+   r   rd   r;   r   	rope.baser   r   r   r   rP   rt   r   r%   r   r   <module>r      s   8 
			 				        4 4 4 4 4 4 4 4 4 4L! L! L! L! L! L! L! L!^(+ (+ (+ (+ (+8 (+ (+ (+VE- E- E- E- E-X E- E- E-P&' &' &' &' &' &' &' &' &' &'r   