
    gT                       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  cg c]  } ej;                  |        c} ZdD  cg c]  } ej;                  |        c} ZdD  cg c]  } ej;                  |        c} Z dD  cg c]  } ej;                  |        c} Z!e"e#df   Z$	 e"e$ef   Z%	 d#dZ&e G d d             Z'e G d d             Z( G d d      Z) ej:                  d      Z* ej:                  d      Z+ ej:                  d      Z,d$dZ-d%dZ.e G d d              Z/d&d!Z0d&d"Z1yc c} w c c} w c c} w c c} w )'    )annotationsN)defaultdict)suppress)	dataclass)chain)Path)TYPE_CHECKINGClassVar)UnhandledEditableModuleError)logger)IteratorSequence)Pattern)Module)z^__editables_\w+\.py$z^_editable_impl_\w+\.py$)z^__editable__\w+\.py$)z^_\w+_editable.py$)z^_\w+_editable_loader.py$.c                ,     t         fd|D              S )Nc              3  @   K   | ]  }|j                          y wN)match).0patternstrings     C/var/www/openai/venv/lib/python3.12/site-packages/_griffe/finder.py	<genexpr>z!_match_pattern.<locals>.<genexpr>4   s     =Hw}}V$Hs   )any)r   patternss   ` r   _match_patternr   3   s    =H===    c                  8    e Zd ZU dZded<   	 ded<   	 dZded<   y)	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).
    strnamer   pathNzPath | Nonestubs)__name__
__module____qualname____doc____annotations__r#    r   r   r   r   7   s&     I
JE;r   r   c                  (    e Zd ZU dZded<   	 ded<   y)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'   r(   r)   r   r   r+   r+   I   s     I!
)r   r+   c                      e Zd ZU dZg dZded<   	  ee      Zded<   	 dddZdd	Z	dd
Z
ddd	 	 	 	 	 	 	 ddZddZ	 d	 	 	 	 	 ddZddZd dZd!dZddZd"dZd#dZd$dZy)%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_setNc                    i | _         g | _        	 |xs t        j                  D ]  }| j	                  t        |              t        t              | _        | j                          y)zmInitialize the finder.

        Parameters:
            search_paths: Optional paths to search into.
        N)
_paths_contentssearch_pathssysr"   append_search_pathr   r   list_always_scan_for_extend_from_pth_files)selfr5   r"   s      r   __init__zModuleFinder.__init__f   s]     8:(*& !,CHH,D##DJ/ - 8C47H##%r   c                x    |j                         }|| j                  vr| j                  j                  |       yy)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)resolver5   appendr;   r"   s     r   r7   zModuleFinder.append_search_pathw   s6     ||~t((($$T* )r   c                z    |j                         }|| j                  vr| j                  j                  ||       yy)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)r>   r5   insert)r;   positionr"   s      r   insert_search_pathzModuleFinder.insert_search_path   s8     ||~t((($$Xt4 )r   TF)try_relative_pathfind_stubs_packagec               .   t        |t              r&| j                  |      \  }}| j                  |      }nI|r0	 | j                  t        |            \  }}| j                  |      }n|}|j                  dd      d   }|s|| j                  |      fS 	 | j                  |      }	 | j                  |dz         }||t        |      |rz|rxt        |t              r%t        |t              r|j                  |_
        ||fS t        |t              r/t        |t              r|xj                  |j                  z  c_	        ||fS ||xs |fS # t        $ r |}|j                  dd      d   }Y w xY w# t        $ r d}Y w xY w# t        $ r d}Y w xY w)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#   r+   )	r;   modulerE   rF   module_namemodule_pathtop_module_namepackager#   s	            r   	find_speczModuleFinder.find_spec   s   N fd#'+'='=f'E$K"33K@OE+/+A+A$v,+O([
 #'"7"7"D K$ll3215O " 1 1/ BBB	''8G	%%o&@AE
 ?u}%o66 u'7+
5'0J %

 '' G%56:eM];^

*'' G,u,,I % :$"(,,sA"6q"9:  # 	G	 # 	E	s5   E E5 (F  E21E25FFFFc                   t        |      t        | d      g}|}|j                  d      r|dd }g }| j                  D ]  }| j                  |      }|s|D ]  }||z  }||v s|j                  r4|j                  d      }	t        |||	j                         r|	nd      c c S |dz  }
|
j                         r?t        |
      s4|
