
    wg>                       d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
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 dd
lmZmZ ddlmZmZmZmZ ddlmZm Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' erddl(m)Z) ddl*m+Z+  ed      Z, edd      Z- G d d      Z.	 d/	 	 	 	 	 d0dZ/	 d/	 	 	 	 	 d1dZ0d2dZ1d3dZ2d4dZ3	 	 d5	 	 	 	 	 	 	 d6dZ4d7dZ5d8d Z6	 	 	 	 	 	 	 	 d9d!Z7	 	 d5	 	 	 	 	 	 	 d:d"Z8	 	 d5	 	 	 	 	 	 	 d;d#Z9dddd$	 	 	 	 	 d<d%Z:d=d&Z;d>d'Z<d?d(Z=	 d/	 	 	 	 	 d@d)Z>	 dA	 	 	 	 	 dBd*Z? G d+ d,      Z@ G d- d.ee,e-f         ZAy)Cai  Utility functions to expand configuration directives or special values
(such glob patterns).

We can split the process of interpreting configuration files into 2 steps:

1. The parsing the file contents from strings to value objects
   that can be understand by Python (for example a string with a comma
   separated list of keywords into an actual Python list of strings).

2. The expansion (or post-processing) of these values according to the
   semantics ``setuptools`` assign to them (for example a configuration field
   with the ``file:`` directive should be expanded from a list of file paths to
   a single string with the contents of those files concatenated)

This module focus on the second step, and therefore allow sharing the expansion
functions among several configuration file formats.

**PRIVATE MODULE**: API reserved for setuptools internal usage only.
    )annotationsN)IterableIteratorMapping)ConfigParser)iglob)
ModuleSpecall_suffixes)chain)Path)
ModuleTypeTracebackType)TYPE_CHECKINGAnyCallableTypeVar   )StrPath	same_path)find_package_path)SetuptoolsWarning)DistutilsOptionError)Self)Distribution_K_V_coT)	covariantc                  (    e Zd ZdZddZddZddZy)	StaticModulez>Proxy to a module object that avoids executing arbitrary code.c                    t        j                  t        j                  |j                        j                               }t        |       j                  t                      | `	y N)
astparsepathlibr   origin
read_bytesvarsupdatelocalsself)r*   namespecmodules       ]/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/setuptools/config/expand.py__init__zStaticModule.__init__7   s?    7<<4??ABT
&(#I    c              #  <  K   | j                   j                  D ]|  t        t        j                        rfdj
                  D        E d {    ;t        t        j                        sVj                  scj                  j                  f ~ y 7 Jw)Nc              3  :   K   | ]  }|j                   f  y wr!   )value).0target	statements     r.   	<genexpr>z1StaticModule._find_assignments.<locals>.<genexpr>?   s     V&VY__5Vs   )	r-   body
isinstancer"   Assigntargets	AnnAssignr3   r5   )r*   r6   s    @r.   _find_assignmentszStaticModule._find_assignments<   sn     )) 	:I)SZZ0VIDUDUVVVIs}}5)// ''99		:Vs   ABBB0B=Bc                    	 t        fd| j                         D              S # t        $ r }t        | j                   d       |d}~ww xY w)zHAttempt to load an attribute "statically", via :func:`ast.literal_eval`.c              3     K   | ]E  \  }}t        |t        j                        r&|j                  k(  rt        j                  |       G y wr!   )r9   r"   Nameidliteral_eval)r4   r5   r3   attrs      r.   r7   z+StaticModule.__getattr__.<locals>.<genexpr>F   sA      !FEfchh/FII4E   's   AAz has no attribute N)nextr=   	ExceptionAttributeErrorr+   )r*   rC   es    ` r.   __getattr__zStaticModule.__getattr__C   s]    	P %)%;%;%=  
  	P DII;.@!GHaO	Ps   !% 	AA		AN)r+   strr,   r	   returnNone)rJ   z!Iterator[tuple[ast.AST, ast.AST]])rC   rI   )__name__
