o
    JAfJ                     @   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	rNddlmZ eeejf Zee Z!dede"fddZ#de"dede$fddZ%	d/dddeddfddZ&			d0deded fddZ'de"de"ded de$fddZ(			d1de"dee d e$ded de"f
d!d"Z)G d#d$ d$Z*d%d& Z+ed e$fd'd(Z,G d)d* d*ej-Z.G d+d, d,e/Z0G d-d. d.e/Z1dS )2z
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   sD   ddl m} t| d}||W  d    S 1 sw   Y  d S )Nr   )tomlirb)Zsetuptools.externr   openload)r   r   file r   D/usr/lib/python3.10/site-packages/setuptools/config/pyprojecttoml.py	load_file   s   $r   configc              
   C   s   ddl m} |jd}t|dr|  z|| W S  |jyB } zt	d|j
  t|j td|j }|d d }~ww )Nr   )_validate_pyprojectztrove-classifier_disable_downloadzconfiguration error: zinvalid pyproject.toml config: ) r   ZFORMAT_FUNCTIONSgethasattrr   validateZValidationError_loggererrorsummarydebugdetails
ValueErrorname)r   r   	validatorZtrove_classifierexr%   r   r   r   r#   !   s   
r#   Fdistr   c                 C   s   t |d|| }t| ||S )zeApply the configuration from a ``pyproject.toml`` file into an existing
    distribution object.
    T)read_configuration_apply)r-   r   ignore_option_errorsr   r   r   r   apply_configuration2   s   r1   Tc              
   C   sd  t j| } t j| std| dt| pi }|di }|di }|di }|r2|s4|s4i S |r>d}t|t	 |
 }	|rSt|ddurS|d	|j n|d	d
 ||d< ||d< z|d|id}
t|
|  W n1 ty } z%t||	|ri W  Y d}~S |rtd|jj d|  n W Y d}~nd}~ww |rt 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.projecttool
setuptoolszDSupport for `[tool.setuptools]` in `pyproject.toml` is still *beta*.include_package_dataNzinclude-package-dataT)r2   r3   ignored error:  - )ospathabspathisfiler
   r   r!   warningswarn_BetaConfigurationcopygetattr
setdefaultr5   r#   	Exception_skip_bad_configr$   r'   	__class____name__dirnameexpand_configuration)r   r   r0   r-   asdictZproject_tableZ
tool_tableZsetuptools_tablemsgZorig_setuptools_tableZsubsetr,   root_dirr   r   r   r.   >   sD   r.   project_cfgsetuptools_cfgc                 C   sn   |du s|j jdu r|j jdu r|jdu rdS |rdS t|  }h d}||kr5tjt	 tdd dS dS )z8Be temporarily forgiving with invalid ``pyproject.toml``NF>   r*   zrequires-pythonZpython_requiresversion   )
stacklevelT)
metadatar*   rM   Zinstall_requiressetkeysr<   r=   _InvalidFilemessage)rK   rL   r-   Zgiven_configZpopular_subsetr   r   r   rC      s   
rC   rJ   r0   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   rJ   r0   r-   r   r   r   rG      s   rG   c                
   @   sr  e Zd Z			d3dedee deded fdd	Zd4ddZdede	de
fddZd5ddZdd Zdd Zdd Zdee	e	f fddZdddee	e	f fddZddde	fd d!Zd"e	dee	e	f fd#d$Zddde	dee	e	f fd%d&Zdddee	e	f fd'd(Zddd
eee	e	f  fd)d*Zdddee	e	f d
eee	ef  fd+d,Zd6d-d.Zd6d/d0Zd6d1d2ZdS )7rU   NFr   rJ   r0   r-   r   c                 C   sh   || _ |pt | _|di | _| jdg | _|di di | _| jdi | _|| _	|| _
d S )Nr2   dynamicr3   r4   )r   r8   getcwdrJ   r!   rK   rV   rL   dynamic_cfgr0   _dist)selfr   rJ   r0   r-   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   rJ   rK   r!   rY   )rZ   r   attrsr   r   r   _ensure_dist   s   z_ConfigExpander._ensure_dist	containerfieldfnc                 C   sL   ||v r$t | j ||| ||< W d    d S 1 sw   Y  d S d S N)_ignore_errorsr0   )rZ   r_   r`   ra   r   r   r   _process_field   s
   "z_ConfigExpander._process_fieldpackage-datac                 C   s   | j |i }t|S rb   )rL   r!   _expandZcanonic_package_data)rZ   r`   package_datar   r   r   _canonic_package_data   s   
