o
    Ee@                     @   s  d 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 ddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZ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$ er|ddl%m&Z& ddl'm(Z( ddl)m*Z* ej+Z,ee-ej.f Z/edZ0edddZ1G dd dZ2	dRdee- dee/ dee- fddZ3dRdee-e4ee/ f de-fddZ5dee/ dee/ fdd Z6d!ee4e/f de-fd"d#Z7d!e/de-fd$d%Z8		dSd&e-d'eee-e-f  dee/ fd(d)Z9d*e-d+ee/ defd,d-Z:d.ed*e-defd/d0Z;d*e-d'eee-e-f  de/dee/ee- e-f fd1d2Z<		dSd3e-d'eee-e-f  dee/ defd4d5Z=		dSd6ee-e-f d'eee-e-f  dee/ dee-ef fd7d8Z>dddd9d:eee-e-f  dee/ dee- fd;d<Z?d=e/d>e/de-fd?d@Z@dAeeeee-eAf  e-f de-fdBdCZBdDeCdeCfdEdFZD	dRdGeeEeCf dee/ deee-ee- f  fdHdIZFdTdKe-dee-eCf fdLdMZGG dNdO dOZHG dPdQ dQee0e1f ZIdS )Uai  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.
    N)iglob)ConfigParser)
ModuleSpec)chain)TYPE_CHECKINGCallableDictIterableIteratorListMappingOptionalTupleTypeVarUnioncast)Path)
ModuleType)DistutilsOptionError   )	same_path)SetuptoolsWarning)Distribution)ConfigDiscovery)DistributionMetadata_K_VT)	covariantc                   @   sH   e Zd ZdZdedefddZdeee	j
e	j
f  fddZd	d
 ZdS )StaticModulez>Proxy to a module object that avoids executing arbitrary code.namespecc                 C   s.   t t|j }t| t  | `	d S N)
astparsepathlibr   origin
read_bytesvarsupdatelocalsself)r*   r   r    module r,   Y/var/www/bmteknikk.ddns.net/venv/lib/python3.10/site-packages/setuptools/config/expand.py__init__A   s   zStaticModule.__init__returnc                 #   s\    | j jD ]& t tjr fdd jD E d H  qt tjr+ jr+ j jfV  qd S )Nc                 3   s    | ]}| j fV  qd S r!   value).0target	statementr,   r-   	<genexpr>I   s    z1StaticModule._find_assignments.<locals>.<genexpr>)	r+   body
isinstancer"   Assigntargets	AnnAssignr1   r3   r*   r,   r4   r-   _find_assignmentsF   s   zStaticModule._find_assignmentsc              
      sL   zt  fdd|  D W S  ty% } zt| j d  |d}~ww )zHAttempt to load an attribute "statically", via :func:`ast.literal_eval`.c                 3   s4    | ]\}}t |tjr|j krt|V  qd S r!   )r8   r"   Nameidliteral_eval)r2   r3   r1   attrr,   r-   r6   P   s    

z+StaticModule.__getattr__.<locals>.<genexpr>z has no attribute N)nextr=   	ExceptionAttributeErrorr   )r*   rB   er,   rA   r-   __getattr__M   s   
zStaticModule.__getattr__N)__name__
__module____qualname____doc__strr   r.   r
   r   r"   ASTr=   rG   r,   r,   r,   r-   r   >   s
    r   patternsroot_dirr/   c              	      s   h d}g } pt   | D ]<tfdd|D r9t jt j }|t fddt|ddD  qt j	 
t jd}|| q|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   s    | ]}| v V  qd S r!   r,   )r2   charr0   r,   r-   r6   h   s    z glob_relative.<locals>.<genexpr>c                 3   s(    | ]}t j| t jd V  qdS )/N)ospathrelpathreplacesepr2   rY   rO   r,   r-   r6   l   s
    
T)	recursiverW   )rX   getcwdanyrY   abspathjoinextendsortedr   rZ   r[   r\   append)rN   rO   glob_charactersexpanded_values	glob_pathrY   r,   )rO   r1   r-   glob_relativeY   s   

	rj   	filepathsc                    sR   ddl m} tj pt   fdd|| D }d fd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   s    | ]
}t j |V  qd S r!   )rX   rY   rc   r]   r^   r,   r-   r6      s    zread_files.<locals>.<genexpr>
c                 3   s"    | ]}t | rt|V  qd S r!   )_assert_local
_read_filer]   r^   r,   r-   r6      s    
) setuptools.extern.more_itertoolsrl   rX   rY   rb   r`   rc   _filter_existing_files)rk   rO   rl   
_filepathsr,   r^   r-   
read_filesz   s   rs   c                 c   s6    | D ]}t j|r|V  qtd|d qd S )NzFile z cannot be found)rX   rY   isfiler   emit)rk   rY   r,   r,   r-   rq      s   rq   filepathc                 C   s8   t | dd}| W  d    S 1 sw   Y  d S )Nzutf-8)encoding)openread)rv   fr,   r,   r-   ro      s   $ro   c                 C   s@   t tj|t tj| jvrd| d|d}t|dS )NzCannot access z (or anything outside )T)r   rX   rY   rb   parentsr   )rv   rO   msgr,   r,   r-   rn      s   "rn   	attr_descpackage_dirc           
      C   s   |pt  }|  d}| }d|}|pd}t|||\}}}t||}z	tt	|||W S  t
