B
    UvgO                 @   s   d dl mZm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mZ d dlmZmZ d dlmZmZ d d	lmZmZ e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 )    )absolute_importunicode_literalsN)defaultdict)partial)wraps)import_module)DistutilsOptionErrorDistutilsFileError)LegacyVersionparse)string_typesPY3Fc       	   	   C   s   ddl m}m} tj| } tj| s4td|  t }t	tj
|  zJ| }|rb| ng }| |krx||  |j||d t||j|d}W dt	| X 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_errorsN)Zsetuptools.distr   r   ospathabspathisfiler	   getcwdchdirdirnamefind_config_filesappendparse_config_filesparse_configurationcommand_optionsconfiguration_to_dict)	filepathZfind_othersr   r   r   Zcurrent_directorydistr   handlers r"   9/tmp/pip-install-r_9ig3yj/setuptools/setuptools/config.pyread_configuration   s$    

r$   c             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)
target_objkeyZgetter_nameZby_attributegetterr"   r"   r#   _get_optionD   s    r,   c             C   sD   t t}x6| D ].}x(|jD ]}t|j|}|||j |< qW qW |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,   r)   section_prefix)r!   Zconfig_dicthandleroptionvaluer"   r"   r#   r   P   s    
r   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)distributionr   r   optionsmetar"   r"   r#   r   b   s    
r   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   sb   i }| j }x:| D ].\}}||s(q||dd}|||< qW || _|| _|| _g | _d S )N .)	r/   items
startswithreplacestripr   r)   sectionsr.   )selfr)   r8   r   rA   r/   section_namesection_optionsr"   r"   r#   __init__   s    
zConfigHandler.__init__c             C   s   t d| jj dS )z.Metadata item name to parser function mapping.z!%s must provide .parsers propertyN)NotImplementedError	__class____name__)rB   r"   r"   r#   parsers   s    zConfigHandler.parsersc       	      C   s   t  }| j}| j||}t|||}||kr6t||r>d S d}| j|}|ry||}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)   aliasesgetr(   KeyErrorrI   	Exceptionr   setattrr.   r   )	rB   Zoption_namer2   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
        
c             S   s   g | ]}|  r|  qS r"   )r@   ).0chunkr"   r"   r#   
<listcomp>   s    z-ConfigHandler._parse_list.<locals>.<listcomp>)
isinstancelist
splitlinessplit)clsr2   	separatorr"   r"   r#   _parse_list   s    

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

        :param value:
        :rtype: dict
        =z(Unable to parse option value to dict: %s)r_   	partitionr   r@   )r]   r2   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]   r2   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 )Nzfile:zCOnly strings are accepted for the {0} field, files are not accepted)r>   
ValueErrorr%   )r2   Zexclude_directive)r*   r"   r#   rQ     s    
z3ConfigHandler._exclude_files_parser.<locals>.parserr"   )r]   r*   rQ   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
        zfile:Nc             s   s   | ]}t j| V  qd S )N)r   r   r   r@   )rV   r   r"   r"   r#   	<genexpr>$  s    z,ConfigHandler._parse_file.<locals>.<genexpr>rT   rU   c             3   s.   | ]&}  |stj|r |V  qdS )TN)_assert_localr   r   r   
_read_file)rV   r   )r]   r"   r#   rn   &  s   
)rY   r   r>   lenr\   join)r]   r2   Zinclude_directivespecZ	filepathsr"   )r]   r#   _parse_file  s    


zConfigHandler._parse_filec             C   s   |  t std|  d S )Nz#`file:` directive can not access %s)r>   r   r   r   )r   r"   r"   r#   ro   ,  s    zConfigHandler._assert_localc          	   C   s"   t j| dd
}| S Q R X d S )Nzutf-8)encoding)ioopenread)r   fr"   r"   r#   rp   2  s    zConfigHandler._read_filec          	   C   s   d}| |s|S ||d d}| }d|}|p@d}t }|r|d |kr||d  }|dd}	t	|	dkrtj
t |	d }|	d }q|}nd|krtj
t |d }tj
d| zt|}
t|
|}W dtj
dd t_
X |S )	zRepresents value as a module attribute.

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

        :param str value:
        :rtype: str
        zattr:r;   r<   rE   r   /   N)r>   r?   r@   r\   poprr   r   r   rsplitrq   r   sysinsertr   r(   )r]   r2   r6   Zattr_directiveZ
attrs_path	attr_namemodule_nameparent_pathZcustom_pathpartsmoduler"   r"   r#   _parse_attr7  s0    


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   | }x D ]}||}q
W |S )Nr"   )r2   parsedmethod)parse_methodsr"   r#   r   m  s    
z1ConfigHandler._get_parser_compound.<locals>.parser"   )r]   r   r   r"   )r   r#   _get_parser_compoundd  s    	z"ConfigHandler._get_parser_compoundc             C   s:   i }|pdd }x$|  D ]\}\}}||||< qW |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 )Nr"   )re   r"   r"   r#   <lambda>      z6ConfigHandler._parse_section_to_dict.<locals>.<lambda>)r=   )r]   rD   Zvalues_parserr2   r*   _re   r"   r"   r#   _parse_section_to_dictw  s
    
z$ConfigHandler._parse_section_to_dictc          	   C   s@   x:|  D ].\}\}}y|| |< W q
 tk