__module____qualname____doc__r/   r=   rH    r0   r.   r   r   4   s    H
:	Pr0   r   c           
        h d}g }xs t        j                         | D ]  t        fd|D              rlt         j                  j	                  t         j                  j                              }|j                  t        fdt        |d      D                     t         j                  j                        j                  t         j                  d      }|j                  |        |S )a  Expand the list of glob patterns, but preserving relative paths.

    :param list[str] patterns: List of glob patterns
    :param str root_dir: Path to which globs should be relative
                         (current directory by default)
    :rtype: list
    >   *?[]{}c              3  &   K   | ]  }|v  
 y wr!   rP   )r4   charr3   s     r.   r7   z glob_relative.<locals>.<genexpr>^   s     9tu}9s   c              3     K   | ]B  }t         j                  j                  |      j                  t         j                  d        D yw)/N)ospathrelpathreplacesepr4   r]   root_dirs     r.   r7   z glob_relative.<locals>.<genexpr>b   s9       GGOOD(3;;BFFCHs   AAT)	recursiver[   )r\   getcwdanyr]   abspathjoinextendsortedr   r^   r_   r`   append)patternsrb   glob_charactersexpanded_values	glob_pathr]   r3   s    `    @r.   glob_relativero   O   s     5OO&299;H )999Xu(EFI""  %i4 @  77??5(3;;BFFCHD""4()" r0   c                    ddl m} t        j                  j	                  xs t        j
                               fd ||       D        }dj                  fdt        |      D              S )zReturn the content of the files concatenated using ``
`` as str

    This function is sandboxed and won't reach anything outside ``root_dir``

    (By default ``root_dir`` is the current directory).
    r   )always_iterablec              3  ^   K   | ]$  }t         j                  j                  |       & y wr!   r\   r]   rg   ra   s     r.   r7   zread_files.<locals>.<genexpr>|   s      V4"'',,x.Vs   *-
c              3  L   K   | ]  }t        |      rt        |        y wr!   )_assert_local
_read_filera   s     r.   r7   zread_files.<locals>.<genexpr>}   s(      x( 	4s   !$)more_itertoolsrq   r\   r]   rf   rd   rg   _filter_existing_files)	filepathsrb   rq   
_filepathss    `  r.   
read_filesr|   p   sW     /wwx6299;7HV?9;UVJ99 *:6  r0   c              #     K   | D ]?  }t         j                  j                  |      r| 't        j                  d|d       A y w)NzFile z cannot be found)r\   r]   isfiler   emit)rz   r]   s     r.   ry   ry      sD      E77>>$J""U4(2B#CD	Es   AAc                h    t        | d      5 }|j                         cd d d        S # 1 sw Y   y xY w)Nzutf-8)encoding)openread)filepathfs     r.   rw   rw      s,    	h	) Qvvx  s   (1c                    t        t        j                  j                  |            t        t        j                  j                  |             j                  vrd| d|d}t        |      y)NzCannot access z (or anything outside )T)r   r\   r]   rf   parentsr   )r   rb   msgs      r.   rv   rv      sW    BGGOOH%&d277??83L.M.U.UUxl*@AN"3''r0   c                l   |xs t        j                         }| j                         j                  d      }|j	                         }dj                  |      }|xs d}t        |||      }t        ||      }	 t        t        ||      |      S # t        $ r t        ||      }t        ||      cY S w xY w)a  Reads the value of an attribute from a module.

    This function will try to read the attributed statically first
    (via :func:`ast.literal_eval`), and only evaluate the module if it fails.

    Examples:
        read_attr("package.attr")
        read_attr("package.module.attr")

    :param str attr_desc: Dot-separated string describing how to reach the
        attribute (see examples above)
    :param dict[str, str] package_dir: Mapping of package names to their
        location in disk (represented by paths relative to ``root_dir``).
    :param str root_dir: Path to directory containing all the packages in
        ``package_dir`` (current directory by default).
    :rtype: str
    .r/   )r\   rd   stripsplitpoprg   _find_module
_find_specgetattrr   rE   
_load_spec)		attr_descpackage_dirrb   
attrs_path	attr_namemodule_namer]   r,   r-   s	            r.   	read_attrr      s    , &299;H"((-J I((:&K+K[(;Dk4(D*|K6	BB *D+.vy))*s   9B !B32B3c                    t         j                  j                  | |      }|xs t         j                  j                  |       }|t	        |       |S r!   )	importlibutilspec_from_file_location	find_specModuleNotFoundError)r   module_pathr,   s      r.   r   r      sF    >>11+{KD89>>++K8D|!+..Kr0   c                    t        | d|      }|t        j                  v rt        j                  |   S t        j                  j                  |       }|t        j                  |<   | j                  J | j                  j                  |       |S )NrL   )r   sysmodulesr   r   module_from_specloaderexec_module)r,   r   r+   r-   s       r.   r   r      st    4[1Ds{{{{4  ^^,,T2FCKK;;"""KKF#Mr0   c                    t        | |xs i |      t        j                  fdt               D              }t	        d |D        d      S )a  Find the path to the module named ``module_name``,
    considering the ``package_dir`` in the build configuration and ``root_dir``.

    >>> tmp = getfixture('tmpdir')
    >>> _ = tmp.ensure("a/b/c.py")
    >>> _ = tmp.ensure("a/b/d/__init__.py")
    >>> r = lambda x: x.replace(str(tmp), "tmp").replace(os.sep, "/")
    >>> r(_find_module("a.b.c", None, tmp))
    'tmp/a/b/c.py'
    >>> r(_find_module("f.g.h", {"": "1", "f": "2", "f.g": "3", "f.g.h": "a/b/d"}, tmp))
    'tmp/a/b/d/__init__.py'
    c              3  p   K   | ]-  } | t         j                  j                  d |       f / yw)r/   Nrs   )r4   ext
path_starts     r.   r7   z_find_module.<locals>.<genexpr>   s=      % <u	rww||J(3%8HIJ%s   36c              3  `   K   | ]&  }t         j                  j                  |      s#| ( y wr!   )r\   r]   r~   )r4   xs     r.   r7   z_find_module.<locals>.<genexpr>   s     <q"''..*;<s   $..N)r   r   from_iterabler
   rD   )r   r   rb   
