a
    `S                     @   s  d dl Z d dlZd dl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Zd dlmZmZ d dlmZmZ d dlmZ G dd	 d	Zejd
d ZdddZdd Zdd ZdddZG dd dZG dd deZG dd deZdS )    N)defaultdict)partialwraps)DistutilsOptionErrorDistutilsFileError)LegacyVersionparse)SpecifierSetc                   @   s    e Zd ZdZdd Zdd ZdS )StaticModulez0
    Attempt to load the module by the name
    c                 C   s`   t j|}t|j}| }W d    n1 s40    Y  t|}t| 	t
  | `d S N)	importlibutil	find_specopenoriginreadastr	   varsupdatelocalsself)r   namespecZstrmsrcmodule r   3/usr/lib/python3/dist-packages/setuptools/config.py__init__   s    &
zStaticModule.__init__c              
      s^   zt  fdd| jjD W S  tyX } z$tdjf i t |W Y d }~n
d }~0 0 d S )Nc                 3   sH   | ]@}t |tjr|jD ](}t |tjr|j krt|jV  qqd S r   )
isinstancer   ZAssigntargetsNameidZliteral_evalvalue).0Z	statementtargetattrr   r   	<genexpr>!   s
   
z+StaticModule.__getattr__.<locals>.<genexpr>z#{self.name} has no attribute {attr})nextr   body	ExceptionAttributeErrorformatr   )r   r'   er   r&   r   __getattr__   s    
zStaticModule.__getattr__N)__name__
__module____qualname____doc__r   r/   r   r   r   r   r      s   r   c              	   c   s8   z$t jd|  dV  W t j|  nt j|  0 dS )zH
    Add path to front of sys.path for the duration of the context.
    r   N)syspathinsertremove)r5   r   r   r   
patch_path.   s    r8   Fc           	   	   C   s   ddl m}m} tj| } tj| s4td|  t }t	tj
|  zT| }|rb| ng }| |vrx||  |j||d t||j|d}W t	| nt	| 0 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   )Distribution_Distributionz%Configuration file %s does not exist.)	filenames)ignore_option_errors)Zsetuptools.distr9   r:   osr5   abspathisfiler   getcwdchdirdirnameZfind_config_filesappendZparse_config_filesparse_configurationcommand_optionsconfiguration_to_dict)	filepathZfind_othersr<   r9   r:   Zcurrent_directoryZdistr;   handlersr   r   r   read_configuration:   s(    
rI   c                 C   s2   dj f 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})r-   r   	functoolsr   getattr)
target_objkeyZgetter_nameZby_attributegetterr   r   r   _get_optioni   s    rO   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_optionsrO   rL   section_prefix)rH   Zconfig_dictZhandlerZoptionr#   r   r   r   rF   u   s    
rF   c                 C   s6   t | ||}|  t| j||| j}|  ||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
    )ConfigOptionsHandlerr	   ConfigMetadataHandlermetadatapackage_dir)ZdistributionrE   r<   optionsmetar   r   r   rD      s    rD   c                   @   s   e Zd ZdZdZi Z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
dd Zedd Zedd Ze
d'ddZe
dd Ze
d(ddZdd  Zd!d" Zd#d$ ZdS ))ConfigHandlerz1Handles metadata supplied in configuration files.NFc                 C   s^   i }| j }| D ].\}}||s&q||dd}|||< q|| _|| _|| _g | _d S )N .)	rR   items
startswithreplacestripr<   rL   sectionsrQ   )r   rL   rW   r<   r`   rR   section_namesection_optionsr   r   r   r      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__r0   )r   r   r   r   parsers   s    
zConfigHandler.parsersc           	      C   s   t  }| j}| j||}t|||}||u r6t||r>d S d}| j|}|r~z||}W n ty|   d}| jsx Y n0 |rd S t|d| d }|d u rt	||| n|| | j
| d S )NFTzset_%s)tuplerL   aliasesgetrK   KeyErrorrf   r+   r<   setattrrQ   rC   )	r   Zoption_namer#   unknownrL   Zcurrent_valueZskip_optionparsersetterr   r   r   __setitem__   s0    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   )r_   )r$   chunkr   r   r   
<listcomp>       z-ConfigHandler._parse_list.<locals>.<listcomp>)r   list
splitlinessplit)clsr#   	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)rz   	partitionr   r_   )rx   r#   ry   resultlinerM   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trueZyes)lower)rx   r#   r   r   r   _parse_bool  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)r]   
ValueErrorr-   )r#   Zexclude_directiverM   r   r   rm   )  s    
z3ConfigHandler._exclude_files_parser.<locals>.parserr   )rx   rM   rm   r   r   r   _exclude_files_parser  s    z#ConfigHandler._exclude_files_parserc                    s\   d}t |ts|S ||s |S |t|d }dd |dD }d fdd|D 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   | ]}t j| V  qd S r   )r=   r5   r>   r_   r$   r5   r   r   r   r(   I  rt   z,ConfigHandler._parse_file.<locals>.<genexpr>rp   rq   c                 3   s.   | ]&}  |stj|r |V  qdS )TN)_assert_localr=   r5   r?   
_read_filer   rx   r   r   r(   J  s   
)r   strr]   lenrw   join)rx   r#   Zinclude_directiver   Z	filepathsr   r   r   _parse_file2  s    

