U
    -cnb                     @   s  d Z ddl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mZ dd
lmZ ddl m!Z! ddl"m#Z# e
rd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-d5e*e.dddZ/de*ddddZ0d6de*ee* e1ed ddd Z2e-e(d!d"d#Z3ed e.d$d%d&Z4d7de,ed' d(d)d*Z5e(e(e6d+d,d-Z7G d.d/ d/ee- Z8G d0d1 d1e8d2 Z9G d3d4 d4e8d Z:d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)RequirementInvalidRequirement)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   find_config_files_applyconfiguration_to_dict)r!   Zfind_othersignore_option_errorsr   dist	filenameshandlers r+   >/tmp/pip-unpacked-wheel-izj_87as/setuptools/config/setupcfg.pyread_configuration(   s
    r-   r   )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_configurationD   s    
r.   r+   )ConfigHandler.)r(   r!   other_filesr'   r"   c              	   C   s   ddl m} tj|}tj|s0td| t }ttj	| ||f}z*|j
| |d t| | j|d}|   W 5 t| X |S )zHRead configuration from ``filepath`` and applies to the ``dist`` object.r   )_Distributionz%Configuration file %s does not exist.)r)   )r'   )r#   r1   ospathabspathisfiler   getcwdchdirdirnameparse_config_filesparse_configurationcommand_optionsZ_finalize_license_files)r(   r!   r0   r'   r1   Zcurrent_directoryr)   r*   r+   r+   r,   r%   M   s"    
  r%   )
target_objkeyc                 C   s.   dj f 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})formatlocals	functoolsr   getattr)r<   r=   Zgetter_nameZby_attributegetterr+   r+   r,   _get_optionj   s    rC   )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_optionsrC   r<   section_prefix)r*   Zconfig_dicthandleroptionvaluer+   r+   r,   r&   v   s    
r&   )ConfigMetadataHandlerConfigOptionsHandler)distributionr;   r"   c              	   C   sb   t | J}t| |||}|  | js0|j| _t| j|||| j| j}|  W 5 Q R X ||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
    )r   EnsurePackagesDiscoveredrK   parsepackage_dirrJ   metadatasrc_root)rL   r;   r'   ensure_discoveredoptionsmetar+   r+   r,   r:      s(    r:   )label
orig_valueparsedc              	   C   sp   d|kst |dkrdS ttD d|}t|}|jdk	rbd|  d|d  d}t|t	 W 5 Q R X dS )	am  Because users sometimes misinterpret this configuration:

    [options.extras_require]
    foo = bar;python_version<"4"

    It looks like one requirement with an environment marker
    but because there is no newline, it's parsed as two requirements
    with a semicolon as separator.

    Therefore, if:
        * input string does not contain a newline AND
        * parsed result contains two requirements AND
        * parsing of the two parts from the result ("<first>;<second>")
        leads in a valid Requirement with a valid marker
    a UserWarning is shown to inform the user about the possible problem.
    
   N;z#One of the parsed requirements in `z*` looks like a valid environment marker: 'r   z}'
Make sure that the config is correct and check https://setuptools.pypa.io/en/latest/userguide/declarative_config.html#opt-2)
len
contextlibsuppressr   joinr   markerwarningswarnUserWarning)rU   rV   rW   Zoriginal_requirements_strreqmsgr+   r+   r,   %_warn_accidental_env_marker_misconfig   s    

re   c                   @   s   e Zd ZU dZeed< i Zeeef ed< ee	e
jd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edddZedddZe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.rF   aliases)r<   rS   rR   c           	      C   sd   i }| j }| D ].\}}||s&q||dd}|||< q|| _|| _|| _g | _|| _	d S )N .)
rF   items
startswithreplacestripr'   r<   sectionsrE   rR   )	selfr<   rS   r'   rR   rm   rF   section_namesection_optionsr+   r+   r,   __init__   s    

