B
    Vp                 @   s   d 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m	Z	 dddZ
dd	 Ze d
d Zdd Zdd Ze	 fddZdddZdddZdd Zdd ZdS )z2A few useful functions for using rope as a library    N)pyobjectsdef)utils)
taskhandlec             C   sf   t | |}|dkr,tjj|}tjj } |dkr>| |S |dkrP| |S |dkrb| |S dS )aL  Get the resource at path

    You only need to specify `type` if `path` does not exist.  It can
    be either 'file' or 'folder'.  If the type is `None` it is assumed
    that the resource already exists.

    Note that this function uses `Project.get_resource()`,
    `Project.get_file()`, and `Project.get_folder()` methods.

    NfileZfolder)	path_relative_to_project_rootropebaseproject	_realpathZget_no_projectZget_resourceZget_fileZ
get_folder)r	   pathtypeZproject_path r   1lib/python3.7/site-packages/rope/base/libutils.pypath_to_resource   s    



r   c             C   s   t | j|S )N)relativeZaddress)r	   r   r   r   r   r   #   s    r   c             C   sf   t jj| tjjd} t jj|tjjd}|| kr@dS || d rb|t	| d d  S d S )N/    )
r   r   r	   r
   replaceosr   sep
startswithlen)rootr   r   r   r   r   &   s    r   c             C   sR   t | |}|dkrdS xt| jD ]}|| q"W | jjrNtjj| || dS )z|Report that the contents of file at `path` was changed

    The new contents of file is retrieved by reading the file.

    N)	r   listZ	observersZresource_changedpycoreZautomatic_soar   r   Zperform_soa_on_changed_scopes)r	   r   Zold_contentresourceZobserverr   r   r   report_change0   s    
r   c             C   s   | j | dS )zwPerform static object analysis on a python file in the project

    Note that this might be really time consuming.
    N)r   analyze_module)r	   r   r   r   r   r   @   s    r   c             C   sH   |   }|dt|}x*|D ]"}||j t| | |  qW dS )zzPerform static object analysis on all python files in the project

    Note that this might be really time consuming.
    zAnalyzing ModulesN)Zget_python_filesZcreate_jobsetr   Zstarted_jobr   r   Zfinished_job)r	   Ztask_handleZ	resourcesZjob_setr   r   r   r   analyze_modulesH   s    

r   Fc             C   s   t j| j|||dS )zReturns a `PyObject` object for the given code

    If `force_errors` is `True`, `exceptions.ModuleSyntaxError` is
    raised if module has syntax errors.  This overrides
    ``ignore_syntax_errors`` project config.

    )force_errors)r   ZPyModuler   )r	   coder   r    r   r   r   get_string_moduleU   s    r"   c             C   s   t | || S )z+Returns a `Scope` object for the given code)r"   Z	get_scope)r	   r!   r   r   r   r   get_string_scopea   s    r#   c             C   s   | j |S )N)r   is_python_file)r	   r   r   r   r   r$   f   s    r$   c             C   sx   |   r| j}| j}n0| jdkr2| jj}| jj}n| jd d }| j}x,||jkrr|drr|jd | }|j}qHW |S )Nz__init__.py.)Z	is_foldernameparentZ	has_child)r   Zmodule_nameZsource_folderr   r   r   modnamej   s    



r)   )N)NF)N)__doc__os.pathr   Zrope.base.projectr   Zrope.base.pycoreZ	rope.baser   r   r   r   r   Z
deprecatedr   r   r   ZNullTaskHandler   r"   r#   r$   r)   r   r   r   r   <module>   s    



