3
yYðiý?  ã               @   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 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)Ú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-ue6tjl93/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 )z»Returns 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   s2   t | j||ƒ}|jƒ  t| ||ƒ}|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
    )ÚConfigMetadataHandlerÚmetadataÚparseÚConfigOptionsHandler)Údistributionr   r   ÚmetaÚoptionsr   r   r   r   Y   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„ ƒ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#   r0   r   r8   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__)r9   r   r   r   Úparsers“   s    zConfigHandler.parsersc       	      C   sÆ   t ƒ }| j}| jj||ƒ}t|||ƒ}||kr6t|ƒ‚|r>d S d}| jj|ƒ}|r€y||ƒ}W n  tk
r~   d}| jsz‚ Y nX |rˆd S t|d| d ƒ}|d kr®t	|||ƒ n||ƒ | j
j|ƒ d S )NFTzset_%s)Útupler#   ÚaliasesÚgetr%   ÚKeyErrorr@   Ú	Exceptionr   Úsetattrr$   r   )	r9   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 )z×Represents 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   )r7   )Ú.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)rV   Ú	partitionr   r7   )rT   r)   rU   Ú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)r_   r`   ra   )Úlower)rT   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   r7   )rM   r   r   r   r   ú	<genexpr>  s    z,ConfigHandler._parse_file.<locals>.<genexpr>rK   rL   c             3   s2   | ]*}ˆ j |ƒsd rtjj|ƒrˆ j|ƒV  qdS )TN)Ú_assert_localr   r   r   Ú
_read_file)rM   r   )rT   r   r   rd   	  s   )rP   r	   r5   ÚlenrS   Újoin)rT   r)   Zinclude_directiveÚspecZ	filepathsr   )rT   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)r5   r   r   r   )r   r   r   r   re     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   rf     s    zConfigHandler._read_filec             C   s‚   d}|j |ƒs|S |j|dƒjƒ jdƒ}|jƒ }dj|ƒ}|p@d}tjjdt	j
ƒ ƒ zt|ƒ}t||ƒ}W dtjdd… t_X |S )z·Represents value as a module attribute.

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

        :param str value:
        :rtype: str
        zattr:r2   r3   r<   r   Né   )r5   r6   r7   rS   Úpoprh   Úsysr   Úinsertr   r   r   r%   )rT   r)   Zattr_directiveZ
attrs_pathÚ	attr_nameÚmodule_nameÚmoduler   r   r   Ú_parse_attr  s    

zConfigHandler._parse_attrc                s   ‡ fdd„}|S )z¹Returns 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,   B  s    
z1ConfigHandler._get_parser_compound.<locals>.parser   )rT   rz   r,   r   )rz   r   Ú_get_parser_compound9  s    	z"ConfigHandler._get_parser_compoundc             C   s:   i }|pdd„ }x$|j ƒ D ]\}\}}||ƒ||< qW |S )zÊParses 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>W  s    z6ConfigHandler._parse_section_to_dict.<locals>.<lambda>)r4   )rT   r;   Zvalues_parserr)   r[   Ú_r]   r   r   r   Ú_parse_section_to_dictL  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)r4   rD   )r9   r;   Únamer}   r)   r   r   r   Úparse_section\  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.

        r2   z_%szparse_section%sr3   Ú__Nz0Unsupported distribution option section: [%s.%s])r8   r4   r%   r6   r   r"   )r9   r:   r;   Zmethod_postfixZsection_parser_methodr   r   r   r,   h  s    zConfigHandler.parse)F)rK   )N)r?   Ú
__module__Ú__qualname__Ú__doc__r"   rB   r<   Úpropertyr@   rJ   ÚclassmethodrV   r^   rc   rj   Ústaticmethodre   rf   rw   r{   r~   r€   r,   r   r   r   r   r1   s   s&   
&
 r1   c               @   s6   e Zd ZdZdddddœZdZedd	„ ƒZd
d„ ZdS )r*   r+   ÚurlÚdescriptionÚclassifiersÚ	platforms)Z	home_pageÚsummaryÚ
classifierÚplatformFc             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)rV   rj   r^   r{   Ú_parse_version)r9   Ú
parse_listZ
parse_fileÚ
parse_dictr   r   r   r@   ’  s    
zConfigMetadataHandler.parsersc             C   sJ   | j |ƒ}t|ƒr|ƒ }t|tƒsFt|dƒr>djtt|ƒƒ}nd| }|S )zSParses `version` option value.

        :param value:
        :rtype: str

        Ú__iter__r3   z%s)rw   ÚcallablerP   r	   Úhasattrrh   ÚmapÚstr)r9   r)   r•   r   r   r   r–   §  s    


z$ConfigMetadataHandler._parse_versionN)	r?   r‚   rƒ   r"   rB   Zstrict_moder…   r@   r–   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-   r0   c             C   sL   | j }t| j dd�}| j}| j}||||||||||||||| j| j|dœS )z.Metadata item name to parser function mapping.ú;)rU   )Zzip_safeZuse_2to3Zinclude_package_dataÚpackage_dirZ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)rV   r   rc   r^   Ú_parse_packagesrj   )r9   r—   Zparse_list_semicolonZ
parse_boolr˜   r   r   r   r@   À  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)r5   rV   Úparse_section_packages__findr8   rC   Ú
setuptoolsr¦   )r9   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 )zœParses `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   )rM   ÚkÚv)Ú
valid_keysr   r   rO   ü  s    zEConfigOptionsHandler.parse_section_packages__find.<locals>.<listcomp>Nr   )r~   rV   r!   r4   rC   )r9   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~   rV   )r9   r;   rx   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Ú*r2   )r~   rV   rC   )r9   r;   rx   Ú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³   )r9   r;   r   r   r   Úparse_section_package_data  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³   )r9   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ž   )rU   Zextras_requireN)r   rV   r~   )r9   r;   r—   r   r   r   Úparse_section_extras_require%  s    z1ConfigOptionsHandler.parse_section_extras_requireN)r?   r‚   rƒ   r"   r…   r@   r¥   r§   r°   r³   rµ   r¶   r·   r   r   r   r   r-   ¼  s   
r-   )FF)F)Ú
__future__r   r   rl   r   rr   Úcollectionsr   Ú	functoolsr   Ú	importlibr   Zdistutils.errorsr   r   Zsetuptools.extern.sixr	   r    r   r   Úobjectr1   r*   r-   r   r   r   r   Ú<module>   s    
.
  ;