U
    e»{jË  ã                   @   sÆ   d 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	„ Zd
gZdgZdd„ ZG dd„ dƒZG dd„ dƒZG dd
„ d
ƒZG dd„ dƒZG dd„ dƒZdd„ ZdS )z
Utility functions and classes
é    N)ÚStringIO)Úurlopené   )ÚSAMP_STATUS_ERROR)ÚSAMPProxyErrorc                  C   sD   ddl m}  | jsdS ztddd� W dS  tk
r>   Y dS X d S )Nr   ©ÚconfFzhttp://google.comg      ð?)ÚtimeoutT)Ú r   Zuse_internetr   Ú	Exceptionr   © r   úQ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/samp/utils.pyÚinternet_on   s    r   ÚSAMPMsgReplierWrapperÚ.c                 C   s6   d|kr(|  dd¡\}}tt| |ƒ|ƒS t| |ƒS dS )z%
    Get attributes recursively.
    r   r   N)ÚsplitÚgetattr_recursiveÚgetattr)ÚvariableÚ	attributeÚtopÚ	remainingr   r   r   r   $   s    r   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú_ServerProxyPoolMethodc                 C   s   || _ || _d S ©N)Ú_ServerProxyPoolMethod__proxiesÚ_ServerProxyPoolMethod__name)ÚselfÚproxiesÚnamer   r   r   Ú__init__3   s    z_ServerProxyPoolMethod.__init__c                 C   s   t | j| j› d|› �ƒS ©Nr   )r   r   r   ©r   r   r   r   r   Ú__getattr__7   s    z"_ServerProxyPoolMethod.__getattr__c              
   O   sp   | j  ¡ }t|| jƒ}zFz|||Ž}W n2 tjk
rX } zt|j|j	ƒ‚W 5 d }~X Y nX W 5 | j  |¡ X |S r   )
r   Úgetr   r   ÚputÚxmlrpcÚFaultr   Ú	faultCodeÚfaultString)r   ÚargsZkwrdsÚproxyÚfunctionÚresponseÚexcr   r   r   Ú__call__:   s    
$z_ServerProxyPoolMethod.__call__N©Ú__name__Ú
__module__Ú__qualname__r   r"   r.   r   r   r   r   r   /   s   r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚServerProxyPoolz=
    A thread-safe pool of `xmlrpc.ServerProxy` objects.
    c                 O   s0   t  |¡| _t|ƒD ]}| j |||Ž¡ qd S r   )ÚqueueÚQueueÚ_proxiesÚranger$   )r   ÚsizeZproxy_classr)   ÚkeywordsÚir   r   r   r   K   s    zServerProxyPool.__init__c                 C   s   t | j|ƒS r   )r   r6   r!   r   r   r   r"   P   s    zServerProxyPool.__getattr__c                 C   s8   z| j  ¡ }W n tjk
r(   Y q4Y nX |dƒ q dS )z;Shut down the proxy pool by closing all active connections.ÚcloseN)r6   Ú
get_nowaitr4   ÚEmpty)r   r*   r   r   r   ÚshutdownT   s
    
zServerProxyPool.shutdownN)r0   r1   r2   Ú__doc__r   r"   r>   r   r   r   r   r3   F   s   r3   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   a�  
    Function decorator that allows to automatically grab errors and returned
    maps (if any) from a function bound to a SAMP call (or notify).

    Parameters
    ----------
    cli : :class:`~astropy.samp.SAMPIntegratedClient` or :class:`~astropy.samp.SAMPClient`
        SAMP client instance. Decorator initialization, accepting the instance
        of the client that receives the call or notification.
    c                 C   s
   || _ d S r   )Úcli)r   r@   r   r   r   r   m   s    zSAMPMsgReplierWrapper.__init__c                    s   ‡ ‡fdd„}|S )Nc               
      s®   t ˆ ƒdks| d d kr"ˆ | Ž  nˆz2ˆ | Ž }|rRˆjj ˆj ¡ | d t|dœ¡ W nT tk
r¨   tƒ }tj	|d� | 
¡ }ˆjj ˆj ¡ | d td|idœ¡ Y nX d S )Né   é   )zsamp.statuszsamp.result)ÚfileÚtxt)Úget_num_argsr@   ZhubZreplyZget_private_keyr   r   r   Ú	tracebackÚ	print_excÚgetvalue)r)   ÚresultÚerrrD   ©Úfr   r   r   Ú	wrapped_fq   s&    
ýýz1SAMPMsgReplierWrapper.__call__.<locals>.wrapped_fr   )r   rL   rM   r   rK   r   r.   p   s    zSAMPMsgReplierWrapper.__call__N)r0   r1   r2   r?   r   r.   r   r   r   r   r   a   s   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú_HubAsClientc                 C   s
   || _ d S r   )Ú_handler)r   Úhandlerr   r   r   r   Œ   s    z_HubAsClient.__init__c                 C   s   t | j|ƒS r   )Ú_HubAsClientMethodrO   r!   r   r   r   r"   �   s    z_HubAsClient.__getattr__N)r0   r1   r2   r   r"   r   r   r   r   rN   ‹   s   rN   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )rQ   c                 C   s   || _ || _d S r   ©Ú_HubAsClientMethod__sendÚ_HubAsClientMethod__name)r   Úsendr   r   r   r   r   •   s    z_HubAsClientMethod.__init__c                 C   s   t | j| j› d|› �ƒS r    )rQ   rS   rT   r!   r   r   r   r"   ™   s    z_HubAsClientMethod.__getattr__c                 G   s   |   | j|¡S r   rR   )r   r)   r   r   r   r.   œ   s    z_HubAsClientMethod.__call__Nr/   r   r   r   r   rQ   ”   s   rQ   c                 C   s6   t  | ¡r| jjjd S t  | ¡r*| jjS tdƒ‚dS )zW
    Find the number of arguments a function or method takes (excluding ``self``).
    r   z"f should be a function or a methodN)ÚinspectÚismethodÚ__func__Ú__code__Úco_argcountÚ
isfunctionÚ	TypeError)rL   r   r   r   rE       s
    

rE   )r?   rV   r4   rF   Úxmlrpc.clientÚclientr%   Úior   Úurllib.requestr   Ú	constantsr   Úerrorsr   r   Ú__all__Z__doctest_skip__r   r   r3   r   rN   rQ   rE   r   r   r   r   Ú<module>   s$   *	