σ
ίΘ[c           @` sE  d  Z  d d l m Z m Z m Z m Z d d l Z d d l Z d d l m	 Z	 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   Z d g Z d g Z d   Z d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d   Z d S(   u   
Utility functions and classes
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   queuet   range(   t   urlopen(   t   xmlrpc_client(   t   StringIOi   (   t   SAMP_STATUS_ERROR(   t   SAMPProxyErrorc          C` sN   d d l  m }  |  j s t Sy t d d d t SWn t k
 rI t SXd  S(   Ni   (   t   confu   http://google.comt   timeoutg      π?(   t    R   t   use_internett   FalseR   t   Truet	   Exception(   R   (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyt   internet_on   s    	u   SAMPMsgReplierWrapperu   .c         C` sK   d | k r: | j  d d  \ } } t t |  |  |  St |  |  Sd S(   u%   
    Get attributes recursively.
    u   .i   N(   t   splitt   getattr_recursivet   getattr(   t   variablet	   attributet   topt	   remaining(    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR   '   s    t   _ServerProxyPoolMethodc           B` s#   e  Z d    Z d   Z d   Z RS(   c         C` s   | |  _  | |  _ d  S(   N(   t   _ServerProxyPoolMethod__proxiest   _ServerProxyPoolMethod__name(   t   selft   proxiest   name(    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyt   __init__7   s    	c         C` s   t  |  j d j |  j |   S(   Nu   {}.{}(   R   R   t   formatR   (   R   R   (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyt   __getattr__;   s    c         O` s~   |  j  j   } t | |  j  } zE y | | |   } Wn+ t j k
 rd } t | j | j   n XWd  |  j  j	 |  X| S(   N(
   R   t   getR   R   t   xmlrpct   FaultR
   t	   faultCodet   faultStringt   put(   R   t   argst   kwrdst   proxyt   functiont   responset   exc(    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyt   __call__>   s     (   t   __name__t
   __module__R    R"   R/   (    (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR   2   s   		t   ServerProxyPoolc           B` s    e  Z d  Z d   Z d   Z RS(   u=   
    A thread-safe pool of `xmlrpc.ServerProxy` objects.
    c         O` sF   t  j |  |  _ x- t |  D] } |  j j | | |    q Wd  S(   N(   R   t   Queuet   _proxiesR   R(   (   R   t   sizet   proxy_classR)   t   keywordst   i(    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR    O   s    c         C` s   t  |  j |  S(   N(   R   R4   (   R   R   (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR"   U   s    (   R0   R1   t   __doc__R    R"   (    (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR2   J   s   	t   SAMPMsgReplierWrapperc           B` s    e  Z d  Z d   Z d   Z RS(   u  
    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(   N(   t   cli(   R   R;   (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR    f   s    c         ` s      f d   } | S(   Nc          ` sτ   t     d k s" |  d d  k r/   |    nΑ yM   |    } | r{  j j j  j j   |  d i t d 6| d 6 n  Wnq t k
 rο t   } t	 j
 d |  | j   }  j j j  j j   |  d i t d 6i | d 6d 6 n Xd  S(   Ni   i   u   samp.statusu   samp.resultt   fileu   txt(   t   get_num_argst   NoneR;   t   hubt   replyt   get_private_keyR	   R   R   t	   tracebackt	   print_exct   getvalue(   R)   t   resultt   errt   txt(   t   fR   (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyt	   wrapped_fk   s    "
	
(    (   R   RH   RI   (    (   RH   R   s1   lib/python2.7/site-packages/astropy/samp/utils.pyR/   i   s    (   R0   R1   R9   R    R/   (    (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR:   Z   s   
	t   _HubAsClientc           B` s   e  Z d    Z d   Z RS(   c         C` s   | |  _  d  S(   N(   t   _handler(   R   t   handler(    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR       s    c         C` s   t  |  j |  S(   N(   t   _HubAsClientMethodRK   (   R   R   (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR"      s    (   R0   R1   R    R"   (    (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyRJ      s   	RM   c           B` s#   e  Z d    Z d   Z d   Z RS(   c         C` s   | |  _  | |  _ d  S(   N(   t   _HubAsClientMethod__sendt   _HubAsClientMethod__name(   R   t   sendR   (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR       s    	c         C` s   t  |  j d j |  j |   S(   Nu   {}.{}(   RM   RN   R!   RO   (   R   R   (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR"      s    c         G` s   |  j  |  j |  S(   N(   RN   RO   (   R   R)   (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR/      s    (   R0   R1   R    R"   R/   (    (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyRM      s   		c         C` sI   t  j |   r  |  j j j d St  j |   r9 |  j j St d   d S(   uW   
    Find the number of arguments a function or method takes (excluding ``self``).
    i   u"   f should be a function or a methodN(   t   inspectt   ismethodt   __func__t   __code__t   co_argcountt
   isfunctiont	   TypeError(   RH   (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyR=      s
    
(    R9   t
   __future__R    R   R   R   RQ   RB   t   extern.six.movesR   R   t   extern.six.moves.urllib.requestR   R   R$   t
   extern.sixR   t	   constantsR	   t   errorsR
   R   t   __all__t   __doctest_skip__R   t   objectR   R2   R:   RJ   RM   R=   (    (    (    s1   lib/python2.7/site-packages/astropy/samp/utils.pyt   <module>   s&   "				*
