o
    iT                     @  s  d dl m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
mZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ er^d dlmZmZ d dlmZ d dlmZ dd dD Zdd dD Zdd dD Zdd dD Z e!e"df Z#	 e!e#ef Z$	 d:ddZ%eG d d! d!Z&eG d"d# d#Z'G d$d% d%Z(e)d&Z*e)d'Z+e)d(Z,d;d+d,Z-d<d0d1Z.eG d2d3 d3Z/d=d6d7Z0d=d8d9Z1dS )>    )annotationsN)defaultdict)suppress)	dataclass)chain)Path)TYPE_CHECKINGClassVar)UnhandledEditableModuleError)logger)IteratorSequence)Pattern)Modulec                 C     g | ]}t |qS  recompile.0patr   r   ]/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/griffe/_internal/finder.py
<listcomp>(       r   )z^__editables_\w+\.py$z^_editable_impl_\w+\.py$c                 C  r   r   r   r   r   r   r   r   )   r   )z^__editable__\w+\.py$c                 C  r   r   r   r   r   r   r   r   *   r   )z^_\w+_editable.py$c                 C  r   r   r   r   r   r   r   r   +   r   )z^_\w+_editable_loader.py$.stringstrpatternsSequence[Pattern]returnboolc                   s   t  fdd|D S )Nc                 3  s    | ]}|  V  qd S N)match)r   patternr   r   r   	<genexpr>4   s    z!_match_pattern.<locals>.<genexpr>)any)r   r   r   r$   r   _match_pattern3   s   r'   c                   @  s2   e Zd ZU dZded< 	 ded< 	 dZded< dS )	PackagezThis class is a simple placeholder used during the process of finding packages.

    Parameters:
        name: The package name.
        path: The package path(s).
        stubs: An optional path to the related stubs file (.pyi).
    r   namer   pathNzPath | Nonestubs)__name__
__module____qualname____doc____annotations__r+   r   r   r   r   r(   7   s   
 r(   c                   @  s$   e Zd ZU dZded< 	 ded< dS )NamespacePackagezThis class is a simple placeholder used during the process of finding packages.

    Parameters:
        name: The package name.
        path: The package paths.
    r   r)   
list[Path]r*   N)r,   r-   r.   r/   r0   r   r   r   r   r1   I   s   
 r1   c                   @  s   e Zd ZU dZg dZded< 	 eeZded< 	 d?d@ddZdAddZ	dAddZ
dBddZddddCd!d"ZdDd&d'Z	d?dEd,d-ZdFd0d1ZdGd3d4ZdHd6d7ZdId8d9ZdJd;d<ZdKd=d>ZdS )LModuleFinderaf  The Griffe finder, allowing to find modules on the file system.

    The module finder is generally not used directly.
    Each [`GriffeLoader`][griffe.GriffeLoader] instance creates its own module finder instance.
    The finder can be configured when instantiating the loader
    thanks to the [loader][griffe.GriffeLoader]'s `search_paths` parameter.
    ).pyz.pycz.pyoz.pyd.pyiz.sozClassVar[list[str]]accepted_py_module_extensionszClassVar[set[str]]extensions_setNsearch_pathsSequence[str | Path] | Noner   Nonec                 C  sB   i | _ g | _	 |ptjD ]	}| t| qtt| _| 	  dS )zmInitialize the finder.

        Parameters:
            search_paths: Optional paths to search into.
        N)
_paths_contentsr8   sysr*   append_search_pathr   r   list_always_scan_for_extend_from_pth_files)selfr8   r*   r   r   r   __init__f   s   
zModuleFinder.__init__r*   r   c                 C  s   |  |  dS )zAppend a search path.

        The path will be resolved (absolute, normalized).
        The path won't be appended if it is already in the search paths list.

        Parameters:
            path: The path to append.
        N)_append_search_pathresolverA   r*   r   r   r   r=   w   s   	zModuleFinder.append_search_pathc                 C  s   || j vr| j | d S d S r!   )r8   appendrE   r   r   r   rC      s   
z ModuleFinder._append_search_pathpositionintc                 C  s(   |  }|| jvr| j|| dS dS )a/  Insert a search path at the given position.

        The path will be resolved (absolute, normalized).
        The path won't be inserted if it is already in the search paths list.

        Parameters:
            position: The insert position in the list.
            path: The path to insert.
        N)rD   r8   insert)rA   rG   r*   r   r   r   insert_search_path   s   

