
§|j@  ã               @   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 m Z d  d l m Z d  d l m Z d d d	 d
 „ Z d d „  Z d d d „ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z d S)é    )Úabsolute_importÚunicode_literalsN)Údefaultdict)Úpartial)ÚDistutilsOptionErrorÚDistutilsFileError)Úimport_module)Ústring_typesFc       	      C   só   d d l  m } m } t j j |  ƒ }  t j j |  ƒ sJ t d |  ƒ ‚ t j ƒ  } t j	 t j j
 |  ƒ ƒ zi | ƒ  } | r� | j ƒ  n g  } |  | k r¬ | j |  ƒ | j | d | ƒt | | j d | ƒ} Wd t j	 | ƒ Xt | ƒ 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   Úcurrent_directoryÚdistr   Úhandlers© r   ú7/tmp/pip-build-5c7ija6t/setuptools/setuptools/config.pyÚread_configuration   s$    	r!   c             C   s’   t  t ƒ } x |  D]w } | j } | j } x\ | j D]Q } t | d | d ƒ } | d k ro t | | ƒ } n	 | ƒ  } | | | | <q5 Wq 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   sE   t  |  j | | ƒ } | j ƒ  t |  | | ƒ } | j ƒ  | | g 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 d „ ƒ Z d d „  Z d d „  Z d S)ÚConfigHandlerz1Handles metadata supplied in configuration files.NFc             C   s‹   i  } |  j  } xQ | j ƒ  D]C \ } } | j | ƒ s: q | j | d ƒ j d ƒ } | | | <q W| |  _ | |  _ | |  _ g  |  _ d  S)NÚ Ú.)	r#   ÚitemsÚ
startswithÚreplaceÚstripr   r$   Úsectionsr%   )Úselfr$   r1   r   r9   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 rQ t | ƒ ‚ | r[ d  Sd } |  j j | ƒ } | r° y | | ƒ } Wn$ t k
 r¯ d } |  j s« ‚  Yn X| rº d  St | d | d  ƒ } | d  k rï t	 | | | ƒ n
 | | ƒ |  j
 j | ƒ d  S)NFTzset_%s)Útupler$   ÚaliasesÚgetr&   ÚKeyErrorrA   Ú	Exceptionr   Úsetattrr%   r   )	r:   Zoption_namer*   Úunknownr$   Zcurrent_valueZskip_optionÚparserÚsetterr   r   r    Ú__setitem__™   s0    			
