a
    ߙfbC                     @   s6   d dl mZ d dlmZ dgZdgZG dd dZdS )   )
SAMPClient)SAMPHubProxySAMPIntegratedClientzSAMPIntegratedClient.*c                   @   s  e Zd ZdZdPddZedd ZdQd
dZdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 ZdRd4d5Zd6d7 Zejje_d8d9 Zejje_d:d; Z ej je _dSd<d=Z!ej!je!_dTd>d?Z"ej"je"_dUd@dAZ#ej#je#_dBdC Z$ej$je$_dVdDdEZ%ej%je%_dWdFdGZ&ej&je&_dHdI Z'ej'je'_dXdJdKZ(ej(je(_dLdM Z)ej)je)_dNdO Z*ej*je*_dS )Yr   ao  
    A Simple SAMP client.

    This class is meant to simplify the client usage providing a proxy class
    that merges the :class:`~astropy.samp.SAMPClient` and
    :class:`~astropy.samp.SAMPHubProxy` functionalities in a
    simplified API.

    Parameters
    ----------
    name : str, optional
        Client name (corresponding to ``samp.name`` metadata keyword).

    description : str, optional
        Client description (corresponding to ``samp.description.text`` metadata
        keyword).

    metadata : dict, optional
        Client application metadata in the standard SAMP format.

    addr : str, optional
        Listening address (or IP). This defaults to 127.0.0.1 if the internet
        is not reachable, otherwise it defaults to the host name.

    port : int, optional
        Listening XML-RPC server socket port. If left set to 0 (the default),
        the operating system will select a free port.

    callable : bool, optional
        Whether the client can receive calls and notifications. If set to
        `False`, then the client can send notifications and calls, but can not
        receive any.
    N    Tc                 C   s&   t  | _||||||d| _d | _d S )N)namedescriptionmetadataaddrportcallable)r   hubclient_argumentsclient)selfr   r   r   r	   r
   r    r   =lib/python3.9/site-packages/astropy/samp/integrated_client.py__init__/   s    zSAMPIntegratedClient.__init__c                 C   s   | j jo| jjS )z
        Testing method to verify the client connection with a running Hub.

        Returns
        -------
        is_connected : bool
            True if the client is connected to a Hub, False otherwise.
        )r   is_connectedr   
is_runningr   r   r   r   r   H   s    
z!SAMPIntegratedClient.is_connected   c                 C   s>   | j ||| t| j fi | j| _| j  | j  dS )aN  
        Connect with the current or specified SAMP Hub, start and register the
        client.

        Parameters
        ----------
        hub : `~astropy.samp.SAMPHubServer`, optional
            The hub to connect to.

        hub_params : dict, optional
            Optional dictionary containing the lock-file content of the Hub
            with which to connect. This dictionary has the form
            ``{<token-name>: <token-string>, ...}``.

        pool_size : int, optional
            The number of socket connections opened to communicate with the
            Hub.
        N)r   connectr   r   r   startregister)r   r   Z
hub_paramsZ	pool_sizer   r   r   r   T   s    
zSAMPIntegratedClient.connectc                 C   sT   | j rPz*| j  W | jjr&| j  | j  n| jjrD| j  | j  0 dS )zw
        Unregister the client from the current SAMP Hub, stop the client and
        disconnect from the Hub.
        N)r   r   Z
unregisterr   stopr   
disconnectr   r   r   r   r   u   s    

