o
    dC                     @   s  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rRd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(			d,de ded fddZ)			d-d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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   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   S/app/.heroku/python/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yR } z*d|j }|j	
ddkr=t| t|j d|j	 d	}t| d
| d d }~ww )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*   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)r4   r   ignore_option_errorsr    r   r   r   apply_configuration:   s   r8   Tc           
      C   s  t j| } t j| std| dt| pi }|di }|di }|di }|r2|s4|s4i S |r:t  |rLt	|dddurL|
d|j n|
dd	 ||d< ||d< t| |d|id
}t||  W d   n1 suw   Y  |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 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   asdictZproject_tableZ
tool_tableZsetuptools_tableZsubsetroot_dirr   r   r   r5   F   s0   
r5   rH   r7   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    rH   r7   r4   r   r   r   rF      s   rF   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 )7rI   NFr    rH   r7   r4   r   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<   getcwdrH   r(   project_cfgrJ   setuptools_cfgdynamic_cfgr7   _distset_referenced_files)selfr    rH   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   rH   rL   r(   rO   )rR   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)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   s   |    |   | d |  }t|| j| j}|}|j}|   | | | 	|| W d    n1 s8w   Y  |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 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findrH   package-dirZfill_package_dirr   )rM   r(   
isinstancelisttupledictrH   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)rH   z
data-files)r   r]   Zcanonic_data_filesrH   r[   rM   )rR   
data_filesr   r   r   rc      s   z"_ConfigExpander._expand_data_filesrb   c                 C   s*   | j }ttj||d}| | jd| d S )N)rb   rH   cmdclass)rH   r   r]   rp   r[   rM   )rR   rb   rH   rp   r   r   r   rd      s   z _ConfigExpander._expand_cmdclassc              	      s   d fddj D }|j pi      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.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 u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_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   r7   r   )rR   r4   rX   previousmsgr   r   r   _ensure_previously_set  s   
z&_ConfigExpander._ensure_previously_set	specifierc                 C   s   ddl m} t| j? | j}d|v r,| j||d  t|d |W  d    S d|v r@t	|d ||W  d    S t
d| d|1 sMw   Y  d S )Nr   )always_iterabler   attrz	invalid `z`: )Z setuptools.extern.more_itertoolsr   rD   r7   rH   rQ   rf   r]   Z
read_filesZ	read_attrr1   )rR   r   	directiverb   r   rH   r   r   r   _expand_directive  s   z!_ConfigExpander._expand_directivec                 C   s4   || j v 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 v rd| jv rt| |d|S d S )Nrq   )rJ   rN   r]   rq   ry   )rR   r4   rb   r   r   r   r   (  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 )Nrr   content-typez
text/x-rst)textr   )rJ   rN   ry   r(   r   )rR   r4   rN   r   r   r   r   .  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)rs   rt   ru   c                 3   s    | ]}| j v V  qd S rZ   )rJ   rz   rR   r   r   	<genexpr>@  s    z7_ConfigExpander._obtain_entry_points.<locals>.<genexpr>rs   rX   groupc                    s:   |v r |}| jvrtj| |d | | < d S 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]   Zentry_pointsstr)rR   r4   rb   fieldsr   r   r   r   r   r   <  s   

	
z$_ConfigExpander._obtain_entry_pointsc                 C   s(   d| j v r| |di }|r| S d S )Nrv   )rJ   ry   
splitlinesrR   r4   r   r   r   r   r   X  
   
z#_ConfigExpander._obtain_classifiersc                 C   s(   d| j v r| |di }|rt|S d S )Nrw   )rJ   ry   _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 )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   r   r   )rR   r4   Zoptional_dependencies_mapr   r   r   r   f  s   



z-_ConfigExpander._obtain_optional_dependenciesNFN)r   r   )r\   )r4   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   rI      sP    










rI   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   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 )Nzignored error: z - )	Exceptionr.   r/   	__class__r   )r7   r2   r   r   r   rD     s   (rD   c                       sB   e Zd Zdddedef fddZ fddZ fd	d
Z  ZS )ra   distributionr   rL   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u r+| j	
d|j_|jdu r6|
d|_|jdu rA|
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``.
        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 )zWhen 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)TFNr   )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   rI   r   rD   ZEnsurePackagesDiscoveredra   r@   r   r   r   r   <module>   sl    
$


E
 Y(