z%_ConfigExpander._canonic_package_datac                 C   s|   |    |   | d |  }t|| j}|j}|   | | | || W d    | j	S 1 s6w   Y  | j	S )Nzexclude-package-data)
_expand_packagesrh   r^   _EnsurePackagesDiscoveredrL   package_dir_expand_data_files_expand_cmdclass_expand_all_dynamicr   )rZ   r-   Zensure_discoveredrk   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rN| j|d< | j di |d< t| j	 t
jdi || j d< W d    d S 1 sGw   Y  d S d S )NpackagesfindrJ   package-dirZfill_package_dirr   )rL   r!   
isinstancelisttupledictrJ   rA   rc   r0   rf   Zfind_packages)rZ   ro   rp   r   r   r   ri      s   


"z _ConfigExpander._expand_packagesc                 C   s$   t tj| jd}| | jd| d S )N)rJ   z
data-files)r   rf   Zcanonic_data_filesrJ   rd   rL   )rZ   Z
data_filesr   r   r   rl      s   z"_ConfigExpander._expand_data_filesrk   c                 C   s*   | j }ttj||d}| | jd| d S )N)rk   rJ   cmdclass)rJ   r   rf   rv   rd   rL   )rZ   rk   rJ   rv   r   r   r   rm     s   z _ConfigExpander._expand_cmdclassc              	      s   d fddj D }|j pi      d dd | D }j	| d S )N)rM   readmeentry-pointsscriptsgui-scriptsclassifiersdependenciesoptional-dependenciesc                    s$   i | ]}|vr|  |qS r   )_obtain.0r`   r-   rk   rZ   Zspecialr   r   
<dictcomp>  s
    z7_ConfigExpander._expand_all_dynamic.<locals>.<dictcomp>)rM   rw   r{   r|   Zoptional_dependenciesc                 S   s   i | ]\}}|d ur||qS rb   r   )r   kvr   r   r   r   "  s    )
rV   update_obtain_entry_points_obtain_version_obtain_readme_obtain_classifiers_obtain_dependencies_obtain_optional_dependenciesitemsrK   )rZ   r-   rk   Zobtained_dynamicZupdatesr   r   r   rn     s   

