B
    îq\.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.7/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| 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   s8   | j r4z| j ¡  W d| jjr(| j ¡  | j ¡  X 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   Ú
disconnect)r   r   r   r   r   u   s    
zSAMPIntegratedClient.disconnectc             C   s
   | j  ¡ S )zL
        Proxy to ``ping`` SAMP Hub method (Standard Profile only).
        )r   Úping)r   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   Ú	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   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|k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 )a9  
        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 keywords
            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 keywords
            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   r.     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 keywords
            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"})
        )r.   r'   )r   r)   r/   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   r/   r*   r   r   r   r1   /  s    zSAMPIntegratedClient.call_allc             K   s   |   ||  ||¡¡ dS )a[  
        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 keywords
            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)r1   r'   )r   r/   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   r3   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 keywords
            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"})
        )r3   r'   )r   r)   r#   r4   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   r6   ‚  s    zSAMPIntegratedClient.replyc             C   s8   d|i}|d k	r|  d|i¡ |d k	r4|  d|i¡ |S )Nzsamp.statuszsamp.resultz
samp.error)r$   )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"})
        )r6   r<   )r   r7   r9   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   r>   ³  s    z)SAMPIntegratedClient.receive_notificationc             C   s   | j  ||||¡S )N)r   Úreceive_call)r   r?   r@   r7   r*   r   r   r   rA   ¸  s    z!SAMPIntegratedClient.receive_callc             C   s   | j  ||||¡S )N)r   Úreceive_response)r   r?   Zresponder_idr/   r8   r   r   r   rB   ½  s    z%SAMPIntegratedClient.receive_responsec             C   s   | j j||dd d d S )NT)Údeclarer   )r   Úbind_receive_message)r   r#   ÚfunctionrC   r   r   r   r   rD   Â  s    z)SAMPIntegratedClient.bind_receive_messagec             C   s   | j  ||||¡ d S )N)r   Úbind_receive_notification)r   r#   rE   rC   r   r   r   r   rF   Ç  s    z.SAMPIntegratedClient.bind_receive_notificationc             C   s   | j  ||||¡ d S )N)r   Úbind_receive_call)r   r#   rE   rC   r   r   r   r   rG   Ì  s    z&SAMPIntegratedClient.bind_receive_callc             C   s   | j  ||¡ d S )N)r   Úbind_receive_response)r   r/   rE   r   r   r   rH   Ñ  s    z*SAMPIntegratedClient.bind_receive_responsec             C   s   | j  ||¡ d S )N)r   Úunbind_receive_notification)r   r#   rC   r   r   r   rI   Ö  s    z0SAMPIntegratedClient.unbind_receive_notificationc             C   s   | j  ||¡ d S )N)r   Úunbind_receive_call)r   r#   rC   r   r   r   rJ   Û  s    z(SAMPIntegratedClient.unbind_receive_callc             C   s   | j  |¡ d S )N)r   Úunbind_receive_response)r   r/   r   r   r   rK   à  s    z,SAMPIntegratedClient.unbind_receive_responsec             C   s   | j  |¡ d S )N)r   Údeclare_subscriptions)r   Zsubscriptionsr   r   r   rL   å  s    z*SAMPIntegratedClient.declare_subscriptionsc             C   s
   | j  ¡ S )N)r   r   )r   r   r   r   r   ê  s    z$SAMPIntegratedClient.get_private_keyc             C   s
   | j  ¡ S )N)r   Úget_public_id)r   r   r   r   rM   ï  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-   r.   r0   r1   r2   r3   r5   r6   r<   r=   r>   r   rA   rB   rD   rF   rG   rH   rI   rJ   rK   rL   r   rM   r   r   r   r   r      sh   ! 

!!&"%

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