candidatesr   s       @r.   r   r      sL     #;0Ar8LJ$$ %>% J <J<dCCr0   c                    |xs t        j                         }| j                  d      }| |dz   d }| d| }t        |||      }t	        t        ||      |      }t        ||      S )z@Given a qualified class name, return the associated class objectr      N)r\   rd   rfindr   r   r   r   )qualified_class_namer   rb   idx
class_namepkg_namer]   r-   s           r.   resolve_classr      sn     &299;H

$
$S
)C%cAgi0J#DS)H+x8D
8T2H=F6:&&r0   c           
     l    | j                         D ci c]  \  }}|t        |||       c}}S c c}}w )zGiven a dictionary mapping command names to strings for qualified class
    names, apply :func:`resolve_class` to the dict values.
    )itemsr   )valuesr   rb   kvs        r.   cmdclassr      s2     DJ<<>R41aA}QX66RRRs   0)
namespacesfill_package_dirrb   c                P   ddl m}m} ddlm} | sddlm} nddlm} |xs t        j                  }|j                  ddg      }g }	|i n|}t         | ||                  t              dk(  r+t        fd	d|fD              r|j                  d
d          D ]  }
t        ||
      } |j                  |fi |}|	j!                  |       |s6|j#                  d
      |
k(  rKt        j$                  j'                  ||      rl|j)                   |||
              |	S )a   Works similarly to :func:`setuptools.find_packages`, but with all
    arguments given as keyword arguments. Moreover, ``where`` can be given
    as a list (the results will be simply concatenated).

    When the additional keyword argument ``namespaces`` is ``True``, it will
    behave like :func:`setuptools.find_namespace_packages`` (i.e. include
    implicit namespaces as per :pep:`420`).

    The ``where`` argument will be considered relative to ``root_dir`` (or the current
    working directory when ``root_dir`` is not given).

    If the ``fill_package_dir`` argument is passed, this function will consider it as a
    similar data structure to the ``package_dir`` configuration parameter add fill-in
    any missing package location.

    :rtype: list
    r   )rq   unique_everseen)construct_package_dir)PackageFinder)PEP420PackageFinderwherer   r   c              3  >   K   | ]  }t        d    |         yw)r   N)
_same_path)r4   r   searchs     r.   r7   z find_packages.<locals>.<genexpr>.  s     VJvay!$< <Vs    )rx   rq   r   setuptools.discoveryr   r   r   r\   curdirr   listlenall
setdefault
_nest_pathfindrh   getr]   samefiler(   )r   r   rb   kwargsrq   r   r   r   r   packagesr]   package_pathpkgsr   s                @r.   find_packagesr     s   0 @: 6M$299HJJw&EH-5r;K//%"89:F
6{aCVsHoVV##Bq	2 G!(D1!}!!,9&9  $,0@0@x0X##$9$$EFG Or0   c                    |dv r| nt         j                  j                  | |      }t         j                  j                  |      S )N>   r   r   )r\   r]   rg   normpath)parentr]   s     r.   r   r   =  s4    Y&6BGGLL,FD77D!!r0   c                    t        |       r |        n| }t        |t              r|S t        |d      rdj	                  t        t        |            S d|z  S )z`When getting the version directly from an attribute,
    it should be normalised to string.
    __iter__r   z%s)callabler9   rI   hasattrrg   map)r3   _values     r.   versionr   B  sL     !UWUF&#vz"xxC())&=r0   c                6    d| v r| j                  d      | d<   | S )NrR   r   )r   )package_datas    r.   canonic_package_datar   O  s%    
