3
5^_P                 @   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 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)SpecifierSet)string_typesPY3Fc       	      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)setuptools.distr   r   ospathabspathisfiler	   getcwdchdirdirnamefind_config_filesappendparse_config_filesparse_configurationcommand_optionsconfiguration_to_dict)	filepathfind_othersr   r   r   current_directorydistr   handlers r&   C/home/birch/.local/lib/python3.6/site-packages/setuptools/config.pyread_configuration   s$    

r(   c             C   s.   dj f 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})formatlocals	functoolsr   getattr)
target_objkeygetter_nameby_attributegetterr&   r&   r'   _get_optionE   s    r2   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_optionsr2   r-   section_prefix)r%   config_dicthandleroptionvaluer&   r&   r'   r    Q   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   c   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:|j D ].\}}|j|s(q|j|djd}|||< qW || _|| _|| _g | _d S )N .)	r5   items
startswithreplacestripr   r-   sectionsr4   )selfr-   r?   r   rH   r5   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__)rI   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,   KeyErrorrP   	Exceptionr   setattrr4   r   )	rI   option_namer9   unknownr-   current_value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&   )rG   ).0chunkr&   r&   r'   
<listcomp>   s    z-ConfigHandler._parse_list.<locals>.<listcomp>)
isinstancelist
splitlinessplit)clsr9   	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)ri   	partitionr   rG   )rg   r9   rh   resultliner.   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)rq   rr   rs   )lower)rg   r9   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)rE   
ValueErrorr)   )r9   exclude_directive)r.   r&   r'   r[     s    
z3ConfigHandler._exclude_files_parser.<locals>.parserr&   )rg   r.   r[   r&   )r.   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)r   r   r   rG   )r`   r   r&   r&   r'   	<genexpr>%  s    z,ConfigHandler._parse_file.<locals>.<genexpr>r^   r_   c             3   s2   | ]*} j |sd rtjj|r j|V  qdS )TN)_assert_localr   r   r   
_read_file)r`   r   )rg   r&   r'   ry   '  s   )rc   r   rE   lenrf   join)rg   r9   include_directivespec	filepathsr&   )rg   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)rE   r   r   r   )r!   r&   r&   r'   rz   -  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'   r{   3  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:rB   rC   rL   r   /   N)rE   rF   rG   rf   popr}   r   r   rsplitr|   r   sysinsertr   r,   )rg   r9   r=   attr_directive
attrs_path	attr_namemodule_nameparent_pathcustom_pathpartsmoduler&   r&   r'   _parse_attr8  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&   )r9   parsedmethod)parse_methodsr&   r'   r   n  s    
z1ConfigHandler._get_parser_compound.<locals>.parser&   )rg   r   r   r&   )r   r'   _get_parser_compounde  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&   )ro   r&   r&   r'   <lambda>  s    z6ConfigHandler._parse_section_to_dict.<locals>.<lambda>)rD   )rg   rK   values_parserr9   r.   _ro   r&   r&   r'   _parse_section_to_dictx  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)rD   rT   )rI   rK   namer   r9   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.

        rB   z_%szparse_section%srC   __Nz0Unsupported distribution option section: [%s.%s])rH   rD   r,   rF   r   r5   )rI   rJ   rK   method_postfix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   )rI   r   r   r   r   r&   )r   r   r   r'   _deprecated_config_handler  s    z(ConfigHandler._deprecated_config_handler)F)r^   )N)N)rO   
__module____qualname____doc__r5   rR   rL   propertyrP   r]   classmethodri   rp   ru   rx   r   staticmethodrz   r{   r   r   r   r   r   r   r&   r&   r&   r'   rA   ~   s,   
&
,rA   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)	home_pagesummary
classifierplatformFNc                s   t t| j||| || _d S )N)superr;   rL   r=   )rI   r-   r?   r   r=   )rN   r&   r'   rL     s    zConfigMetadataHandler.__init__c             C   sN   | j }| j}| j}| j}|||| j|dt|| j|||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   license_filesr   long_descriptionversionproject_urls)ri   r   rp   rx   r   DeprecationWarningr   _parse_version)rI   
parse_list
parse_file
parse_dictexclude_files_parserr&   r&   r'   rP     s&    
zConfigMetadataHandler.parsersc             C   s   | j |}||krB|j }tt|tr>d}t|jf t |S | j|| j	}t
|r^| }t|tst|drd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__rC   z%s)r   rG   rc   r   r
   r   r)   r*   r   r=   callabler   hasattrr}   mapstr)rI   r9   r   tmplr&   r&   r'   r     s    


z$ConfigMetadataHandler._parse_version)FN)rO   r   r   r5   rR   strict_moderL   r   rP   r   __classcell__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:   r?   c             C   sN   | j }t| j dd}| j}| j}||||||||||||||| j| j|tdS )z.Metadata item name to parser function mapping.;)rh   )zip_safeuse_2to3include_package_datar=   use_2to3_fixersuse_2to3_exclude_fixersconvert_2to3_doctestsscriptseager_resourcesdependency_linksnamespace_packagesinstall_requiressetup_requirestests_requirepackagesentry_points
py_modulespython_requires)ri   r   ru   rp   _parse_packagesr   r   )rI   r   parse_list_semicolon
parse_boolr   r&   r&   r'   rP     s,    zConfigOptionsHandler.parsersc             C   s|   ddg}|j  }||kr"| j|S ||d k}|r@t r@td| j| jjdi }|rfd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)
rG   ri   r   r   parse_section_packages__findrH   rS   
setuptoolsr   r   )rI   r9   find_directivestrimmed_valuefindnsfind_kwargsr   r&   r&   r'   r   2  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&   )r`   kv)
valid_keysr&   r'   rb   [  s    zEConfigOptionsHandler.parse_section_packages__find.<locals>.<listcomp>Nr   )r   ri   r3   rD   rS   )rI   rK   section_datar   r   r&   )r   r'   r   N  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   ri   )rI   rK   r   r&   r&   r'   parse_section_entry_pointsc  s    z/ConfigOptionsHandler.parse_section_entry_pointsc             C   s.   | j || j}|jd}|r*||d< |d= |S )N*rB   )r   ri   rS   )rI   rK   r   rootr&   r&   r'   _parse_package_datak  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   )rI   rK   r&   r&   r'   parse_section_package_datau  s    z/ConfigOptionsHandler.parse_section_package_datac             C   s   | j || d< dS )zhParses `exclude_package_data` configuration file section.

        :param dict section_options:
        exclude_package_dataN)r   )rI   rK   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   )rh   extras_requireN)r   ri   r   )rI   rK   r   r&   r&   r'   parse_section_extras_require  s    z1ConfigOptionsHandler.parse_section_extras_requirec             C   s(   | 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S r&   r&   )r`   r   r   r&   r&   r'   rb     s    zAConfigOptionsHandler.parse_section_data_files.<locals>.<listcomp>
data_filesN)r   ri   rD   )rI   rK   r   r&   r&   r'   parse_section_data_files  s    z-ConfigOptionsHandler.parse_section_data_filesN)rO   r   r   r5   r   rP   r   r   r   r   r   r   r   r  r&   r&   r&   r'   r:     s   
	r:   )FF)F)"
__future__r   r   r   r   r   r   r+   collectionsr   r   r   	importlibr   distutils.errorsr   r	   #setuptools.extern.packaging.versionr
   r   &setuptools.extern.packaging.specifiersr   setuptools.extern.sixr   r   type__metaclass__r(   r2   r    r   rA   r;   r:   r&   r&   r&   r'   <module>   s.   
.
  ?V