zModuleFinder.insert_search_pathTF)try_relative_pathfind_stubs_packagemodule
str | PathrK   r    rL   &tuple[str, Package | NamespacePackage]c          	      C  sd  t |tr| |\}}| |}n1|r9z| t|\}}W n ty2   |}|ddd }Y nw | |}n
|}|ddd }|sL|| |fS z| |}W n ty^   d}Y nw z	| |d }W n tys   d}Y nw |du r|du rt||r|rt |trt |tr|j	|_
||fS t |trt |tr| j	|j	7  _	||fS ||p|fS )a5  Find the top-level parent module of a module.

        If a Path is passed, only try to find the module as a file path.
        If a string is passed, first try to find the module as a file path,
        then look into the search paths.

        Parameters:
            module: The module name or path.
            try_relative_path: Whether to try finding the module as a relative path,
                when the given module is not already a path.
            find_stubs_package: Whether to search for stubs-only package.
                If both the package and its stubs are found, they'll be merged together.
                If only the stubs are found, they'll be used as the package itself.

        Raises:
            FileNotFoundError: When a Path was passed and the module could not be found:

                - the directory has no `__init__.py` file in it
                - the path does not exist

            ModuleNotFoundError: When a string was passed and the module could not be found:

                - no `module/__init__.py`
                - no `module.py`
                - no `module.pth`
                - no `module` directory (namespace packages)
                - or unsupported .pth file

        Returns:
            The name of the module, and an instance representing its (namespace) package.
        .   r   N-stubs)
isinstancer   _module_name_path_top_module_nameFileNotFoundErrorsplitfind_packageModuleNotFoundErrorr(   r*   r+   r1   )	rA   rM   rK   rL   module_namemodule_pathtop_module_namepackager+   r   r   r   	find_spec   sF   
'zModuleFinder.find_specrZ   r   Package | NamespacePackagec                 C  s  t |t | dg}|}|d}g }| jD ]f}| |}|r}|D ]Z}|| }||v r||jrD|d}	t|||	 r=|	nd    S |d }
|
 ret|
se|
d}	t||
|	 r^|	nd    S |d }
|
 rwt||
d    S |	| q"q|rt
||S t|)a  Find a package or namespace package.

        Parameters:
            module_name: The module name.

        Raises:
            ModuleNotFoundError: When the module cannot be found.

        Returns:
            A package or namespace package wrapper.
        r4   rR   r5   N__init__.pyz__init__.pyi)r   removesuffixr8   	_contentssuffixwith_suffixr(   exists_is_pkg_style_namespacerF   r1   rY   )rA   rZ   	filepathsreal_module_namenamespace_dirsr*   path_contentschoiceabs_pathr+   init_moduler   r   r   rX      s6   



 
 

zModuleFinder.find_packagePath | list[Path]seen
set | NoneIterator[NamePartsAndPathType]c           	      c  s&   t |trt }|D ]}| ||E dH  qdS |jdkr"|j}n|j| jv r*dS t|p.d}| |D ][}|	|}|j|v rHt
d| q5|jdk}|j}|sZ|ddd }|dkr{t|jdkrfq5|jdd	 |fV  |durz||j q5|r|d
j|fV  q5||j|fV  q5dS )a  Iterate on a module's submodules, if any.

        Parameters:
            path: The module path.
            seen: If not none, this set is used to skip some files.
                The goal is to replicate the behavior of Python by
                only using the first packages (with `__init__` modules)
                of the same name found in different namespace packages.
                As soon as we find an `__init__` module, we add its parent
                path to the `seen` set, which will be reused when scanning
                the next namespace packages.

        Yields:
            name_parts (tuple[str, ...]): The parts of a submodule name.
            filepath (Path): A submodule filepath.
        NrB   r   z'Skip %s, another module took precedencer4   rP   rQ   r    )rS   r>   setiter_submodulesstemparentrc   r7   _filter_py_modulesrelative_tor   debugrW   lenpartsaddrd   	with_name)	rA   r*   ro   	path_elemskipsubpathrel_subpathpy_filerv   r   r   r   ru     s>   




zModuleFinder.iter_submodulesr   list[NamePartsAndPathType]c                 C  s(   t t| |j| | j|j tdS )zReturn the list of a module's submodules.

        Parameters:
            module: The parent module.

        Returns:
            A list of tuples containing the parts of the submodule name and its path.
        )key)sortedr   ru   filepathr?   r)   _module_depth)rA   rM   r   r   r   
submodulesX  s   	
zModuleFinder.submodulestuple[str, Path]c                 C  sv   |  }| r%| jD ]}|d|  }| r|j|f  S q|j|fS | r9|jdkr4|jj|fS |j|fS t)NrB   )absoluteis_dirr6   re   r)   rv   rw   rV   )rA   r*   extr[   r   r   r   rT   i  s   



zModuleFinder._module_name_pathr2   c              	   C  sL   || j vr!zt| | j |< W n ttfy    g | j |< Y nw | j | S r!   )r;   r>   iterdirrV   NotADirectoryErrorrE   r   r   r   rb   x  s   

zModuleFinder._contentsc                 C  sf   | j D ]-}| |D ]%}|jdkr/t|D ]}|j }r(| j| |j| | 	|j qq
qd S )Nz.pth)
r8   rb   rc   _handle_pth_filealways_scan_forr?   rF   r*   joinpathr=   )rA   r*   item	directoryscanr   r   r   r@     s   


