U
    ñŠÐdôC  ã                   @   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r¤d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-xwugzb3_/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 |rtt ¡  |r˜t	|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Úinclude_package_dataNzinclude-package-dataT)r$   r9   )ÚosÚpathÚabspathÚisfiler   r   r(   Ú_BetaConfigurationÚ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    rG   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    rG   r7   r4   r   r   r   rF   ‰   s    rF   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 )6rH   NFr   rI   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<   ÚgetcwdrG   r(   Úproject_cfgrJ   Úsetuptools_cfgÚdynamic_cfgr7   Ú_distÚsetÚ_referenced_files)Úselfr    rG   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   rG   rL   r(   rO   )rR   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)rD   r7   )rR   rW   rX   rY   r   r   r   Ú_process_field¸   s    z_ConfigExpander._process_fieldúpackage-datac                 C   s   | j  |i ¡}t |¡S rZ   )rM   r(   Ú_expandZcanonic_package_data)rR   rX   Ú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_packagesr_   rV   Ú_EnsurePackagesDiscoveredrL   rM   Úpackage_dirÚ_expand_data_filesÚ_expand_cmdclassÚ_expand_all_dynamicrQ   Úupdater    )rR   r4   ÚctxZensure_discoveredrb   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ÚfindrG   úpackage-dirZfill_package_dir)rM   r(   Ú
isinstanceÚlistÚtupleÚdictrG   rC   rD   r7   r]   Zfind_packages)rR   rh   ri   r   r   r   r`   Ò   s    


z _ConfigExpander._expand_packagesc                 C   s$   t tj| jd}|  | jd|¡ d S )N)rG   z
data-files)r   r]   Zcanonic_data_filesrG   r[   rM   )rR   Ú
data_filesr   r   r   rc   Þ   s    z"_ConfigExpander._expand_data_files)rb   c                 C   s*   | j }ttj||d}|  | jd|¡ d S )N)rb   rG   Úcmdclass)rG   r   r]   rp   r[   rM   )rR   rb   rG   rp   r   r   r   rd   â   s    z _ConfigExpander._expand_cmdclass)r4   rb   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©Ú.0rX   ©r4   rb   rR   Zspecialr   r   Ú
<dictcomp>ó   s   þ z7_ConfigExpander._expand_all_dynamic.<locals>.<dictcomp>)rq   rr   rv   rw   Zoptional_dependenciesc                 S   s   i | ]\}}|d k	r||“qS rZ   r   )r{   ÚkÚvr   r   r   r}     s       )
rJ   rf   Ú_obtain_entry_pointsÚ_obtain_versionÚ_obtain_readmeÚ_obtain_classifiersÚ_obtain_dependenciesÚ_obtain_optional_dependenciesÚitemsrL   )rR   r4   rb   Zobtained_dynamicZupdatesr   r|   r   re   ç   s    þ
ú
z#_ConfigExpander._expand_all_dynamic)r4   rX   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   )rR   r4   rX   ÚpreviousÚmsgr   r   r   Ú_ensure_previously_set  s
    
ÿz&_ConfigExpander._ensure_previously_set)Ú	specifierrb   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‹   rD   r7   rG   rQ   rf   r]   Z
read_filesZ	read_attrr1   )rR   rŠ   Ú	directiverb   r‹   rG   r   r   r   Ú_expand_directive  s    z!_ConfigExpander._expand_directive)r4   rX   rb   c                 C   s4   || j kr$|  d|› | j | |¡S |  ||¡ d S )Nztool.setuptools.dynamic.)rN   rŽ   r‰   )rR   r4   rX   rb   r   r   r   ry     s    
ýz_ConfigExpander._obtainc                 C   s,   d| j kr(d| jkr(t |  |d|¡¡S d S )Nrq   )rJ   rN   r]   rq   ry   )rR   r4   rb   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 )Nrr   úcontent-typez
text/x-rst)Útextr   )rJ   rN   ry   r(   r‰   )rR   r4   rN   r   r   r   r‚   .  s    
þz_ConfigExpander._obtain_readme)r4   rb   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)rs   rt   ru   c                 3   s   | ]}|ˆ j kV  qd S rZ   )rJ   rz   ©rR   r   r   Ú	<genexpr>@  s     z7_ConfigExpander._obtain_entry_points.<locals>.<genexpr>rs   )rX   Úgroupc                    s6   |ˆkr2ˆ  |¡}| ˆjkr*tj| |d |ˆ | < d S )N)rX   Úvalue)ÚpoprJ   r   rA   )rX   r“   r”   ©ÚexpandedÚgroupsrR   r   r   Ú_set_scriptsJ  s
    

z:_ConfigExpander._obtain_entry_points.<locals>._set_scriptsrt   Úconsole_scriptsru   Úgui_scripts)Úanyry   r]   Úentry_pointsÚstr)rR   r4   rb   Ú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 )Nrv   )rJ   ry   Ú
splitlines©rR   r4   r”   r   r   r   rƒ   X  s
    
z#_ConfigExpander._obtain_classifiersc                 C   s(   d| j kr$|  |di ¡}|r$t|ƒS d S )Nrw   )rJ   ry   Ú_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 )Nrx   c              	      s*   i | ]"\}}|t ˆ  d |› |i ¡ƒ“qS )z.tool.setuptools.dynamic.optional-dependencies.)r¢   rŽ   )r{   r“   r   r‘   r   r   r}   l  s   û ýzA_ConfigExpander._obtain_optional_dependencies.<locals>.<dictcomp>)rJ   rN   rk   rn   ÚAssertionErrorr†   r‰   )rR   r4   Zoptional_dependencies_mapr   r‘   r   r…   f  s    



úz-_ConfigExpander._obtain_optional_dependencies)NFN)r\   )Ú__name__Ú
__module__Ú__qualname__rn   r   Ú_PathÚboolrS   rV   rž   r   r[   r_   r   r`   rc   r   rd   re   r‰   rŽ   ry   r   r   r‚   r€   rƒ   r„   r…   r   r   r   r   rH       s>      ûû
 