z#_ConfigExpander._expand_all_dynamicc                 C   s6   t | |}|d u r| jsd|d}t|d S 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   r0   r   )rZ   r-   r`   previousrI   r   r   r   _ensure_previously_set%  s   
z&_ConfigExpander._ensure_previously_set	specifierc                 C   s   t | j5 | j}d|v rt|d |W  d    S d|v r0t|d ||W  d    S td| d|1 s=w   Y  d S )Nr   attrz	invalid `z`: )rc   r0   rJ   rf   Z
read_filesZ	read_attrr)   )rZ   r   	directiverk   rJ   r   r   r   _expand_directive/  s   z!_ConfigExpander._expand_directivec                 C   s4   || j v r| d| | j | |S | || d S )Nztool.setuptools.dynamic.)rX   r   r   )rZ   r-   r`   rk   r   r   r   r~   ;  s   
z_ConfigExpander._obtainc                 C   s,   d| j v rd| jv rt| |d|S d S )NrM   )rV   rX   rf   rM   r~   )rZ   r-   rk   r   r   r   r   E  s   z_ConfigExpander._obtain_versionc                 C   sL   d| j vrd S | j}d|v r| |di |d dddS | |d d S )Nrw   content-typez
text/x-rst)textr   )rV   rX   r~   r!   r   )rZ   r-   rX   r   r   r   r   K  s   
z_ConfigExpander._obtain_readmec                    s|   d}t fdd|D sd S |d|}|d u rd S t|di dtdtf fdd}|d	d
 |dd  S )N)rx   ry   rz   c                 3   s    | ]}| j v V  qd S rb   )rV   r   rZ   r   r   	<genexpr>]  s    z7_ConfigExpander._obtain_entry_points.<locals>.<genexpr>rx   r`   groupc                    sD   |v r  |}| jvrt| |}t|t | | < d S d S rb   )poprV   r   rT   r<   r=   )r`   r   valuerI   expandedgroupsrZ   r   r   _set_scriptsg  s   

z:_ConfigExpander._obtain_entry_points.<locals>._set_scriptsry   console_scriptsrz   gui_scripts)anyr~   rf   Zentry_pointsstr)rZ   r-   rk   fieldsr   r   r   r   r   r   Y  s   



z$_ConfigExpander._obtain_entry_pointsc                 C   s(   d| j v r| |di }|r| S d S )Nr{   )rV   r~   
splitlinesrZ   r-   r   r   r   r   r   v  
   
z#_ConfigExpander._obtain_classifiersc                 C   s(   d| j v r| |di }|rt|S d S )Nr|   )rV   r~   _parse_requirements_listr   r   r   r   r   }  r   z$_ConfigExpander._obtain_dependenciesc                    sV   d j vrd S d jv r# jd }t|tsJ  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>)rV   rX   rr   ru   r   r   )rZ   r-   Zoptional_dependencies_mapr   r   r   r     s   



z-_ConfigExpander._obtain_optional_dependenciesNFN)r   r   )re   )r-   r   )rE   
__module____qualname__ru   r   _Pathboolr[   r^   r   r   rd   rh   r   ri   rl   r   rm   rn   r   r   r~   r   r   r   r   r   r   r   r   r   r   r   rU      sP    










rU   c                 C   s   dd |   D S )Nc                 S   s&   g | ]}|  r|  d s|qS )#)strip
startswith)r   liner   r   r   
<listcomp>  s    z,_parse_requirements_list.<locals>.<listcomp>)r   )r   r   r   r   r     s   r   c              
   c   s^    | sd V  d S zd V  W d S  t y. } ztd|jj d|  W Y d }~d S d }~ww )Nr6   r7   )rB   r$   r'   rD   rE   )r0   r,   r   r   r   rc     s   (rc   c                       s>   e Zd Zdddef fddZ fddZ fdd	Z  ZS )
rj   distributionr   rL   c                    s   t  | || _d S rb   )superr[   _setuptools_cfg)rZ   r   rL   rD   r   r   r[     s   
z"_EnsurePackagesDiscovered.__init__c                    sp   | j | j}}|di }||jpi  ||_|j  |jdu r(|d|_|j	du r3|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``.
        rq   N
py-modulesro   )rY   r   rA   r   rk   set_defaultsZ_ignore_ext_modules
py_modulesr!   ro   r   	__enter__)rZ   r-   cfgrk   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``.
        ro   r   )r   rA   rY   ro   r   r   __exit__)rZ   exc_type	exc_value	tracebackr   r   r   r     s   z"_EnsurePackagesDiscovered.__exit__)rE   r   r   ru   r[   r   r   __classcell__r   r   r   r   rj     s    rj   c                   @   s   e Zd ZdZdS )r>   zJExplicitly inform users that some `pyproject.toml` configuration is *beta*N)rE   r   r   __doc__r   r   r   r   r>     s    r>   c                   @   s   e Zd ZdZedd ZdS )rS   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   rT     s   
z_InvalidFile.messageN)rE   r   r   r   classmethodrT   r   r   r   r   rS     s    rS   )F)TFNr   )2r   loggingr8   r<   
contextlibr   	functoolsr   typingr   r   r   r   r   r	   Zsetuptools.errorsr
   r   r    r   rf   Z_apply_pyprojecttomlr   r/   r   r   r\   r   r   PathLiker   	getLoggerrE   r$   ru   r   r   r#   r1   r.   rC   rG   rU   r   rc   ZEnsurePackagesDiscoveredrj   UserWarningr>   rS   r   r   r   r   <module>   s     


O
 
 U#