o
    Ê`
bÚ  ã                   @   sæ   d dl Z d dl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
mZmZmZ zd dlZW n eyK   zd dlZW n eyH   dZY nw Y nw G dd„ dƒZG d	d
„ d
ƒZe jdksee ¡ dkrgeneZG dd„ deƒZdS )é    N)Údatetime)Úopené   )ÚDEFAULT_SHELLSÚSHELLS)Úensure_parent_dir_existsÚget_flat_outputÚget_parent_process_nameÚlocation_in_pathÚnormpathc                   @   s4   e Zd Zdd„ Zedd„ ƒZddd„Zdd	d
„ZdS )ÚWindowsInterfacec                 K   s   d S ©N© )ÚselfÚkwargsr   r   ú4/usr/lib/python3/dist-packages/userpath/interface.pyÚ__init__   s   zWindowsInterface.__init__c                  C   sH   t  t jddt j¡�} t  | d¡d W  d   ƒ S 1 sw   Y  d S )NÚEnvironmentr   ÚPATH)ÚwinregÚOpenKeyÚHKEY_CURRENT_USERÚKEY_READÚQueryValueEx)Úkeyr   r   r   Ú_get_new_path   s   $ÿzWindowsInterface._get_new_pathFc                 C   sJ   t |ƒ tj¡}|  ¡ }|D ]}t||ƒs"|rtd ||¡ƒ‚ dS qdS )NzUnable to find `{}` in:
{}FT)r   ÚsplitÚosÚpathsepr   r
   Ú	ExceptionÚformat)r   ÚlocationÚcheckÚ	locationsÚnew_pathr   r   r   Úlocation_in_new_path   s   
üz%WindowsInterface.location_in_new_pathTc           
   
   K   s¶   dd l }t|ƒ}|r||  ¡ fn|  ¡ |f\}}d |tj|¡}t tjddtj	¡�}	t 
|	ddtj|¡ W d   ƒ n1 s?w   Y  |jj dddddd|j ¡ ¡ | j||d	�S )
Nr   z{}{}{}r   r   iÿÿ  é   é   iˆ  ©r"   )Úctypesr   r   r    r   r   r   r   r   Ú	KEY_WRITEÚ
SetValueExÚREG_EXPAND_SZÚwindllÚuser32ÚSendMessageTimeoutWÚwintypesÚDWORDr%   )
r   r!   Úfrontr"   r   r)   ÚheadÚtailr$   r   r   r   r   Úput(   s"    ÿù
zWindowsInterface.putN©F)TF)Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr   r%   r5   r   r   r   r   r      s    

r   c                   @   s6   e Zd Zddd„Zedd„ ƒZddd„Zdd
d„ZdS )ÚUnixInterfaceNFc                    s²   |rd}n|rt tƒ}n|  ¡ g}dd„ |D ƒ}dd„ |D ƒ}|s#t}tƒ }g }|D ]}||vr:| |¡ | |¡ q*‡ fdd„|D ƒ| _|rS‡ fdd„tD ƒ| _d S | j| _d S )NFc                 S   s    g | ]}|rt j |¡ ¡ ‘qS r   )r   ÚpathÚbasenameÚlower©Ú.0Úshellr   r   r   Ú
<listcomp>L   s     z*UnixInterface.__init__.<locals>.<listcomp>c                 S   s   g | ]}|t v r|‘qS r   ©r   r?   r   r   r   rB   M   ó    c                    ó   g | ]}t | ˆ ƒ‘qS r   rC   r?   ©Úhomer   r   rB   Z   rD   c                    rE   r   rC   r?   rF   r   r   rB   [   rD   )	Úsortedr   Údetect_shellr   ÚsetÚaddÚappendÚshellsÚshells_to_verify)r   rM   Ú
all_shellsrG   Údeduplicated_shellsÚselected_shellsrA   r   rF   r   r   C   s$   



€(zUnixInterface.__init__c                 C   sN   t ƒ  ¡ }|tv r|S dtjv rdS tj tj dd¡¡ ¡ }|tv r%|S d S )NÚBASH_VERSIONÚbashÚSHELLÚ )r	   r>   r   r   Úenvironr<   r=   Úget)ÚclsrA   r   r   r   rI   ]   s   

ÿzUnixInterface.detect_shellc              	   C   sj   t |ƒ tj¡}| jD ]'}| ¡ D ] }t|ƒ}|D ]}t||ƒs0|r+td 	|||¡ƒ‚   dS qqqdS )Nz-Unable to find `{}` in the output of `{}`:
{}FT)
r   r   r   r   rN   Úshow_path_commandsr   r
   r   r    )r   r!   r"   r#   rA   Úshow_path_commandr$   r   r   r   r%   n   s"   

ÿÿ
øÿþz"UnixInterface.location_in_new_pathTc           	         s.  t |ƒ}|pd}| jD ]„}|j||d� ¡ D ]x\}‰ zkt|ƒ tj |¡rAt|ddd��}| 	¡ }W d   ƒ n1 s;w   Y  ng }t
‡ fdd„|D ƒƒrPW q| d |j|t ¡  d	¡¡¡ | d
 ˆ ¡¡ t|ddd��}| |¡ W d   ƒ n1 sw   Y  W q tyŽ   Y qw q| j||d�S )NÚuserpath)r2   Úrzutf-8)Úencodingc                 3   s   � | ]}ˆ |v V  qd S r   r   )r@   Úline©Úcontentsr   r   Ú	<genexpr>�   s   € z$UnixInterface.put.<locals>.<genexpr>z
{} Created by `{}` on {}
z%Y-%m-%d %H:%M:%Sz{}
Úwr(   )r   rM   ÚconfigÚitemsr   r   r<   Úexistsr   Ú	readlinesÚanyrL   r    Úcomment_starterr   ÚutcnowÚstrftimeÚ
writelinesr   r%   )	r   r!   r2   Úapp_namer"   rA   ÚfileÚfÚlinesr   r_   r   r5   �   s:   

ÿ€ÿÿÿ€ÿêzUnixInterface.put)NFNr6   )TNF)r7   r8   r9   r   ÚclassmethodrI   r%   r5   r   r   r   r   r;   B   s    


r;   ÚntÚWindowsc                   @   s   e Zd ZdS )Ú	InterfaceN)r7   r8   r9   r   r   r   r   rs   ¥   s    rs   )r   Úplatformr   Úior   rM   r   r   Úutilsr   r   r	   r
   r   r   ÚImportErrorÚ_winregr   r;   ÚnameÚsystemÚ__default_interfacers   r   r   r   r   Ú<module>   s(    ÿ€ý0`