
    IR-e                         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 Zd	gZd
gZd Z G d d          Z G d d          Z G d d	          Z G d d          Z G d d          Zd ZdS )z 
Utility functions and classes.
    N)StringIO)urlopen   )SAMP_STATUS_ERROR)SAMPProxyErrorc                  j    ddl m}  | j        sdS 	 t          dd           dS # t          $ r Y dS w xY w)Nr   confFzhttp://google.comg      ?)timeoutT) r
   use_internetr   	Exceptionr	   s    2lib/python3.11/site-packages/astropy/samp/utils.pyinternet_onr      sd     u	'55554 	 	 	55	s   $ 
22SAMPMsgReplierWrapper.c                     d|v r7|                     dd          \  }}t          t          | |          |          S t          | |          S )z%
    Get attributes recursively.
    r   r   )splitgetattr_recursivegetattr)variable	attributetop	remainings       r   r   r   $   sP     i"a00Y 3!7!7CCCx+++    c                        e Zd Zd Zd Zd ZdS )_ServerProxyPoolMethodc                 "    || _         || _        d S N)_ServerProxyPoolMethod__proxies_ServerProxyPoolMethod__name)selfproxiesnames      r   __init__z_ServerProxyPoolMethod.__init__3   s     r   c                 @    t          | j        | j         d|           S Nr   )r   r    r!   r"   r$   s     r   __getattr__z"_ServerProxyPoolMethod.__getattr__7   s$    %dn6M6Mt6M6MNNNr   c                 N   | j                                         }t          || j                  }	  ||i |}n1# t          j        $ r}t          |j        |j                  d }~ww xY w	 | j         	                    |           n# | j         	                    |           w xY w|S r   )
r    getr   r!   xmlrpcFaultr   	faultCodefaultStringput)r"   argskwrdsproxyfunctionresponseexcs          r   __call__z_ServerProxyPoolMethod.__call__:   s    ""$$$UDK88	&x///HH| 	A 	A 	A @@@	A  Nu%%%%DNu%%%%s&   9 B A'A""A''B B"N__name__
__module____qualname__r%   r)   r7    r   r   r   r   /   sD          O O O	 	 	 	 	r   r   c                   $    e Zd ZdZd Zd Zd ZdS )ServerProxyPoolz=
    A thread-safe pool of `xmlrpc.ServerProxy` objects.
    c                     t          j        |          | _        t          |          D ]"}| j                             ||i |           #d S r   )queueQueue_proxiesranger0   )r"   sizeproxy_classr1   keywordsis         r   r%   zServerProxyPool.__init__K   sZ    D))t 	> 	>AMkk4<8<<====	> 	>r   c                 ,    t          | j        |          S r   )r   rB   r(   s     r   r)   zServerProxyPool.__getattr__P   s    %dmT:::r   c                 ~    	 	 | j                                         }n# t          j        $ r Y dS w xY w |d           =)z;Shut down the proxy pool by closing all active connections.TcloseN)rB   
get_nowaitr@   Empty)r"   r3   s     r   shutdownzServerProxyPool.shutdownT   sY    	0022;    E'NNN	s    00N)r9   r:   r;   __doc__r%   r)   rM   r<   r   r   r>   r>   F   sK         > > >
; ; ;	 	 	 	 	r   r>   c                       e Zd ZdZd Z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                     || _         d S r   )cli)r"   rQ   s     r   r%   zSAMPMsgReplierWrapper.__init__l   s    r   c                       fd}|S )Nc                     t                    dk    s| d          |   d S 	  |  }|rHj        j                            j                                        | d         t
          |d           d S d S # t          $ r t                      }t          j	        |           |
                                }j        j                            j                                        | d         t
          d|id           Y d S w xY w)N      )zsamp.statuszsamp.result)filetxt)get_num_argsrQ   hubreplyget_private_keyr   r   r   	traceback	print_excgetvalue)r1   resulterrrW   fr"   s       r   	wrapped_fz1SAMPMsgReplierWrapper.__call__.<locals>.wrapped_fp   s0   A!##tAw4QXF ** H4466 G,=fUU      !   "**C'S1111,,..CHL&&0022Q(95RU,WW     	s   AA6 6B	DDr<   )r"   ra   rb   s   `` r   r7   zSAMPMsgReplierWrapper.__call__o   s*    	 	 	 	 	 	. r   N)r9   r:   r;   rN   r%   r7   r<   r   r   r   r   `   s<        	 	      r   c                       e Zd Zd Zd ZdS )_HubAsClientc                     || _         d S r   )_handler)r"   handlers     r   r%   z_HubAsClient.__init__   s    r   c                 ,    t          | j        |          S r   )_HubAsClientMethodrf   r(   s     r   r)   z_HubAsClient.__getattr__   s    !$-666r   N)r9   r:   r;   r%   r)   r<   r   r   rd   rd      s2             7 7 7 7 7r   rd   c                        e Zd Zd Zd Zd ZdS )ri   c                 "    || _         || _        d S r   _HubAsClientMethod__send_HubAsClientMethod__name)r"   sendr$   s      r   r%   z_HubAsClientMethod.__init__   s    r   c                 @    t          | j        | j         d|           S r'   )ri   rm   rn   r(   s     r   r)   z_HubAsClientMethod.__getattr__   s$    !$+$+/F/F/F/FGGGr   c                 8    |                      | j        |          S r   rl   )r"   r1   s     r   r7   z_HubAsClientMethod.__call__   s    {{4;---r   Nr8   r<   r   r   ri   ri      sD          H H H. . . . .r   ri   c                     t          j        |           r| j        j        j        dz
  S t          j        |           r| j        j        S t          d          )zW
    Find the number of arguments a function or method takes (excluding ``self``).
    r   z"f should be a function or a method)inspectismethod__func____code__co_argcount
isfunction	TypeError)ra   s    r   rX   rX      sW      >z".22		A		 >z%%<===r   )rN   rs   r@   r\   xmlrpc.clientclientr,   ior   urllib.requestr   	constantsr   errorsr   r   __all____doctest_skip__r   r   r>   r   rd   ri   rX   r<   r   r   <module>r      s   
                   " " " " " " ( ( ( ( ( ( " " " " " "
 
 
 #
#5 , , ,       .       4' ' ' ' ' ' ' 'T7 7 7 7 7 7 7 7	. 	. 	. 	. 	. 	. 	. 	.	> 	> 	> 	> 	>r   