
    ÆeI;                    X   d dl mZ 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 d dlm	Z	 d dl
Zd dlmc mZ d dlmZmZmZmZmZ d dlmZ d dlmZmZ d dlmZmZmZ 	 d dlZ G d	 d
      Z! G d de!      Z" G d de!      Z#d Z$ G d d      Z% G d d      Z&d Z'd Z(y# e $ r d dlZY Iw xY w)    )annotationsN)	ExitStack)Optional)
exceptionshistorypycore
taskhandleutils)ModuleNotFoundError)Prefs
get_config)FileFolder_ResourceMatcherc                     e Zd ZU ded<   d Zd ZddZd Zd Zd	 Z	d
 Z
d Zej                  fdZddZd Zd Zd Zd Zd ZdddZd Zd Zd Zeej6                  d               Zeej6                  d               Zd ZdZy)_Projectr   prefsc                l    g | _         || _        t               | _        t	        |       | _        g | _        y N)	observers
fscommandsr   r   
_DataFiles
data_files_custom_source_folders)selfr   s     3/usr/lib/python3/dist-packages/rope/base/project.py__init__z_Project.__init__   s-    $W
$T*&(#    c                n   | j                  |      }t        j                  j                  |      st	        j
                  d|z        t        j                  j                  |      rt        | |      S t        j                  j                  |      rt        | |      S t	        j
                  d|z         )a  Get a resource in a project.

        `resource_name` is the path of a resource in a project.  It is
        the path of a resource relative to project root.  Project root
        folder address is an empty string.  If the resource does not
        exist a `exceptions.ResourceNotFound` exception would be
        raised.  Use `get_file()` and `get_folder()` when you need to
        get nonexistent `Resource`.

        zResource <%s> does not existzUnknown resource )
_get_resource_pathospathexistsr   ResourceNotFoundErrorisfiler   isdirr   )r   resource_namer"   s      r   get_resourcez_Project.get_resource$   s     &&}5ww~~d#22.>  WW^^D!m,,WW]]4 $..223F3VWWr   Nc                    | j                   j                  |      }||S | j                  ||      }|t        d|z        | j                   j	                  |      S )z-Returns a `PyObject` if the module was found.Module %s not found)r   builtin_modulefind_moduler   resource_to_pyobject)r   namefolderpymodmodules        r   
get_modulez_Project.get_module;   s`     **40L!!$/>%&;d&BCC{{//77r   c                2   g }| j                   j                  dg       t        j                  z   D ]X  }t	        j
                  t        j                        5  t               j                  |      }|j                  |       d d d        Z |S # 1 sw Y   fxY w)Npython_path)r   getsysr"   
contextlibsuppressr   r$   get_no_projectr(   append)r   resultsrc
src_folders       r   get_python_path_foldersz _Project.get_python_path_foldersF   s    ::>>-4sxx? 	*C$$Z%E%EF *+-::3?
j)* *	* * *s   +BB	c                    | j                   g S t        | j                        }|j                  | j                  j                  | j                                |S )zReturns project source folders)rootlistr   extendr   _find_source_folders)r   r;   s     r   get_source_foldersz_Project.get_source_foldersS   sE    99Id112dkk66tyyABr   c                Z    t        | j                        D ]  }|j                  |        y)zValidate files and folders contained in this folder

        It validates all of the files and folders contained in this
        folder if some observers are interested in them.

        N)rA   r   validate)r   r/   observers      r   rF   z_Project.validate[   s*     T^^, 	&Hf%	&r   c                :    | j                   j                  |       y)zORegister a `ResourceObserver`

        See `FilteredResourceObserver`.
        N)r   r:   r   rG   s     r   add_observerz_Project.add_observere   s    
 	h'r   c                X    || j                   v r| j                   j                  |       yy)z&Remove a registered `ResourceObserver`N)r   removerI   s     r   remove_observerz_Project.remove_observerl   s%    t~~%NN!!(+ &r   c                >    | j                   j                  ||       y)zApply the changes in a `ChangeSet`

        Most of the time you call this function for committing the
        changes for a refactoring.
        )task_handleN)r   do)r   changesrO   s      r   rP   z_Project.doq   s     	[9r   c                :    | j                   j                  ||      S r   )r   r-   )r   resourceforce_errorss      r   get_pymodulez_Project.get_pymoduley   s    {{//,GGr   c                    | j                   S r   r   r   s    r   
get_pycorez_Project.get_pycore|   s    {{r   c                    t        | |      S )z+Get the file with `path` (it may not exist))r   r   r"   s     r   get_filez_Project.get_file   s    D$r   c                    t        | |      S )z-Get the folder with `path` (it may not exist))r   r[   s     r   
get_folderz_Project.get_folder   s    dD!!r   c                    | j                   S r   )r   rX   s    r   	get_prefsz_Project.get_prefs   s    zzr   c                ~    | j                  |||      }|t        d|z        | j                  j                  |      S )Nr*   )find_relative_moduler   r   r-   )r   r.   r/   levelr1   s        r   get_relative_modulez_Project.get_relative_module   sA    **4?>%&;d&BCC{{//77r   c                    | j                         D ]  }t        ||      }||c S  | j                         D ]  }t        ||      }||c S  |t        ||      }||S y)zjReturns a resource corresponding to the given module

        returns None if it can not be found
        N)rD   _find_module_in_folderr>   )r   modnamer/   r<   r1   s        r   r,   z_Project.find_module   s    
 **, 	C+C9F!	 //1 	C+C9F!	 +FG<F!r   c                f    t        |dz
        D ]  }|j                  } |dk(  r|S t        ||      S )N    )rangeparentrf   )r   rg   r/   rc   is        r   rb   z_Project.find_relative_module   s<    uqy! 	#A]]F	#b=M)&'::r   c                     y)NF r   rS   s     r   
is_ignoredz_Project.is_ignored   s    r   c                     y r   ro   r   r.   s     r   r    z_Project._get_resource_path   s    r   c                ,    t        j                  |       S r   )r   HistoryrX   s    r   r   z_Project.history   s     t$$r   c                ,    t        j                  |       S r   )r   PyCorerX   s    r   r   z_Project.pycore   s     }}T""r   c                <    t        j                  dt        d       y )NzCannot close a NoProject   )
stacklevel)warningswarnDeprecationWarningrX   s    r   closez_Project.close   s    02DQRSr   r   )F)returnzOptional[File]) __name__
__module____qualname____annotations__r   r(   r2   r>   rD   rF   rJ   rM   r	   DEFAULT_TASK_HANDLErP   rU   rY   r\   r^   r`   rd   r,   rb   rq   r    propertyr
   saveitr   r   r~   
