o
    JAfY                     @   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 ddlm	Z	m
Z
mZmZ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mZ dd	lmZ dd
lmZ ddlmZ e	rjddl m!Z! ddl"m#Z# ee$ej%f Z&ededef f Z'	 ede'f Z(eded dZ)		d5de&de*fddZ+ddde&ddfddZ,		d6ddde&dee& de-ded f
d d!Z.d"e)d#e$fd$d%Z/d&ed de*fd'd(Z0	d7d)dd*e(ded+ fd,d-Z1G d.d/ d/ee) Z2G d0d1 d1e2d2 Z3G d3d4 d4e2d Z4dS )8ze
Load setuptools configuration from ``setup.cfg`` files.

**API will be made private in the future**
    N)defaultdict)partialwraps)TYPE_CHECKINGCallableAnyDictGenericIterableListOptionalTupleTypeVarUnion)DistutilsOptionErrorDistutilsFileError)VersionInvalidVersion)SpecifierSet)SetuptoolsDeprecationWarning   )expandDistribution)DistributionMetadatastrTarget)r   r   )boundFfilepathreturnc                 C   s8   ddl m} | }|r| ng }t|| ||}t|S )a,  Read given configuration file and returns options from it as a dict.

    :param str|unicode filepath: Path to configuration file
        to get options from.

    :param bool find_others: Whether to search for other configuration files
        which could be on in various places.

    :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.

    :rtype: dict
    r   r   )setuptools.distr   Zfind_config_files_applyconfiguration_to_dict)r   Zfind_othersignore_option_errorsr   dist	filenameshandlers r(   ?/usr/lib/python3.10/site-packages/setuptools/config/setupcfg.pyread_configuration&   s
   r*   r%   r   c                 C   s   t | | |   | S )z`Apply the configuration from a ``setup.cfg`` file into an existing
    distribution object.
    )r"   Z_finalize_requires)r%   r   r(   r(   r)   apply_configurationB   s   
r+   r(   other_filesr$   )ConfigHandler.c              	   C   s   ddl m} tj|}tj|std| t }ttj	| g ||}z|j
| |d t| | j|d}|   W t| |S t| w )zHRead configuration from ``filepath`` and applies to the ``dist`` object.r   )_Distributionz%Configuration file %s does not exist.)r&   )r$   )r!   r.   ospathabspathisfiler   getcwdchdirdirnameZparse_config_filesparse_configurationcommand_optionsZ_finalize_license_files)r%   r   r,   r$   r.   Zcurrent_directoryr&   r'   r(   r(   r)   r"   K   s    

r"   
target_objkeyc                 C   s2   dj di t }tt| |}t| ||}| S )z
    Given a target object and option key, get that option from
    the target object, either through a get_{key} method or
    from an attribute directly.
    z	get_{key}Nr(   )formatlocals	functoolsr   getattr)r8   r9   Zgetter_nameZby_attributegetterr(   r(   r)   _get_optionh   s   r?   r'   c                 C   s<   t t}| D ]}|jD ]}t|j|}|||j |< qq|S )zReturns configuration data gathered by given handlers as a dict.

    :param list[ConfigHandler] handlers: Handlers list,
        usually from parse_configuration()

    :rtype: dict
    )r   dictset_optionsr?   r8   section_prefix)r'   Zconfig_dicthandleroptionvaluer(   r(   r)   r#   t   s   
r#   distributionr7   )ConfigMetadataHandlerConfigOptionsHandlerc                 C   s|   t | -}t| |||}|  | js|j| _t| j|||| j| j}|  W d   ||fS 1 s5w   Y  ||fS )a  Performs additional parsing of configuration options
    for a distribution.

    Returns a list of used option handlers.

    :param Distribution distribution:
    :param dict command_options:
    :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.
    :rtype: list
    N)r   EnsurePackagesDiscoveredrH   parsepackage_dirrG   metadatasrc_root)rF   r7   r$   ensure_discoveredoptionsmetar(   r(   r)   r6      s.   

r6   c                   @   s   e Zd ZU dZeed< 	 i Zeeef ed< 	 dede	de
jfddZed	d
 Zdd Zed&ddZedd Zedd Zedd ZedefddZdefddZedd Zed'ddZd d! Zd"d# Zd$d% ZdS )(r-   z1Handles metadata supplied in configuration files.rB   aliasesr8   rO   rN   c           	      C   sd   i }| j }| D ]\}}||sq	||dd}|||< q	|| _|| _|| _g | _|| _	d S )N .)
rB   items
startswithreplacestripr$   r8   sectionsrA   rN   )	selfr8   rO   r$   rN   rX   rB   section_namesection_optionsr(   r(   r)   __init__   s   


zConfigHandler.__init__c                 C   s   t d| jj ).Metadata item name to parser function mapping.z!%s must provide .parsers property)NotImplementedError	__class____name__)rY   r(   r(   r)   parsers   s   
zConfigHandler.parsersc           	      C   s   t  }| j}| j||}t|||}||u rt||rd S d}| j|}|r?z||}W n ty>   d}| js< Y nw |rCd S t|d| d }|d u rVt	||| n|| | j
| d S )NFTzset_%s)tupler8   rQ   getr=   KeyErrorra   	Exceptionr$   setattrrA   append)	rY   Zoption_namerE   unknownr8   current_valueZskip_optionparsersetterr(   r(   r)   __setitem__   s4   zConfigHandler.__setitem__,c                 C   s8   t |tr|S d|v r| }n||}dd |D S )zRepresents value as a list.

        Value is split either by separator (defaults to comma) or by lines.

        :param value:
        :param separator: List items separator character.
        :rtype: list
        
c                 S   s   g | ]
}|  r|  qS r(   rW   ).0chunkr(   r(   r)   
<listcomp>  s    z-ConfigHandler._parse_list.<locals>.<listcomp>)
isinstancelist
splitlinessplit)clsrE   	separatorr(   r(   r)   _parse_list  s   



zConfigHandler._parse_listc                 C   sP   d}i }|  |D ]}||\}}}||krtd| | || < q	|S )zPRepresents value as a dict.

        :param value:
        :rtype: dict
        =z(Unable to parse option value to dict: %s)ry   	partitionr   rW   )rw   rE   rx   resultliner9   sepvalr(   r(   r)   _parse_dict  s   zConfigHandler._parse_dictc                 C   s   |  }|dv S )zQRepresents value as boolean.

        :param value:
        :rtype: bool
        )1trueyes)lowerrw   rE   r(   r(   r)   _parse_bool,  s   zConfigHandler._parse_boolc                        fdd}|S )zReturns a parser function to make sure field inputs
        are not files.

        Parses a value after getting the key so error messages are
        more informative.

        :param key:
        :rtype: callable
        c                    s    d}|  |rtd | S )Nfile:zCOnly strings are accepted for the {0} field, files are not accepted)rU   
ValueErrorr:   )rE   Zexclude_directiver9   r(   r)   rj   B  s   
z3ConfigHandler._exclude_files_parser.<locals>.parserr(   )rw   r9   rj   r(   r   r)   _exclude_files_parser6  s   	z#ConfigHandler._exclude_files_parserroot_dirc                 C   sP   d}t |ts	|S ||s|S |t|d }dd |dD }t||S )aO  Represents value as a string, allowing including text
        from nearest files using `file:` directive.

        Directive is sandboxed and won't reach anything outside
        directory with setup.py.

        Examples:
            file: README.rst, CHANGELOG.md, src/file.txt

        :param str value:
        :rtype: str
        r   Nc                 s   s    | ]}|  V  qd S Nro   )rp   r0   r(   r(   r)   	<genexpr>d  s    z,ConfigHandler._parse_file.<locals>.<genexpr>rm   )rs   r   rU   lenrv   r   Z
read_files)rw   rE   r   Zinclude_directivespecZ	filepathsr(   r(   r)   _parse_fileM  s   

zConfigHandler._parse_filec                 C   s:   d}| |s	|S ||d}|| jj t|||S )zRepresents value as a module attribute.

        Examples:
            attr: package.attr
            attr: package.module.attr

        :param str value:
        :rtype: str
        zattr:rR   )rU   rV   updaterN   rK   r   Z	read_attr)rY   rE   rK   r   Zattr_directiveZ	attr_descr(   r(   r)   _parse_attrg  s   

zConfigHandler._parse_attrc                    r   )zReturns parser function to represents value as a list.

        Parses a value applying given methods one after another.

        :param parse_methods:
        :rtype: callable
        c                    s   | } D ]}||}q|S r   r(   )rE   parsedmethodparse_methodsr(   r)   rJ     s   
z1ConfigHandler._get_parser_compound.<locals>.parser(   )rw   r   rJ   r(   r   r)   _get_parser_compound{  s   
z"ConfigHandler._get_parser_compoundNc                 C   s6   i }|pdd }|  D ]\}\}}||||< q|S )zParses section options into a dictionary.

        Optionally applies a given parser to values.

        :param dict section_options:
        :param callable values_parser:
        :rtype: dict
        c                 S   s   | S r   r(   )r   r(   r(   r)   <lambda>  s    z6ConfigHandler._parse_section_to_dict.<locals>.<lambda>)rT   )rw   r[   Zvalues_parserrE   r9   _r   r(   r(   r)   _parse_section_to_dict  s
   
z$ConfigHandler._parse_section_to_dictc              	   C   s8   |  D ]\}\}}z|| |< W q ty   Y qw dS )zQParses configuration file section.

        :param dict section_options:
        N)rT   rd   )rY   r[   namer   rE   r(   r(   r)   parse_section  s   zConfigHandler.parse_sectionc                 C   sb   | j  D ])\}}d}|rd| }t| d| ddd}|du r*td| j|f || qdS )zTParses configuration file items from one
        or more related sections.

        rR   z_%szparse_section%srS   __Nz0Unsupported distribution option section: [%s.%s])rX   rT   r=   rV   r   rB   )rY   rZ   r[   Zmethod_postfixZsection_parser_methodr(   r(   r)   rJ     s"   
zConfigHandler.parsec                    s   t   fdd}|S )zthis function will wrap around parameters that are deprecated

        :param msg: deprecation message
        :param warning_class: class of warning exception to be raised
        :param func: function to be wrapped around
        c                     s   t   | i |S r   )warningswarn)argskwargsfuncmsgwarning_classr(   r)   config_handler  s   z@ConfigHandler._deprecated_config_handler.<locals>.config_handlerr   )rY   r   r   r   r   r(   r   r)   _deprecated_config_handler  s   z(ConfigHandler._deprecated_config_handler)rm   r   )r`   
