
(hU                 @   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 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 d  d	 l m Z m Z e Z Gd
 d   d  Z e j d d    Z d d d d  Z d d   Z  d d   Z! d d d  Z" Gd d   d  Z# Gd d   d e#  Z$ Gd d   d e#  Z% d S)    )absolute_importunicode_literalsN)defaultdict)partial)wraps)DistutilsOptionErrorDistutilsFileError)LegacyVersionparse)SpecifierSet)string_typesPY3c               @   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   sf   t  j j |  } t | j   } | j   } Wd  QRXt j |  } t |   j	 t
    |  ` d  S)N)	importlibutil	find_specopenoriginreadastr
   varsupdatelocalsself)r   namespecstrmsrcmodule r   7/tmp/pip-build-kmztzv_d/setuptools/setuptools/config.py__init__   s    zStaticModule.__init__c                sn   y' t    f d d   |  j j D  SWn@ t k
 ri } z  t d j t      |  WYd  d  } ~ Xn Xd  S)Nc             3   sd   |  ]Z } t  | t j  r | j D]8 } t  | t j  r" | j   k r" t j | j  Vq" q d  S)N)
isinstancer   AssigntargetsNameid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!   r3   r   r   r   r    r      s   r   c             c   s4   z t  j j d |   d VWd t  j j |   Xd S)zH
    Add path to front of sys.path for the duration of the context.
    r   N)syspathinsertremove)r9   r   r   r    
patch_path3   s    	r<   Fc       	      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>   osr9   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?   handlersr   r   r    read_configuration?   s$    	rP   c             C   s@   d j  t     } t j 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})r1   r   	functoolsr   getattr)
target_objkeyZgetter_nameZby_attributegetterr   r   r    _get_optionn   s    rV   c             C   sX   t  t  } xE |  D]= } x4 | j D]) } t | j |  } | | | j | <q# Wq 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_optionsrV   rS   section_prefix)rO   Zconfig_dicthandleroptionr(   r   r   r    rL   z   s    rL   c             C   sK   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)distributionrK   r@   optionsmetar   r   r    rJ      s    

rJ   c               @   s0  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  } xQ | j   D]C \ } } | j |  s: q | j | d  j d  } | | | <q W| |  _ | |  _ | |  _ g  |  _ d  S)N .)	rY   items
startswithreplacestripr@   rS   sectionsrX   )r   rS   ra   r@   rj   rY   section_namesection_optionsr   r   r    r!      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__r4   )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)tuplerS   aliasesgetrR   KeyErrorro   r/   r@   setattrrX   rH   )	r   Zoption_namer(   unknownrS   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)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 Sr   )ri   )r)   chunkr   r   r    
<listcomp>  s   	 z-ConfigHandler._parse_list.<locals>.<listcomp>)r"   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)r   	partitionr   ri   )r   r(   r   resultlinerT   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   ztruezyes)lower)r   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 } |  j  |  r* t d j      |  S)Nzfile:zCOnly strings are accepted for the {0} field, files are not accepted)rg   
ValueErrorr1   )r(   Zexclude_directive)rT   r   r    rv   .  s    z3ConfigHandler._exclude_files_parser.<locals>.parserr   )r   rT   rv   r   )rT   r    _exclude_files_parser#  s    z#ConfigHandler._exclude_files_parserc                s~   d } t  | t  s | S| j |  s, | S| t |  d  } d d   | j d  D } d j   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 j | j    Vq d  S)N)rA   r9   rB   ri   )r)   r9   r   r   r    r,   N  s    z,ConfigHandler._parse_file.<locals>.<genexpr>ry   rz   c             3   sE   |  ]; }   j  |  s d  r t j j |  r   j |  Vq d S)TN)_assert_localrA   r9   rC   
_read_file)r)   r9   )r   r   r    r,   P  s   )r"   r   rg   lenr   join)r   r(   Zinclude_directiver   Z	filepathsr   )r   r    _parse_file7  s    zConfigHandler._parse_filec             C   s)   |  j  t j    s% t d |    d  S)Nz#`file:` directive can not access %s)rg   rA   rD   r   )rM   r   r   r    r   V  s    zConfigHandler._assert_localc          	   C   s-   t  j |  d d  } | j   SWd  QRXd  S)Nencodingzutf-8)ior   r   )rM   fr   r   r    r   \  s    zConfigHandler._read_filec             C   sq  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 r t j
 j t j   |	 d  } |	 d } q| } n+ d | k rt j
 j t j   | d  } t |  @ y t t |  |  SWn! t k
 r\t j |  }
 Yn XWd QRXt |
 |  S)	zRepresents value as a module attribute.

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

        :param str value:
        :rtype: str
        zattr:rd   re   r!   r   /   N)rg   rh   ri   r   popr   rA   rD   rsplitr   r9   r<   rR   r   r/   r   import_module)r   r(   r_   Zattr_directiveZ
attrs_path	attr_namemodule_nameparent_pathZcustom_pathpartsr   r   r   r    _parse_attra  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
     s    z1ConfigHandler._get_parser_compound.<locals>.parser   )r   r   r
   r   )r   r    _get_parser_compound  s    	z"ConfigHandler._get_parser_compoundc             C   sO   i  } | p d d   } x0 | 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>  s    z6ConfigHandler._parse_section_to_dict.<locals>.<lambda>)rf   )r   rl   Zvalues_parserr(   rT   _r   r   r   r    _parse_section_to_dict  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)rf   rs   )r   rl   r   r   r(   r   r   r    parse_section  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.

        rd   z_%szparse_section%sre   __Nz0Unsupported distribution option section: [%s.%s])rj   rf   rR   rh   r   rY   )r   rk   rl   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  j      |  |   S)N)warningswarn)argskwargs)funcmsgwarning_classr   r    config_handler  s    z@ConfigHandler._deprecated_config_handler.<locals>.config_handler)r   )r   r   r   r   r   r   )r   r   r   r    _deprecated_config_handler  s    $z(ConfigHandler._deprecated_config_handler)r4   r5   r6   r7   rY   rq   r!   propertyro   rx   classmethodr   r   r   r   r   staticmethodr   r   r   r   r   r   r
   r   r   r   r   r    rc      s,   &
-rc   c                   sp   e  Z d  Z d Z d d d d d d d d	 i Z d
 Z d
 d   f d d  Z e d d    Z d d   Z	   S)r]   r^   Z	home_pageurlsummarydescription
classifierclassifiersplatform	platformsFNc                s)   t  t |   j | | |  | |  _ d  S)N)superr]   r!   r_   )r   rS   ra   r@   r_   )rn   r   r    r!     s    zConfigMetadataHandler.__init__c             C   s   |  j  } |  j } |  j } |  j } d | d | d | d |  j | d t  d | d |  j | |  d | d  d	 | d
 | d | d |  j d | i S)z.Metadata item name to parser function mapping.r   keywordsprovidesrequiresz[The requires parameter is deprecated, please use install_requires for runtime dependencies.	obsoletesr   licenseZlicense_filesr   long_descriptionversionZproject_urls)r   r   r   r   r   DeprecationWarningr   _parse_version)r   
parse_listZ
parse_file
parse_dictZexclude_files_parserr   r   r    ro     s&    						zConfigMetadataHandler.parsersc             C   s   |  j  |  } | | k r^ | j   } t t |  t  rZ d } t | j t       | S|  j | |  j	  } t
 |  r |   } t | t  s t | d  r d j 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__re   z%s)r   ri   r"   r
   r	   r   r1   r   r   r_   callabler   hasattrr   mapstr)r   r(   r   tmplr   r   r    r     s    	
z$ConfigMetadataHandler._parse_version)
r4   r5   r6   rY   rq   Zstrict_moder!   r   ro   r   r   r   )rn   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 d   Z d S)r\   ra   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 d | d t i S)z.Metadata item name to parser function mapping.r   ;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_modulesZpython_requires)r   r   r   r   _parse_packagesr   r   )r   r   Zparse_list_semicolonZ
parse_boolr   r   r   r    ro   ?  s,    					zConfigOptionsHandler.parsersc             C   s   d d g } | j    } | | k r1 |  j |  S| | d k } | rZ t rZ t d   |  j |  j j d i    } | r d d l m } n d d l m	 } | |   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)
ri   r   r   r   parse_section_packages__findrj   rr   
setuptoolsr   r   )r   r(   Zfind_directivesZtrimmed_valueZfindns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)zParses `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   )r)   kv)
valid_keysr   r    r|     s   	 zEConfigOptionsHandler.parse_section_packages__find.<locals>.<listcomp>Nr   )r   r   rW   rf   rr   )r   rl   Zsection_datar   r   r   )r   r    r   x  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   r   )r   rl   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*rd   )r   r   rr   )r   rl   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   rl   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   rl   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   r   Zextras_requireN)r   r   r   )r   rl   r   r   r   r    parse_section_extras_require  s    z1ConfigOptionsHandler.parse_section_extras_requirec             C   s6   |  j  | |  j  } d d   | j   D |  d <d S)z^Parses `data_files` configuration file section.

        :param dict section_options:
        c             S   s"   g  |  ] \ } } | | f  q Sr   r   )r)   r   r   r   r   r    r|     s   	 zAConfigOptionsHandler.parse_section_data_files.<locals>.<listcomp>
data_filesN)r   r   rf   )r   rl   r   r   r   r    parse_section_data_files  s    z-ConfigOptionsHandler.parse_section_data_filesN)r4   r5   r6   rY   r   ro   r   r   r   r   r   r   r   r   r   r   r   r    r\   ;  s   
	r\   )&
__future__r   r   r   r   rA   r8   r   rQ   r   collectionsr   r   r   
contextlibZdistutils.errorsr   r   Z#setuptools.extern.packaging.versionr	   r
   Z&setuptools.extern.packaging.specifiersr   Zsetuptools.extern.sixr   r   typeZ__metaclass__r   contextmanagerr<   rP   rV   rL   rJ   rc   r]   r\   r   r   r   r    <module>   s4   . ?V