ó
‹²,]c           @   sd  d  d l  Z  d  d l Z d  d l j Z d  d l Z d  d l Z d  d l m Z m	 Z	 m
 Z
 m Z m Z m Z d  d l m Z d  d l Z d  d l m Z m Z d Z d Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ e d k r`e e j ƒ Z e d ƒ Z e j ƒ  d „  Z d „  Z d „  Z e j  j! e ƒ e j" j! e ƒ e j# j! e ƒ e j$ ƒ  n  d S(   iÿÿÿÿN(   t   QObjectt   QProcesst   QProcessEnvironmentt   QSocketNotifiert   QTimert   Signal(   t   QApplication(   t   debug_printt   get_module_pathiè  s	   127.0.0.1t   AsyncClientc           B   s†   e  Z d  Z e ƒ  Z e ƒ  Z e e ƒ Z d	 d	 d	 d	 d	 d	 d „ Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(
   sL   
    A class which handles a connection to a client through a QProcess.
    c         C   sÖ   t  t |  ƒ j ƒ  | p t j |  _ | |  _ | |  _ | p@ |  |  _ | |  _ | |  _	 | |  _
 t |  _ t |  _ d  |  _ d  |  _ t j ƒ  |  _ t j ƒ  j j |  j ƒ t |  ƒ |  _ |  j j j |  j ƒ d  S(   N(   t   superR	   t   __init__t   syst
   executablet
   extra_argst   targett   namet   libst   cwdt   envt   Falset   is_initializedt   closingt   Nonet   notifiert   processt   zmqt   Contextt   contextR   t   instancet   aboutToQuitt   connectt   closeR   t   timert   timeoutt
   _heartbeat(   t   selfR   R   R   R   R   R   R   (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyR   ,   s     									c         C   s`  |  j  j t j ƒ |  _ |  j j d t ƒ |  _ t |  ƒ |  _ |  j	 r_ |  j j
 |  j	 ƒ n  d |  j t |  j ƒ g } |  j d	 k	 r™ | |  j 7} n  t ƒ  } |  j j ƒ  } |  j rÉ d |  j k sØ |  j d	 k rat j t d ƒ ƒ } x] |  j D]R } y8 t j t j | ƒ d ƒ } t j j | | g ƒ } Wq÷ t k
 rHq÷ Xq÷ W| j d | ƒ n  |  j r}| j |  j ƒ n  x6 | D]. } | j d ƒ \ } }	 }
 | j | |
 ƒ q„W|  j j | ƒ |  j j  |  j! | ƒ |  j j" j# |  j$ ƒ |  j j% ƒ  } | st& d |  ƒ d	 S|  j j' t j( ƒ } t) | t) j* |  ƒ |  _+ |  j+ j, j# |  j- ƒ d	 S(
   s/   Handle the connection with the server.
        s   tcp://%ss   -ut
   PYTHONPATHt   spyderi   s   PYTHONPATH=%st   =s   Could not start %sN(.   R   t   socketR   t   PAIRt   bind_to_random_portt	   LOCALHOSTt   portR   R   R   t   setWorkingDirectoryR   t   strR   R   R   t   systemEnvironmentR   t   ospt   dirnameR   R   t   impt   find_modulet   pathsept   joint   ImportErrort   appendt   updatet	   partitiont   insertt   setProcessEnvironmentt   startR   t   finishedR   t   _on_finishedt   waitForStartedR   t
   getsockoptt   FDR   t   ReadR   t	   activatedt   _on_msg_received(   R$   t   p_argst   processEnvironmentR   t   python_patht   libt   patht   envItemt   envNamet	   separatort   envValuet   runningt   fid(    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyt   runA   sD    		'	c      	   O   sN   |  j  s d St j ƒ  j } t d | d | d | d | ƒ } |  j | ƒ | S(   sù   Send a request to the server.

        The response will be a dictionary the 'request_id' and the
        'func_name' as well as a 'result' field with the object returned by
        the function call or or an 'error' field with a traceback.
        Nt	   func_namet   argst   kwargst
   request_id(   R   t   uuidt   uuid4t   hext   dictt   _send(   R$   RQ   RR   RS   RT   t   request(    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyRZ   t   s    		c         C   s­   t  |  _ t |  _ |  j j ƒ  |  j d k	 r` |  j j j	 |  j
 ƒ |  j j t ƒ d |  _ n  |  j d ƒ |  j d k	 rœ |  j j d ƒ |  j j ƒ  n  |  j j ƒ  d S(   s4   Cleanly close the connection to the server.
        t   server_quitiè  N(   t   TrueR   R   R   R!   t   stopR   R   RC   t
   disconnectRD   t
   setEnabledRZ   R   t   waitForFinishedR    R   t   destroy(   R$   (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyR    …   s    		c         C   s»   |  j  r d S|  j rs t d |  j ƒ t |  j j ƒ  ƒ t |  j j ƒ  ƒ t |  _ |  j j	 t ƒ |  j
 ƒ  nD t d |  j ƒ t |  j j ƒ  ƒ t |  j j ƒ  ƒ |  j j ƒ  d S(   s3   Handle a finished signal from the process.
        Ns   Restarting %ss
   Errored %s(   R   R   R   R   R   t   readAllStandardOutputt   readAllStandardErrorR   R   R_   RP   t   erroredt   emit(   R$   (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyR>   ˜   s    			c         C   sÂ   |  j  j t ƒ x« y |  j j d t j ƒ } Wn% t j k
 rV |  j  j t ƒ d SX|  j	 s t |  _	 t
 d |  j ƒ |  j j ƒ  |  j j t ƒ q n  |  j | d <|  j j | ƒ q Wd S(   s2   Handle a message trigger from the socket.
        t   flagsNs   Initialized %sR   (   R   R_   R   R(   t
   recv_pyobjR   t   NOBLOCKt   ZMQErrorR\   R   R   R   t   initializedRe   R!   R<   t	   HEARTBEATt   received(   R$   t   resp(    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyRD   ª   s    		c         C   s   |  j  t d d ƒ ƒ d S(   s3   Send a heartbeat to keep the server alive.
        RQ   t   server_heartbeatN(   RY   RX   (   R$   (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyR#   ½   s    c         C   sQ   y |  j  j | t j ƒ Wn0 t k
 rL } t | ƒ t |  _ |  j ƒ  n Xd S(   s&   Send an object to the server.
        N(	   R(   t
   send_pyobjR   Rh   t	   ExceptionR   R   R   R>   (   R$   t   objt   e(    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyRY   Â   s    
	N(   t   __name__t
   __module__t   __doc__R   Rj   Rd   t   objectRl   R   R   RP   RZ   R    R>   RD   R#   RY   (    (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyR	      s   			3					t   PluginClientc           B   s   e  Z d d d d  „ Z RS(   c         C   sY   t  j j t ƒ } t t |  ƒ j d d | d | d | d | g d | g ƒ| |  _ d  S(   Ns   plugin_server.pyR   R   R   R   R   (   t   osRI   R1   t   __file__R
   Rw   R   R   (   R$   t   plugin_nameR   R   t
   extra_pathR   (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyR   Ï   s    N(   Rs   Rt   R   R   (    (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyRw   Í   s   t   __main__t   jedic         C   s3   |  GH|  d d k r" t  j ƒ  n t j d ƒ d  S(   NRQ   t   foo(   t   appt   quitt   pluginRZ   (   t   value(    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyt   handle_returnÞ   s    c           C   s   d GHt  j d ƒ d  S(   NRd   i   (   R   t   exit(    (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyt   handle_erroredå   s    c           C   s   d GHt  j d ƒ d  S(   NR<   t   validate(   R   RZ   (    (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyR<   é   s    (%   R2   Rx   t   os.pathRI   R0   R   RU   t   qtpy.QtCoreR    R   R   R   R   R   t   qtpy.QtWidgetsR   R   t   spyder.config.baseR   R   Rk   R+   R	   Rw   Rs   t   argvR   R   RP   Rƒ   R…   R<   Rd   R   Rl   Rj   t   exec_(    (    (    sG   lib/python2.7/site-packages/spyder/utils/introspection/plugin_client.pyt   <module>   s.   .°
			