3
h`VF                 @   s   d dl mZm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 d dlmZmZ d dlmZ dd
dZdd ZdddZG dd deZG dd deZG dd deZdS )    )absolute_importunicode_literalsN)defaultdict)partial)import_module)DistutilsOptionErrorDistutilsFileError)LegacyVersionparse)string_typesFc       	      C   s   ddl m}m} tjj| } tjj| s4td|  tj }tj	tjj
|  zJ| }|rb|j ng }| |krx|j|  |j||d t||j|d}W dtj	| 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    7/tmp/pip-build-ikjamrr1/setuptools/setuptools/config.pyread_configuration   s$    

r"   c             C   sl   t t}x^| D ]V}|j}|j}xD|jD ]:}t|d| d}|dkrNt||}n| }||| |< 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
    zget_%sN)r   dictsection_prefix
target_objset_optionsgetattr)r   Zconfig_dicthandlerZ	obj_aliasr%   optiongettervaluer    r    r!   r   =   s    
r   c             C   s6   t | ||}|j  t| j||| 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   Z   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dZe
dd Ze
d$ddZdd Zdd  ZdS )%ConfigHandlerz1Handles metadata supplied in configuration files.NFc             C   sb   i }| j }x:|j D ].\}}|j|s(q|j|djd}|||< qW || _|| _|| _g | _d S )N .)	r$   items
startswithreplacestripr   r%   sectionsr&   )selfr%   r1   r   r:   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__)r;   r    r    r!   parsers   s    zConfigHandler.parsersc       	      C   s   t  }| j}| jj||}t|||}||kr6t||r>d S d}| jj|}|ry||}W n  tk
r~   d}| jsz Y nX |rd S t|d| d }|d krt	||| n|| | j
j| d S )NFTzset_%s)tupler%   aliasesgetr'   KeyErrorrB   	Exceptionr   setattrr&   r   )	r;   Zoption_namer+   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 |j }n
|j|}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 | ]}|j  r|j  qS r    )r9   ).0chunkr    r    r!   
<listcomp>   s    z-ConfigHandler._parse_list.<locals>.<listcomp>)
isinstancelist
splitlinessplit)clsr+   	separatorr    r    r!   _parse_list   s    

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

        :param value:
        :rtype: dict
        =z(Unable to parse option value to dict: %s)rX   	partitionr   r9   )rV   r+   rW   resultlinekeysepvalr    r    r!   _parse_dict   s    
zConfigHandler._parse_dictc             C   s   |j  }|dkS )zQRepresents value as boolean.

        :param value:
        :rtype: bool
        1trueyes)ra   rb   rc   )lower)rV   r+   r    r    r!   _parse_bool   s    zConfigHandler._parse_boolc                s\   d}t |ts|S |j|s |S |t|d }dd |jdD }dj fdd|D S )ai  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: LICENSE
            file: README.rst, CHANGELOG.md, src/file.txt

        :param str value:
        :rtype: str
        zfile:Nc             s   s   | ]}t jj|j V  qd S )N)r   r   r   r9   )rO   r   r    r    r!   	<genexpr>  s    z,ConfigHandler._parse_file.<locals>.<genexpr>rM   rN   c             3   s2   | ]*} j |sd rtjj|r j|V  qdS )TN)_assert_localr   r   r   
_read_file)rO   r   )rV   r    r!   rf   
  s   )rR   r   r7   lenrU   join)rV   r+   Zinclude_directivespecZ	filepathsr    )rV   r!   _parse_file   s    


zConfigHandler._parse_filec             C   s   | j tj std|  d S )Nz#`file:` directive can not access %s)r7   r   r   r   )r   r    r    r!   rg     s    zConfigHandler._assert_localc          	   C   s"   t j| dd
}|j S Q R X d S )Nzutf-8)encoding)ioopenread)r   fr    r    r!   rh     s    zConfigHandler._read_filec             C   s   d}|j |s|S |j|dj jd}|j }dj|}|p@d}tj }|r|d |kr||d  }|jdd}	t	|	dkrtj
jtj |	d }|	d }q|}nd|krtj
jtj |d }tj
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:r4   r5   r>   r   /   N)r7   r8   r9   rU   poprj   r   r   rsplitri   r   sysinsertr   r'   )rV   r+   r/   Zattr_directiveZ
attrs_path	attr_namemodule_nameparent_pathZcustom_pathpartsmoduler    r    r!   _parse_attr  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    )r+   parsedmethod)parse_methodsr    r!   r
   Q  s    
z1ConfigHandler._get_parser_compound.<locals>.parser    )rV   r   r
   r    )r   r!   _get_parser_compoundH  s    	z"ConfigHandler._get_parser_compoundc             C   s:   i }|pdd }x$|j  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    )r_   r    r    r!   <lambda>f  s    z6ConfigHandler._parse_section_to_dict.<locals>.<lambda>)r6   )rV   r=   Zvalues_parserr+   r]   _r_   r    r    r!   _parse_section_to_dict[  s
    
z$ConfigHandler._parse_section_to_dictc             C   s@   x:|j  D ].\}\}}y|| |< W q
 tk
r6   Y q
X q
W dS )zQParses configuration file section.

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

        r4   z_%szparse_section%sr5   __Nz0Unsupported distribution option section: [%s.%s])r:   r6   r'   r8   r   r$   )r;   r<   r=   Zmethod_postfixZsection_parser_methodr    r    r!   r
   w  s    zConfigHandler.parse)F)rM   )N)N)rA   
__module____qualname____doc__r$   rD   r>   propertyrB   rL   classmethodrX   r`   re   rl   staticmethodrg   rh   r}   r   r   r   r
   r    r    r    r!   r3   t   s(   
&
 ,r3   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 )r-   r.   urldescriptionclassifiers	platforms)Z	home_pagesummary