yD   t||}	t|	| Y S 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.   )rX   r`   stripsplitpoprc   _find_module
_find_specgetattrr   rD   
_load_spec)
r~   r   rO   
attrs_path	attr_namemodule_name_parent_pathrY   r    r+   r,   r,   r-   	read_attr   s   


r   r   module_pathc                 C   s2   t j| |}|pt j| }|d u rt| |S r!   )	importlibutilspec_from_file_location	find_specModuleNotFoundError)r   r   r    r,   r,   r-   r      s
   r   r    c                 C   sF   t | d|}|tjv rtj| S tj| }|tj|< | j| |S )NrH   )r   sysmodulesr   r   module_from_specloaderexec_module)r    r   r   r+   r,   r,   r-   r      s   


r   c                 C   s   |}|  d}|rK|d |v r>||d  }|dd}t|dkr/tj||d }|d }n|}d|g|dd } nd|v rKtj||d }tjj|g|  dR  }t| dtj|dft| d	}	td
d |	D d}
||
| fS )a0  Given a module (that could normally be imported by ``module_name``
    after the build is complete), find the path to the parent directory where
    it is contained and the canonical name that could be used to import it
    considering the ``package_dir`` in the build configuration and ``root_dir``
    r   r   rW      N z.pyz__init__.pyz.*c                 s   s     | ]}t j|r|V  qd S r!   )rX   rY   rt   r2   xr,   r,   r-   r6          z_find_module.<locals>.<genexpr>)	r   rsplitlenrX   rY   rc   r   r   rC   )r   r   rO   parent_pathmodule_partscustom_pathpartsparent_module
path_start
candidatesr   r,   r,   r-   r      s(   


r   qualified_class_namec           
      C   s^   |pt  }| d}| |d d }| d| }t|||\}}}tt|||}	t|	|S )z@Given a qualified class name, return the associated class objectr   r   N)rX   r`   rfindr   r   r   r   )
r   r   rO   idx
class_namepkg_namer   rY   r   r+   r,   r,   r-   resolve_class   s   

r   valuesc                    s    fdd|   D S )zGiven a dictionary mapping command names to strings for qualified class
    names, apply :func:`resolve_class` to the dict values.
    c                    s   i | ]\}}|t | qS r,   )r   r2   kvr   rO   r,   r-   
<dictcomp>      zcmdclass.<locals>.<dictcomp>)items)r   r   rO   r,   r   r-   cmdclass  s   r   )
namespacesfill_package_dirrO   r   c                    s  ddl m} ddlm}m} | rddl m} nddl m} |p!tj}|	ddg}g }	|du r1i n|}t
||| t d	krVt fd
dd|fD rV|d d   D ]-}
t||
}|j|fi |}|	| |r|d|
kstj||s||||
 qX|	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   )construct_package_dir)unique_everseenrl   )PEP420PackageFinder)PackageFinderwherer   Nr   c                 3   s     | ]}t  d  | V  qdS )r   N)
_same_pathr   searchr,   r-   r6   >  r   z find_packages.<locals>.<genexpr>r   )setuptools.discoveryr   rp   r   rl   r   r   rX   curdirr   listr   all
setdefault
_nest_pathfindrd   getrY   samefiler(   )r   r   rO   kwargsr   r   rl   r   r   packagesrY   package_pathpkgsr,   r   r-   find_packages  s*   
&

