o
    hT                     @  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   X/var/www/html/openai_agents/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	dBddZ
ddddCdd ZdDd$d%Z	d?dEd*d+ZdFd.d/ZdGd1d2ZdHd4d5ZdAd6d7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&   |  }|| jvr| j| d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)resolver8   appendrA   r*   r   r   r   r=   w   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)rC   r8   insert)rA   rF   r*   r   r   r   insert_search_path   s   

zModuleFinder.insert_search_pathTF)try_relative_pathfind_stubs_packagemodule
str | PathrJ   r    rK   &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   rL   rJ   rK   module_namemodule_pathtop_module_namepackager+   r   r   r   	find_spec   sF   
'zModuleFinder.find_specrY   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   rQ   r5   N__init__.pyz__init__.pyi)r   removesuffixr8   	_contentssuffixwith_suffixr(   exists_is_pkg_style_namespacerD   r1   rX   )rA   rY   	filepathsreal_module_namenamespace_dirsr*   path_contentschoiceabs_pathr+   init_moduler   r   r   rW      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   rO   rP   r    )rR   r>   setiter_submodulesstemparentrb   r7   _filter_py_modulesrelative_tor   debugrV   lenpartsaddrc   	with_name)	rA   r*   rn   	path_elemskipsubpathrel_subpathpy_fileru   r   r   r   rt     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   rt   filepathr?   r)   _module_depth)rA   rL   r   r   r   
submodulesV  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   rd   r)   ru   rv   rU   )rA   r*   extrZ   r   r   r   rS   g  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>   iterdirrU   NotADirectoryErrorrE   r   r   r   ra   v  s   

zModuleFinder._contentsc                 C  s   || j vr| j | d S d S r!   )r8   rD   rE   r   r   r   _append_search_path~  s   
z ModuleFinder._append_search_pathc                 C  sf   | j D ]-}| |D ]%}|jdkr/t|D ]}|j }r(| j| |j| | 	|j qq
qd S )Nz.pth)
r8   ra   rb   _handle_pth_filealways_scan_forr?   rD   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>rP   )oswalkr*   splitextr7   r   )rA   r*   rootdirsfilesrelfiler   r   r   rw     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   rv   rC   r8   r   
ValueError
IndexErrorrx   r{   rd   rI   r)   )rA   r*   parent_pathsearch_pathrel_pathr   r   r   rT     s   
&zModuleFinder._top_module_namer!   )r8   r9   r   r:   )r*   r   r   r:   )rF   rG   r*   r   r   r:   )rL   rM   rJ   r    rK   r    r   rN   )rY   r   r   r^   )r*   rm   rn   ro   r   rp   )rL   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   rs   r7   rB   r=   rI   r]   rW   rt   r   rS   ra   r   r@   rw   rT   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+$rl   r   c                 C  s$   | j dd}tt|pt|S )N	utf-8-sigencoding)	read_textr    _re_pkgresourcessearch_re_pkgutil)rl   coder   r   r   re     s   re   name_parts_and_pathNamePartsAndPathTyperG   c                 C  s   t | d S )Nr   )rz   )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*   rr   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"   rv   rz   lstripr   r
   _handle_editable_module
startswithr   r*   rd   rD   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 ]6}t|tjrf|jd	 }nt|tjrp|j}nqXt|tjr|jd
krt|jtjrdd |jjD   S qXt | jtrt|  }|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   rq   '   rB   r   MAPPINGc                 S  s(   g | ]}t |tjrtt|jjqS r   )rR   astConstantr   r   valuerv   )r   cstr   r   r   r     s   ( z+_handle_editable_module.<locals>.<listcomp>installrP   src)r   )$r'   r)   _editable_editables_patterns$_editable_scikit_build_core_patternsr   r   r   rU   r
   r   rV   r   r   rv   _editable_setuptools_patternsr   parsebodyrR   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    )rl   r   r   r    )r   r   r   rG   )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   re   r   r   r   r   r   r   r   r   <module>   sT   
  
N




