U
    O8c2                     @   s  d dl Z d dlZd dlZd dlmZ ddddddgZed	ZG d
d deZ	G dd deZ
dd Zdd ZG dd dZG dd dZdd Zdd Zdd Zdd ZdBddZdCddZi ZdDddZedkrd dlmZ d dlZe Zejd d!d"d#d$ ejd%d&d"d'd$ ejd(d)d*d+ ejd,d-d"d.d$ ejd/d0d1d+ ejd2d3d"d1d$ ejd4d5d6d+ ee j\ZZ e!e d7k re"d8ej#red9Z$e$D ]&Z%ee%Z&e'd:e&j(e&j(e&j)f  qe d; Z*ej+,d<Z-e-ree*d=d>e-gZ&nee*d=d>gZ&ej.rej.Z.nd?Z.ej/r^e0d@ej/Z1e1s@e"dAne12d;Z(e12d7Z3e3e&j4e(< ej5rte'e&5e. ej6re'e&6e. ej7re'e&j7 ej8re'e&j7ej8k dS )E    N)RawConfigParserFormatErrorPkgNotFoundLibraryInfoVariableSetread_configparse_flagsz\$\{([a-zA-Z0-9_-]+)\}c                   @   s    e Zd ZdZdd Zdd ZdS )r   zQ
    Exception thrown when there is a problem parsing a configuration file.

    c                 C   s
   || _ d S Nmsgselfr    r   B/tmp/pip-unpacked-wheel-fd_gsd75/numpy/distutils/npy_pkg_config.py__init__   s    zFormatError.__init__c                 C   s   | j S r	   r
   r   r   r   r   __str__   s    zFormatError.__str__N__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z3Exception raised when a package can not be located.c                 C   s
   || _ d S r	   r
   r   r   r   r   r      s    zPkgNotFound.__init__c                 C   s   | j S r	   r
   r   r   r   r   r      s    zPkgNotFound.__str__Nr   r   r   r   r   r      s   c                 C   s   g g g g g d}d|   d}|D ]}d| }t|dkr"|dr`|d |dd	   q"|d
r|d |dd	   q"|dr|d |dd	   q"|dr|d |dd	   q"|d | q"|S )a  
    Parse a line from a config file containing compile flags.

    Parameters
    ----------
    line : str
        A single line containing one or more compile flags.

    Returns
    -------
    d : dict
        Dictionary of parsed flags, split into relevant categories.
        These categories are the keys of `d`:

        * 'include_dirs'
        * 'library_dirs'
        * 'libraries'
        * 'macros'
        * 'ignored'

    )include_dirslibrary_dirs	librariesmacrosignored z --r   z-Ir      Nz-Lr   z-lr   z-Dr   r   )splitlen
startswithappendstrip)linedflagsflagr   r   r   r      s$     



c                 C   s   |  ddS )N\z\\)replace)valr   r   r   _escape_backslashI   s    r,   c                   @   s>   e Zd ZdZdddZdd Zddd	Zdd
dZdd ZdS )r   a$  
    Object containing build information about a library.

    Parameters
    ----------
    name : str
        The library name.
    description : str
        Description of the library.
    version : str
        Version string.
    sections : dict
        The sections of the configuration file for the library. The keys are
        the section headers, the values the text under each header.
    vars : class instance
        A `VariableSet` instance, which contains ``(name, value)`` pairs for
        variables defined in the configuration file for the library.
    requires : sequence, optional
        The required libraries for the library to be installed.

    Notes
    -----
    All input parameters (except "sections" which is a method) are available as
    attributes of the same name.

    Nc                 C   s4   || _ || _|r|| _ng | _|| _|| _|| _d S r	   )namedescriptionrequiresversion	_sectionsvars)r   r-   r.   r0   sectionsr2   r/   r   r   r   r   g   s    zLibraryInfo.__init__c                 C   s   t | j S )z
        Return the section headers of the config file.

        Parameters
        ----------
        None

        Returns
        -------
        keys : list of str
            The list of section headers.

        )listr1   keysr   r   r   r   r3   r   s    zLibraryInfo.sectionsdefaultc                 C   s   | j | j| d }t|S )Ncflagsr2   interpolater1   r,   r   sectionr+   r   r   r   r7      s    zLibraryInfo.cflagsc                 C   s   | j | j| d }t|S )Nlibsr8   r:   r   r   r   r<      s    zLibraryInfo.libsc                 C   sV   d| j  d| j g}| jr&|d n|dd| j  |d| j  d|S )NzName: %szDescription: %sz	Requires:zRequires: %s,zVersion: %s
)r-   r.   r/   r#   joinr0   )r   mr   r   r   r      s    zLibraryInfo.__str__)N)r6   )r6   )	r   r   r   r   r   r3   r7   r<   r   r   r   r   r   r   L   s   


