U
    _dRD                     @   sv  d Z ddlZddl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 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 erddlmZ eeejf Z e!e"Z#e e$dddZ%e$e e&dddZ'd+de ddddZ(d,e ed dddZ)d-e$ee  e&ed e$dddZ*G d d! d!Z+d"d# Z,ee&d$d%d&Z-G d'd( d(ej.Z/G d)d* d*eZ0dS ).ac  
Load setuptools configuration from ``pyproject.toml`` files.

**PRIVATE MODULE**: API reserved for setuptools internal usage only.

To read project metadata, consider using
``build.util.project_wheel_metadata`` (https://pypi.org/project/build/).
For simple scenarios, you can also try parsing the file directly
with the help of ``tomllib`` or ``tomli``.
    N)contextmanager)partial)TYPE_CHECKINGCallableDictMappingOptionalSetUnion   )	FileErrorOptionError)SetuptoolsWarning   )expand)_PREVIOUSLY_DEFINED_WouldIgnoreField)applyDistribution)filepathreturnc              
   C   s8   ddl m} t| d}||W  5 Q R  S Q R X d S )Nr   )tomlirb)Zsetuptools.externr   openload)r   r   file r   G/tmp/pip-install-m_ue_qtn/setuptools/setuptools/config/pyprojecttoml.py	load_file   s    r   )configr   r   c              
   C   s   ddl m} |jd}t|dr*|  z|| W S  |jk
r } zXd|j }|j	
ddkr|t| t|j d|j	 d	}t| d
| d W 5 d }~X Y nX d S )Nr   )_validate_pyprojectztrove-classifier_disable_downloadzconfiguration error: `projectzinvalid pyproject.toml config: .
) r!   ZFORMAT_FUNCTIONSgethasattrr"   validateZValidationErrorsummarynamestrip_loggerdebugdetails
ValueError)r    r   Z	validatorZtrove_classifierexr+   errorr   r   r   r*   %   s    

r*   Fr   )distr   r   c                 C   s   t |d|| }t| ||S )zeApply the configuration from a ``pyproject.toml`` file into an existing
    distribution object.
    T)read_configuration_apply)r4   r   ignore_option_errorsr    r   r   r   apply_configuration:   s    r8   T)r   r4   c           
   	   C   s  t j| } t j| s(td| dt| p2i }|di }|di }|di }|rd|sh|shi S d|kr|tjdd |rt	|d	d
d
k	r|
d|j n|
dd ||d< ||d< t| |d|id}t||  W 5 Q R X |rt j| }	t||	||S |S )a  Read given configuration file and returns options from it as a dict.

    :param str|unicode filepath: Path to configuration file in the ``pyproject.toml``
        format.

    :param bool expand: Whether to expand directives and other computed values
        (i.e. post-process the given configuration)

    :param bool ignore_option_errors: Whether to silently ignore
        options, values of which could not be resolved (e.g. due to exceptions
        in directives such as file:, attr:, etc.).
        If False exceptions are propagated as expected.

    :param Distribution|None: Distribution object to which the configuration refers.
        If not given a dummy object will be created and discarded after the
        configuration is read. This is used for auto-discovery of packages and in the
        case a dynamic configuration (e.g. ``attr`` or ``cmdclass``) is expanded.
        When ``expand=False`` this object is simply ignored.

    :rtype: dict
    zConfiguration file z does not exist.r$   tool
setuptools	distutilsz[tool.distutils])subjectinclude_package_dataNzinclude-package-dataT)r$   r9   )ospathabspathisfiler   r   r(   _ExperimentalConfigurationemitgetattr
setdefaultr=   _ignore_errorsr*   dirnameexpand_configuration)
r   r   r7   r4   ZasdictZproject_tableZ
tool_tableZsetuptools_tableZsubsetroot_dirr   r   r   r5   F   s.    
r5   )r    rI   r7   r4   r   c                 C   s   t | ||| S )a  Given a configuration with unresolved fields (e.g. dynamic, cmdclass, ...)
    find their final values.

    :param dict config: Dict containing the configuration for the distribution
    :param str root_dir: Top-level directory for the distribution/project
        (the same directory where ``pyproject.toml`` is place)
    :param bool ignore_option_errors: see :func:`read_configuration`
    :param Distribution|None: Distribution object to which the configuration refers.
        If not given a dummy object will be created and discarded after the
        configuration is read. Used in the case a dynamic configuration
        (e.g. ``attr`` or ``cmdclass``).

    :rtype: dict
    )_ConfigExpanderr   r    rI   r7   r4   r   r   r   rH      s    rH   c                   @   s`  e Zd Zd4eee eed dddZdddd	Zee	e