zConfigHandler.__init__c                 C   s   t d| jj dS ).Metadata item name to parser function mapping.z!%s must provide .parsers propertyN)NotImplementedError	__class____name__rn   r+   r+   r,   parsers   s    
zConfigHandler.parsersc           	      C   s   t  }| j}| j||}t|||}||kr6t||r>d S d}| j|}|rz||}W n  tk
r~   d}| jsz Y nX |rd S t|d| d }|d krt	||| n|| | j
| d S )NFTzset_%s)tupler<   rf   getrA   KeyErrorrw   	Exceptionr'   setattrrE   append)	rn   Zoption_namerI   unknownr<   Zcurrent_valueZskip_optionparsersetterr+   r+   r,   __setitem__  s0    zConfigHandler.__setitem__,c                 C   s8   t |tr|S d|k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
        rX   c                 S   s   g | ]}|  r|  qS r+   rl   ).0chunkr+   r+   r,   
<listcomp>:  s      z-ConfigHandler._parse_list.<locals>.<listcomp>)
isinstancelist
splitlinessplit)clsrI   	separatorr+   r+   r,   _parse_list(  s    

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

        :param value:
        :rtype: dict
        =z(Unable to parse option value to dict: %s)r   	partitionr   rl   )r   rI   r   resultliner=   sepvalr+   r+   r,   _parse_dict<  s    zConfigHandler._parse_dictc                 C   s   |  }|dkS )zQRepresents value as boolean.

        :param value:
        :rtype: bool
        )1trueyes)lowerr   rI   r+   r+   r,   _parse_boolO  s    zConfigHandler._parse_boolc                    s    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)rj   
ValueErrorr>   )rI   Zexclude_directiver=   r+   r,   r   e  s    
z3ConfigHandler._exclude_files_parser.<locals>.parserr+   )r   r=   r   r+   r   r,   _exclude_files_parserY  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 Nr   )r   r3   r+   r+   r,   	<genexpr>  s     z,ConfigHandler._parse_file.<locals>.<genexpr>r   )r   r   rj   r[   r   r   Z
read_files)r   rI   r   Zinclude_directivespecZ	filepathsr+   r+   r,   _parse_filep  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:rg   )rj   rk   updaterR   rO   r   Z	read_attr)rn   rI   rO   r   Zattr_directiveZ	attr_descr+   r+   r,   _parse_attr  s    

zConfigHandler._parse_attrc                    s    fdd}|S )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+   )rI   rW   methodparse_methodsr+   r,   rN     s    
z1ConfigHandler._get_parser_compound.<locals>.parser+   )r   r   rN   r+   r   r,   _get_parser_compound  s    
z"ConfigHandler._get_parser_compoundc                 C   s,   i }|  D ]\}\}}|||||< q|S )a  Parses section options into a dictionary.

        Applies a given parser to each option in a section.

        :param dict section_options:
        :param callable values_parser: function with 2 args corresponding to key, value
        :rtype: dict
        )ri   )r   rp   values_parserrI   r=   _r   r+   r+   r,   _parse_section_to_dict_with_key  s    
z-ConfigHandler._parse_section_to_dict_with_keyNc                    s$    r fddndd }|  ||S )a   Parses section options into a dictionary.

        Optionally applies a given parser to each value.

        :param dict section_options:
        :param callable values_parser: function with 1 arg corresponding to option value
        :rtype: dict
        c                    s    |S r   r+   r   vr   r+   r,   <lambda>      z6ConfigHandler._parse_section_to_dict.<locals>.<lambda>c                 S   s   |S r   r+   r   r+   r+   r,   r     r   r   )r   rp   r   r   r+   r   r,   _parse_section_to_dict  s    
z$ConfigHandler._parse_section_to_dictc              
   C   s8   |  D ]*\}\}}tt || |< W 5 Q R X qdS )zQParses configuration file section.

        :param dict section_options:
        N)ri   r\   r]   rz   )rn   rp   namer   rI   r+   r+   r,   parse_section  s    zConfigHandler.parse_sectionc                 C   sb   | j  D ]R\}}d}|r"d| }t| d| ddd}|dkrTtd| j|f || q