zConfigHandler._parse_filec                 C   s   |  t std|  d S )Nz#`file:` directive can not access %s)r]   r=   r@   r   )rG   r   r   r   r   Q  s    zConfigHandler._assert_localc                 C   s:   t j| dd}| W  d    S 1 s,0    Y  d S )Nzutf-8)encoding)ior   r   )rG   fr   r   r   r   W  s    zConfigHandler._read_filec              	   C   s4  d}| |s|S ||d d}| }d|}|p@d}t }|r|d |v r||d  }|dd}	t	|	dkrtj
t |	d }|	d }q|}nd|v rtj
t |d }t|N ztt||W W  d   S  ty   t|}
Y n0 W d   n1 s 0    Y  t|
|S )	zRepresents value as a module attribute.

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

        :param str value:
        :rtype: str
        zattr:rZ   r[   r   r   /   N)r]   r^   r_   rw   popr   r=   r@   rsplitr   r5   r8   rK   r   r+   r   import_module)rx   r#   rV   Zattr_directiveZ
attrs_pathZ	attr_nameZmodule_nameparent_pathZcustom_pathpartsr   r   r   r   _parse_attr\  s0    



0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   )r#   parsedmethodparse_methodsr   r   r	     s    
z1ConfigHandler._get_parser_compound.<locals>.parser   )rx   r   r	   r   r   r   _get_parser_compound  s    	z"ConfigHandler._get_parser_compoundc                 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>  rt   z6ConfigHandler._parse_section_to_dict.<locals>.<lambda>)r\   )rx   rb   Zvalues_parserr#   rM   _r   r   r   r   _parse_section_to_dict  s
    
z$ConfigHandler._parse_section_to_dictc              	   C   s:   |  D ],\}\}}z|| |< W q ty2   Y q0 qdS )zQParses configuration file section.

        :param dict section_options:
        N)r\   rj   )r   rb   r   r   r#   r   r   r   parse_section  s
    zConfigHandler.parse_sectionc                 C   sb   | j  D ]R\}}d}|r"d| }t| d| ddd}|du rTtd| j|f || q