r6   Y q
X q
W dS )zQParses configuration file section.

        :param dict section_options:
        N)r=   rM   )rB   rD   namer   r2   r"   r"   r#   parse_section  s
    zConfigHandler.parse_sectionc             C   sf   x`| j  D ]R\}}d}|r$d| }t| d| ddd}|dkrVtd| j|f || qW dS )zTParses configuration file items from one
        or more related sections.

        r;   z_%szparse_section%sr<   __Nz0Unsupported distribution option section: [%s.%s])rA   r=   r(   r?   r   r/   )rB   rC   rD   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   | |S )N)warningswarn)argskwargs)funcmsgwarning_classr"   r#   config_handler  s    z@ConfigHandler._deprecated_config_handler.<locals>.config_handler)r   )rB   r   r   r   r   r"   )r   r   r   r#   _deprecated_config_handler  s    z(ConfigHandler._deprecated_config_handler)F)rT   )N)N)rH   
__module____qualname____doc__r/   rK   rE   propertyrI   rS   classmethodr_   rf   rk   rm   rt   staticmethodro   rp   r   r   r   r   r   r   r"   r"   r"   r#   r:   }   s,   
&
,r:   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 )r4   r5   urldescriptionclassifiers	platforms)Z	home_pagesummary
classifierplatformFNc                s   t t| ||| || _d S )N)superr4   rE   r6   )rB   r)   r8   r   r6   )rG   r"   r#   rE     s    zConfigMetadataHandler.__init__c             C   sL   | j }| j}| j}| j}|||| |dt|| |||d||| j|dS )z.Metadata item name to parser function mapping.z[The requires parameter is deprecated, please use install_requires for runtime dependencies.license)r   keywordsprovidesrequires	obsoletesr   r   r   long_descriptionversionZproject_urls)r_   rt   rf   rm   r   DeprecationWarningr   _parse_version)rB   
parse_listZ
parse_file
parse_dictZexclude_files_parserr"   r"   r#   rI     s$    
zConfigMetadataHandler.parsersc             C   s   |  |}||krB| }tt|tr>d}t|jf t |S | || j	}t
|r^| }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)rt   r@   rY   r   r
   r   r%   r&   r   r6   callabler   hasattrrr   mapstr)rB   r2   r   tmplr"   r"   r#   r     s    


z$ConfigMetadataHandler._parse_version)FN)rH   r   r   r/   rK   Zstrict_moderE   r   rI   r   __classcell__r"   r"   )rG   r#   r4     s    r4   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 )r3   r8   c             C   sL   | j }t| j dd}| j}| j}||||||||||||||| j| j|dS )z.Metadata item name to parser function mapping.;)r^   )Zzip_safeZuse_2to3Zinclude_package_datar6   Zuse_2to3_fixersZuse_2to3_exclude_fixersZconvert_2to3_doctestsscriptsZeager_resourcesdependency_linksZnamespace_packagesZinstall_requiresZsetup_requiresZtests_requirepackagesentry_points
py_modules)r_   r   rk   rf   _parse_packagesrt   )rB   r   Zparse_list_semicolonZ
parse_boolr   r"   r"   r#   rI     s*    zConfigOptionsHandler.parsersc             C   sz   ddg}|  }||kr"| |S ||d k}|r>ts>td| | jdi }|rdddlm} nddlm	} |f |S )	zTParses `packages` option value.

        :param value:
        :rtype: list
        zfind:zfind_namespace:r{   z8find_namespace: directive is unsupported on Python < 3.3zpackages.findr   )find_namespace_packages)find_packages)
r@   r_   r   r   parse_section_packages__findrA   rL   
setuptoolsr   r   )rB   r2   Zfind_directivesZtrimmed_valueZfindnsfind_kwargsr   r"   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"   )rV   kv)
valid_keysr"   r#   rX   X  s    zEConfigOptionsHandler.parse_section_packages__find.<locals>.<listcomp>Nr   )r   r_   r-   r=   rL   )rB   rD   Zsection_datar   r   r"   )r   r#   r   K  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_   )rB   rD   r   r"   r"   r#   parse_section_entry_points`  s    z/ConfigOptionsHandler.parse_section_entry_pointsc             C   s.   |  || j}|d}|r*||d< |d= |S )N*r;   )r   r_   rL   )rB   rD   r   rootr"   r"   r#   _parse_package_datah  s    
z(ConfigOptionsHandler._parse_package_datac             C   s   |  || d< dS )z`Parses `package_data` configuration file section.

        :param dict section_options:
        package_dataN)r   )rB   rD   r"   r"   r#   parse_section_package_datar  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_dataN)r   )rB   rD   r"   r"   r#   "parse_section_exclude_package_datay  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   r_   r   )rB   rD   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"   )rV   r   r   r"   r"   r#   rX     s    zAConfigOptionsHandler.parse_section_data_files.<locals>.<listcomp>
data_filesN)r   r_   r=   )rB   rD   r   r"   r"   r#   parse_section_data_files  s    z-ConfigOptionsHandler.parse_section_data_filesN)rH   r   r   r/   r   rI   r   r   r   r   r   r   r   r   r"   r"   r"   r#   r3     s   
	r3   )FF)F) 
__future__r   r   rv   r   r~   r   r'   collectionsr   r   r   	importlibr   Zdistutils.errorsr   r	   Z#setuptools.extern.packaging.versionr
   r   Zsetuptools.extern.sixr   r   type__metaclass__r$   r,   r   r   r:   r4   r3   r"   r"   r"   r#   <module>   s,   
.
  ?U