ropefolderro   r   r   r   r      s    L)X.	8&(,
 '1&D&D :H "8&; 
\\%  % 
\\#  #T Jr   r   c                       e Zd ZdZ	 d fd	Zd Z ej                  d      d        Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zed        Zd fd	Z ed       Z ed       Z xZS )Projectz&A Project containing files and foldersc                   |dk7  rt        |      j                  d      }t        |t              sJ || _        || _        t        j                  j                  | j                        s t        j                  | j                         n>t        j                  j                  | j                        st        j                  d      |3t        j                  j                  j!                  | j                        }t"        | I  |       t'               | _        t+        |       | _        | j/                  |       || j0                  j3                  d|       | j5                          y)a&  A rope project

        :parameters:
            - `projectroot`: The address of the root folder of the project
            - `fscommands`: Implements the file system operations used
              by rope; have a look at `rope.base.fscommands`
            - `ropefolder`: The name of the folder in which rope stores
              project configurations and data.  Pass `None` for not using
              such a folder at all.
            - `prefs`: Specify project preferences.  These values
              overwrite config file preferences.

        /z/\z*Project root exists and is not a directoryNignored_resources)	_realpathrstrip
isinstancestr_address_ropefolder_namer!   r"   r#   mkdirr&   r   	RopeErrorropebaser   create_fscommandssuperr   r   ignored_FileListCacher	file_list_init_prefsr   add_init_source_folders)r   projectrootr   r   r   	__class__s        r   r   zProject.__init__   s     ##K077>K+s+++# *ww~~dmm,HHT]]#t}}-&&'VWW--??NJ$')(.!JJNN.
;!!#r   c                    dj                  | j                  j                  | j                  j                  | j                        S )Nz<{}.{} "{}">)formatr   r   r   addressrX   s    r   __repr__zProject.__repr__   s6    $$NN%%NN##LL
 	