j                  d      }	t        ||
|	j                         r|	nd      c c S |dz  }
|
j                         rt        ||
d      c c S |j                  |         |rt        ||      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.
        r/   rJ   Nir0   __init__.pyz__init__.pyi)r   endswithr5   	_contentssuffixwith_suffixr   exists_is_pkg_style_namespacer?   r+   rQ   )r;   rS   	filepathsreal_module_namenamespace_dirsr"   path_contentschoiceabs_pathr#   init_modules              r   rP   zModuleFinder.find_package   s]     K=$%
	 '$$X./4%%D NN40M'F#f}H=0#??$,$8$8$@E#*+;XPUP\P\P^udh#ii&.&>&--/8OP[8\$/$;$;F$CE#*+;[SXS_S_Sa%gk#ll&.&?&--/#*+;[$#OO&--h7 ( && #K@@!+..r   c              #  0  K   t        |t              r,t               }|D ]  }| j                  ||      E d{     y|j                  dk(  r|j
                  }n|j                  | j                  v ryt        |xs d      }| j                  |      D ]  }|j                  |      }|j
                  |v rt        j                  d|       9|j                  dk(  }|j                  }|s|j                  dd      d   }|dk(  rKt        |j                        dk(  r|j                  dd	 |f ||j                  |j
                         |r |j!                  d
      j                  |f |j#                  |      j                  |f  y7 \w)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.
        Nr<   r)   z'Skip %s, another module took precedencer/   rH   rI   r    )rK   r8   setiter_submodulesstemparentr\   r2   _filter_py_modulesrelative_tor   debugrO   lenpartsaddr]   	with_name)	r;   r"   seen	path_elemskipsubpathrel_subpathpy_filerl   s	            r   rk   zModuleFinder.iter_submodules  s    * dD! 5D!	//	4@@@ "99
";;D [[D/// 4:2..t4G!--d3K!!T)FP!((E1G##Dzz#q)!,z! {(()Q.!'',g55#HH[//0!--b177@@!++D177@@- 5! As   5FFC:F3A!Fc           	         t        t        | j                  |j                        | j                  | j                  |j
                                 t              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   rk   filepathr9   r!   _module_depth)r;   rR   s     r   
submoduleszModuleFinder.submodulesW  sM     $$V__5$$T%:%:6;;%GH 
 	
r   c                f   |j                         }|j                         rG| j                  D ]*  }|d| z  }|j                         s|j                  |fc S  |j                  |fS |j                         r5|j
                  dk(  r|j                  j                  |fS |j
                  |fS t        )Nr<   )absoluteis_dirr1   r^   r!   rl   rm   rN   )r;   r"   extrT   s       r   rL   zModuleFinder._module_name_pathh  s    }};;=99"xu%55%%'99k11 : 99d?";;=yyJ&{{''--99d?"r   c                    || j                   vr'	 t        |j                               | j                   |<   | j                   |   S # t        t        f$ r g | j                   |<   Y /w xY wr   )r4   r8   iterdirrN   NotADirectoryErrorr@   s     r   r[   zModuleFinder._contentsw  sj    t+++0-1$,,.-A$$T* ##D)) &'9: 0-/$$T*0s   &A A&%A&c                X    || j                   vr| j                   j                  |       y y r   )r5   r?   r@   s     r   _append_search_pathz ModuleFinder._append_search_path  s)    t((($$T* )r   c                R   | j                   D ]  }| j                  |      D ]  }|j                  dk(  st        |      D ]b  }|j                  x}r7| j
                  |   j                  |j                  j                  |             | j                  |j                         d   y )Nz.pth)
r5   r[   r\   _handle_pth_filealways_scan_forr9   r?   r"   joinpathr7   )r;   r"   item	directoryscans        r   r:   z#ModuleFinder._extend_from_pth_files  s    %%Dt,;;&(%5d%;	#,#<#<<4< 11$7>>y~~?V?VW[?\]//	? &< - &r   c              #    K   t        j                  |d      D ]c  \  }}}|D cg c]
  }|dk7  s	| c}|d d  |D ]?  }t         j                  j                  |      d   | j                  v s2t        ||       A e y c c}w w)NT)topdown__pycache__rI   )oswalkr"   splitextr2   r   )r;   r"   rootdirsfilesdirrelfiles          r   rn   zModuleFinder._filter_py_modules  s}     !#t!<D$&*Cdsc].BsdCDG 77##G,Q/43F3FFtW-- ! "=Cs   #B
BB9B.Bc                L   |j                         r|n|j                  }|j                         }| j                  D ]O  }t	        t
        t              5  |j                  |j                               }|j                  d   cd d d        c S  |j                  |k7  rV|j                  dz  j                         r9|j                  }|j                  |k7  r|j                  dz  j                         r9| j                  d|j                         |j                  S # 1 sw Y   xY w)Nr   rY   )r   rm   r>   r5   r   
ValueError
IndexErrorro   rr   r^   rD   r!   )r;   r"   parent_pathsearch_pathrel_paths        r   rM   zModuleFinder._top_module_name  s    "kkmd!))+,,K*j1&22;3F3F3HI~~a( 21 -   K/[5G5G-5W4_4_4a%,,K   K/[5G5G-5W4_4_4a;#5#56 21s   .DD#	r   )r5   zSequence[str | Path] | NonereturnNone)r"   r   r   r   )rC   intr"   r   r   r   )rR   z
str | PathrE   boolrF   r   r   z&tuple[str, Package | NamespacePackage])rS   r    r   zPackage | NamespacePackage)r"   zPath | list[Path]ru   z
set | Noner   zIterator[NamePartsAndPathType])rR   r   r   zlist[NamePartsAndPathType])r"   r   r   ztuple[str, Path])r"   r   r   r,   )r   r   )r"   r   r   zIterator[Path])r"   r   r   r    )r$   r%   r&   r'   r1   r(   rj   r2   r<   r7   rD   rW   rP   rk   r   rL   r[   r   r:   rn   rM   r)   r   r   r.   r.   X   s     :h!#6g5),-J)KN&K4&"+5$ #'#(Q-Q-  	Q-
 !Q- 
