3
ûâdeù  ã               @   s–   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	 ej
jdƒrZd dl mZ ned	œd
d„ZG dd„ de	ƒZeee dœdd„ZdgZdS )é    N)ÚConfigParser)ÚPath)ÚOptionalé   )ÚPlatformDirsABCÚlinux)Úgetuid)Úreturnc               C   s   t dƒ‚d S )Nzshould only be used on Linux)ÚRuntimeError© r   r   ú</tmp/pip-build-red6nood/pip/pip/_vendor/platformdirs/unix.pyr      s    r   c               @   sö   e Zd ZdZeedœdd„ƒZeedœdd„ƒZeedœdd	„Zeedœd
d„ƒZ	eedœdd„ƒZ
eedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ƒZeedœdd„ZdS ) ÚUnixaD  
    On Unix/Linux, we follow the
    `XDG Basedir Spec <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_. The spec allows
    overriding directories with environment variables. The examples show are the default values, alongside the name of
    the environment variable that overrides them. Makes use of the
    `appname <platformdirs.api.PlatformDirsABC.appname>`,
    `version <platformdirs.api.PlatformDirsABC.version>`,
    `multipath <platformdirs.api.PlatformDirsABC.multipath>`,
    `opinion <platformdirs.api.PlatformDirsABC.opinion>`.
    )r	   c             C   s,   t jjddƒ}|jƒ s"t jjdƒ}| j|ƒS )z–
        :return: data directory tied to the user, e.g. ``~/.local/share/$appname/$version`` or
         ``$XDG_DATA_HOME/$appname/$version``
        ÚXDG_DATA_HOMEÚ z~/.local/share)ÚosÚenvironÚgetÚstripÚpathÚ
expanduserÚ_append_app_name_and_version)Úselfr   r   r   r   Úuser_data_dir   s    zUnix.user_data_dirc             C   s.   t jjddƒ}|jƒ s$dt j› d�}| j|ƒS )aY  
        :return: data directories shared by users (if `multipath <platformdirs.api.PlatformDirsABC.multipath>` is
         enabled and ``XDG_DATA_DIR`` is set and a multi path the response is also a multi path separated by the OS
         path separator), e.g. ``/usr/local/share/$appname/$version`` or ``/usr/share/$appname/$version``
        ÚXDG_DATA_DIRSr   z/usr/local/sharez
/usr/share)r   r   r   r   ÚpathsepÚ_with_multi_path)r   r   r   r   r   Úsite_data_dir(   s    zUnix.site_data_dir)r   r	   c                s<   |j tjƒ}ˆ js|dd… }‡ fdd„|D ƒ}tjj|ƒS )Nr   r   c                s   g | ]}ˆ j tjj|ƒƒ‘qS r   )r   r   r   r   )Ú.0Úp)r   r   r   ú
<listcomp>9   s    z)Unix._with_multi_path.<locals>.<listcomp>)Úsplitr   r   Ú	multipathÚjoin)r   r   Ú	path_listr   )r   r   r   5   s
    zUnix._with_multi_pathc             C   s,   t jjddƒ}|jƒ s"t jjdƒ}| j|ƒS )z•
        :return: config directory tied to the user, e.g. ``~/.config/$appname/$version`` or
         ``$XDG_CONFIG_HOME/$appname/$version``
        ÚXDG_CONFIG_HOMEr   z	~/.config)r   r   r   r   r   r   r   )r   r   r   r   r   Úuser_config_dir<   s    zUnix.user_config_dirc             C   s$   t jjddƒ}|jƒ sd}| j|ƒS )a/  
        :return: config directories shared by users (if `multipath <platformdirs.api.PlatformDirsABC.multipath>`
         is enabled and ``XDG_DATA_DIR`` is set and a multi path the response is also a multi path separated by the OS
         path separator), e.g. ``/etc/xdg/$appname/$version``
        ÚXDG_CONFIG_DIRSr   z/etc/xdg)r   r   r   r   r   )r   r   r   r   r   Úsite_config_dirG   s    zUnix.site_config_dirc             C   s,   t jjddƒ}|jƒ s"t jjdƒ}| j|ƒS )z”
        :return: cache directory tied to the user, e.g. ``~/.cache/$appname/$version`` or
         ``~/$XDG_CACHE_HOME/$appname/$version``
        ÚXDG_CACHE_HOMEr   z~/.cache)r   r   r   r   r   r   r   )r   r   r   r   r   Úuser_cache_dirT   s    zUnix.user_cache_dirc             C   s,   t jjddƒ}|jƒ s"t jjdƒ}| j|ƒS )z˜
        :return: state directory tied to the user, e.g. ``~/.local/state/$appname/$version`` or
         ``$XDG_STATE_HOME/$appname/$version``
        ZXDG_STATE_HOMEr   z~/.local/state)r   r   r   r   r   r   r   )r   r   r   r   r   Úuser_state_dir_   s    zUnix.user_state_dirc             C   s   | j }| jrtjj|dƒ}|S )zx
        :return: log directory tied to the user, same as `user_data_dir` if not opinionated else ``log`` in it
        Úlog)r)   Úopinionr   r   r"   )r   r   r   r   r   Úuser_log_dirj   s    zUnix.user_log_dirc             C   s6   t dƒ}|dkr2tjjddƒjƒ }|s2tjjdƒ}|S )zU
        :return: documents directory tied to the user, e.g. ``~/Documents``
        ZXDG_DOCUMENTS_DIRNr   z~/Documents)Ú_get_user_dirs_folderr   r   r   r   r   r   )r   Zdocuments_dirr   r   r   Úuser_documents_dirt   s    zUnix.user_documents_dirc             C   s,   t jjddƒ}|jƒ s"dtƒ › �}| j|ƒS )zŸ
        :return: runtime directory tied to the user, e.g. ``/run/user/$(id -u)/$appname/$version`` or
         ``$XDG_RUNTIME_DIR/$appname/$version``
        ZXDG_RUNTIME_DIRr   z
/run/user/)r   r   r   r   r   r   )r   r   r   r   r   Úuser_runtime_dir�   s    zUnix.user_runtime_dirc             C   s   | j | jƒS )zd:return: data path shared by users. Only return first item, even if ``multipath`` is set to ``True``)Ú _first_item_as_path_if_multipathr   )r   r   r   r   Úsite_data_pathŒ   s    zUnix.site_data_pathc             C   s   | j | jƒS )zj:return: config path shared by the users. Only return first item, even if ``multipath`` is set to ``True``)r1   r'   )r   r   r   r   Úsite_config_path‘   s    zUnix.site_config_path)Ú	directoryr	   c             C   s   | j r|jtjƒd }t|ƒS )Nr   )r!   r    r   r   r   )r   r4   r   r   r   r1   –   s    z%Unix._first_item_as_path_if_multipathN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚpropertyÚstrr   r   r   r%   r'   r)   r*   r-   r/   r0   r   r2   r3   r1   r   r   r   r   r      s2   




	
r   )Úkeyr	   c             C   sŠ   t jjtƒ jdƒ}t jj|ƒr†tƒ }t|ƒ�}|jd|j	ƒ › �ƒ W dQ R X | |d kr\dS |d |  j
dƒ}|jdt jjdƒƒ}|S dS )zjReturn directory from user-dirs.dirs config file. See https://freedesktop.org/wiki/Software/xdg-user-dirs/zuser-dirs.dirsz[top]
NÚtopú"z$HOMEú~)r   r   r"   r   r%   Úexistsr   ÚopenÚread_stringÚreadr   Úreplacer   )r;   Zuser_dirs_config_pathÚparserÚstreamr   r   r   r   r.   �   s    
r.   )r   ÚsysÚconfigparserr   Zpathlibr   Útypingr   Úapir   ÚplatformÚ
startswithr   Úintr   r:   r.   Ú__all__r   r   r   r   Ú<module>   s    