B
    Ú‹dZ+  ã               @   sŒ   d Z ddlmZ ddlZddlZddlZddddgZdd	„ Zd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dd„ZdS )zx This module contains functions to determine where configuration and
data/cache files used by Astropy should be placed.
é    )ÚwrapsNÚget_config_dirÚget_cache_dirÚset_temp_configÚset_temp_cachec              C   sf  yt j d¡} W nN tk
r`   t jdkrPdt jkrDt jd } ntdƒ‚nt jdkr<dt jkr~t j d¡r~t jd } n¼dt jkr®dt jkr®t j t jd t jd ¡} nŒd	t jkrÌt j t jd	 ¡} nny6d
dl	}d}| 
|j|¡}| |d¡d
 } | ¡  W n6 tk
r8   dt jkr,t jd } ntdƒ‚Y nX n dt jkrTt jd } ntdƒ‚Y nX | S )a'  Locates and return the home directory (or best approximation) on this
    system.

    Raises
    ------
    OSError
        If the home directory cannot be located - usually means you are running
        Astropy on some obscure platform that doesn't have standard home
        directories.
    ú~ÚposixÚHOMEzCCould not find unix home directory to search for astropy config dirÚntZMSYSTEMÚ	HOMEDRIVEÚHOMEPATHÚUSERPROFILEr   Nz@Software\Microsoft\Windows\CurrentVersion\Explorer\Shell FoldersZPersonalzFCould not find windows home directory to search for astropy config dirzfCould not find a home directory to search for astropy config dir - are you on an unsupported platform?)ÚosÚpathÚ
expanduserÚ	ExceptionÚnameÚenvironÚOSErrorÚgetÚjoinÚwinregÚOpenKeyÚHKEY_CURRENT_USERÚQueryValueExÚClose)ÚhomedirZwregZshell_foldersÚkey© r   úa/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/config/paths.pyÚ
_find_home   s:    


r    Úastropyc             C   s®   d}t jdk	rDt j}tj || ¡}tj |¡s8t |¡ tj |¡S tj 	d¡}|dk	rštj |¡rštj || ¡}tj 
|¡sštj |¡r–tj |¡S |}tj td|| ƒ¡S )a  
    Determines the package configuration directory name and creates the
    directory if it doesn't exist.

    This directory is typically ``$HOME/.astropy/config``, but if the
    XDG_CONFIG_HOME environment variable is set and the
    ``$XDG_CONFIG_HOME/astropy`` directory exists, it will be that directory.
    If neither exists, the former will be created and symlinked to the latter.

    Parameters
    ----------
    rootname : str
        Name of the root configuration directory. For example, if ``rootname =
        'pkgname'``, the configuration directory would be ``<home>/.pkgname/``
        rather than ``<home>/.astropy`` (depending on platform).

    Returns
    -------
    configdir : str
        The absolute path to the configuration directory.

    NÚXDG_CONFIG_HOMEÚconfig)r   Ú
_temp_pathr   r   r   ÚexistsÚmkdirÚabspathr   r   ÚislinkÚ_find_or_create_root_dir)ÚrootnameÚlinktoÚxchZconfig_pathÚxchpthr   r   r   r   L   s    

c             C   s®   d}t jdk	rDt j}tj || ¡}tj |¡s8t |¡ tj |¡S tj 	d¡}|dk	rštj |¡rštj || ¡}tj 
|¡sštj |¡r–tj |¡S |}tj td|| ƒ¡S )a©  
    Determines the Astropy cache directory name and creates the directory if it
    doesn't exist.

    This directory is typically ``$HOME/.astropy/cache``, but if the
    XDG_CACHE_HOME environment variable is set and the
    ``$XDG_CACHE_HOME/astropy`` directory exists, it will be that directory.
    If neither exists, the former will be created and symlinked to the latter.

    Parameters
    ----------
    rootname : str
        Name of the root cache directory. For example, if
        ``rootname = 'pkgname'``, the cache directory will be
        ``<cache>/.pkgname/``.

    Returns
    -------
    cachedir : str
        The absolute path to the cache directory.

    NÚXDG_CACHE_HOMEÚcache)r   r$   r   r   r   r%   r&   r'   r   r   r(   r)   )r*   r+   r,   Ú
cache_pathr-   r   r   r   r   |   s    

c               @   s6   e Zd ZdZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )Ú_SetTempPathNFc             C   s.   |d k	rt j |¡}|| _|| _| jj| _d S )N)r   r   r'   Ú_pathÚ_deleteÚ	__class__r$   Ú
_prev_path)Úselfr   Údeleter   r   r   Ú__init__±   s
    z_SetTempPath.__init__c             C   s:   | j | j_y
|  d¡S  tk
r4   | j| j_‚ Y nX d S )Nr!   )r2   r4   r$   Ú_default_path_getterr   r5   )r6   r   r   r   Ú	__enter__¹   s    


z_SetTempPath.__enter__c             G   s*   | j | j_| jr&| jd k	r&t | j¡ d S )N)r5   r4   r$   r3   r2   ÚshutilÚrmtree)r6   Úargsr   r   r   Ú__exit__Á   s    
z_SetTempPath.__exit__c                s   t ˆ ƒ‡ ‡fdd„ƒ}|S )zImplements use as a decorator.c           	      s   ˆ ˆ | |Ž W d Q R X d S )Nr   )r=   Úkwargs)Úfuncr6   r   r   ÚwrapperÊ   s    z&_SetTempPath.__call__.<locals>.wrapper)r   )r6   r@   rA   r   )r@   r6   r   Ú__call__Ç   s    z_SetTempPath.__call__)NF)	Ú__name__Ú
__module__Ú__qualname__r$   r9   r8   r:   r>   rB   r   r   r   r   r1   ­   s   
r1   c                   s4   e Zd ZdZeeƒZ‡ fdd„Z‡ fdd„Z‡  Z	S )r   a¢  
    Context manager to set a temporary path for the Astropy config, primarily
    for use with testing.

    If the path set by this context manager does not already exist it will be
    created, if possible.

    This may also be used as a decorator on a function to set the config path
    just within that function.

    Parameters
    ----------

    path : str, optional
        The directory (which must exist) in which to find the Astropy config
        files, or create them if they do not already exist.  If None, this
        restores the config path to the user's default config path as returned
        by `get_config_dir` as though this context manager were not in effect
        (this is useful for testing).  In this case the ``delete`` argument is
        always ignored.

    delete : bool, optional
        If True, cleans up the temporary directory after exiting the temp
        context (default: False).
    c                s(   ddl m} | ¡ | _| ¡  tƒ  ¡ S )Né   )Ú_cfgobjs)ÚconfigurationrG   ÚcopyÚ_cfgobjs_copyÚclearÚsuperr:   )r6   rG   )r4   r   r   r:   ï   s    
zset_temp_config.__enter__c                s4   ddl m} | ¡  | | j¡ | `tƒ j|Ž  d S )NrF   )rG   )rH   rG   rK   ÚupdaterJ   rL   r>   )r6   r=   rG   )r4   r   r   r>   ù   s
    zset_temp_config.__exit__)