r   z)Delete once deprecated functions are gonec                    | j                   j                  dg       D ].  }| j                  |      }| j                  j	                  |       0 y )Nsource_folders)r   r5   r(   r   r:   )r   r"   r/   s      r   r   zProject._init_source_folders   sE    JJNN#3R8 	7D&&t,F''..v6	7r   c                6    | j                   j                         S r   )r   	get_filesrX   s    r   r   zProject.get_files   s    ~~''))r   c                ~    | j                         D cg c]  }| j                  j                  |      r|! c}S c c}w )z1Returns all python files available in the project)r   r   is_python_filerp   s     r   get_python_fileszProject.get_python_files   s=     !NN,
{{))(3 
 	
 
s   $:c                t    t        j                  j                  | j                  g|j	                  d       S Nr   )r!   r"   joinr   splitrs   s     r   r    zProject._get_resource_path   s&    ww||DMM<DJJsO<<r   c                    | j                   7| j                   j                         s| j                  | j                          y y y r   )r   r#   _create_recursivelyrX   s    r   _init_ropefolderzProject._init_ropefolder  s6    ??&t/E/E/G$$T__5 0H&r   c                    |j                   | j                  k7  r5|j                   j                         s| j                  |j                          |j	                          y r   )rl   r@   r#   r   create)r   r/   s     r   r   zProject._create_recursively  s<    ==DII%fmm.B.B.D$$V]]3r   c                   t        | j                  | j                        j                         }|| _        | j                  j                  d| j                  j                         | j                  j                  | j                  j                         |j                         D ]!  \  }}| j                  j                  ||       # | j                          | j                          |j                  r|j                  |        y y )Nr   )r   r@   r   parser   add_callbackr   set_patternsr   itemsset_init_other_partsr   project_opened)r   r   configkeyvalues        r   r   zProject._init_prefs  s    DIIt7==?


 3T\\5N5NO!!$**">">?++- 	'JCJJNN3&	'   !!$' !r   c                    | j                    y r   rW   rX   s    r   r   zProject._init_other_parts  s	    r   c                8    | j                   j                  |      S r   )r   
does_matchrp   s     r   rq   zProject.is_ignored  s    ||&&x00r   c                $    | j                          yzCloses project open resourcesN)r~   rX   s    r   synczProject.sync  s    

r   c                8    | j                   j                          yr   )r   writerX   s    r   r~   zProject.close"  s    r   c                <    | j                   j                  ||       y)z#Set the `key` preference to `value`N)r   r   )r   r   r   s      r   r   zProject.set&  s    

sE"r   c                R    | j                   | j                  | j                         S y r   )r   r^   rX   s    r   r   zProject.ropefolder*  s(      ,??4#8#899 -r   c                @    || j                   }t        | 	  |       y r   )r@   r   rF   )r   r/   r   s     r   rF   zProject.validate/  s    >YYF r   c                $    | j                  d      S )Nrj   )r(   rX   s    r   <lambda>zProject.<lambda>4  s    !2!22!6 r   c                    | j                   S r   )r   rX   s    r   r   zProject.<lambda>5  s
    DMM r   )Nz.ropeprojectr   )r   r   r   __doc__r   r   r
   
deprecatedr   r   r   r    r   r   r   r   rq   r   r~   r   r   r   rF   r@   r   __classcell__r   s   @r   r   r      s    0 8F!$F
 UAB7 C7
*
=6

(1 # : :!
 67D12Gr   r   c                  B     e Zd ZdZ fdZd Z fdZd Zd ZdZ	 xZ
S )	NoProjectzvA null object for holding out of project files.

    This class is singleton use `get_no_project` global function
    c                t    t         j                  j                  j                         }t        |   |       y r   )r   r   r   FileSystemCommandsr   r   )r   r   r   s     r   r   zNoProject.__init__>  s(    YY))<<>
$r   c                l    |j                  dt        j                  j                        }t	        |      S r   )replacer!   r"   sepr   )r   r.   	real_names      r   r    zNoProject._get_resource_pathB  s%    LLbggkk2	##r   c                    t        |      j                  t        j                  j                  d      }t
        |   |      S r   )r   r   r!   r"   r   r   r(   )r   r.   universal_namer   s      r   r(   zNoProject.get_resourceF  s1    "400cBw#N33r   c                    g S r   ro   rX   s    r   r   zNoProject.get_filesJ      	r   c                    g S r   ro   rX   s    r   r   zNoProject.get_python_filesM  r   r   N)r   r   r   r   r   r    r(   r   r   _no_projectr   r   s   @r   r   r   8  s(    
%$4 Kr   r   c                 h    t         j                  t               t         _        t         j                  S r   )r   r   ro   r   r   r9   r9   S  s$    $ )	   r   c                  ,    e Zd Zd Zd Zd Zd ZddZy)r   c                    || _         d | _        t        j                  | j                  | j
                  | j
                  | j
                  | j
                        }| j                   j                  |       y r   )projectfilesresourceobserverResourceObserver_changed_invalidrJ   )r   r   rawobservers      r   r   z_FileListCacher.__init__Z  sS    
&77MM4==$--
 	!!+.r   c                    | j                   4t               | _         | j                  | j                  j                         | j                   S r   )r   r   
