U
    e»{jo  ã                   @   s¶   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 d dlmZ ddlmZmZ d	d
„ Zdd„ Zddd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS )é    N)Úsuppress)Úurlparse)Úlog)Ú
_find_home)Úget_readable_fileobjé   )ÚSAMPHubErrorÚSAMPWarningc              	   C   sN   i }t | ƒ�8}|D ],}| d¡s| d¡\}}| ¡ || ¡ < qW 5 Q R X |S )zK
    Read in the lockfile given by ``lockfilename`` into a dictionary.
    ú#ú=)r   Ú
startswithÚsplitÚstrip)ÚlockfilenameÚlockfiledictÚfÚlineÚkwÚval© r   ú\/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/samp/lockfile_helpers.pyÚread_lockfile   s    

r   c                 C   sŽ   t | dƒ}| ¡  t | tjtj ¡ t | dƒ}tj ¡  	¡ }| 
d|› d�¡ | 
d¡ | ¡ D ]\}}| 
|› d|› d�¡ qb| ¡  d S )NÚwz# SAMP lockfile written on Ú
z!# Standard Profile required keys
r   )ÚopenÚcloseÚosÚchmodÚstatÚS_IREADÚS_IWRITEÚdatetimeÚnowÚ	isoformatÚwriteÚitems)r   r   ZlockfileZnow_isoÚkeyÚvaluer   r   r   Úwrite_lockfile%   s    


r(   c                 C   sT  t ƒ  d}dtjkrntjd  d¡rltjd tdƒd … } t| ƒ}|d dkrdt d | ¡t	¡ dS |d } n¨| d k�rt
 d	| ¡ |d
kr tj tƒ d¡} njtj tƒ d¡}z(zt |¡ W n tk
rÔ   Y nX W 5 t |tjtj tj ¡ X tj |d|› �¡} n
t
 d¡ t| ƒ\}}|�r8t dt	¡ dS t
 d|  ¡ t| |ƒ | S )NÚ ÚSAMP_HUBústd-lockurl:r   ÚfilezAUnable to start a Hub with lockfile {}. Start-up process aborted.Fé   zRunning mode: Zsingleú.sampú.samp-1z	samp-hub-zRunning mode: multiplez>Another SAMP Hub is already running. Start-up process aborted.zLock-file: )Úremove_garbage_lock_filesr   Úenvironr   Úlenr   ÚwarningsÚwarnÚformatr	   r   ÚdebugÚpathÚjoinr   r   r   r   r    ÚS_IEXECÚmkdirÚOSErrorÚcheck_running_hubr(   )r   ÚmodeZhub_idZ
hub_paramsÚlockfiledirZlockfile_parsedÚhub_is_runningr   r   r   r   Úcreate_lock_file3   sJ    
ÿý



þ
r@   c                  C   sf   t ƒ } | stdƒ‚dtjkrNtjd  d¡rDtjd tdƒd… }q^tdƒ‚ntj tƒ d¡}| | S )z�
    Get either the hub given by the environment variable SAMP_HUB, or the one
    given by the lockfile .samp in the user home directory.
    z"Unable to find a running SAMP Hub.r*   r+   NzSAMP Hub profile not supported.r.   )	Úget_running_hubsr   r   r1   r   r2   r7   r8   r   )Úhubsr   r   r   r   Úget_main_running_hubp   s    

rC   c                  C   sÆ   i } d}dt jkr:t jd  d¡rJt jd tdƒd… }nt j tƒ d¡}t|ƒ\}}|rb|| |< d}t j tƒ d¡}t j |¡rÂt  	|¡D ]4}| d¡rŒt j ||¡}t|ƒ\}}|rŒ|| |< qŒ| S )aä  
    Return a dictionary containing the lock-file contents of all the currently
    running hubs (single and/or multiple mode).

    The dictionary format is:

    ``{<lock-file>: {<token-name>: <token-string>, ...}, ...}``

    where ``{<lock-file>}`` is the lock-file name, ``{<token-name>}`` and
    ``{<token-string>}`` are the lock-file tokens (name and content).

    Returns
    -------
    running_hubs : dict
        Lock-file contents of all the currently running hubs.
    r)   r*   r+   Nr.   r/   úsamp-hub)
r   r1   r   r2   r7   r8   r   r<   ÚisdirÚlistdir)rB   r   r?   r   r>   Úfilenamer   r   r   rA   ‡   s&    


rA   c                 C   sž   d}i }zt | ƒ}W n tk
r0   ||f Y S X d|kr–z.tj|d  dd¡dd�}|jj ¡  d}W n, tjk
r‚   d}Y n tk
r”   Y nX ||fS )a|  
    Test whether a hub identified by ``lockfilename`` is running or not.

    Parameters
    ----------
    lockfilename : str
        Lock-file name (path + file name) of the Hub to be tested.

    Returns
    -------
    is_running : bool
        Whether the hub is running
    hub_params : dict
        If the hub is running this contains the parameters from the lockfile
    Fzsamp.hub.xmlrpc.urlú\r)   r   )Ú
allow_noneT)	r   r;   ÚxmlrpcÚServerProxyÚreplaceZsampZhubZpingÚProtocolError)r   Z
is_runningr   Úproxyr   r   r   r<   ¼   s&     ÿr<   c               
   C   sÐ   d} t j tƒ d¡} t| ƒ\}}|sNt j | ¡rNttƒ� t  | ¡ W 5 Q R X t j tƒ d¡}t j 	|¡rÌt  
|¡D ]V}| d¡rtt j ||¡} t| ƒ\}}|stt j | ¡rtttƒ� t  | ¡ W 5 Q R X qtd S )Nr)   r.   r/   rD   )r   r7   r8   r   r<   Úisfiler   r;   ÚremoverE   rF   r   )r   r?   r   r>   rG   r   r   r   r0   ç   s"    


r0   )NNNN)r!   r   r   r3   Úxmlrpc.clientÚclientrJ   Ú
contextlibr   Úurllib.parser   Zastropyr   Zastropy.config.pathsr   Zastropy.utils.datar   Úerrorsr   r	   r   r(   r@   rC   rA   r<   r0   r   r   r   r   Ú<module>   s"   
=5+