dS )zTParses configuration file items from one
        or more related sections.

        rZ   z_%szparse_section%sr[   __Nz0Unsupported distribution option section: [%s.%s])r`   r\   rK   r^   r   rR   )r   ra   rb   Zmethod_postfixZsection_parser_methodr   r   r   r	     s     zConfigHandler.parsec                    s   t   fdd}|S )z this 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   )r   r   r   r   r   r   r   r   _deprecated_config_handler  s    z(ConfigHandler._deprecated_config_handler)F)rp   )N)N)r0   r1   r2   r3   rR   rh   r   propertyrf   ro   classmethodrz   r   r   r   r   staticmethodr   r   r   r   r   r   r	   r   r   r   r   r   rY      s<   

&

	



-
rY   c                       sH   e Zd ZdZdddddZdZd fd	d
	Zedd Zdd Z	  Z
S )rT   rU   Zurldescriptionclassifiers	platforms)Z	home_pageZsummaryZ
classifierplatformFNc                    s   t t| ||| || _d S r   )superrT   r   rV   )r   rL   rW   r<   rV   re   r   r   r     s    zConfigMetadataHandler.__init__c                 C   sN   | j }| j}| j}| j}|||| |dt|| |||d|||| j|dS )rc   z[The requires parameter is deprecated, please use install_requires for runtime dependencies.license)r   keywordsZprovidesZrequiresZ	obsoletesr   r   Zlicense_filesr   Zlong_descriptionversionZproject_urls)rz   r   r   r   r   DeprecationWarningr   _parse_version)r   
parse_listZ
parse_file
parse_dictZexclude_files_parserr   r   r   rf     s*    
zConfigMetadataHandler.parsersc                 C   s   |  |}||krF| }tt|trBd}t|jf i t |S | || j	}t
|rb| }t|tst|drdtt|}nd| }|S )zSParses `version` option value.

        :param value:
        :rtype: str

        zCVersion loaded from {value} does not comply with PEP 440: {version}__iter__r[   z%s)r   r_   r   r	   r   r   r-   r   r   rV   callabler   hasattrr   map)r   r#   r   Ztmplr   r   r   r     s     


z$ConfigMetadataHandler._parse_version)FN)r0   r1   r2   rR   rh   Zstrict_moder   r   rf   r   __classcell__r   r   r   r   rT     s     
rT   c                   @   s\   e Zd Z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S )rS   rW   c                 C   sN   | j }t| j dd}| j}| j}||||||||||||||| j| j|tdS )rc   ;ry   )Zzip_safeZuse_2to3Zinclude_package_datarV   Zuse_2to3_fixersZuse_2to3_exclude_fixersZconvert_2to3_doctestsZscriptsZeager_resourcesZdependency_linksZnamespace_packagesZinstall_requiresZsetup_requiresZtests_requireZpackagesentry_pointsZ
py_modulesZpython_requires)rz   r   r   r   _parse_packagesr   r
   )r   r   Zparse_list_semicolonZ
parse_boolr   r   r   r   rf   :  s.    zConfigOptionsHandler.parsersc                 C   sn   ddg}|  }||vr"| |S ||d k}| | jdi }|rTddlm} nddlm} |f i |S )zTParses `packages` option value.

        :param value:
        :rtype: list
        zfind:zfind_namespace:r   zpackages.findr   )find_namespace_packages)find_packages)r_   rz   parse_section_packages__findr`   ri   Z
setuptoolsr   r   )r   r#   Zfind_directivesZtrimmed_valueZfindnsfind_kwargsr   r   r   r   r   W  s    
z$ConfigOptionsHandler._parse_packagesc                    sR   |  || j}g d t fdd| D }|d}|durN|d |d< |S )zParses `packages.find` configuration file section.

        To be used in conjunction with _parse_packages().

        :param dict section_options:
        )whereZincludeZexcludec                    s$   g | ]\}}| v r|r||fqS r   r   r$   kvZ
valid_keysr   r   rs   }  rt   zEConfigOptionsHandler.parse_section_packages__find.<locals>.<listcomp>r   Nr   )r   rz   rP   r\   ri   )r   rb   Zsection_datar   r   r   r   r   r   p  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   rz   r   rb   r   r   r   r   parse_section_entry_points  s    z/ConfigOptionsHandler.parse_section_entry_pointsc                 C   s.   |  || j}|d}|r*||d< |d= |S )N*rZ   )r   rz   ri   )r   rb   r   rootr   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:
        Zpackage_dataNr   r   rb   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                 C   s"   t | jdd}| ||| d< dS )zbParses `extras_require` configuration file section.

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

        :param dict section_options:
        c                 S   s   g | ]\}}||fqS r   r   r   r   r   r   rs     rt   zAConfigOptionsHandler.parse_section_data_files.<locals>.<listcomp>Z
data_filesN)r   rz   r\   r   r   r   r   parse_section_data_files  s    z-ConfigOptionsHandler.parse_section_data_filesN)r0   r1   r2   rR   r   rf   r   r   r   r   r   r   r   r   r   r   r   r   rS   6  s   

	rS   )FF)F)r   r   r=   r4   r   rJ   r   collectionsr   r   r   
contextlibZdistutils.errorsr   r   Z#setuptools.extern.packaging.versionr   r	   Z&setuptools.extern.packaging.specifiersr
   r   contextmanagerr8   rI   rO   rF   rD   rY   rT   rS   r   r   r   r   <module>   s6   
 
/ 
  @V