0Q-f./f  @A@A @A 
(	@AD
" *+@. r   r.   zG(?:__import__\([\"']pkg_resources[\"']\).declare_namespace\(__name__\))zP(?:__path__ = __import__\([\"']pkgutil[\"']\).extend_path\(__path__, __name__\))z^import[ \t]+\w+$c                    | j                  d      }t        t        j                  |      xs t        j                  |            S )Nutf8encoding)	read_textr   _re_pkgresourcessearch_re_pkgutil)rf   codes     r   r_   r_     s<      & 1D ''-I1C1CD1IJJr   c                    t        | d         S )Nr   )rq   )name_parts_and_paths    r   r   r     s    "1%&&r   c                  &    e Zd ZU ded<   dZded<   y)_SPr   r"   ri   r    r   N)r$   r%   r&   r(   r   r)   r   r   r   r     s    
JOSr   r   c                r   g }	 | j                  d      }|j                         j                  dd      j	                  d      D ]  }|j                         }t
        j                  |      rR| j                  |t        d      d  j                          dz  }t        t              5  t        |      cd d d        c S |s}|j                  d	      rt        j                  j!                  |      s|j#                  t%        t'        |                    |S # t        $ r |cY S w xY w# 1 sw Y   vxY w)
Nr   r   ;
Fkeependsimportr/   #)r   UnicodeDecodeErrorstripreplace
splitlines_re_import_liner   rm   rq   lstripr   r   _handle_editable_module
startswithr   r"   r^   r?   r   r   )r"   directoriestextlineeditable_modules        r   r   r     s     K ~~v~. 

$$S$/::E:Jzz|  &"kktCMN/C/J/J/L.MS,QQO67.? 87,1Es4:/ K    87s   D )D-D*)D*-D6	c           	     j   t        | j                  g t        t              r	 | j	                  d      j                         j                  d      }t        |d   j                  d      d         }|j                  j                  d      r t        |j                  j                        gS t        |      gS t        | j                  t              r=t        j                   | j	                               }|j"                  D ]
  }t%        |t        j&                        r|j(                  d	   }n(t%        |t        j*                        r|j,                  }nVt%        |t        j.                        sq|j0                  d
k(  st%        |j2                  t        j4                        s|j2                  j6                  D cg c]E  }t%        |t        j8                        st        t        |j2                        j                        G c}c S  t        | j                  t:              rYt        j                   | j	                               }|j"                  D ]&  }t%        |t        j<                        st%        |j2                  t        j>                        sDt%        |j2                  j@                  t        j.                        ss|j2                  j@                  j0                  dk(  st%        |j2                  jB                  d   t        j8                        st        |j2                  jB                  d   j2                  d      }tE        |jG                               j                  }	t        ||	      gc S  t        |       # t        $ r}t        |       |d }~ww xY wc c}w )Nr   r   Fr   rh   '   r<   r   MAPPINGinstallrI   src)r   )$r   r!   _editable_editables_patterns$_editable_scikit_build_core_patternsr   r   r   rN   r   r   rO   r   r   rm   _editable_setuptools_patternsastparsebodyrK   Assigntargets	AnnAssigntargetNameidvalueDictvaluesConstant_editable_meson_python_patternsExprCallfuncargsnextr   )
r"   editable_lineserrornew_pathparsed_modulenoder   cst
build_pathpkg_names
             r   r   r     s   dii!g#?!gBf!gh
	@!^^V^<BBDOOY^O_N r*005a89==##J/../00Hdii!>? 		$.."23!&&D$

+aD#--0&#((+		Y0F:VZV`V`bebjbjKk?Czz?P?Pr?PT^_bdgdpdpTqDO223?Prr ' dii!@A 		$.."23!&&D4*tzz3884tzz9JJOO&&)3tzzq13<<@!$**//!"4":":EB

 2 2 45::JABB ' 't
,,K ! 	@.t4%?	@& ss#   0N N0$*N0	N-N((N-)r   r    r   zSequence[Pattern]r   r   )rf   r   r   r   )r   NamePartsAndPathTyper   r   )r"   r   r   z	list[_SP])2
__future__r   r   r   rer6   collectionsr   
contextlibr   dataclassesr   	itertoolsr   pathlibr   typingr	   r
   _griffe.exceptionsr   _griffe.loggerr   collections.abcr   r   r   _griffe.modelsr   compiler   r   r   r   tupler    NamePartsTyper   r   r   r+   r.   r   r   r   r_   r   r   r   r   )pats   0r   <module>r      s  " # 
 	 	 
 #  !   * ; !2% <ss;rC

3;rs <W X<WSC<W X C['\C[C

3C['\ $>]"^>]s2::c?>]"^ c3h 0]D01  =>   " * * *J  J Z
 2::hi bjjlm"**12
K
'   
<--c  t X'\"^s   #E-E2!E7 E<