a
    6RhG                     @  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 ddl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(d7dddddddZ)d8dddd!d"d#d$d%Z*d9dd&dd!dd'd(d)Z+G d*d+ d+Z,d,d- Z-edd.d/d0Z.G d1d2 d2ej/Z0G d3d4 d4eZ1G d5d6 d6eZ2d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``.
    )annotationsN)Mapping)contextmanager)partial)TracebackType)TYPE_CHECKINGAnyCallable   )StrPath)	FileErrorInvalidConfigError)SetuptoolsWarning   )expand)_PREVIOUSLY_DEFINED_MissingDynamicapply)SelfDistributionr   dict)filepathreturnc                 C  sD   ddl m} t| d}||W  d    S 1 s60    Y  d S )Nr
   )tomllibrb)Zcompat.py310r   openload)r   r   file r   U/opt/python-3.9.24/usr/lib/python3.9/site-packages/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y } z`d|j }|j	
ddkrzt| t|j d|j	 d	}t| d
| d W Y d }~n
d }~0 0 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   	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)r8   r   r9   r#   r   r   r    apply_configuration@   s    r<   TDistribution | Nonezdict[str, Any])r   r   r9   r8   r   c           
      C  sH  t j| } t j| s(td| dt| p2i }|di }|di }|di }|rd|sh|shi S d|di v rt  d|v rt	jdd	 |r|j
d
ur|d|j
 n|dd ||d< ||d< d|v rt	jdd	 t|( |d|id}t||  W d
   n1 s0    Y  |rDt 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   r9   r8   asdictZproject_tableZ
tool_tableZsetuptools_tableZsubsetroot_dirr   r   r    r:   L   s6    
*r:   StrPath | Noner#   rN   r9   r8   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#   rN   r9   r8   r   r   r    rL      s    rL   c                   @  s  e Zd Zd>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/d0d1d2Zddd3d4d5d6Zdd7d8d9Zdd7d:d;Zdd7d<d=ZdS )@rQ   NFr   rO   r"   r=   NonerP   c                 C  st   || _ |pt | _|di | _| jdg | _|di di | _| jdi | _|| _	|| _
tt  | _d S )Nr'   dynamicr>   r?   )r#   rB   getcwdrN   r+   project_cfgrS   setuptools_cfgdynamic_cfgr9   _distsetstr_referenced_files)selfr#   rN   r9   r8   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   rN   rU   r+   rX   )r\   r   attrsr   r   r    _ensure_dist   s    z_ConfigExpander._ensure_distrZ   r	   )	containerfieldfnc                 C  sF   ||v rBt | j  ||| ||< W d    n1 s80    Y  d S N)rJ   r9   )r\   rb   rc   rd   r   r   r    _process_field   s    z_ConfigExpander._process_fieldpackage-datac                 C  s   | j |i }t|S re   )rV   r+   _expandZcanonic_package_data)r\   rc   package_datar   r   r    _canonic_package_data   s    z%_ConfigExpander._canonic_package_datac                 C  s   |    |   | d |  }t|| j| j}|4}|j}|   | | | 	|| W d    n1 sp0    Y  |j
| j
 | jS )Nzexclude-package-data)_expand_packagesrj   ra   _EnsurePackagesDiscoveredrU   rV   package_dir_expand_data_files_expand_cmdclass_expand_all_dynamicr[   updater#   )r\   r8   ctxZensure_discoveredrm   r   r   r    r      s    

*z_ConfigExpander.expandc                 C  s   | j d}|d u s"t|ttfr&d S |d}t|tr| j|d< | j di |d< t| j	& t
jf i || j d< W d    n1 s0    Y  d S )NpackagesfindrN   package-dirZfill_package_dir)rV   r+   
isinstancelisttupler   rN   rI   rJ   r9   rh   Zfind_packages)r\   rs   rt   r   r   r    rk      s    


z _ConfigExpander._expand_packagesc                 C  s$   t tj| jd}| | jd| d S )N)rN   z
data-files)r   rh   Zcanonic_data_filesrN   rf   rV   )r\   
data_filesr   r   r    rn      s    z"_ConfigExpander._expand_data_fileszMapping[str, str])rm   c                 C  s*   | j }ttj||d}| | jd| d S )N)rm   rN   cmdclass)rN   r   rh   rz   rf   rV   )r\   rm   rN   rz   r   r   r    ro      s    z _ConfigExpander._expand_cmdclass)r8   rm   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 | ]}|vr|  |qS r   )_obtain.0rc   r8   rm   r\   Zspecialr   r    
<dictcomp>   s   z7_ConfigExpander._expand_all_dynamic.<locals>.<dictcomp>)r{   r|   r   r   Zoptional_dependenciesc                 S  s   i | ]\}}|d ur||qS re   r   )r   kvr   r   r    r         )
rS   rq   _obtain_entry_points_obtain_version_obtain_readme_obtain_classifiers_obtain_dependencies_obtain_optional_dependenciesitemsrU   )r\   r8   rm   Zobtained_dynamicZupdatesr   r   r    rp      s    

z#_ConfigExpander._expand_all_dynamic)r8   rc   c                 C  s2   t | |}|d u 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   r9   r   )r\   r8   rc   previousmsgr   r   r    _ensure_previously_set  s
    
z&_ConfigExpander._ensure_previously_set)	specifierrm   c                 C  s   ddl m} t| j | j}d|v rX| j||d  t|d |W  d    S d|v rt	|d ||W  d    S t
d| d|W d    n1 s0    Y  d S )Nr   )always_iterabler   attrz	invalid `z`: )Zmore_itertoolsr   rJ   r9   rN   r[   rq   rh   Z
read_filesZ	read_attrr4   )r\   r   	directiverm   r   rN   r   r   r    _expand_directive  s     2z!_ConfigExpander._expand_directive)r8   rc   rm   c                 C  s4   || j v r$| d| | j | |S | || d S )Nztool.setuptools.dynamic.)rW   r   r   )r\   r8   rc   rm   r   r   r    r   *  s    
z_ConfigExpander._obtainc                 C  s,   d| j v r(d| jv r(t| |d|S d S )Nr{   )rS   rW   rh   r{   r   )r\   r8   rm   r   r   r    r   4  s
    z_ConfigExpander._obtain_versionzdict[str, str] | None)r8   r   c                 C  sL   d| j vrd S | j}d|v r<| |di |d dddS | |d d S )Nr|   content-typez
text/x-rst)textr   )rS   rW   r   r+   r   )r\   r8   rW   r   r   r    r   =  s    
z_ConfigExpander._obtain_readmez dict[str, dict[str, Any]] | None)r8   rm   r   c                   sz   d}t fdd|D sd S |d|}|d u r8d S t|di ddd fdd}|d	d
 |dd  S )N)r}   r~   r   c                 3  s   | ]}| j v V  qd S re   )rS   r   r\   r   r    	<genexpr>P  r   z7_ConfigExpander._obtain_entry_points.<locals>.<genexpr>r}   rZ   )rc   groupc                   s8   |v r4 |}| jvr,tt| || | < d S re   )poprS   r   r   r3   )rc   r   valueexpandedgroupsr\   r   r    _set_scripts[  s
    

z:_ConfigExpander._obtain_entry_points.<locals>._set_scriptsr~   console_scriptsr   gui_scripts)anyr   rh   entry_points)r\   r8   rm   fieldsr   r   r   r   r    r   L  s    


z$_ConfigExpander._obtain_entry_points)r8   c                 C  s(   d| j v r$| |di }|r$| S d S )Nr   )rS   r   
splitlinesr\   r8   r   r   r   r    r   g  s
    
z#_ConfigExpander._obtain_classifiersc                 C  s(   d| j v r$| |di }|r$t|S d S )Nr   )rS   r   _parse_requirements_listr   r   r   r    r   n  s
    
z$_ConfigExpander._obtain_dependenciesc                   sV   d j vrd S d jv rF jd }t|ts0J  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>)rS   rW   rv   r   r   r   )r\   r8   Zoptional_dependencies_mapr   r   r    r   u  s    




z-_ConfigExpander._obtain_optional_dependencies)NFN)rg   )__name__
__module____qualname__r]   ra   rf   rj   r   rk   rn   ro   rp   r   r   r   r   r   r   r   r   r   r   r   r   r    rQ      s*      


	rQ   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   )r9   c              
   c  s\   | sd V  d S z
d V  W n> t yV } z&td|jj d|  W Y d }~n
d }~0 0 d S )Nzignored error: z - )	Exceptionr1   r2   	__class__r   )r9   r6   r   r   r    rJ     s    
rJ   c                      sR   e Zd Zd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 )rl   r   r   rR   )distributionrU   rV   r   c                   s   t  | || _|| _d S re   )superr]   _project_cfg_setuptools_cfg)r\   r   rU   rV   r   r   r    r]     s    z"_EnsurePackagesDiscovered.__init__r   r^   c                   s   | j | j }}|di }||jp&i  ||_|j  |jjdu rV| j	
d|j_|jdu rl|
d|_|jdu 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   )rX   r   rI   rq   rm   set_defaultsZ_ignore_ext_modulesmetadatar/   r   r+   
py_modulesrs   r   	__enter__)r\   r8   cfgrm   r   r   r    r     s    


z#_EnsurePackagesDiscovered.__enter__ztype[BaseException] | NonezBaseException | NonezTracebackType | 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``.
        rs   r   )r   rI   rX   rs   r   r   __exit__)r\   r   r   r   r   r   r    r     s    
z"_EnsurePackagesDiscovered.__exit__)r   r   r   r]   r   r   __classcell__r   r   r   r    rl     s   rl   c                   @  s   e Zd ZdZdS )rH   z``{subject}` in `pyproject.toml` is still *experimental* and likely to change in future releases.Nr   r   r   Z_SUMMARYr   r   r   r    rH     s   rH   c                   @  s   e Zd ZdZdS )rF   zNIgnoring [tools.setuptools] in pyproject.toml, did you mean [tool.setuptools]?Nr   r   r   r   r    rF     s   rF   )F)TFN)NFN)3__doc__
__future__r   loggingrB   collections.abcr   
contextlibr   	functoolsr   typesr   typingr   r   r	   _pathr   errorsr   r   warningsr   r*   r   rh   Z_apply_pyprojecttomlr   r   r   r;   Ztyping_extensionsr   r_   r   	getLoggerr   r1   r!   r-   r<   r:   rL   rQ   r   rJ   ZEnsurePackagesDiscoveredrl   rH   rF   r   r   r   r    <module>   sJ   
    K    ^-