d
ddZd5ddZdd Zdd Zdd Zee	e	f dddZdee	e	f dddZde	dddZe	ee	e	f dd d!Zde	ee	e	f d"d#d$Zdee	e	f dd%d&Zdeee	e	f  d'd(d)Zdee	e	f eee	ef  d*d+d,Zdd-d.d/Zdd-d0d1Zdd-d2d3ZdS )6rJ   NFr   rK   c                 C   sp   || _ |pt | _|di | _| jdg | _|di di | _| jdi | _|| _	|| _
t | _d S )Nr$   dynamicr9   r:   )r    r>   getcwdrI   r(   project_cfgrL   setuptools_cfgdynamic_cfgr7   _distset_referenced_files)selfr    rI   r7   r4   r   r   r   __init__   s    z_ConfigExpander.__init__)r   c                 C   s0   ddl m} | j| jdd d}| jp.||S )Nr   r   r,   )Zsrc_rootr,   )setuptools.distr   rI   rN   r(   rQ   )rT   r   attrsr   r   r   _ensure_dist   s    z_ConfigExpander._ensure_dist)	containerfieldfnc              	   C   s2   ||kr.t | j ||| ||< W 5 Q R X d S N)rF   r7   )rT   rY   rZ   r[   r   r   r   _process_field   s    z_ConfigExpander._process_fieldpackage-datac                 C   s   | j |i }t|S r\   )rO   r(   _expandZcanonic_package_data)rT   rZ   package_datar   r   r   _canonic_package_data   s    z%_ConfigExpander._canonic_package_datac              	   C   sz   |    |   | d |  }t|| j| j}|*}|j}|   | | | 	|| W 5 Q R X |j
| j
 | jS )Nzexclude-package-data)_expand_packagesra   rX   _EnsurePackagesDiscoveredrN   rO   package_dir_expand_data_files_expand_cmdclass_expand_all_dynamicrS   updater    )rT   r4   ctxZensure_discoveredrd   r   r   r   r      s    

z_ConfigExpander.expandc              	   C   s   | j d}|d ks"t|ttfr&d S |d}t|tr~| j|d< | j di |d< t| j	 t
jf || j d< W 5 Q R X d S )NpackagesfindrI   package-dirZfill_package_dir)rO   r(   
isinstancelisttupledictrI   rE   rF   r7   r_   Zfind_packages)rT   rj   rk   r   r   r   rb      s    


z _ConfigExpander._expand_packagesc                 C   s$   t tj| jd}| | jd| d S )N)rI   z
data-files)r   r_   Zcanonic_data_filesrI   r]   rO   )rT   
data_filesr   r   r   re      s    z"_ConfigExpander._expand_data_files)rd   c                 C   s*   | j }ttj||d}| | jd| d S )N)rd   rI   cmdclass)rI   r   r_   rr   r]   rO   )rT   rd   rI   rr   r   r   r   rf      s    z _ConfigExpander._expand_cmdclass)r4   rd   c              	      s   d fddj D }|j p0i      d dd | D }j	| d S )N)versionreadmeentry-pointsscriptsgui-scriptsclassifiersdependenciesoptional-dependenciesc                    s$   i | ]}|kr|  |qS r   )_obtain.0rZ   r4   rd   rT   Zspecialr   r   
<dictcomp>   s    z7_ConfigExpander._expand_all_dynamic.<locals>.<dictcomp>)rs   rt   rx   ry   Zoptional_dependenciesc                 S   s   i | ]\}}|d k	r||qS r\   r   )r}   kvr   r   r   r     s       )
rL   rh   _obtain_entry_points_obtain_version_obtain_readme_obtain_classifiers_obtain_dependencies_obtain_optional_dependenciesitemsrN   )rT   r4   rd   Zobtained_dynamicZupdatesr   r~   r   rg      s    

z#_ConfigExpander._expand_all_dynamic)r4   rZ   c                 C   s2   t | |}|d kr.| js.d|d}t|d S )Nz#No configuration found for dynamic z.
Some dynamic fields need to be specified via `tool.setuptools.dynamic`
others must be specified via the equivalent attribute in `setup.py`.)r   r7   r   )rT   r4   rZ   previousmsgr   r   r   _ensure_previously_set  s
    
z&_ConfigExpander._ensure_previously_set)	specifierrd   c              
   C   s   ddl m} t| j~ | j}d|krV| j||d  t|d |W  5 Q R  S d|kr|t	|d ||W  5 Q R  S t
d| d|W 5 Q R X d S )Nr   )always_iterabler   attrz	invalid `z`: )Z setuptools.extern.more_itertoolsr   rF   r7   rI   rS   rh   r_   Z
read_filesZ	read_attrr1   )rT   r   	directiverd   r   rI   r   r   r   _expand_directive  s    z!_ConfigExpander._expand_directive)r4   rZ   rd   c                 C   s4   || j kr$| d| | j | |S | || d S )Nztool.setuptools.dynamic.)rP   r   r   )rT   r4   rZ   rd   r   r   r   r{     s    
z_ConfigExpander._obtainc                 C   s,   d| j kr(d| jkr(t| |d|S d S )Nrs   )rL   rP   r_   rs   r{   )rT   r4   rd   r   r   r   r   '  s    z_ConfigExpander._obtain_version)r4   r   c                 C   sL   d| j krd S | j}d|kr<| |di |d dddS | |d d S )Nrt   content-typez
text/x-rst)textr   )rL   rP   r{   r(   r   )rT   r4   rP   r   r   r   r   -  s    
z_ConfigExpander._obtain_readme)r4   rd   r   c                    sz   d}t fdd|D sd S |d|}|d kr8d S t|di ttd fdd}|dd	 |d
d  S )N)ru   rv   rw   c                 3   s   | ]}| j kV  qd S r\   )rL   r|   rT   r   r   	<genexpr>?  s     z7_ConfigExpander._obtain_entry_points.<locals>.<genexpr>ru   )rZ   groupc                    s6   |kr2 |}| jkr*tj| |d | | < d S )N)rZ   value)poprL   r   rC   )rZ   r   r   expandedgroupsrT   r   r   _set_scriptsI  s
    