ÿ
 
þrH   c                 C   s   dd„ |   ¡ D ƒS )Nc                 S   s&   g | ]}|  ¡ r|  ¡  d ¡s|‘qS )ú#)r-   Ú
startswith)r{   Úliner   r   r   Ú
<listcomp>y  s    þz,_parse_requirements_list.<locals>.<listcomp>)r    )r”   r   r   r   r¢   x  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   rD   €  s    
rD   c                       s>   e Zd Zdeedœ‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )	ra   r   )ÚdistributionrL   rM   c                    s   t ƒ  |¡ || _|| _d S rZ   )ÚsuperrS   Ú_project_cfgÚ_setuptools_cfg)rR   r¯   rL   rM   ©r®   r   r   rS     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 )z«When entering the context, the values of ``packages``, ``py_modules`` and
        ``package_dir`` that are missing in ``dist`` are copied from ``setuptools_cfg``.
        rj   Nr,   ú
py-modulesrh   )rO   r²   rC   rf   rb   Úset_defaultsZ_ignore_ext_modulesÚmetadatar,   r±   r(   Ú
py_modulesrh   r°   Ú	__enter__)rR   r4   Úcfgrb   r³   r   r   r¸   ”  s    


z#_EnsurePackagesDiscovered.__enter__c                    s4   | j  d| jj¡ | j  d| jj¡ tƒ  |||¡S )zŸWhen exiting the context, if values of ``packages``, ``py_modules`` and
        ``package_dir`` are missing in ``setuptools_cfg``, copy from ``dist``.
        rh   r´   )r²   rC   rO   rh   r·   r°   Ú__exit__)rR   Úexc_typeÚ	exc_valueÚ	tracebackr³   r   r   rº   ª  s    z"_EnsurePackagesDiscovered.__exit__)r¤   r¥   r¦   rn   rS   r¸   rº   Ú__classcell__r   r   r³   r   ra   Œ  s     ÿra   c                   @   s   e Zd ZdZdS )r@   zDSupport for `[tool.setuptools]` in `pyproject.toml` is still *beta*.N)r¤   r¥   r¦   Z_SUMMARYr   r   r   r   r@   ´  s   r@   )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   rT   r   rž   ÚPathLiker§   Ú	getLoggerr¤   r.   rn   r   r¨   r*   r8   r5   rF   rH   r¢   rD   ZEnsurePackagesDiscoveredra   r@   r   r   r   r   Ú<module>   s\   
$
 ýü   üüE   üû Y(