l'++C0Rr0   c           	         t        | t              r| S | j                         D cg c]  \  }}|t        ||      f c}}S c c}}w )zFor compatibility with ``setup.py``, ``data_files`` should be a list
    of pairs instead of a dict.

    This function also expands glob patterns.
    )r9   r   r   ro   )
data_filesrb   destrk   s       r.   canonic_data_filesr   U  sO     *d# )..0D( 
}Xx01  s   Ac           	        t        dd      }t        |_        |j                  | |       |j	                         D ci c]  \  }}|t        |j	                               ! }}}|j                  |j                  d       |S c c}}w )a?  Given the contents of entry-points file,
    process it into a 2-level dictionary (``dict[str, dict[str, str]]``).
    The first level keys are entry-point groups, the second level keys are
    entry-point names, and the second level values are references to objects
    (that correspond to the entry-point value).
    N)=)default_section
delimiters)r   rI   optionxformread_stringr   dictr   r   )texttext_sourceparserr   r   groupss         r.   entry_pointsr   f  sr     $6BFF
t[)-3\\^<TQaaggi <F<
JJv%%t,M =s   $Bc                  T    e Zd ZdZd	dZd Zd
dZ	 	 	 	 	 	 ddZddZe	dd       Z
y)EnsurePackagesDiscovereda  Some expand functions require all the packages to already be discovered before
    they run, e.g. :func:`read_attr`, :func:`resolve_class`, :func:`cmdclass`.

    Therefore in some cases we will need to run autodiscovery during the evaluation of
    the configuration. However, it is better to postpone calling package discovery as
    much as possible, because some parameters can influence it (e.g. ``package_dir``),
    and those might not have been processed yet.
    c                     || _         d| _        y )NF)_dist_called)r*   distributions     r.   r/   z!EnsurePackagesDiscovered.__init__  s    !