zConfigHandler.__setitem__ú,c             C   sN   t  | t ƒ r | Sd | 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 Sr   )r8   )Ú.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  } xa |  j  | ƒ D]P } | j | ƒ \ } } } | | k rV 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)rW   Ú	partitionr   r8   )rU   r*   rV   Ú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`   ra   rb   )Úlower)rU   r*   r   r   r    Ú_parse_boolå   s    zConfigHandler._parse_boolc          	   C   sÂ   t  | t ƒ s | Sd } | j | ƒ s, | St j ƒ  } | j | d ƒ j ƒ  } t j j | ƒ } | j | ƒ s� t	 d | ƒ ‚ t j j
 | ƒ r¾ t j | d d ƒ� } | j ƒ  } Wd QRX| S)aU  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:
            include: LICENSE
            include: src/file.txt

        :param str value:
        :rtype: str
        zfile:r3   z#`file:` directive can not access %sÚencodingzutf-8N)rQ   r	   r6   r   r   r7   r8   r   r   r   r   ÚioÚopenÚread)rU   r*   Zinclude_directiver   r   Úfr   r   r    Ú_parse_fileï   s    zConfigHandler._parse_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 | | ƒ } Wd 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:r3   r4   r=   r   Né   )r6   r7   r8   rT   ÚpopÚjoinÚsysr   Úinsertr   r   r   r&   )rU   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-   <  s    z1ConfigHandler._get_parser_compound.<locals>.parser   )rU   rv   r-   r   )rv   r    Ú_get_parser_compound3  s    	z"ConfigHandler._get_parser_compoundc             C   sO   i  } | p d d „  } x0 | 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>Q  s    z6ConfigHandler._parse_section_to_dict.<locals>.<lambda>)r5   )rU   r<   Zvalues_parserr*   r\   Ú_r^   r   r   r    Ú_parse_section_to_dictF  s
    
z$ConfigHandler._parse_section_to_dictc             C   sJ   xC | j  ƒ  D]5 \ } \ } } y | |  | <Wq t k
 rA Yq Xq Wd S)zQParses configuration file section.

        :param dict section_options:
        N)r5   rE   )r:   r<   Únamery   r*   r   r   r    Úparse_sectionV  s
    zConfigHandler.parse_sectionc             C   s‹   x„ |  j  j ƒ  D]s \ } } d } | r2 d | } t |  d | j d d ƒ d ƒ } | d k ry t d |  j | f ƒ ‚ | | ƒ q Wd S)zTParses configuration file items from one
        or more related sections.

        r3   z_%szparse_section%sr4   Ú__Nz0Unsupported distribution option section: [%s.%s])r9   r5   r&   r7   r   r#   )r:   r;   r<   Zmethod_postfixZsection_parser_methodr   r   r    r-   b  s    
	zConfigHandler.parse)r@   Ú
__module__Ú__qualname__Ú__doc__r#   rC   r=   ÚpropertyrA   rK   ÚclassmethodrW   r_   rd   rj   rs   rw   rz   r|   r-   r   r   r   r    r2   s   s"   &
%r2   c               @   sd   e  Z d  Z d Z d d d d d d d d	 i Z d
 Z e d d „  ƒ Z d d „  Z d d „  Z	 d S)r+   r,   Z	home_pageÚurlÚsummaryÚdescriptionÚ
classifierÚclassifiersÚplatformÚ	platformsFc             C   sa   |  j  } |  j } d | d | d | d | d | d |  j | | ƒ d | d | d	 | d
 |  j i
 S)z.Metadata item name to parser function mapping.r‰   ÚkeywordsÚprovidesÚrequiresÚ	obsoletesr‡   Úlicenser…   Úlong_descriptionÚversion)rW   rj   rw   Ú_parse_version)r:   Ú
parse_listZ
parse_filer   r   r    rA   Œ  s    		zConfigMetadataHandler.parsersc             C   sT   g  } x= | j  ƒ  D]/ \ } \ } } | j d | j ƒ  | f ƒ q W| |  d <d S)zQParses configuration file section.

        :param dict section_options:
        z%s :%sr‡   N)r5   r   Útitle)r:   r<   r‡   Úbeginry   Úrestr   r   r    Úparse_section_classifiersŸ  s    !z/ConfigMetadataHandler.parse_section_classifiersc             C   sk   |  j  | ƒ } t | ƒ r$ | ƒ  } t | t ƒ sg t | d ƒ r] d j t t | ƒ ƒ } n
 d | } | S)zSParses `version` option value.

        :param value:
        :rtype: str

        Ú__iter__r4   z%s)rs   ÚcallablerQ   r	   Úhasattrrm   ÚmapÚstr)r:   r*   r�   r   r   r    r‘   ª  s    	
z$ConfigMetadataHandler._parse_versionN)
r@   r~   r   r#   rC   Zstrict_moder�   rA   r–   r‘   r   r   r   r    r+   {  s   r+   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 S)r.   r1   c              C   sš   |  j  } t |  j  d d ƒ} |  j } |  j } d | d | d | d | d | d | d	 | d
 | d | d | d | d | d | d | d |  j d |  j i S)z.Metadata item name to parser function mapping.rV   ú;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)rW   r   rd   r_   Ú_parse_packagesrj   )r:   r’   Zparse_list_semicolonZ
parse_boolZ
parse_dictr   r   r    rA   Ã  s(    				zConfigOptionsHandler.parsersc             C   sZ   d } | j  | ƒ s" |  j | ƒ S|  j |  j j d i  ƒ ƒ } d d l m } | | �  S)zTParses `packages` option value.

        :param value:
        :rtype: list
        zfind:zpackages.findr   )Úfind_packages)r6   rW   Úparse_section_packages__findr9   rD   Ú
setuptoolsr£   )r:   r*   Zfind_directiveÚfind_kwargsr£   r   r   r    r¢   Þ  s    z$ConfigOptionsHandler._parse_packagesc                sv   |  j  | |  j ƒ } d d d g ‰  t ‡  f d d †  | j ƒ  Dƒ ƒ } | j d ƒ } | d k	 rr | 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                s4   g  |  ]* \ } } | ˆ  k r | r | | f ‘ q Sr   r   )rN   ÚkÚv)Ú
valid_keysr   r    rP   þ  s   	 zEConfigOptionsHandler.parse_section_packages__find.<locals>.<listcomp>Nr   )rz   rW   r"   r5   rD   )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)rz   rW   )r:   r<   rt   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Ú*r3   )rz   rW   rD   )r:   r<   rt   Ú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_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°   )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:
        rV   rœ   Zextras_requireN)r   rW   rz   )r:   r<   r’   r   r   r    Úparse_section_extras_require'  s    z1ConfigOptionsHandler.parse_section_extras_requireN)r@   r~   r   r#   r�   rA   r¢   r¤   r­   r°   r²   r³   r´   r   r   r   r    r.   ¿  s   
r.   )Ú
__future__r   r   rf   r   rn   Úcollectionsr   Ú	functoolsr   Zdistutils.errorsr   r   Zsetuptools.py26compatr   Zsetuptools.extern.sixr	   r!   r   r   Úobjectr2   r+   r.   r   r   r   r    Ú<module>   s   .ÿ 	D