c                   @   sH   e Zd Z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   a  
    Container object for the variables defined in a config file.

    `VariableSet` can be used as a plain dictionary, with the variable names
    as keys.

    Parameters
    ----------
    d : dict
        Dict of items in the "variables" section of the configuration file.

    c                 C   s0   t dd | D | _i | _i | _|   d S )Nc                 S   s   g | ]\}}||fqS r   r   ).0kvr   r   r   
<listcomp>   s     z(VariableSet.__init__.<locals>.<listcomp>)dictitems	_raw_data_re_re_sub_init_parse)r   r&   r   r   r   r      s    zVariableSet.__init__c                 C   s$   | j  D ]\}}| || q
d S r	   )rG   rF   _init_parse_var)r   rB   rC   r   r   r   rJ      s    zVariableSet._init_parsec                 C   s"   t d| | j|< || j|< d S )Nz\$\{%s\})recompilerH   rI   r   r-   valuer   r   r   rK      s    zVariableSet._init_parse_varc                    s2    fdd}t |r.||}||kr(q.|}q|S )Nc                    s,    j  D ]} j |  j| | } q
| S r	   )rH   r5   subrI   )rO   rB   r   r   r   _interpolate   s    z-VariableSet.interpolate.<locals>._interpolate)_VARsearch)r   rO   rQ   Znvaluer   r   r   r9      s    
zVariableSet.interpolatec                 C   s   t | j S )z
        Return the list of variable names.

        Parameters
        ----------
        None

        Returns
        -------
        names : list of str
            The names of all variables in the `VariableSet` instance.

        )r4   rG   r5   r   r   r   r   	variables   s    zVariableSet.variablesc                 C   s
   | j | S r	   )rG   )r   r-   r   r   r   __getitem__   s    zVariableSet.__getitem__c                 C   s   || j |< | || d S r	   )rG   rK   rN   r   r   r   __setitem__   s    
zVariableSet.__setitem__N)r   r   r   r   r   rJ   rK   r9   rT   rU   rV   r   r   r   r   r      s   c                 C   sR   |  dstdt| d}dD ]}||kr$td| q$d|krNg |d< |S )NmetazNo meta section found !)r-   r.   r0   z6Option %s (section [meta]) is mandatory, but not foundr/   )has_sectionr   rE   rF   )configr&   rB   r   r   r   
parse_meta   s    
rZ   c                 C   s:   |  dstdi }| dD ]\}}|||< q t|S )NrT   zNo variables section found !)rX   r   rF   r   )rY   r&   r-   rO   r   r   r   parse_variables   s    