dS )zTParses configuration file items from one
        or more related sections.

        rg   z_%szparse_section%srh   __Nz0Unsupported distribution option section: [%s.%s])rm   ri   rA   rk   r   rF   )rn   ro   rp   Zmethod_postfixZsection_parser_methodr+   r+   r,   rN     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   | |S r   )r`   ra   )argskwargsfuncrd   warning_classr+   r,   config_handler  s    z@ConfigHandler._deprecated_config_handler.<locals>.config_handlerr   )rn   r   rd   r   r   r+   r   r,   _deprecated_config_handler  s    z(ConfigHandler._deprecated_config_handler)r   )N)ru   
__module____qualname____doc__r   __annotations__rf   r	   r   AllCommandOptionsr   rM   rq   propertyrw   r   classmethodr   r   r   r   _Pathr   r   r   r   r   r   rN   r   r+   r+   r+   r,   r/      s<   
	
&

	



r/   c                       sd   e Zd ZdZdddddZdZdejfd	ee	e
jee ed
 fddZedd Zdd Z  ZS )rJ   rP   urldescriptionclassifiers	platforms)Z	home_pagesummary
classifierplatformFNr   )r<   rS   r'   rR   rO   r   c                    s"   t  |||| || _|| _d S r   )superrq   rO   r   )rn   r<   rS   r'   rR   rO   r   rt   r+   r,   rq     s    	zConfigMetadataHandler.__init__c                 C   sh   | j }t| j| jd}| j}| j}|||| |dt|| |||d| |ddt|||| j	|dS )rr   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   keywordsprovidesrequires	obsoletesr   r   r   Zlicense_filesr   long_descriptionversionproject_urls)
r   r   r   r   r   r   r   r   r   _parse_version)rn   
parse_listZ
parse_file
parse_dictZexclude_files_parserr+   r+   r,   rw      s4    
zConfigMetadataHandler.parsersc                 C   sr   |  || j}||krZ| }zt| W n* tk
rT   d}t|jf t Y nX |S t	| 
|| j| jS )zSParses `version` option value.

        :param value:
        :rtype: str

        zCVersion loaded from {value} does not comply with PEP 440: {version})r   r   rl   r   r   r   r>   r?   r   r   r   rO   )rn   rI   r   tmplr+   r+   r,   r   B  s    z$ConfigMetadataHandler._parse_version)ru   r   r   rF   rf   Zstrict_moder2   curdirr   boolr   rM   r   rD   r   rq   r   rw   r   __classcell__r+   r+   r   r,   rJ     s(   
!rJ   r   c                       s   e Zd ZdZdeeejd fddZe	dd Z
dd	 Zeed
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 )!rK   rS   r   )r<   rS   r'   rR   c                    s$   t  |||| |j| _i | _d S r   )r   rq   rQ   r   rO   )rn   r<   rS   r'   rR   r   r+   r,   rq   a  s    zConfigOptionsHandler.__init__c                 C   s   | j |ddS )NrZ   )r   )r   r   r+   r+   r,   _parse_list_semicolonl  s    z*ConfigOptionsHandler._parse_list_semicolonc                 C   s   | j || jdS )Nr   )r   r   )rn   rI   r+   r+   r,   _parse_file_in_rootp  s    z(ConfigOptionsHandler._parse_file_in_root)rU   rI   c                 C   s*   |  | |}t||| dd |D S )Nc                 S   s   g | ]}| d s|qS )#)rj   )r   r   r+   r+   r,   r   y  s     
 zAConfigOptionsHandler._parse_requirements_list.<locals>.<listcomp>)r   r   re   )rn   rU   rI   rW   r+   r+   r,   _parse_requirements_lists  s    z-ConfigOptionsHandler._parse_requirements_listc                 C   sV   | j }| j}| j}| j}||||||| |dtt| jd| j| j| j	| j
|t|dS )rr   zeThe namespace_packages parameter is deprecated, consider using implicit namespaces instead (PEP 420).install_requires)Zzip_safeZinclude_package_datarO   scriptsZeager_resourcesZdependency_linksZnamespace_packagesr   Zsetup_requiresZtests_requirepackagesentry_points
py_modulesZpython_requirescmdclass)r   r   r   _parse_cmdclassr   r   r   r   r   _parse_packagesr   r   )rn   r   Z
parse_boolr   Zparse_cmdclassr+   r+   r,   rw   {  s6     zConfigOptionsHandler.parsersc                 C   s   | j j}t| ||| jS r   )rR   rO   r   r   r   r   )rn   rI   rO   r+   r+   r,   r     s    z$ConfigOptionsHandler._parse_cmdclassc                 C   s^   ddg}|  }||kr"| |S | | jdi }|j||d k| j| jd tj	f |S )zTParses `packages` option value.

        :param value:
        :rtype: list
        zfind:zfind_namespace:zpackages.findr   )
namespacesr   Zfill_package_dir)
rl   r   parse_section_packages__findrm   ry   r   r   rO   r   Zfind_packages)rn   rI   Zfind_directivesZtrimmed_valuefind_kwargsr+   r+   r,   r     s    

z$ConfigOptionsHandler._parse_packagesc                    sT   |  || j}dddg t fdd| D }|d}|dk	rP|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 | ]\}}| kr|r||fqS r+   r+   )r   kr   Z
valid_keysr+   r,   r     s       zEConfigOptionsHandler.parse_section_packages__find.<locals>.<listcomp>Nr   )r   r   rD   ri   ry   )rn   rp   Zsection_datar   r   r+   r   r,   r     s    

z1ConfigOptionsHandler.parse_section_packages__findc                 C   s   |  || j}|| d< dS )z`Parses `entry_points` configuration file section.

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

        :param dict section_options:
        r   Nr   rn   rp   r+   r+   r,   parse_section_package_data  s    z/ConfigOptionsHandler.parse_section_package_datac                 C   s   |  || d< dS )zhParses `exclude_package_data` configuration file section.

        :param dict section_options:
        Zexclude_package_dataNr   r   r+   r+   r,   "parse_section_exclude_package_data  s    z7ConfigOptionsHandler.parse_section_exclude_package_datac                    s      | fdd}| d< dS )zbParses `extras_require` configuration file section.

        :param dict section_options:
        c                    s     d|  d|S )Nzextras_require[])r   )r   r   rv   r+   r,   r     r   zCConfigOptionsHandler.parse_section_extras_require.<locals>.<lambda>Zextras_requireNr   r   r+   rv   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:
        
data_filesN)r   r   r   Zcanonic_data_filesr   r   r+   r+   r,   parse_section_data_files  s    z-ConfigOptionsHandler.parse_section_data_files)ru   r   r   rF   r   r   r   rM   rq   r   r   r   r   r   r   rw   r   r   r   r   r   r   r   r   r   r   r+   r+   r   r,   rK   ]  s*   

 rK   )FF)r+   F)F);r   r2   r\   r@   r`   collectionsr   r   r   typingr   r   r   r	   r
   r   r   r   r   r   r   distutils.errorsr   r   Z(setuptools.extern.packaging.requirementsr   r   Z#setuptools.extern.packaging.versionr   r   Z&setuptools.extern.packaging.specifiersr   Zsetuptools._deprecation_warningr   rg   r   r#   r   distutils.distr   r   PathLiker   ZSingleCommandOptionsr   r   rD   r-   r.   r   r%   rC   r&   r:   r   re   r/   rJ   rK   r+   r+   r+   r,   <module>   sf   4      +!  0[