classifierplatformFNc                s   t t| j||| || _d S )N)superr-   r>   r/   )r;   r%   r1   r   r/   )r@   r    r!   r>     s    zConfigMetadataHandler.__init__c             C   s8   | j }| j}| j}|||||| j|||||| j|dS )z.Metadata item name to parser function mapping.)r   keywordsprovidesrequires	obsoletesr   licenser   long_descriptionversionZproject_urls)rX   rl   r`   r   _parse_version)r;   
parse_listZ
parse_file
parse_dictr    r    r!   rB     s    
zConfigMetadataHandler.parsersc             C   s   | j |}||kr<|j }tt|tr8td||f |S | j|| j}t|rX| }t|t	st
|dr~djtt|}nd| }|S )zSParses `version` option value.

        :param value:
        :rtype: str

        z7Version loaded from %s does not comply with PEP 440: %s__iter__r5   z%s)rl   r9   rR   r
   r	   r   r}   r/   callabler   hasattrrj   mapstr)r;   r+   r   r    r    r!   r     s    


z$ConfigMetadataHandler._parse_version)FN)rA   r   r   r$   rD   Zstrict_moder>   r   rB   r   __classcell__r    r    )r@   r!   r-     s    r-   c               @   sT   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S )r,   r1   c             C   sL   | j }t| j dd}| j}| j}||||||||||||||| j| j|dS )z.Metadata item name to parser function mapping.;)rW   )Zzip_safeZuse_2to3Zinclude_package_datar/   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)rX   r   re   r`   _parse_packagesrl   )r;   r   Zparse_list_semicolonZ
parse_boolr   r    r    r!   rB     s*    zConfigOptionsHandler.parsersc             C   sB   d}|j |s| j|S | j| jjdi }ddlm} |f |S )zTParses `packages` option value.

        :param value:
        :rtype: list
        zfind:zpackages.findr   )find_packages)r7   rX   parse_section_packages__findr:   rE   
setuptoolsr   )r;   r+   Zfind_directivefind_kwargsr   r    r    r!   r     s    

z$ConfigOptionsHandler._parse_packagesc                sT   | j || j}dddg t fdd|j D }|j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    )rO   kv)
valid_keysr    r!   rQ     s    zEConfigOptionsHandler.parse_section_packages__find.<locals>.<listcomp>Nr   )r   rX   r#   r6   rE   )r;   r=   Zsection_datar   r   r    )r   r!   r     s    


z1ConfigOptionsHandler.parse_section_packages__findc             C   s   | j || j}|| d< dS )z`Parses `entry_points` configuration file section.

        :param dict section_options:
        r   N)r   rX   )r;   r=   r~   r    r    r!   parse_section_entry_points%  s    z/ConfigOptionsHandler.parse_section_entry_pointsc             C   s.   | j || j}|jd}|r*||d< |d= |S )N*r4   )r   rX   rE   )r;   r=   r~   rootr    r    r!   _parse_package_data-  s    
z(ConfigOptionsHandler._parse_package_datac             C   s   | j || d< dS )z`Parses `package_data` configuration file section.

        :param dict section_options:
        package_dataN)r   )r;   r=   r    r    r!   parse_section_package_data7  s    z/ConfigOptionsHandler.parse_section_package_datac             C   s   | j || d< dS )zhParses `exclude_package_data` configuration file section.

        :param dict section_options:
        Zexclude_package_dataN)r   )r;   r=   r    r    r!   "parse_section_exclude_package_data>  s    z7ConfigOptionsHandler.parse_section_exclude_package_datac             C   s"   t | jdd}| j||| d< dS )zbParses `extras_require` configuration file section.

        :param dict section_options:
        r   )rW   Zextras_requireN)r   rX   r   )r;   r=   r   r    r    r!   parse_section_extras_requireF  s    z1ConfigOptionsHandler.parse_section_extras_requireN)rA   r   r   r$   r   rB   r   r   r   r   r   r   r   r    r    r    r!   r,     s   
r,   )FF)F)
__future__r   r   rn   r   rv   collectionsr   	functoolsr   	importlibr   Zdistutils.errorsr   r   Z#setuptools.extern.packaging.versionr	   r
   Zsetuptools.extern.sixr   r"   r   r   objectr3   r-   r,   r    r    r    r!   <module>   s"   
.
  M