U
    uiG                     @  s  d Z ddlm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mZmZ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mZ erddlm Z  ddl!m"Z" e#e$Z%dddddZ&ddddddZ'd6dddddddZ(d7dddd d!d"d#d$Z)d8dd%dd dd&d'd(Z*G d)d* d*Z+d+d, Z,edd-d.d/Z-G d0d1 d1ej.Z/G d2d3 d3eZ0G d4d5 d5eZ1dS )9ac  
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``.
    )annotationsN)contextmanager)partial)TracebackType)TYPE_CHECKINGAnyCallableMapping   )StrPath)	FileErrorInvalidConfigError)SetuptoolsWarning   )expand)_PREVIOUSLY_DEFINED_MissingDynamicapply)SelfDistributionr   dict)filepathreturnc              
   C  s8   ddl m} t| d}||W  5 Q R  S Q R X d S )Nr
   )tomllibrb)Zcompat.py310r   openload)r   r   file r   C/tmp/pip-unpacked-wheel-9osl9suo/setuptools/config/pyprojecttoml.py	load_file#   s    r!   bool)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   ignore_option_errorsr   c                 C  s   t |d|| }t| ||S )zeApply the configuration from a ``pyproject.toml`` file into an existing
    distribution object.
    T)read_configuration_apply)r7   r   r8   r#   r   r   r    apply_configuration?   s    r;   TDistribution | Nonezdict[str, Any])r   r   r8   r7   r   c           
   	   C  s2  t j| } t j| s(td| dt| p2i }|di }|di }|di }|rd|sh|shi S d|di krt  d|krt	jdd	 |r|j
d
k	r|d|j
 n|dd ||d< ||d< d|krt	j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
setuptoolsZtools	distutilsz[tool.distutils])subjectNzinclude-package-dataTzext-modulesz[tool.setuptools.ext-modules])r'   r=   )ospathabspathisfiler   r!   r+   _ToolsTypoInMetadataemit_ExperimentalConfigurationZinclude_package_data
setdefault_ignore_errorsr-   dirnameexpand_configuration)
r   r   r8   r7   ZasdictZproject_tableZ
tool_tableZsetuptools_tableZsubsetroot_dirr   r   r    r9   K   s6    
r9   StrPath | None)r#   rL   r8   r7   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   r8   r7   r   r   r    rK      s    rK   c                   @  s  e Zd Zd=ddddddd	Zd
dddZddddddZd>ddZdd Zdd Zdd Z	ddddZ
d
dd d!d"Zd
dd#d$d%Zddd&d'd(Zd
ddd)d*d+Zd
dd d,d-Zd
d.d/d0d1Zd
dd2d3d4d5Zd
d6d7d8Zd
d6d9d:Zd
d6d;d<ZdS )?rN   NFr   rM   r"   r<   rO   c                 C  sp   || _ |pt | _|di | _| jdg | _|di di | _| jdi | _|| _	|| _
t | _d S )Nr'   dynamicr=   r>   )r#   rA   getcwdrL   r+   project_cfgrP   setuptools_cfgdynamic_cfgr8   _distset_referenced_files)selfr#   rL   r8   r7   r   r   r    __init__   s    z_ConfigExpander.__init__r   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   rR   r+   rU   )rX   r   attrsr   r   r    _ensure_dist   s    z_ConfigExpander._ensure_diststrr   )	containerfieldfnc              	   C  s2   ||kr.t | j ||| ||< W 5 Q R X d S N)rI   r8   )rX   r_   r`   ra   r   r   r    _process_field   s    z_ConfigExpander._process_fieldpackage-datac                 C  s   | j |i }t|S rb   )rS   r+   _expandZcanonic_package_data)rX   r`   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_packagesrg   r]   _EnsurePackagesDiscoveredrR   rS   package_dir_expand_data_files_expand_cmdclass_expand_all_dynamicrW   updater#   )rX   r7   ctxZensure_discoveredrj   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)rS   r+   
isinstancelisttupler   rL   rH   rI   r8   re   Zfind_packages)rX   rp   rq   r   r   r    rh      s    


z _ConfigExpander._expand_packagesc                 C  s$   t tj| jd}| | jd| d S )N)rL   z
data-files)r   re   Zcanonic_data_filesrL   rc   rS   )rX   
data_filesr   r   r    rk      s    z"_ConfigExpander._expand_data_fileszMapping[str, str])rj   c                 C  s*   | j }ttj||d}| | jd| d S )N)rj   rL   cmdclass)rL   r   re   rw   rc   rS   )rX   rj   rL   rw   r   r   r    rl      s    z _ConfigExpander._expand_cmdclass)r7   rj   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.0r`   r7   rj   rX   specialr   r    
<dictcomp>   s    z7_ConfigExpander._expand_all_dynamic.<locals>.<dictcomp>)rx   ry   r}   r~   Zoptional_dependenciesc                 S  s   i | ]\}}|d k	r||qS rb   r   )r   kvr   r   r    r     s       )
rP   rn   _obtain_entry_points_obtain_version_obtain_readme_obtain_classifiers_obtain_dependencies_obtain_optional_dependenciesitemsrR   )rX   r7   rj   Zobtained_dynamicZupdatesr   r   r    rm      s    

z#_ConfigExpander._expand_all_dynamic)r7   r`   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   r8   r   )rX   r7   r`   previousmsgr   r   r    _ensure_previously_set  s
    