r   parentrY   c                 C   s&   |dv r| nt j| |}t j|S )N>   r   r   )rX   rY   rc   normpath)r   rY   r,   r,   r-   r   M  s   r   r1   c                 C   sX   t | r|  } tttttf  | } t| ts*t| dr&dt	t| } | S d|  } | S )z`When getting the version directly from an attribute,
    it should be normalised to string.
    __iter__r   z%s)
callabler   r	   r   rL   intr8   hasattrrc   mapr0   r,   r,   r-   versionR  s   

r   package_datac                 C   s   d| v r|  d| d< | S )NrP   r   )r   )r   r,   r,   r-   canonic_package_datad  s   r   
data_filesc                    s$   t | tr| S  fdd|  D S )zFor compatibility with ``setup.py``, ``data_files`` should be a list
    of pairs instead of a dict.

    This function also expands glob patterns.
    c                    s   g | ]\}}|t | fqS r,   )rj   )r2   destrN   r^   r,   r-   
<listcomp>u  s    z&canonic_data_files.<locals>.<listcomp>)r8   r   r   )r   rO   r,   r^   r-   canonic_data_filesj  s
   

r   entry-pointstextc                 C   sB   t ddd}t|_|| | dd | D }||jd |S )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
delimitersc                 S   s   i | ]\}}|t | qS r,   )dictr   r   r,   r,   r-   r     r   z entry_points.<locals>.<dictcomp>)r   rL   optionxformread_stringr   r   r   )r   text_sourceparsergroupsr,   r,   r-   entry_points{  s   r   c                   @   sb   e Zd ZdZdddZdd Zdd	 Zd
d Zdee	e	f fddZ
edee	e	f fddZdS )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.
    distributionr   c                 C   s   || _ d| _d S )NF)_dist_called)r*   r   r,   r,   r-   r.        
z!EnsurePackagesDiscovered.__init__c                 C   s"   | j sd| _ | jjdd dS dS )zBTrigger the automatic package discovery, if it is still necessary.TF)r   N)r   r   set_defaultsr<   r,   r,   r-   __call__  s   z!EnsurePackagesDiscovered.__call__c                 C   s   | S r!   r,   r<   r,   r,   r-   	__enter__  s   z"EnsurePackagesDiscovered.__enter__c                 C   s   | j r| jj  d S d S r!   )r   r   r   analyse_name)r*   	_exc_type
_exc_value
_tracebackr,   r,   r-   __exit__  s   z!EnsurePackagesDiscovered.__exit__r/   c                 C   s   |   | j j}|d u ri S |S r!   )r   r   )r*   pkg_dirr,   r,   r-   _get_package_dir  s   z)EnsurePackagesDiscovered._get_package_dirc                 C   s
   t | jS )zCProxy to ``package_dir`` that may trigger auto-discovery when used.)LazyMappingProxyr   r<   r,   r,   r-   r     s   
z$EnsurePackagesDiscovered.package_dirN)r   r   )rH   rI   rJ   rK   r.   r   r   r   r   rL   r   propertyr   r,   r,   r,   r-   r     s    
	r   c                   @   sv   e Zd ZdZdeg eeef f fddZdeeef fddZ	dedefd	d
Z
defddZdee fddZdS )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'
    obtain_mapping_valuec                 C   s   || _ d | _d S r!   )_obtain_value)r*   r   r,   r,   r-   r.     r   zLazyMappingProxy.__init__r/   c                 C   s   | j d u r
|  | _ | j S r!   )r   r   r<   r,   r,   r-   _target  s   

zLazyMappingProxy._targetkeyc                 C   s   |   | S r!   )r   )r*   r   r,   r,   r-   __getitem__     zLazyMappingProxy.__getitem__c                 C      t |  S r!   )r   r   r<   r,   r,   r-   __len__  r   zLazyMappingProxy.__len__c                 C   r   r!   )iterr   r<   r,   r,   r-   r     r   zLazyMappingProxy.__iter__N)rH   rI   rJ   rK   r   r   r   r   r.   r   r   r   r   r
   r   r,   r,   r,   r-   r     s    r   r!   )NN)r   )JrK   r"   r   rX   r$   r   globr   configparserr   importlib.machineryr   	itertoolsr   typingr   r   r   r	   r
   r   r   r   r   r   r   r   r   typesr   distutils.errorsr   _pathr   r   warningsr   setuptools.distr   r   r   distutils.distr   from_iterable
chain_iterrL   PathLike_Pathr   r   r   rj   bytesrs   rq   ro   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r,   r,   r,   r-   <module>   s    8
"!

&


$




5(

&