_add_filesr   r@   rX   s    r   r   z_FileListCacher.get_filesb  s5    ::DJOODLL--.zzr   c                    |j                         D ][  }|j                         r| j                  |       %| j                  j	                  |      rA| j
                  j                  |       ] y r   )get_children	is_folderr   r   rq   r   r   )r   r/   childs      r   r   z_FileListCacher._add_filesh  sR    ((* 	&E &\\,,U3

u%		&r   c                4    |j                         rd | _        y y r   )r   r   rp   s     r   r   z_FileListCacher._changedo  s    DJ  r   Nc                    d | _         y r   )r   )r   rS   new_resources      r   r   z_FileListCacher._invalids  s	    
r   r   )r   r   r   r   r   r   r   r   ro   r   r   r   r   Y  s    /&r   r   c                  0    e Zd Zd Zd Zd Zd Zd Zd Zy)r   c                     || _         g | _        y r   )r   hooks)r   r   s     r   r   z_DataFiles.__init__x  s    
r   c                   | j                   j                  y | j                  |      }|j                         r@t	        |j
                  d      5 }g }	 	 |j                  t        j                  |             %y # t        $ r Y nw xY wt        |      dk(  r|d   cd d d        S t        |      dkD  r|cd d d        S 	 d d d        y # 1 sw Y   y xY w)Nrbri   r   )r   r   	_get_filer#   open	real_pathr:   pickleloadEOFErrorlen)r   r.   file
input_filer;   s        r   	read_dataz_DataFiles.read_data|  s    <<""*~~d#;;=dnnd+ 
"zfkk*&=> 	    v;!#!!9
" 
" v;?!
" 
" #
" 
" 
"s0   C&A::	BCBC%CCc                   | j                   j                  | j                  |      }t               5 }|j	                  t        |j                  d            }|j	                  t        |j                  dz   d            }t        j                  ||d       t        j                  ||d        d d d        y y # 1 sw Y   y xY w)Nwbz.jsonwry   c                "    | j                         S r   )__getstate__)os    r   r   z'_DataFiles.write_data.<locals>.<lambda>  s    @P r   )default)
r   r   r   r   enter_contextr   r   r   dumpjson)r   r.   datar   cmoutput_fileoutput_file2s          r   
write_dataz_DataFiles.write_data  s    <<"".>>$'D R ..tDNND/IJ!//T^^g5Ms0STD+q1		$6PQ	R R /R Rs   A>B::Cc                :    | j                   j                  |       y r   )r   r:   r   hooks     r   add_write_hookz_DataFiles.add_write_hook  s    

$r   c                4    | j                   D ]	  } |         y r   )r   r  s     r   r   z_DataFiles.write  s    JJ 	DF	r   c                    | j                   j                  j                  dz   |z   }| j                   j                  |      S r   )r   r   r"   r\   )r   r.   r"   s      r   r   z_DataFiles._get_file  s6    ||&&++c1D8||$$T**r   N)	r   r   r   r   r  r  r  r   r   ro   r   r   r   r   w  s!    ""R +r   r   c                   t         j                  dk(  r\| dd dk(  r| S | dd dk(  rd| d   z   | dd	 z   } t        j                  j	                  t        j                  j                  |             S t        j                  j                  t        j                  j	                  t        j                  j                  |                   S )
a^  Return the real path of `path`

    Is equivalent to ``realpath(abspath(expanduser(path)))``.

    Of the particular notice is the hack dealing with the unfortunate
    situation of running native-Windows python (os.name == 'nt') inside
    of Cygwin (abspath starts with '/'), which apparently normal
    os.path.realpath completely messes up.

    cygwinri      z:\z:/z
/cygdrive/r   ry   N)r6   platformr!   r"   abspath
expanduserrealpath)r"   s    r   r   r     s     ||x!9K!AY$$q')DH4Dwwrww11$78877BGGOOBGG,>,>t,DEFFr   c                   | }|j                  d      }|d d D ]6  }|j                         r#|j                  |      r|j                  |      }6 y  |j                         r|j                  |d         r6|j                  |d         j                         r|j                  |d         S |j                  |d   dz         r=|j                  |d   dz         j                         s|j                  |d   dz         S y y y )N.z.py)r   r   	has_child	get_child)r/   rg   r1   packagespkgs        r   rf   rf     s   F}}S!H} &"2"23"7%%c*F	
 Xb\*  ".88:##HRL11Xb\E12$$Xb\E%9:DDF##HRL5$899 G 3 r   ))
__future__r   r7   r  r!   r6   r{   r   typingr   rope.base.fscommandsr   rope.base.resourceobserverr   r   	rope.baser   r   r   r	   r
   rope.base.exceptionsr   rope.base.prefsr   r   rope.base.resourcesr   r   r   cPickler   ImportErrorr   r   r   r9   r   r   r   rf   ro   r   r   <module>r/     s    "   	 
      5 5 D D 4 . > >
d dNt3h t3n 6! <(+ (+VG*:C  s   B 	B)(B)