__module____qualname____doc__r   __annotations__rQ   r	   r   AllCommandOptionsr   rI   r\   propertyra   rl   classmethodry   r   r   r   _Pathr   r   r   r   r   rJ   r   r(   r(   r(   r)   r-      sD   
 

&

	

r-   c                       sp   e Zd ZdZdddddZdZ	 dejfd	d
dede	de
jdee def fddZedd Zdd Z  ZS )rG   rL   urldescriptionclassifiers	platforms)Z	home_pagesummaryZ
classifierplatformFNr8   r   rO   r$   rN   rK   r   c                    s"   t  |||| || _|| _d S r   )superr\   rK   r   )rY   r8   rO   r$   rN   rK   r   r_   r(   r)   r\     s   	
zConfigMetadataHandler.__init__c                 C   sh   | j }t| j| jd}| j}| j}|||| |dt|| |||d| |ddt|||| j	|dS )r]   r   z[The requires parameter is deprecated, please use install_requires for runtime dependencies.licenselicense_filezDThe license_file parameter is deprecated, use license_files instead.)r   keywordsZprovidesrequiresZ	obsoletesr   r   r   Zlicense_filesr   Zlong_descriptionversionZproject_urls)
ry   r   r   r   r   r   r   r   r   _parse_version)rY   
parse_list
parse_file
parse_dictZexclude_files_parserr(   r(   r)   ra     s4   
zConfigMetadataHandler.parsersc                 C   sn   |  || j}||kr+| }zt| W |S  ty*   d}t|jdi t w t	| 
|| j| jS )zSParses `version` option value.

        :param value:
        :rtype: str

        zCVersion loaded from {value} does not comply with PEP 440: {version}Nr(   )r   r   rW   r   r   r   r:   r;   r   r   r   rK   )rY   rE   r   tmplr(   r(   r)   r     s   
	z$ConfigMetadataHandler._parse_version)r`   r   r   rB   rQ   Zstrict_moder/   curdirr   boolr   rI   r   r@   r   r\   r   ra   r   __classcell__r(   r(   r   r)   rG     s6    
!rG   r   c                       s   e Zd ZdZdddededejf fddZe	dd	 Z
d
d Zdd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Z  ZS )"rH   rO   r8   r   r$   rN   c                    s$   t  |||| |j| _i | _d S r   )r   r\   rM   r   rK   )rY   r8   rO   r$   rN   r   r(   r)   r\   4  s   
zConfigOptionsHandler.__init__c                 C   s   | j |ddS )N;)rx   )ry   r   r(   r(   r)   _parse_list_semicolon?  s   z*ConfigOptionsHandler._parse_list_semicolonc                 C   s   | j || jdS )Nr   )r   r   )rY   rE   r(   r(   r)   _parse_file_in_rootC  s   z(ConfigOptionsHandler._parse_file_in_rootc                 C   s   |  | |}dd |D S )Nc                 S   s   g | ]	}| d s|qS )#)rU   )rp   r}   r(   r(   r)   rr   K  s    zAConfigOptionsHandler._parse_requirements_list.<locals>.<listcomp>)r   r   )rY   rE   r   r(   r(   r)   _parse_requirements_listF  s   z-ConfigOptionsHandler._parse_requirements_listc                 C   sP   | j }| j}| j}| j}||||||| |dt| j| j| j| j| j	|t
|dS )r]   zeThe namespace_packages parameter is deprecated, consider using implicit namespaces instead (PEP 420).)Zzip_safeZinclude_package_datarK   scriptsZeager_resourcesZdependency_linksnamespace_packagesZinstall_requiresZsetup_requiresZtests_requirepackagesentry_pointsZ
py_modulesZpython_requirescmdclass)ry   r   r   _parse_cmdclassr   r   r   r   _parse_packagesr   r   )rY   r   Z
parse_boolr   Zparse_cmdclassr(   r(   r)   ra   M  s0   zConfigOptionsHandler.parsersc                 C   s   | j j}t| ||| jS r   )rN   rK   r   r   r   r   )rY   rE   rK   r(   r(   r)   r   l  s   z$ConfigOptionsHandler._parse_cmdclassc                 C   sb   ddg}|  }||vr| |S | | jdi }|j||d k| j| jd tj	di |S )zTParses `packages` option value.

        :param value:
        :rtype: list
        zfind:zfind_namespace:zpackages.findr   )
namespacesr   Zfill_package_dirNr(   )
rW   ry   parse_section_packages__findrX   rc   r   r   rK   r   Zfind_packages)rY   rE   Zfind_directivesZtrimmed_valuefind_kwargsr(   r(   r)   r   p  s   

z$ConfigOptionsHandler._parse_packagesc                    sR   |  || j}g d t fdd| D }|d}|dur'|d |d< |S )zParses `packages.find` configuration file section.

        To be used in conjunction with _parse_packages().

        :param dict section_options:
        )whereincludeexcludec                    s$   g | ]\}}| v r|r||fqS r(   r(   )rp   kvZ
valid_keysr(   r)   rr     s   $ zEConfigOptionsHandler.parse_section_packages__find.<locals>.<listcomp>r   Nr   )r   ry   r@   rT   rc   )rY   r[   Zsection_datar   r   r(   r   r)   r     s   
z1ConfigOptionsHandler.parse_section_packages__findc                 C      |  || j}|| d< dS )z`Parses `entry_points` configuration file section.

        :param dict section_options:
        r   N)r   ry   rY   r[   r   r(   r(   r)   parse_section_entry_points  s   z/ConfigOptionsHandler.parse_section_entry_pointsc                 C   s   |  || j}t|S r   )r   ry   r   Zcanonic_package_data)rY   r[   package_datar(   r(   r)   _parse_package_data  s   
z(ConfigOptionsHandler._parse_package_datac                 C      |  || d< dS )z`Parses `package_data` configuration file section.

        :param dict section_options:
        r   Nr   rY   r[   r(   r(   r)   parse_section_package_data     z/ConfigOptionsHandler.parse_section_package_datac                 C   r   )zhParses `exclude_package_data` configuration file section.

        :param dict section_options:
        Zexclude_package_dataNr   r   r(   r(   r)   "parse_section_exclude_package_data  r   z7ConfigOptionsHandler.parse_section_exclude_package_datac                 C   r   )zbParses `extras_require` configuration file section.

        :param dict section_options:
        Zextras_requireN)r   r   r   r(   r(   r)   parse_section_extras_require  s
   z1ConfigOptionsHandler.parse_section_extras_requirec                 C   s$   |  || j}t|| j| d< dS )z^Parses `data_files` configuration file section.

        :param dict section_options:
        Z
data_filesN)r   ry   r   Zcanonic_data_filesr   r   r(   r(   r)   parse_section_data_files  s   z-ConfigOptionsHandler.parse_section_data_files)r`   r   r   rB   r   r   r   rI   r\   r   r   r   r   r   ra   r   r   r   r   r   r   r   r   r   r   r(   r(   r   r)   rH   0  s4    

rH   )FF)r(   F)F)5r   r/   r   r<   collectionsr   r   r   typingr   r   r   r	   r
   r   r   r   r   r   r   Zdistutils.errorsr   r   Z#setuptools.extern.packaging.versionr   r   Z&setuptools.extern.packaging.specifiersr   Zsetuptools._deprecation_warningr   rR   r   r!   r   distutils.distr   r   PathLiker   ZSingleCommandOptionsr   r   r@   r*   r+   r   r"   r?   r#   r6   r-   rG   rH   r(   r(   r(   r)   <module>   sr    4


+  &[