zSAMPIntegratedClient.disconnectc                 C   s
   | j  S )zL
        Proxy to ``ping`` SAMP Hub method (Standard Profile only).
        )r   pingr   r   r   r   r      s    zSAMPIntegratedClient.pingc                 C   s   | j |S )z?
        Proxy to ``declareMetadata`` SAMP Hub method.
        )r   declare_metadata)r   r   r   r   r   r      s    z%SAMPIntegratedClient.declare_metadatac                 C   s   | j |  |S )z;
        Proxy to ``getMetadata`` SAMP Hub method.
        )r   get_metadataget_private_keyr   Z	client_idr   r   r   r      s    z!SAMPIntegratedClient.get_metadatac                 C   s   | j |  |S )z@
        Proxy to ``getSubscriptions`` SAMP Hub method.
        )r   get_subscriptionsr   r    r   r   r   r!      s    z&SAMPIntegratedClient.get_subscriptionsc                 C   s   | j |  S )z
        Proxy to ``getRegisteredClients`` SAMP Hub method.

        This returns all the registered clients, excluding the current client.
        )r   get_registered_clientsr   r   r   r   r   r"      s    z+SAMPIntegratedClient.get_registered_clientsc                 C   s   | j |  |S )zD
        Proxy to ``getSubscribedClients`` SAMP Hub method.
        )r   get_subscribed_clientsr   )r   mtyper   r   r   r#      s    z+SAMPIntegratedClient.get_subscribed_clientsc                 C   s>   i }d|v r0|d }|d= ||d}| | n
||d}|S )NZ	extra_kws)z
samp.mtypezsamp.paramsupdate)r   r$   paramsmsgZextrar   r   r   _format_easy_msg   s    

z%SAMPIntegratedClient._format_easy_msgc                 C   s   | j |  ||S )z6
        Proxy to ``notify`` SAMP Hub method.
        )r   notifyr   )r   recipient_idmessager   r   r   r*      s    zSAMPIntegratedClient.notifyc                 K   s   |  || ||S )a4  
        Easy to use version of :meth:`~astropy.samp.integrated_client.SAMPIntegratedClient.notify`.

        This is a proxy to ``notify`` method that allows to send the
        notification message in a simplified way.

        Note that reserved ``extra_kws`` keyword is a dictionary with the
        special meaning of being used to add extra keywords, in addition to
        the standard ``samp.mtype`` and ``samp.params``, to the message sent.

        Parameters
        ----------
        recipient_id : str
            Recipient ID

        mtype : str
            the MType to be notified

        params : dict or set of str
            Variable keyword set which contains the list of parameters for the
            specified MType.

        Examples
        --------
        >>> from astropy.samp import SAMPIntegratedClient
        >>> cli = SAMPIntegratedClient()
        >>> ...
        >>> cli.enotify("samp.msg.progress", msgid = "xyz", txt = "initialization",
        ...             percent = "10", extra_kws = {"my.extra.info": "just an example"})
        )r*   r)   )r   r+   r$   r'   r   r   r   enotify   s    zSAMPIntegratedClient.enotifyc                 C   s   | j |  |S )z9
        Proxy to ``notifyAll`` SAMP Hub method.
        )r   
