Format: application/x-python-code
⬇ Download File (13.2 KB)
   1  o
   2  
   3  �h�@�@sdZddlZddlmZddlmZddlmZddlm	Z	m
   4  Z
   5  ddlmZddl
   6  mZdd	lmZmZdd
   7  lmZmZmZddlmZddlmZdd
   8  lmZmZddlmZdZdZ dZ!dd�Z"dd�Z#Gdd�d�Z$dd�Z%dd�Z&d dd�Z'dd�Z(dS)!a�
   9  Projects are a way to handle Python projects within Jedi. For simpler plugins
  10  you might not want to deal with projects, but if you want to give the user more
  11  flexibility to define sys paths and Python interpreters for a project,
  12  :class:`.Project` is the perfect way to allow for that.
  13  
  14  Projects can be saved to disk and loaded again, to allow project definitions to
  15  be used across repositories.
  16  �N)�Path)�chain)�debug)�get_cached_default_environment�create_environment)�WrongVersion)�search_in_module)�split_search_string�get_module_names)�load_module_from_path�load_namespace_from_path�iter_module_names)�discover_buildout_paths)�%inference_state_as_method_param_cache)�%recurse_find_python_folders_and_files�search_in_file_ios)�FolderIOz.jedi)zsetup.pyz.gitz.hgzrequirements.txtzMANIFEST.inzpyproject.toml�cs�fdd�}|S)Nc?sx�g}g}�|i|��D]-}|jj}|dur||vrq|jdkr1|jdur1|j|vr+q|�|j�|V|�|�qdS)N�module)�_name�	tree_name�type�module_path�append)�args�kwargsZfound_tree_nodesZ
  17  found_modulesZ
  18  definition�	tree_node��func��Fc:\Users\shubh\Videos\agent\nova\Lib\site-packages\jedi/api/project.py�wrapper"s�
  19  �z(_try_to_skip_duplicates.<locals>.wrapperr)rr!rrr �_try_to_skip_duplicates!s
  20  r"ccs0�t�}|D]}||vr
  21  q|�|�|VqdS�N)�set�add)�path�used�prrr �_remove_duplicates_from_path2s�
  22  �r)c@s�eZdZdZdZedd��Zedd��Zedd��Z	d	d
  23  �Z
  24  ddddd
  25  d�	d)dd�Zedd��Z
  26  edd��Zedd��Zedd��Ze�dd��Ze�d*dd��Zdd�Zdd �d!d"�Zd#d$�Zed+d%d&��Zd'd(�ZdS),�Projectz�
  27      Projects are a simple way to manage Python folders and define how Jedi does
  28      import resolution. It is mostly used as a parameter to :class:`.Script`.
  29      Additionally there are functions to search a whole project.
  30      NcCs
  31  |�t�Sr#)�joinpath�_CONFIG_FOLDER��	base_pathrrr �_get_config_folder_pathCs
  32  zProject._get_config_folder_pathcCst�|��d�S)Nzproject.json)r*r/r+r-rrr �_get_json_pathGszProject._get_json_pathcCslt|t�r	t|�}t|�|���}t�|�\}}Wd�n1s"wY|dkr2|di|��Std��)z�
  33          Loads a project from a specific path. You should not provide the path
  34          to ``.jedi/project.json``, but rather the path to the project folder.
  35  
  36          :param path: The path of the directory you want to use as a project.
  37          NrzEThe Jedi version of this project seems newer than what we can handle.r)�
  38  isinstance�strr�openr0�json�loadr)�clsr&�f�version�datarrr r5Ks
  39  ��zProject.loadcCs�t|j�}|�dd�|�dd�dd�|��D�}t|d�|d<|�|j�jddd�t|�	|j�d	��}t
  40  �t|f|�Wd�S1sIwYdS)
  41  z[
  42          Saves the project configuration in the project in ``.jedi/project.json``.
  43          �_environmentN�_djangocSsi|]
  44  \}}|�d�|�qS)�_)�lstrip)�.0�k�vrrr �
  45  <dictcomp>fsz Project.save.<locals>.<dictcomp>r&T)�parents�exist_ok�w)
  46  �dict�__dict__�pop�itemsr2r/�_path�mkdirr3r0r4�dump�_SERIALIZER_VERSION)�selfr9r7rrr �save_s
  47  $�zProject.saveFrT)�environment_path�load_unsafe_extensions�sys_path�added_sys_path�smart_sys_path�returncCsdt|t�rt|���}||_||_|durttt|��}||_||_	||_
  48  d|_ttt|��|_dS)a$
  49          :param path: The base path for this project.
  50          :param environment_path: The Python executable path, typically the path
  51              of a virtual environment.
  52          :param load_unsafe_extensions: Default False, Loads extensions that are not in the
  53              sys path and in the local directories. With this option enabled,
  54              this is potentially unsafe if you clone a git repository and
  55              analyze it's code, because those compiled extensions will be
  56              important and therefore have execution privileges.
  57          :param sys_path: list of str. You can override the sys path if you
  58              want. By default the ``sys.path.`` is generated by the
  59              environment (virtualenvs, etc).
  60          :param added_sys_path: list of str. Adds these paths at the end of the
  61              sys path.
  62          :param smart_sys_path: If this is enabled (default), adds paths from
  63              local directories. Otherwise you will have to rely on your packages
  64              being properly configured on the ``sys.path``.
  65          NF)
  66  r1r2r�absoluterI�_environment_path�list�map�	_sys_path�_smart_sys_path�_load_unsafe_extensionsr;rR)rMr&rOrPrQrRrSrrr �__init__ms
  67  zProject.__init__cC�|jS)z1
  68          The base path for this project.
  69          �rI�rMrrr r&��zProject.pathcCr])zz
  70          The sys path provided to this project. This can be None and in that
  71          case will be auto generated.
  72          )rYr_rrr rQ��zProject.sys_pathcCr])zu
  73          If the sys path is going to be calculated in a smart way, where
  74          additional paths are added.
  75          )rZr_rrr rS�razProject.smart_sys_pathcCr])z=
  76          Wheter the project loads unsafe extensions.
  77          )r[r_rrr rP�r`zProject.load_unsafe_extensionscCs4t|j���}z|�d�W|StyY|Sw)N�)rW�environmentZget_sys_path�remove�
  78  ValueError)rM�inference_staterQrrr �_get_base_sys_path�s��zProject._get_base_sys_pathc
  79  Cs�t|j�}g}|jdurt|�|��}nt|j�}|jrc|�t|j��|jdurc|t	tt
  80  ||j��7}|rcg}|jjD] }||jksI|j|jvrKn|sU|�d��
  81  �rUq<|�t|��q<|t|�7}|jrn|�t|j��|||}	tt|	��S)z�
  82          Keep this method private for all users of jedi. However internally this
  83          one is used like a public method.
  84          N�__init__.py)rWrRrYrgrZrr2rIZscript_pathrXrrBr+�is_file�reversedr;r))
  85  rMrfZadd_parent_pathsZadd_init_paths�suffixed�prefixedrQZ	traversed�parent_pathr&rrr �
  86  _get_sys_path�s8
  87  
  88  
  89  
  90  �
  91  �zProject._get_sys_pathcCs8|jdur|jdurt|jdd�|_|jSt�|_|jS)NF)�safe)r:rVrrr_rrr �get_environment�s
  92  
  93  �zProject.get_environment��
  94  all_scopescCs|j||d�S)aO
  95          Searches a name in the whole project. If the project is very big,
  96          at some point Jedi will stop searching. However it's also very much
  97          recommended to not exhaust the generator. Just display the first ten
  98          results to the user.
  99  
 100          There are currently three different search patterns:
 101  
 102          - ``foo`` to search for a definition foo in any file or a file called
 103            ``foo.py`` or ``foo.pyi``.
 104          - ``foo.bar`` to search for the ``foo`` and then an attribute ``bar``
 105            in it.
 106          - ``class foo.bar.Bar`` or ``def foo.bar.baz`` to search for a specific
 107            API type.
 108  
 109          :param bool all_scopes: Default False; searches not only for
 110              definitions on the top level of a module level, but also in
 111              functions and classes.
 112          :yields: :class:`.Name`
 113          rq��_search_func)rM�stringrrrrr �search�szProject.searchcKs|j|fddi|��S)au
 114          Like :meth:`.Script.search`, but completes that string. An empty string
 115          lists all definitions in a project, so be careful with that.
 116  
 117          :param bool all_scopes: Default False; searches not only for
 118              definitions on the top level of a module level, but also in
 119              functions and classes.
 120          :yields: :class:`.Completion`
 121          �completeTrs)rMrurrrr �complete_searchs
 122  zProject.complete_searchc#s�ddlm}|d�d�}|j}|��}t�d||�t|�\}}	|	d}
 123  |
 124  d}ttt	�j
 125  ���}g}
 126  |D]\}}|dur�|��}||
 127  ksJ||kr�|�d�}z	t
 128  ||���}WnEty�|�d�}z	t
 129  ||���}Wnty}t||���}YnwYnwq6|
 130  �|�t|j�j|
 131  d	|
 132  d
 133  fvr�t
 134  ||���}nq6t�d|�t|||jg||	|ddd
 135  �EdHq6t||
 136  |
 137  |d�D]$�t�j|d�}�fdd�|D�}t|�}t|�|||	|dd�EdHq��fdd���|�D�}tt|||��}t|||||	|dd�EdHdS)Nr)�Scriptrb)�projectz!Search for string %s, complete=%sz-stubsrhz__init__.pyiz.pyz.pyizSearch of a specific module %sT)�names�wanted_type�wanted_namesrw�convert�ignore_imports)rwrqcsg|]}��|��qSr)Zcreate_name�r>�n)�module_contextrr �
 138  <listcomp>Tsz(Project._search_func.<locals>.<listcomp>)r{r|r}rwrcsg|]	}|�jkr|�qSrr^)r>r(r_rr r�as
 139  
 140  �)r{r|r}rwr~)�jediryZ_inference_stateZ_get_module_contextrZdbgr	rrr2rIZ
 141  get_base_nameZget_file_iorZ
 142  as_context�FileNotFoundErrorrrrr&�namerrr
 143  r�_remove_importsrnrWr
 144  )rMrurwrrry�srfZempty_module_contextr|r}r�Zstub_folder_nameZiosZfile_iosZ	folder_ioZfile_io�	file_namer7�mr{rQr)r�rMr rts��
 145  
 146  ���
 147  �
 148  ��
 149  ��zProject._search_funccCsd|jj|jfS)Nz<%s: %s>)�	__class__�__name__rIr_rrr �__repr__qszProject.__repr__)rTN)TF)FF)r��
 150  __module__�__qualname__�__doc__r:�staticmethodr/r0�classmethodr5rNr\�propertyr&rQrSrPrrgrnrprvrxr"rtr�rrrr r*;sH
 151  
 152  
 153  �	
 154  �-
 155  
 156  
 157  
 158  
 159  	/Sr*c	Cs:tD]}z
 160  |�|���rWdSWqtyYqwdS)NTF)�_CONTAINS_POTENTIAL_PROJECTr+�exists�OSError)r&r�rrr �_is_potential_projectus��r�c
 161  Cs`z"t|�d�d��}d|��vWd�WS1swYWdStttfy/YdSw)zB Detects the path of the very well known Django library (if used) z	manage.py�rbsDJANGO_SETTINGS_MODULENF)r3r+�readr��IsADirectoryError�PermissionError)�	directoryr7rrr �_is_django_paths
 162  (��r�cCs|dur	t��}n	t|t�rt|�}|��}d}d}t|g|j�D]J}zt�|�WSt	t
 163  tfy7Yn	ty?Yq!w|durR|�
 164  d���rLq!|��sR|}t|�rat|�}d|_|S|durkt|�rk|}q!|durtt|�S|dur|t|�S|��r�|n|j}t|�S)aI
 165      If a project is not defined by the user, Jedi tries to define a project by
 166      itself as well as possible. Jedi traverses folders until it finds one of
 167      the following:
 168  
 169      1. A ``.jedi/config.json``
 170      2. One of the following files: ``setup.py``, ``.git``, ``.hg``,
 171         ``requirements.txt`` and ``MANIFEST.in``.
 172      NrhT)r�cwdr1r2rUrrBr*r5r�r�r��NotADirectoryErrorr+r�rir�r;r��is_dir�parent)r&�checkZ
 173  probable_pathZfirst_no_init_file�dirrz�curdirrrr �get_default_project�sB
 174  
 175  
 176  ��r�cCsdd�|D�S)NcSs$g|]}|jdus|jdvr|�qS)N)r�	namespace)rZapi_typer�rrr r��s
 177  �z#_remove_imports.<locals>.<listcomp>r)r{rrr r��s�r�r#))r�r4�pathlibr�	itertoolsrr�rZjedi.api.environmentrrZjedi.api.exceptionsrZjedi.api.completionrZjedi.api.helpersr	r
 178  Zjedi.inference.importsrrr
 179  Zjedi.inference.sys_pathrZjedi.inference.cacherZjedi.inference.referencesrrZjedi.file_iorr,r�rLr"r)r*r�r�r�r�rrrr �<module>s6	�	<
 180  
 181  	4