z&_ConfigExpander._ensure_previously_set)	specifierrj   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more_itertoolsr   rI   r8   rL   rW   rn   re   Z
read_filesZ	read_attrr4   )rX   r   	directiverj   r   rL   r   r   r    _expand_directive  s    z!_ConfigExpander._expand_directive)r7   r`   rj   c                 C  s4   || j kr$| d| | j | |S | || d S )Nztool.setuptools.dynamic.)rT   r   r   )rX   r7   r`   rj   r   r   r    r   )  s    
z_ConfigExpander._obtainc                 C  s,   d| j kr(d| jkr(t| |d|S d S )Nrx   )rP   rT   re   rx   r   )rX   r7   rj   r   r   r    r   3  s
    z_ConfigExpander._obtain_versionzdict[str, str] | None)r7   r   c                 C  sL   d| j krd S | j}d|kr<| |di |d dddS | |d d S )Nry   content-typez
text/x-rst)textr   )rP   rT   r   r+   r   )rX   r7   rT   r   r   r    r   <  s    
z_ConfigExpander._obtain_readmezdict[str, dict] | None)r7   rj   r   c                   sz   d}t fdd|D sd S |d|}|d kr8d S t|di ddd fdd}|d	d
 |dd  S )N)rz   r{   r|   c                 3  s   | ]}| j kV  qd S rb   )rP   r   rX   r   r    	<genexpr>O  s     z7_ConfigExpander._obtain_entry_points.<locals>.<genexpr>rz   r^   )r`   groupc                   s8   |kr4 |}| jkr,tt| || | < d S rb   )poprP   r   r   r3   )r`   r   valueexpandedgroupsrX   r   r    _set_scriptsY  s
    

z:_ConfigExpander._obtain_entry_points.<locals>._set_scriptsr{   console_scriptsr|   gui_scripts)anyr   re   entry_points)rX   r7   rj   fieldsr   r   r   r   r    r   K  s    


z$_ConfigExpander._obtain_entry_points)r7   c                 C  s(   d| j kr$| |di }|r$| S d S )Nr}   )rP   r   
splitlinesrX   r7   r   r   r   r    r   e  s
    
z#_ConfigExpander._obtain_classifiersc                 C  s(   d| j kr$| |di }|r$t|S d S )Nr~   )rP   r   _parse_requirements_listr   r   r   r    r   l  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   y  s    zA_ConfigExpander._obtain_optional_dependencies.<locals>.<dictcomp>)rP   rT   rs   r   AssertionErrorr   r   )rX   r7   Zoptional_dependencies_mapr   r   r    r   s  s    




z-_ConfigExpander._obtain_optional_dependencies)NFN)rd   )__name__
__module____qualname__rY   r]   rc   rg   r   rh   rk   rl   rm   r   r   r   r   r   r   r   r   r   r   r   r   r    rN      s*      


	rN   c                 C  s   dd |   D S )Nc                 S  s&   g | ]}|  r|  d s|qS )#)r0   
startswith)r   liner   r   r    
<listcomp>  s    z,_parse_requirements_list.<locals>.<listcomp>)r   )r   r   r   r    r     s    r   )r8   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 - )	Exceptionr1   r2   	__class__r   )r8   r5   r   r   r    rI     s    
rI   c                      sP   e Zd Zdddd fddZdd fdd	Zd
dddd fddZ  ZS )ri   r   r   )distributionrR   rS   c                   s   t  | || _|| _d S rb   )superrY   _project_cfg_setuptools_cfg)rX   r   rR   rS   r   r   r    rY     s    z"_EnsurePackagesDiscovered.__init__r   rZ   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``.
        rr   Nr/   
py-modulesrp   )rU   r   rH   rn   rj   set_defaultsZ_ignore_ext_modulesmetadatar/   r   r+   
py_modulesrp   r   	__enter__)rX   r7   cfgrj   r   r   r    r     s    


z#_EnsurePackagesDiscovered.__enter__ztype[BaseException] | NonezBaseException | NonezTracebackType | NoneNone)exc_type	exc_value	tracebackr   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``.
        rp   r   )r   rH   rU   rp   r   r   __exit__)rX   r   r   r   r   r   r    r     s    
z"_EnsurePackagesDiscovered.__exit__)r   r   r   rY   r   r   __classcell__r   r   r   r    ri     s   ri   c                   @  s   e Zd ZdZdS )rG   z``{subject}` in `pyproject.toml` is still *experimental* and likely to change in future releases.Nr   r   r   Z_SUMMARYr   r   r   r    rG     s   rG   c                   @  s   e Zd ZdZdS )rE   zNIgnoring [tools.setuptools] in pyproject.toml, did you mean [tool.setuptools]?Nr   r   r   r   r    rE     s   rE   )F)TFN)NFN)2__doc__
__future__r   loggingrA   
contextlibr   	functoolsr   typesr   typingr   r   r   r	   _pathr   errorsr   r   warningsr   r*   r   re   Z_apply_pyprojecttomlr   r   r   r:   Ztyping_extensionsr   r[   r   	getLoggerr   r1   r!   r-   r;   r9   rK   rN   r   rI   ZEnsurePackagesDiscoveredri   rG   rE   r   r   r   r    <module>   sH   
    K    ]-