rC   rD   rE   Ú__doc__Ústaticmethodr   r9   r:   r>   Ú__classcell__r   r   )r4   r   r   Ò   s   
c               @   s   e Zd ZdZeeƒZdS )r   a/  
    Context manager to set a temporary path for the Astropy download cache,
    primarily for use with testing (though there may be other applications
    for setting a different cache directory, for example to switch to a cache
    dedicated to large files).

    If the path set by this context manager does not already exist it will be
    created, if possible.

    This may also be used as a decorator on a function to set the cache path
    just within that function.

    Parameters
    ----------

    path : str
        The directory (which must exist) in which to find the Astropy cache
        files, or create them if they do not already exist.  If None, this
        restores the cache path to the user's default cache path as returned
        by `get_cache_dir` as though this context manager were not in effect
        (this is useful for testing).  In this case the ``delete`` argument is
        always ignored.

    delete : bool, optional
        If True, cleans up the temporary directory after exiting the temp
        context (default: False).
    N)rC   rD   rE   rN   rO   r   r9   r   r   r   r   r     s   c             C   s,  t j tƒ d|› ¡}t j tƒ d|› | ¡}t j |¡süt j |¡szyt  |¡ W qœ tk
rv   t j |¡sr‚ Y qœX n"t j |¡sœd}t| || |¡ƒ‚yt  |¡ W n" tk
rÌ   t j |¡sÈ‚ Y nX t	j
 d¡sú|d k	rút j |¡sút  ||¡ n$t j |¡s d}t| || |¡ƒ‚t j |¡S )NÚ.z2Intended {0} {1} directory {1} is actually a file.Úwin)r   r   r   r    r%   r&   r   ÚisdirÚformatÚsysÚplatformÚ
startswithÚsymlinkr'   )Zdirnmr+   ÚpkgnameZinnerdirZmaindirÚmsgr   r   r   r)   !  s2    
r)   )r!   )r!   )r!   )rN   Ú	functoolsr   r   r;   rU   Ú__all__r    r   r   r1   r   r   r)   r   r   r   r   Ú<module>   s   ;
0
1%/ 