U
    -chK                     @   s  d 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	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 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'e"e"ed e$dddZ(d0e"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/Z0G d,d- d-e/Z1dS )1z
Load setuptools configuration from ``pyproject.toml`` files.

**PRIVATE MODULE**: API reserved for setuptools internal usage only.
    N)contextmanager)partial)TYPE_CHECKINGCallableDictOptionalMappingUnion)	FileErrorOptionError   )expand)apply)_PREVIOUSLY_DEFINED_WouldIgnoreField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   C/tmp/pip-unpacked-wheel-izj_87as/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)r1   r   ignore_option_errorsr   r   r   r   apply_configuration6   s    r5   T)r   r1   c              
   C   sf  t j| } t j| s(td| dt| p2i }|di }|di }|di }|rd|sh|shi S |r|d}t|t	 |
 }	|rt|ddk	r|d	|j n|d	d
 ||d< ||d< z|d|id}
t|
|  W nb tk
r@ } zBt||	|ri  W Y *S |r.td|jj d|  n W 5 d}~X Y nX |rbt 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 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
setuptoolszDSupport for `[tool.setuptools]` in `pyproject.toml` is still *beta*.include_package_dataNzinclude-package-dataT)r!   r6   ignored error:  - )ospathabspathisfiler
   r   r%   warningswarn_BetaConfigurationcopygetattr
setdefaultr8   r'   	Exception_skip_bad_configr+   r,   	__class____name__dirnameexpand_configuration)r   r   r4   r1   ZasdictZproject_tableZ
tool_tableZsetuptools_tablemsgZorig_setuptools_tableZsubsetr/   root_dirr   r   r   r2   B   s>    r2   )project_cfgsetuptools_cfgr1   r   c                 C   sr   |dks*|j jdkr.|j jdkr.|jdkr.dS |r6dS t|  }ddddh}||krntjt	 tdd d	S dS )
z8Be temporarily forgiving with invalid ``pyproject.toml``NFr)   versionZpython_requireszrequires-python   )
stacklevelT)
metadatar)   rO   Zinstall_requiressetkeysr?   r@   _InvalidFilemessage)rM   rN   r1   Zgiven_configZpopular_subsetr   r   r   rF      s     

rF   )r   rL   r4   r1   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   rL   r4   r1   r   r   r   rJ      s    rJ   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 )6rW   NFr   rX   c                 C   sh   || _ |pt | _|di | _| jdg | _|di di | _| jdi | _|| _	|| _
d S )Nr!   dynamicr6   r7   )r   r;   getcwdrL   r%   rM   rY   rN   dynamic_cfgr4   _dist)selfr   rL   r4   r1   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   rL   rM   r%   r\   )r]   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)_ignore_errorsr4   )r]   rb   rc   rd   r   r   r   _process_field   s    z_ConfigExpander._process_fieldpackage-datac                 C   s   | j |i }t|S re   )rN   r%   _expandZcanonic_package_data)r]   rc   package_datar   r   r   _canonic_package_data   s    z%_ConfigExpander._canonic_package_datac              	   C   sl   |    |   | d |  }t|| j| j}|*}|j}|   | | | 	|| W 5 Q R X | j
S )Nzexclude-package-data)_expand_packagesrk   ra   _EnsurePackagesDiscoveredrM   rN   package_dir_expand_data_files_expand_cmdclass_expand_all_dynamicr   )r]   r1   ctxZensure_discoveredrn   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findrL   package-dirZfill_package_dir)rN   r%   
isinstancelisttupledictrL   rD   rf   r4   ri   Zfind_packages)r]   rs   rt   r   r   r   rl      s    


z _ConfigExpander._expand_packagesc                 C   s$   t tj| jd}| | jd| d S )N)rL   z
data-files)r   ri   Zcanonic_data_filesrL   rg   rN   )r]   
data_filesr   r   r   ro     s    z"_ConfigExpander._expand_data_files)rn   c                 C   s*   | j }ttj||d}| | jd| d S )N)rn   rL   cmdclass)rL   r   ri   r{   rg   rN   )r]   rn   rL   r{   r   r   r   rp     s    z _ConfigExpander._expand_cmdclass)r1   rn   c              	      s   d fddj D }|j p0i      d dd | D }j	| d S )N)rO   readmeentry-pointsscriptsgui-scriptsclassifiersdependenciesoptional-dependenciesc                    s$   i | ]}|kr|  |qS r   )_obtain.0rc   r1   rn   r]   specialr   r   
<dictcomp>  s    z7_ConfigExpander._expand_all_dynamic.<locals>.<dictcomp>)rO   r|   r   r   Zoptional_dependenciesc                 S   s   i | ]\}}|d k	r||qS re   r   )r   kvr   r   r   r   &  s       )
rY   update_obtain_entry_points_obtain_version_obtain_readme_obtain_classifiers_obtain_dependencies_obtain_optional_dependenciesitemsrM   )r]   r1   rn   Zobtained_dynamicZupdatesr   r   r   rq     s    

z#_ConfigExpander._expand_all_dynamic)r1   rc   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   r4   r   )r]   r1   rc   previousrK   r   r   r   _ensure_previously_set)  s
    
z&_ConfigExpander._ensure_previously_set)	specifierrn   c              
   C   s~   t | jj | j}d|kr6t|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   attrz	invalid `z`: )rf   r4   rL   ri   Z
