ó
[³XMc           @   sa  d  Z  d d l m Z d d l m Z d d l m Z m Z d d l m	 Z	 d d l
 m Z d d l m Z m Z d d l Z d d	 l m Z d d l Z d d l Z d d l Z d d l Z d
 d d „  ƒ  YZ d e f d „  ƒ  YZ d d d d d e d „ Z d „  Z i d d 6Z d e j f d „  ƒ  YZ d d d „  ƒ  YZ d e j  f d „  ƒ  YZ  d S(   sD   L{twisted.manhole} L{PB<twisted.spread.pb>} service implementation.
iÿÿÿÿ(   t	   copyright(   t   pb(   t   logt   failure(   t   portal(   t   service(   t
   implementst	   InterfaceN(   t   StringIOt	   FakeStdIOc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   typet   list(   t   selft   type_R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   __init__   s    	c         C   sF   t  j d |  j t j t | ƒ ƒ f ƒ |  j j |  j | f ƒ d  S(   Ns   %s: %s(   R   t   msgR
   t   stringt   stript   strR   t   append(   R   t   text(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   write"   s    )c         C   s   d  S(   N(    (   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   flush&   s    c   	      C   sã   |  j  s d S|  j  } g  } | d } d } x° t d t |  j  ƒ ƒ D]– } | | \ } } | | k rm qE qE | | d k r‘ | j | | ƒ n> t d „  | | | !ƒ } t j | d ƒ } | j | | f ƒ | } | } qE Wd S(   s³   Concatenate adjacent messages of same type into one.

        Greatly cuts down on the number of elements, increasing
        network transport friendliness considerably.
        Ni    i   c         S   s   |  d S(   Ni   (    (   t   l(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   <lambda>>   s    t    (   R   t   xranget   lenR   t   mapR   t   join(	   R   t   inlistt   outlistt	   last_typet   block_begint   it   mtypet   messaget   messages(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   consolidate)   s$    		
	(   t   __name__t
   __module__R   R   R   R&   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR	      s   			t   IManholeClientc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   d S(   sO  Takes a list of (type, message) pairs to display.

        Types include:
            - "stdout" -- string sent to sys.stdout

            - "stderr" -- string sent to sys.stderr

            - "result" -- string repr of the resulting value
                 of the expression

            - "exception" -- a L{failure.Failure}
        N(    (   t   list_of_messages(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   consoleG   s    c         C   s   d S(   s&   Receives an explorer.Explorer
        N(    (   t   xplorer(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   receiveExplorerU   s    c           C   s   d S(   sc   List what manholey things I am capable of doing.

        i.e. C{"Explorer"}, C{"Failure"}
        N(    (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   listCapabilitiesY   s    (   R'   R(   R+   R-   R.   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR)   F   s   		c         C   sâ  g  } t  d | ƒ }	 t  d | ƒ }
 t  d | ƒ } d } d } | d k rZ t | ƒ } n  | d k ro d } n  | d k r„ i  } n  | d k r™ | } n  | d k rÁ t |  ƒ rÁ t d ƒ ‚ n  y° t j } t j } |	 t _ |
 t _ zr t |  ƒ rt |  | | ƒ } nM y t	 |  | d ƒ } Wn t	 |  | d ƒ } n X| rYt
 | | | ƒ } n  Wd | t _ | t _ XWnM t j ƒ  \ } } } t j | | | ƒ } ~ | j t j | | ƒ ƒ n X| rÞ|	 j ƒ  | | ƒ n  | S(	   s€  Run this, directing all output to the specified console.

    If command is callable, it will be called with the args and keywords
    provided.  Otherwise, command will be compiled and eval'd.
    (Wouldn't you like a macro?)

    Returns the command's return value.

    The console is called with a list of (type, message) pairs for
    display, see L{IManholeClient.console}.
    t   stdoutt   stderrt	   exceptions,   Need a namespace to evaluate the command in.t   evalt   singleN(    (   R	   t   NoneR   t   callablet
   ValueErrort   sysR/   R0   t   applyt   compileR2   t   exc_infoR   t   FailureR   R   t   failure2CopyableR&   (   t   commandR+   t   globalNSt   localNSt   filenamet   argst   kwt   unsafeTracebackst   outputt   fakeoutt   fakeerrt   errfilet   codet   valt   outt   errt   eTypet   eValt   tbt   fail(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   runInConsole_   sR    								
c      	   C   sƒ   d d l  } g  } xJ |  j D]? } | j | d | d | d | j | d | d ƒ f ƒ q Wi | d 6t j |  j |  j ƒ d 6S(   sò  Pre-Failure manhole representation of exceptions.

    For compatibility with manhole clients without the "Failure"
    capability.

    A dictionary with two members:
        - 'traceback' -- traceback.extract_tb output; a list of tuples
             (filename, line number, function name, text) suitable for
             feeding to traceback.format_list.

        - 'exception' -- a list of one or more strings, each
             ending in a newline. (traceback.format_exception_only output)
    iÿÿÿÿNi   i   i    t	   tracebackR1   (   t	   linecachet   framesR   t   getlineRQ   t   format_exception_onlyR
   t   value(   RO   RR   RN   t   f(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   _failureOldStyle    s    =t   Sett   Explorert   Perspectivec           B   sƒ   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z RS(   i    c         C   s4   i | d 6|  d 6d  d 6|  _ i  |  _ | |  _ d  S(   NR   t   avatart   _(   R4   t   localNamespacet   clientsR   (   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR   Á   s    	c         C   s>   |  j  j ƒ  } i  | d <| d j d ƒ r: | d d =n  | S(   NR_   R^   t   __builtins__(   t   __dict__t   copyt   has_key(   R   t   state(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   __getstate__Ê   s
    
c         C   sÆ   | |  j  | <d j t t | j j j ƒ  d ƒ ƒ } |  j j i t	 | d t | ƒ ƒ d 6| d 6t
 j d 6} | j d d | f g ƒ t | _ | j d	 ƒ j |  j |  j d
 | f d | f ƒd S(   sH   A client has attached -- welcome them and add them to the list.
        t   :i   t   namet   yout   hostt   longversionR+   R/   R.   t   callbackArgst   errbackArgsN(   R_   R   R   R   t   brokert	   transportt   getHostR   t   welcomeMessaget   getattrR    Rj   t
   callRemotet   _defaultCapabilitiest   capabilitiest   addCallbackst   _cbClientCapablet   _ebClientCapable(   R   t   clientt   identityRi   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   attachedÑ   s    (	c         C   s&   y |  j  | =Wn t k
 r! n Xd  S(   N(   R_   t   KeyError(   R   Rx   Ry   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   detachedå   s    c         O   sC   t  | |  j |  j j |  j t |  j ƒ d | d | d |  j j ƒS(   s:   Convience method to "runInConsole with my stuff".
        RA   RB   RC   (   RP   R+   R   t	   namespaceR^   R   RC   (   R   R=   RA   RB   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyRP   ë   s    	c         C   s  |  j  j ƒ  } | } d } xø | D]ð } yÃ | j j d ƒ sÚ | d k rÇ | } xt t t | ƒ ƒ D]] } | | d d k rc t | | d t j	 ƒ rc | | d t
 | | d ƒ f | | <qc qc Wn  | j d | ƒ n | j d | ƒ Wq" t j k
 r|  j | d ƒ q" Xq" Wd S(   s/   Pass a message to my clients' console.
        R;   i    R1   i   R+   N(   R_   t   keysR4   Rt   Rc   R   R   t
   isinstanceR   R;   RX   Rr   R   t   ProtocolErrorR|   (   R   R$   R_   t   origMessaget   compatMessageRx   R"   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR+   ú   s"    %c         C   s_   |  j  j ƒ  } xI | D]A } y | j d | ƒ Wq t j k
 rV |  j | d ƒ q Xq Wd S(   s+   Pass an Explorer on to my clients.
        R-   N(   R_   R~   Rr   R   R€   R|   R4   (   R   t
   objectLinkR_   Rx   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR-     s    c         C   s$   |  j  d d | | f f g ƒ | S(   Nt   results   Deferred #%s Result: %r
(   R+   (   R   RI   t   dnum(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt	   _cbResult  s     c         C   s*   t  j d t | ƒ | f ƒ | | _ d  S(   Ns   client %x has %s(   R   R   t   idRt   (   R   Rt   Rx   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyRv   "  s    c         C   s%   | j  t ƒ t j d | f ƒ d  S(   Ns5   Couldn't get capabilities from %s, assuming defaults.(   t   trapt   AttributeErrorR   R   (   R   t   reasonRx   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyRw   &  s    	c         C   sÑ   t  j d | ƒ |  j | ƒ } | d
 k	 rÀ | |  j d <d d l m } t | | ƒ r |  j d 7_ |  j	 d d |  j f g ƒ | j
 |  j |  j ƒ qÀ |  j	 d t | ƒ d f g ƒ n  t  j d	 ƒ d
 S(   sÈ   Evaluate the given expression, with output to the console.

        The result is stored in the local variable '_', and its repr()
        string is sent to the console as a "result" message.
        s   >>> %sR]   iÿÿÿÿ(   t   Deferredi   R„   s   Waiting for Deferred #%s...
s   
s   <<<N(   R   R   RP   R4   R^   t   twisted.internet.deferR‹   R   t   lastDeferredR+   t   addBothR†   t   repr(   R   t   exprRI   R‹   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   perspective_do-  s    #c         C   s>   |  j  | ƒ } | r: t j j | | ƒ } |  j | ƒ n  d S(   s¬   Browse the object obtained by evaluating the identifier.

        The resulting ObjectLink is passed back through the client's
        receiveBrowserObject method.
        N(   RP   t   explorert   explorerPoolt   getExplorerR-   (   R   t
   identifiert   objectt   expl(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   perspective_exploreA  s    c         C   si   t  ‚ |  j | ƒ } | re |  j |  j j | | ƒ } |  j | ƒ |  j |  j j | | |  j ƒ n  d S(   sÜ   Watch the object obtained by evaluating the identifier.

        Whenever I think this object might have changed, I will pass
        an ObjectLink of it back to the client's receiveBrowserObject
        method.
        N(   t   NotImplementedErrorRP   t   browsert   browseObjectR-   t   watchObject(   R   R•   R–   t   oLink(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   perspective_watchL  s    (   R'   R(   R   R   Re   Rz   R|   RP   R+   R-   R†   Rv   Rw   R‘   R˜   Rž   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR[   ¿   s   													t   Realmc           B   s'   e  Z e e j ƒ d  „  Z d „  Z RS(   c         C   s   | |  _  i  |  _ d  S(   N(   R   t   _cache(   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR   d  s    	c            s   t  j | k r t d ƒ ‚ n  ˆ |  j k r= |  j ˆ ‰ n t |  j ƒ ‰ ˆ j ˆ  ˆ ƒ ‡ ‡ ‡  f d †  } t  j ˆ | f S(   Ns   no interfacec              s   ˆ  j  ˆ ˆ ƒ d  S(   N(   R|   (    (   t   pt   avatarIdt   mind(    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR|   p  s    (   R   t   IPerspectiveR™   R    R[   R   Rz   (   R   R¢   R£   t
   interfacesR|   (    (   R£   R¡   R¢   s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   requestAvatarh  s    (   R'   R(   R   R   t   IRealmR   R¦   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyRŸ   `  s   	t   Servicec           B   s&   e  Z d  Z e d d „ Z d „  Z RS(   sB   
Hello %(you)s, welcome to Manhole on %(host)s.
%(longversion)s.

c         C   sJ   | |  _  i d t |  ƒ f d 6t d 6|  _ | rF |  j j | ƒ n  d  S(   Ns   __manhole%x__R'   R7   (   RC   R‡   R7   R}   t   update(   R   RC   R}   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR   |  s    	c         C   sF   |  j  j ƒ  } | d j ƒ  } | | d <| j d ƒ rB | d =n  | S(   sA   This returns the persistent state of this shell factory.
        R}   R`   (   Ra   Rb   Rc   (   R   t   dictt   ns(    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyRe   …  s    

N(   R'   R(   Rp   t   FalseR4   R   Re   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyR¨   u  s   	(    (    (!   t   __doc__t   twistedR    t   twisted.spreadR   t   twisted.pythonR   R   t   twisted.credR   t   twisted.applicationR   t   zope.interfaceR   R   R’   t	   cStringIOR   R   R7   RQ   t   typesR	   R)   R4   R¬   RP   RX   Rs   t   AvatarR[   RŸ   R¨   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/manhole/service.pyt   <module>   s,   )@	
¡