notify_allr   )r   r,   r   r   r   r.      s    zSAMPIntegratedClient.notify_allc                 K   s   |  | ||S )a  
        Easy to use version of :meth:`~astropy.samp.integrated_client.SAMPIntegratedClient.notify_all`.

        This is a proxy to ``notifyAll`` method that allows to send the
        notification message in a simplified way.

        Note that reserved ``extra_kws`` keyword is a dictionary with the
        special meaning of being used to add extra keywords, in addition to
        the standard ``samp.mtype`` and ``samp.params``, to the message sent.

        Parameters
        ----------
        mtype : str
            MType to be notified.

        params : dict or set of str
            Variable keyword set which contains the list of parameters for
            the specified MType.

        Examples
        --------
        >>> from astropy.samp import SAMPIntegratedClient
        >>> cli = SAMPIntegratedClient()
        >>> ...
        >>> cli.enotify_all("samp.msg.progress", txt = "initialization",
        ...                 percent = "10",
        ...                 extra_kws = {"my.extra.info": "just an example"})
        )r.   r)   )r   r$   r'   r   r   r   enotify_all   s    z SAMPIntegratedClient.enotify_allc                 C   s   | j |  |||S )z4
        Proxy to ``call`` SAMP Hub method.
        )r   callr   )r   r+   msg_tagr,   r   r   r   r0     s    zSAMPIntegratedClient.callc                 K   s   |  ||| ||S )a}  
        Easy to use version of :meth:`~astropy.samp.integrated_client.SAMPIntegratedClient.call`.

        This is a proxy to ``call`` method that allows to send a call message
        in a simplified way.

        Note that reserved ``extra_kws`` keyword is a dictionary with the
        special meaning of being used to add extra keywords, in addition to
        the standard ``samp.mtype`` and ``samp.params``, to the message sent.

        Parameters
        ----------
        recipient_id : str
            Recipient ID

        msg_tag : str
            Message tag to use

        mtype : str
            MType to be sent

        params : dict of set of str
            Variable keyword set which contains the list of parameters for
            the specified MType.

        Examples
        --------
        >>> from astropy.samp import SAMPIntegratedClient
        >>> cli = SAMPIntegratedClient()
        >>> ...
        >>> msgid = cli.ecall("abc", "xyz", "samp.msg.progress",
        ...                   txt = "initialization", percent = "10",
        ...                   extra_kws = {"my.extra.info": "just an example"})
        )r0   r)   )r   r+   r1   r$   r'   r   r   r   ecall	  s    $zSAMPIntegratedClient.ecallc                 C   s   | j |  ||S )z7
        Proxy to ``callAll`` SAMP Hub method.
        )r   call_allr   )r   r1   r,   r   r   r   r3   /  s    zSAMPIntegratedClient.call_allc                 K   s   |  || || dS )aV  
        Easy to use version of :meth:`~astropy.samp.integrated_client.SAMPIntegratedClient.call_all`.

        This is a proxy to ``callAll`` method that allows to send the call
        message in a simplified way.

        Note that reserved ``extra_kws`` keyword is a dictionary with the
        special meaning of being used to add extra keywords, in addition to
        the standard ``samp.mtype`` and ``samp.params``, to the message sent.

        Parameters
        ----------
        msg_tag : str
            Message tag to use

        mtype : str
            MType to be sent

        params : dict of set of str
            Variable keyword set which contains the list of parameters for
            the specified MType.

        Examples
        --------
        >>> from astropy.samp import SAMPIntegratedClient
        >>> cli = SAMPIntegratedClient()
        >>> ...
        >>> msgid = cli.ecall_all("xyz", "samp.msg.progress",
        ...                       txt = "initialization", percent = "10",
        ...                       extra_kws = {"my.extra.info": "just an example"})
        N)r3   r)   )r   r1   r$   r'   r   r   r   	ecall_all5  s     zSAMPIntegratedClient.ecall_allc                 C   s   | j |  |||S )z;
        Proxy to ``callAndWait`` SAMP Hub method.
        )r   call_and_waitr   )r   r+   r,   timeoutr   r   r   r5   W  s    z"SAMPIntegratedClient.call_and_waitc                 K   s   |  || |||S )a  
        Easy to use version of :meth:`~astropy.samp.integrated_client.SAMPIntegratedClient.call_and_wait`.

        This is a proxy to ``callAndWait`` method that allows to send the call
        message in a simplified way.

        Note that reserved ``extra_kws`` keyword is a dictionary with the
        special meaning of being used to add extra keywords, in addition to
        the standard ``samp.mtype`` and ``samp.params``, to the message sent.

        Parameters
        ----------
        recipient_id : str
            Recipient ID

        mtype : str
            MType to be sent

        timeout : str
            Call timeout in seconds

        params : dict of set of str
            Variable keyword set which contains the list of parameters for
            the specified MType.

        Examples
        --------
        >>> from astropy.samp import SAMPIntegratedClient
        >>> cli = SAMPIntegratedClient()
        >>> ...
        >>> cli.ecall_and_wait("xyz", "samp.msg.progress", "5",
        ...                    txt = "initialization", percent = "10",
        ...                    extra_kws = {"my.extra.info": "just an example"})
        )r5   r)   )r   r+   r$   r6   r'   r   r   r   ecall_and_wait]  s    #z#SAMPIntegratedClient.ecall_and_waitc                 C   s   | j |  ||S )z5
        Proxy to ``reply`` SAMP Hub method.
        )r   replyr   )r   msg_idresponser   r   r   r8     s    zSAMPIntegratedClient.replyc                 C   s8   d|i}|d ur| d|i |d ur4| d|i |S )Nzsamp.statuszsamp.resultz