z#ModuleFinder._extend_from_pth_filesIterator[Path]c                 c  sd    t j|dddD ]&\}}}dd |D |d d < |D ]}t j|d | jv r.t||V  qq	d S )NT)topdownfollowlinksc                 S  s   g | ]}|d kr|qS )__pycache__r   )r   dirr   r   r   r     s    z3ModuleFinder._filter_py_modules.<locals>.<listcomp>rQ   )oswalkr*   splitextr7   r   )rA   r*   rootdirsfilesrelfiler   r   r   rx     s   zModuleFinder._filter_py_modulesc              	   C  s   |  r|n|j}| }| jD ]%}ttt || }|jd W  d      S 1 s0w   Y  q|j|krQ|jd 	 rQ|j}|j|krQ|jd 	 sB| 
d|j |jS )Nr   r`   )r   rw   rD   r8   r   
ValueError
IndexErrorry   r|   re   rJ   r)   )rA   r*   parent_pathsearch_pathrel_pathr   r   r   rU     s   
&zModuleFinder._top_module_namer!   )r8   r9   r   r:   )r*   r   r   r:   )rG   rH   r*   r   r   r:   )rM   rN   rK   r    rL   r    r   rO   )rZ   r   r   r_   )r*   rn   ro   rp   r   rq   )rM   r   r   r   )r*   r   r   r   )r*   r   r   r2   )r   r:   )r*   r   r   r   )r*   r   r   r   )r,   r-   r.   r/   r6   r0   rt   r7   rB   r=   rC   rJ   r^   rX   ru   r   rT   rb   r@   rx   rU   r   r   r   r   r3   X   s,   
 



S2
B



	r3   zG(?:__import__\([\"']pkg_resources[\"']\).declare_namespace\(__name__\))zP(?:__path__ = __import__\([\"']pkgutil[\"']\).extend_path\(__path__, __name__\))z^import[ \t]+\w+$rm   r   c                 C  s$   | j dd}tt|pt|S )N	utf-8-sigencoding)	read_textr    _re_pkgresourcessearch_re_pkgutil)rm   coder   r   r   rf     s   rf   name_parts_and_pathNamePartsAndPathTyperH   c                 C  s   t | d S )Nr   )r{   )r   r   r   r   r     s   r   c                   @  s"   e Zd ZU ded< dZded< dS )_SPr   r*   rs   r   r   N)r,   r-   r.   r0   r   r   r   r   r   r     s   
 r   r*   	list[_SP]c              	   C  s   g }z| j dd}W n ty   | Y S w | ddjddD ]K}| }t|rW| j|tdd  	  d }t
t t|W  d      S 1 sRw   Y  |rm|d	smtj|rm|tt| q"|S )
Nr   r   ;
Fkeependsimportr4   #)r   UnicodeDecodeErrorstripreplace
splitlines_re_import_liner"   rw   r{   lstripr   r
   _handle_editable_module
startswithr   r*   re   rF   r   r   )r*   directoriestextlineeditable_moduler   r   r   r     s"   
 
$r   c           	   
   C  s  t | jg ttR rHz| jdd jdd}W n ty* } zt| |d }~ww t	|d 
dd }|jdrCt|jjgS t|gS t | jtrt| jd	d}|jD ]6}t|tjrh|jd
 }nt|tjrr|j}nqZt|tjr|jdkrt|jtjrdd |jjD   S qZt | jtrt| jd	d}|jD ]B}t|tjrt|jtjrt|jjtjr|jjjdkrt|jj d tj!rt	|jj d jd}t"|# j}t||dg  S qt| )Nr   r   Fr   rr   '   rB   utf8r   MAPPINGc                 S  s(   g | ]}t |tjrtt|jjqS r   )rS   astConstantr   r   valuerw   )r   cstr   r   r   r     s   ( z+_handle_editable_module.<locals>.<listcomp>installrQ   src)r   )$r'   r)   _editable_editables_patterns$_editable_scikit_build_core_patternsr   r   r   rV   r
   r   rW   r   r   rw   _editable_setuptools_patternsr   parsebodyrS   Assigntargets	AnnAssigntargetNameidr   Dictvalues_editable_meson_python_patternsExprCallfuncargsr   nextr   )	r*   editable_lineserrornew_pathparsed_modulenoder   
build_pathpkg_namer   r   r   r     sN   


$

r   )r   r   r   r   r   r    )rm   r   r   r    )r   r   r   rH   )r*   r   r   r   )2
__future__r   r   r   r   r<   collectionsr   
contextlibr   dataclassesr   	itertoolsr   pathlibr   typingr   r	   griffe._internal.exceptionsr
   griffe._internal.loggerr   collections.abcr   r   r   griffe._internal.modelsr   r   r   r   r   tupler   NamePartsTyper   r'   r(   r1   r3   r   r   r   r   rf   r   r   r   r   r   r   r   r   <module>   sT   
  
L