r0   c                d    | j                   s$d| _         | j                  j                  d       yy)zBTrigger the automatic package discovery, if it is still necessary.TF)r+   N)r   r   set_defaultsr*   s    r.   __call__z!EnsurePackagesDiscovered.__call__  s*    ||DLJJ###/ r0   c                    | S r!   rP   r   s    r.   	__enter__z"EnsurePackagesDiscovered.__enter__  s    r0   c                f    | j                   r%| j                  j                  j                          y y r!   )r   r   r   analyse_name)r*   exc_type	exc_value	tracebacks       r.   __exit__z!EnsurePackagesDiscovered.__exit__  s&     <<JJ##002 r0   c                H     |         | j                   j                  }|i S |S r!   )r   r   )r*   pkg_dirs     r.   _get_package_dirz)EnsurePackagesDiscovered._get_package_dir  s%    **((_r1'1r0   c                ,    t        | j                        S )zCProxy to ``package_dir`` that may trigger auto-discovery when used.)LazyMappingProxyr  r   s    r.   r   z$EnsurePackagesDiscovered.package_dir  s       5 566r0   N)r   r   rJ   rK   )rJ   r   )r  ztype[BaseException] | Noner  zBaseException | Noner  zTracebackType | None)rJ   zMapping[str, str])rL   rM   rN   rO   r/   r   r   r  r  propertyr   rP   r0   r.   r   r   x  sP    03,3 (3 (	32
 7 7r0   r   c                  8    e Zd ZdZddZd	dZd
dZddZddZy)r
  a  Mapping proxy that delays resolving the target object, until really needed.

    >>> def obtain_mapping():
    ...     print("Running expensive function!")
    ...     return {"key": "value", "other key": "other value"}
    >>> mapping = LazyMappingProxy(obtain_mapping)
    >>> mapping["key"]
    Running expensive function!
    'value'
    >>> mapping["other key"]
    'other value'
    c                     || _         d | _        y r!   )_obtainr   )r*   obtain_mapping_values     r.   r/   zLazyMappingProxy.__init__  s    +15r0   c                \    | j                   | j                         | _         | j                   S r!   )r   r  r   s    r.   _targetzLazyMappingProxy._target  s#    ;;,,.DK{{r0   c                (    | j                         |   S r!   )r  )r*   keys     r.   __getitem__zLazyMappingProxy.__getitem__  s    ||~c""r0   c                4    t        | j                               S r!   )r   r  r   s    r.   __len__zLazyMappingProxy.__len__  s    4<<>""r0   c                4    t        | j                               S r!   )iterr  r   s    r.   r   zLazyMappingProxy.__iter__  s    DLLN##r0   N)r  z Callable[[], Mapping[_K, _V_co]]rJ   rK   )rJ   zMapping[_K, _V_co])r  r   rJ   r   )rJ   int)rJ   zIterator[_K])	rL   rM   rN   rO   r/   r  r  r  r   rP   r0   r.   r
  r
    s     6
##$r0   r
  r!   )rk   zIterable[str]rb   StrPath | NonerJ   	list[str])rz   zStrPath | Iterable[StrPath]rb   r  rJ   rI   )rz   zIterable[StrPath]rJ   zIterator[StrPath])r   zbytes | StrPathrJ   rI   )r   r   rb   rI   )NN)r   rI   r   Mapping[str, str] | Nonerb   r  rJ   r   )r   rI   r   r  rJ   r	   )r,   r	   r   rI   rJ   r   )r   rI   r   r  rb   r   rJ   z
str | None)r   rI   r   r  rb   r  rJ   r   )r   zdict[str, str]r   r  rb   r  rJ   zdict[str, Callable])r   zdict[str, str] | Nonerb   r  rJ   r  )r   r   r]   r   rJ   rI   )r3   z$Callable | Iterable[str | int] | strrJ   rI   )r   r   rJ   r   )r   zlist | dictrb   r  rJ   zlist[tuple[str, list[str]]])zentry-points)r   rI   r   rI   rJ   zdict[str, dict[str, str]])BrO   
__future__r   r"   r   r\   r$   r   collections.abcr   r   r   configparserr   globr   importlib.machineryr	   r
   	itertoolsr   r   typesr   r   typingr   r   r   r   _pathr   r   r   	discoveryr   warningsr   distutils.errorsr   typing_extensionsr   setuptools.distr   r   r   r   ro   r|   ry   rw   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r
  rP   r0   r.   <module>r+     s8  ( # 
  	  
 7 7 %  8   + 8 8 4 ) ( 1&,T]4(P P8 9='5D HL*6D(E
 -1##*#*)#* #* 		#*LDD#;DGNDD2 -1#'')' ' 	'$ -1#SS)S S 	S .2#	4 ,4 	4 4n"

 9='5 $ #1
$(7 (7V$wr5y) $r0   