z:_ConfigExpander._obtain_entry_points.<locals>._set_scriptsrv   console_scriptsrw   gui_scripts)anyr{   r_   entry_pointsstr)rT   r4   rd   fieldsr   r   r   r   r   r   ;  s    
	

z$_ConfigExpander._obtain_entry_points)r4   c                 C   s(   d| j kr$| |di }|r$| S d S )Nrx   )rL   r{   
splitlinesrT   r4   r   r   r   r   r   W  s
    
z#_ConfigExpander._obtain_classifiersc                 C   s(   d| j kr$| |di }|r$t|S d S )Nry   )rL   r{   _parse_requirements_listr   r   r   r   r   ^  s
    
z$_ConfigExpander._obtain_dependenciesc                    sV   d j krd S d jkrF jd }t|ts0t fdd| D S  |d d S )Nrz   c              	      s*   i | ]"\}}|t  d | |i qS )z.tool.setuptools.dynamic.optional-dependencies.)r   r   )r}   r   r   r   r   r   r   k  s    zA_ConfigExpander._obtain_optional_dependencies.<locals>.<dictcomp>)rL   rP   rm   rp   AssertionErrorr   r   )rT   r4   Zoptional_dependencies_mapr   r   r   r   e  s    




z-_ConfigExpander._obtain_optional_dependencies)NFN)r^   )__name__
__module____qualname__rp   r   _PathboolrU   rX   r   r   r]   ra   r   rb   re   r   rf   rg   r   r   r{   r   r   r   r   r   r   r   r   r   r   r   rJ      s>      
 

 
rJ   c                 C   s   dd |   D S )Nc                 S   s&   g | ]}|  r|  d s|qS )#)r-   
startswith)r}   liner   r   r   
<listcomp>z  s    z,_parse_requirements_list.<locals>.<listcomp>)r   )r   r   r   r   r   y  s    r   )r7   c              
   c   sZ   | sd V  d S z
d V  W n< t k
rT } ztd|jj d|  W 5 d }~X Y nX d S )Nzignored error: z - )	Exceptionr.   r/   	__class__r   )r7   r2   r   r   r   rF     s    
rF   c                       s>   e Zd Zdeed fddZ fddZ fddZ  ZS )	rc   r   )distributionrN   rO   c                    s   t  | || _|| _d S r\   )superrU   _project_cfg_setuptools_cfg)rT   r   rN   rO   r   r   r   rU     s    z"_EnsurePackagesDiscovered.__init__c                    s   | j | j }}|di }||jp&i  ||_|j  |jjdkrV| j	
d|j_|jdkrl|
d|_|jdkr|
d|_t  S )zWhen entering the context, the values of ``packages``, ``py_modules`` and
        ``package_dir`` that are missing in ``dist`` are copied from ``setuptools_cfg``.
        rl   Nr,   
py-modulesrj   )rQ   r   rE   rh   rd   set_defaultsZ_ignore_ext_modulesmetadatar,   r   r(   
py_modulesrj   r   	__enter__)rT   r4   cfgrd   r   r   r   r     s    


z#_EnsurePackagesDiscovered.__enter__c                    s4   | j d| jj | j d| jj t |||S )zWhen exiting the context, if values of ``packages``, ``py_modules`` and
        ``package_dir`` are missing in ``setuptools_cfg``, copy from ``dist``.
        rj   r   )r   rE   rQ   rj   r   r   __exit__)rT   exc_type	exc_value	tracebackr   r   r   r     s    z"_EnsurePackagesDiscovered.__exit__)r   r   r   rp   rU   r   r   __classcell__r   r   r   r   rc     s     rc   c                   @   s   e Zd ZdZdS )rB   z``{subject}` in `pyproject.toml` is still *experimental* and likely to change in future releases.N)r   r   r   Z_SUMMARYr   r   r   r   rB     s   rB   )F)TFN)NFN)1__doc__loggingr>   
contextlibr   	functoolsr   typingr   r   r   r   r   r	   r
   errorsr   r   warningsr   r'   r   r_   Z_apply_pyprojecttomlr   r   r   r6   rV   r   r   PathLiker   	getLoggerr   r.   rp   r   r   r*   r8   r5   rH   rJ   r   rF   ZEnsurePackagesDiscoveredrc   rB   r   r   r   r   <module>   s\   
$
    D    [(