samp.errorr%   )r   statusresulterrorr(   r   r   r   _format_easy_response  s    z*SAMPIntegratedClient._format_easy_responsec                 C   s   |  || |||S )aa  
        Easy to use version of :meth:`~astropy.samp.integrated_client.SAMPIntegratedClient.reply`.

        This is a proxy to ``reply`` method that allows to send a reply
        message in a simplified way.

        Parameters
        ----------
        msg_id : str
            Message ID to which reply.

        status : str
            Content of the ``samp.status`` response keyword.

        result : dict
            Content of the ``samp.result`` response keyword.

        error : dict
            Content of the ``samp.error`` response keyword.

        Examples
        --------
        >>> from astropy.samp import SAMPIntegratedClient, SAMP_STATUS_ERROR
        >>> cli = SAMPIntegratedClient()
        >>> ...
        >>> cli.ereply("abd", SAMP_STATUS_ERROR, result={},
        ...            error={"samp.errortxt": "Test error message"})
        )r8   r>   )r   r9   r;   r<   r=   r   r   r   ereply  s    zSAMPIntegratedClient.ereplyc                 C   s   | j |||S N)r   receive_notification)r   private_key	sender_idr,   r   r   r   rA     s    z)SAMPIntegratedClient.receive_notificationc                 C   s   | j ||||S r@   )r   receive_call)r   rB   rC   r9   r,   r   r   r   rD     s    z!SAMPIntegratedClient.receive_callc                 C   s   | j ||||S r@   )r   receive_response)r   rB   Zresponder_idr1   r:   r   r   r   rE     s    z%SAMPIntegratedClient.receive_responsec                 C   s   | j j||dd d d S )NT)declarer   )r   bind_receive_messager   r$   functionrF   r   r   r   r   rG     s    z)SAMPIntegratedClient.bind_receive_messagec                 C   s   | j |||| d S r@   )r   bind_receive_notificationrH   r   r   r   rJ     s    z.SAMPIntegratedClient.bind_receive_notificationc                 C   s   | j |||| d S r@   )r   bind_receive_callrH   r   r   r   rK     s    z&SAMPIntegratedClient.bind_receive_callc                 C   s   | j || d S r@   )r   bind_receive_response)r   r1   rI   r   r   r   rL     s    z*SAMPIntegratedClient.bind_receive_responsec                 C   s   | j || d S r@   )r   unbind_receive_notificationr   r$   rF   r   r   r   rM     s    z0SAMPIntegratedClient.unbind_receive_notificationc                 C   s   | j || d S r@   )r   unbind_receive_callrN   r   r   r   rO     s    z(SAMPIntegratedClient.unbind_receive_callc                 C   s   | j | d S r@   )r   unbind_receive_response)r   r1   r   r   r   rP     s    z,SAMPIntegratedClient.unbind_receive_responsec                 C   s   | j | d S r@   )r   declare_subscriptions)r   Zsubscriptionsr   r   r   rQ     s    z*SAMPIntegratedClient.declare_subscriptionsc                 C   s
   | j  S r@   )r   r   r   r   r   r   r     s    z$SAMPIntegratedClient.get_private_keyc                 C   s
   | j  S r@   )r   get_public_idr   r   r   r   rR     s    z"SAMPIntegratedClient.get_public_id)NNNNr   T)NNr   )NN)TN)TN)TN)T)T)N)+__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r   r!   r"   r#   r)   r*   r-   r.   r/   r0   r2   r3   r4   r5   r7   r8   r>   r?   rA   r   rD   rE   rG   rJ   rK   rL   rM   rO   rP   rQ   r   rR   r   r   r   r   r      sl   "  


!!&"%

!

















N)r   r   Z	hub_proxyr   __all__Z__doctest_skip__r   r   r   r   r   <module>   s   