read_filesZ	read_attrr.   )r]   r   	directivern   rL   r   r   r   _expand_directive3  s    z!_ConfigExpander._expand_directive)r1   rc   rn   c                 C   s4   || j kr$| d| | j | |S | || d S )Nztool.setuptools.dynamic.)r[   r   r   )r]   r1   rc   rn   r   r   r   r   ?  s    
z_ConfigExpander._obtainc                 C   s,   d| j kr(d| jkr(t| |d|S d S )NrO   )rY   r[   ri   rO   r   )r]   r1   rn   r   r   r   r   I  s    z_ConfigExpander._obtain_version)r1   r   c                 C   sL   d| j krd S | j}d|kr<| |di |d dddS | |d d S )Nr|   content-typez
text/x-rst)textr   )rY   r[   r   r%   r   )r]   r1   r[   r   r   r   r   O  s    
z_ConfigExpander._obtain_readme)r1   rn   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)r}   r~   r   c                 3   s   | ]}| j kV  qd S re   )rY   r   r]   r   r   	<genexpr>a  s     z7_ConfigExpander._obtain_entry_points.<locals>.<genexpr>r}   )rc   groupc                    s@   |kr< |}| jkr4t| |}t|t | | < d S re   )poprY   r   rV   r?   r@   )rc   r   valuerK   expandedgroupsr]   r   r   _set_scriptsk  s    

z:_ConfigExpander._obtain_entry_points.<locals>._set_scriptsr~   console_scriptsr   gui_scripts)anyr   ri   entry_pointsstr)r]   r1   rn   fieldsr   r   r   r   r   r   ]  s    



z$_ConfigExpander._obtain_entry_points)r1   c                 C   s(   d| j kr$| |di }|r$| S d S )Nr   )rY   r   
splitlinesr]   r1   r   r   r   r   r   z  s
    
z#_ConfigExpander._obtain_classifiersc                 C   s(   d| j kr$| |di }|r$t|S d S )Nr   )rY   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 )Nr   c              	      s*   i | ]"\}}|t  d | |i qS )z.tool.setuptools.dynamic.optional-dependencies.)r   r   )r   r   r   r   r   r   r     s    zA_ConfigExpander._obtain_optional_dependencies.<locals>.<dictcomp>)rY   r[   rv   ry   AssertionErrorr   r   )r]   r1   Zoptional_dependencies_mapr   r   r   r     s    



z-_ConfigExpander._obtain_optional_dependencies)NFN)rh   )rH   
__module____qualname__ry   r   _Pathboolr^   ra   r   r   rg   rk   r   rl   ro   r   rp   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rW      s>      
 

 
rW   c                 C   s   dd |   D S )Nc                 S   s&   g | ]}|  r|  d s|qS )#)r*   
startswith)r   liner   r   r   
<listcomp>  s    z,_parse_requirements_list.<locals>.<listcomp>)r   )r   r   r   r   r     s    r   )r4   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 )Nr9   r:   )rE   r+   r,   rG   rH   )r4   r/   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 )	rm   r   )distributionrM   rN   c                    s   t  | || _|| _d S re   )superr^   _project_cfg_setuptools_cfg)r]   r   rM   rN   rG   r   r   r^     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``.
        ru   Nr)   
py-modulesrs   )r\   r   rD   r   rn   set_defaultsZ_ignore_ext_modulesrR   r)   r   r%   
py_modulesrs   r   	__enter__)r]   r1   cfgrn   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``.
        rs   r   )r   rD   r\   rs   r   r   __exit__)r]   exc_type	exc_value	tracebackr   r   r   r     s    z"_EnsurePackagesDiscovered.__exit__)rH   r   r   ry   r^   r   r   __classcell__r   r   r   r   rm     s     rm   c                   @   s   e Zd ZdZdS )rA   zJExplicitly inform users that some `pyproject.toml` configuration is *beta*N)rH   r   r   __doc__r   r   r   r   rA     s   rA   c                   @   s   e Zd ZdZedd ZdS )rU   a  The given `pyproject.toml` file is invalid and would be ignored.
    !!


    ############################
    # Invalid `pyproject.toml` #
    ############################

    Any configurations in `pyproject.toml` will be ignored.
    Please note that future releases of setuptools will halt the build process
    if an invalid file is given.

    To prevent setuptools from considering `pyproject.toml` please
    DO NOT include the `[project]` or `[tool.setuptools]` tables in your file.
    

!!
    c                 C   s   ddl m} || jS )Nr   )cleandoc)inspectr   r   )clsr   r   r   r   rV     s    z_InvalidFile.messageN)rH   r   r   r   classmethodrV   r   r   r   r   rU     s   rU   )F)TFN)NFN)2r   loggingr;   r?   
contextlibr   	functoolsr   typingr   r   r   r   r   r	   Zsetuptools.errorsr
   r   r$   r   ri   Z_apply_pyprojecttomlr   r3   r   r   r_   r   r   PathLiker   	getLoggerrH   r+   ry   r   r   r'   r5   r2   rF   rJ   rW   r   rf   ZEnsurePackagesDiscoveredrm   UserWarningrA   rU   r   r   r   r   <module>   sh    
    P       U(