r[   c                 C   s   t tfS r	   )Zmeta_dr)rY   r   r   r   parse_sections   s    r]   c                 C   s   d|  S )Nz%s.inir   )pkg_namer   r   r   pkg_to_filename   s    r_   c                    s   |r fdd|D }n g}t  }||}t|dksJtdt| t|}i }|dr|dD ]\}}t|||< qjdd |	 D }	i }
i }|	D ]H}i }|
|dr||d||< ||D ]\}}|||< q||
|< q|||
|fS )Nc                    s   g | ]}t j| qS r   )ospathr?   )rA   r&   filenamer   r   rD      s     z parse_config.<locals>.<listcomp>   zCould not find file(s) %srT   c                 S   s   g | ]}|d kr|qS ))rW   rT   r   )rA   sr   r   r   rD     s      r/   )r   readr!   r   strrZ   rX   rF   r,   r3   
has_optionget)rc   dirs	filenamesrY   nrW   r2   r-   rO   Zsecsr3   r/   re   r&   r   rb   r   parse_config   s.    



rm   c                    s    fdd  | \}}}}d|krtd|krt|d }|t jkrTtd||d f t j| }ttj|j|d< t|d |d |d |t	|d	S )
Nc                    s   t | \}}}}| D ]p\}} t|\}}}	}
| D ]\}}||kr>|||< q>|	|  D ]$\}}|rd|| |  d| 7  < qdq||||fS )Nz %s)rm   rF   r_   )frW   r2   r3   reqsrnameZrvalueZnmetaZnvarsZ	nsectionsZnreqsrB   rC   ZonameZovalue_read_configrj   r   r   rr     s    
z&_read_config_imp.<locals>._read_configZpkgdirpkgnamez-You should import %s to get information on %sr-   r.   r0   )r-   r.   r0   r3   r2   )
sysmodules
ValueErrorr,   r`   ra   dirname__file__r   r   )rk   rj   rW   r2   r3   ro   rs   modr   rq   r   _read_config_imp  s    


  rz   c                 C   s>   z
t |  W S  tk
r8   tt| |}|t | < | Y S X dS )a  
    Return library info for a package from its configuration file.

    Parameters
    ----------
    pkgname : str
        Name of the package (should match the name of the .ini file, without
        the extension, e.g. foo for the file foo.ini).
    dirs : sequence, optional
        If given, should be a sequence of directories - usually including
        the NumPy base directory - where to look for npy-pkg-config files.

    Returns
    -------
    pkginfo : class instance
        The `LibraryInfo` instance containing the build information.

    Raises
    ------
    PkgNotFound
        If the package is not found.

    See Also
    --------
    misc_util.get_info, misc_util.get_pkg_info

    Examples
    --------
    >>> npymath_info = np.distutils.npy_pkg_config.read_config('npymath')
    >>> type(npymath_info)
    <class 'numpy.distutils.npy_pkg_config.LibraryInfo'>
    >>> print(npymath_info)
    Name: npymath
    Description: Portable, core math library implementing C99 standard
    Requires:
    Version: 0.1  #random

    N)_CACHEKeyErrorrz   r_   )rs   rj   rC   r   r   r   r   F  s    '
__main__)OptionParserz--cflagsr7   
store_truez*output all preprocessor and compiler flags)destactionhelpz--libsr<   zoutput all linker flagsz--use-sectionr;   z/use this section instead of default for options)r   r   z	--versionr0   zoutput versionz--atleast-versionmin_versionzMinimal versionz
--list-alllist_allz--define-variabledefine_variablez%Replace variable with the given valuer   z(Expect package name on the command line:z*.iniz
%s	%s - %srd   ZNPY_PKG_CONFIG_PATHznumpy/core/lib/npy-pkg-config.r6   z([\S]+)=([\S]+)zH--define-variable option should be of the form --define-variable=foo=bar)N)N)N)9rt   rL   r`   configparserr   __all__rM   rR   OSErrorr   r   r   r,   r   r   rZ   r[   r]   r_   rm   rz   r{   r   r   optparser~   globparser
add_option
parse_argsargvoptionsargsr!   rv   r   filesrn   infoprintr-   r.   r^   environri   r&   r;   r   rS   r@   grouprO   r2   r7   r<   r0   r   r   r   r   r   <module>   s    
*HD
$
(
3










