ó
[³XMc           @   s\  d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l m	 Z	 d e
 f d „  ƒ  YZ d „  Z d	 „  Z e d
 ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z e d ƒ Z e d
 ƒ Z d „  Z e d ƒ d Z d e j e j f d „  ƒ  YZ e ƒ  Z e j ƒ  e j d ƒ d „  Z  d „  Z! d S(   sé   
Banana -- s-exp based protocol.

Future Plans: This module is almost entirely stable.  The same caveat applies
to it as applies to L{twisted.spread.jelly}, however.  Read its future plans
for more details.

@author: Glyph Lefkowitz
iÿÿÿÿN(   t   protocol(   t   styles(   t   logt   BananaErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyR      s   c         C   sd   |  d k r  | t  d ƒ ƒ d  S|  d k s5 t d ‚ x( |  r_ | t  |  d @ƒ ƒ |  d ?}  q8 Wd  S(   Ni    s!   can only encode positive integersi   i   (   t   chrt   AssertionError(   t   integert   stream(    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   int2b128   s    	c         C   sE   d } d } x2 |  D]* } t  | ƒ } | | | 7} | d K} q W| S(   sû   
    Convert an integer represented as a base 128 string into an C{int} or
    C{long}.

    @param st: The integer encoded in a string.
    @type st: C{str}

    @return: The integer value extracted from the string.
    @rtype: C{int} or C{long}
    i   i    i   (   t   ord(   t   stt   et   it   chart   n(    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   b1282int"   s    i€   i   i‚   iƒ   i„   i…   i†   i‡   c         C   s
   |  a  d S(   sk  
    Set the limit on the prefix length for all Banana connections
    established after this call.

    The prefix length limit determines how many bytes of prefix a banana
    decoder will allow before rejecting a potential object as too large.

    @type limit: C{int}
    @param limit: The number of bytes of prefix for banana to allow when
    decoding.
    N(   t   _PREFIX_LIMIT(   t   limit(    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   setPrefixLimitD   s    i@   i€  i   t   Bananac           B   s˜  e  Z d  d g Z dL Z e Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d Z d	 „  Z d
 „  Z i d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d! d" 6d# d$ 6d% d& 6d' d( 6d) d* 6d+ d, 6d- d. 6d/ d0 6d1 d2 6d3 d4 6d5 d6 6d7 d8 6d9 d: 6d; d< 6d= d> 6d? d@ 6dA dB 6dC dD 6dE dF 6dG dH 6Z i  Z x$ e j ƒ  D] \ Z Z e e e <q^Wd dI „ Z dJ „  Z dK „  Z RS(M   t   pbt   nonec         C   sJ   | |  _  d | d d |  _ d |  _ d	 |  _ d | d d |  _ d S(
   st   
        Set the prefix limit for decoding done by this protocol instance.

        @see: L{setPrefixLimit}
        i   i   i   i   Nl        lýÿÿÿ     l        l   ÿÿ (   t   prefixLimitt   _smallestLongIntt   _smallestIntt   _largestIntt   _largestLongInt(   t   selfR   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyR   \   s
    			c         C   s   d S(   sP   Surrogate for connectionMade
        Called after protocol negotiation.
        N(    (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   connectionReadyi   s    c         C   s   | |  _  |  j ƒ  d  S(   N(   t   currentDialectR   (   R   t   dialect(    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   _selectDialectn   s    	c         C   sº   |  j  r |  j | ƒ n |  j r} x‘ | D]3 } | |  j k r) |  j | ƒ |  j | ƒ Pq) q) Wt j d ƒ |  j j	 ƒ  n9 | |  j k rœ |  j | ƒ n t j d ƒ |  j j	 ƒ  d  S(   NsS   The client doesn't speak any of the protocols offered by the server: disconnecting.sY   The client selected a protocol the server didn't suggest and doesn't know: disconnecting.(
   R   t   expressionReceivedt   isClientt   knownDialectst   sendEncodedR!   R   t   msgt	   transportt   loseConnection(   R   t   objt	   serverVer(    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   callExpressionReceivedr   s    		c         C   s6   |  j  t ƒ d  |  _ |  j s2 |  j |  j ƒ n  d  S(   N(   R   R   t   NoneR   R#   R%   R$   (   R   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   connectionMade   s    		c         C   s8   |  j  } | r' | d d j | ƒ n |  j | ƒ d  S(   Niÿÿÿÿi   (   t	   listStackt   appendR+   (   R   t   itemt   l(    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   gotItem”   s    	t    c         C   sb  |  j  | } |  j } |  j } x3| rT|  j  | k sY t d t |  j  ƒ t | ƒ f ‚ | |  _  d } xT | D]  } | t k r… Pn  | d } qo W| |  j k r» t d |  j f ƒ ‚ n  d  S| |  } | | } | | d }	 t | ƒ |  j k rt d |  j f ƒ ‚ n  | t	 k r^t
 | ƒ } | t k rBt d ƒ ‚ n  | j | g  f ƒ |	 } nª| t k rÅt
 | ƒ } | t k r‘t d ƒ ‚ n  t |	 ƒ | k r¾|	 | } | |	 |  ƒ qd  SnC| t k rð|	 } t
 | ƒ } | | ƒ n| t k r|	 } t
 | ƒ } | | ƒ ní | t k rG|	 } t
 | ƒ } | | ƒ nÁ | t k rs|	 } t
 | ƒ } | | ƒ n• | t k r¥|	 } t
 | ƒ } | |  j | ƒ nc | t k rõt |	 ƒ d k rî|	 d } | t j d	 |	 d  ƒ d ƒ qd  Sn t d
 | f ƒ ‚ xF | rPt | d d ƒ | d d k rP| j ƒ  d }
 | |
 ƒ qWq" Wd |  _  d  S(   Ns   This ain't right: %s %si    i   s1   Security precaution: more than %d bytes of prefixs9   Security precaution: longer than %d bytes worth of prefixs#   Security precaution: List too long.s%   Security precaution: String too long.i   s   !ds   Invalid Type Byte %riÿÿÿÿR3   (   t   bufferR.   R2   R   t   reprt   HIGH_BIT_SETR   R   t   lent   LISTR   t
   SIZE_LIMITR/   t   STRINGt   INTt   LONGINTt   LONGNEGt   NEGt   VOCABt   incomingVocabularyt   FLOATt   structt   unpackt   NotImplementedErrort   pop(   R   t   chunkR4   R.   R2   t   post   cht   numt   typebytet   restR0   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   dataReceived   s~    			1	

	

!+c         C   s   t  ƒ  ‚ d S(   sF   Called when an expression (list, string, or int) is received.
        N(   RD   (   R   t   lst(    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyR"   ã   s    i   R,   i   t   classi   t   dereferencei   t	   referencei   t
   dictionaryi   t   functioni   t   instancei   t   listi	   t   modulei
   t
   persistenti   t   tuplei   t   unpersistablei   t   copyi   t   cachei   t   cachedi   t   remotei   t   locali   t   lcachei   t   versioni   t   logini   t   passwordi   t	   challengei   t	   logged_ini   t   not_logged_ini   t   cachemessagei   t   messagei   t   answeri   t   errori   t   decrefi   t   decachei   t   uncachec         C   s4   g  |  _  t j |  j ƒ |  _ d |  _ | |  _ d  S(   Ni    (   R.   RY   t   outgoingVocabularyt   outgoingSymbolst   outgoingSymbolCountR#   (   R   R#   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   __init__  s    		c         C   s?   t  j ƒ  } |  j | | j ƒ | j ƒ  } |  j j | ƒ d  S(   N(   t	   cStringIOt   StringIOt   _encodet   writet   getvalueR'   (   R   R)   t   iot   value(    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyR%     s    c         C   sV  t  | t t f ƒ r„ t | ƒ t k rC t d t | ƒ f ƒ ‚ n  t t | ƒ | ƒ | t ƒ xï| D] } |  j | | ƒ qg WnÎt  | t	 t
 f ƒ ra| |  j k  s· | |  j k rÍ t d | f ƒ ‚ n  | |  j k  r÷ t | | ƒ | t ƒ qR| d k  rt | | ƒ | t ƒ qR| |  j k rGt | | ƒ | t ƒ qRt | | ƒ | t ƒ nñ t  | t ƒ r“| t ƒ | t j d | ƒ ƒ n¿ t  | t ƒ r?|  j d k rç| |  j k rç|  j | } t | | ƒ | t ƒ qRt | ƒ t k rt d t | ƒ f ƒ ‚ n  t t | ƒ | ƒ | t ƒ | | ƒ n t d | f ƒ ‚ d  S(   Ns#   list/tuple is too long to send (%d)s"   int/long is too large to send (%d)i    s   !dR   s   string is too long to send (%d)s   could not send object: %r(   t
   isinstanceRT   RW   R7   R9   R   R
   R8   Rr   t   intt   longR   R   R   R=   R>   R   R;   R<   t   floatRA   RB   t   packt   strR   Rm   R?   R:   (   R   R)   Rs   t   elemt   symbolID(    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyRr      sL    


N(   R   R   R$   R,   R   R9   t	   sizeLimitR   R   R!   R+   R-   R2   R4   RL   R"   Rl   R@   t   itemst   kt   vRo   R%   Rr   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyR   V   sd   							F	
	R   c         C   s,   t  j ƒ  } | t _ t j |  ƒ | j ƒ  S(   s   Encode a list s-expression.(   Rp   Rq   t   _iR'   R%   Rt   (   RM   Ru   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   encodeS  s    	c         C   s>   g  } | j  t _ z t j |  ƒ Wd d t _ t ` X| d S(   s)   
    Decode a banana-encoded string.
    NR3   i    (   R/   Rƒ   R"   RL   R4   (   R   R1   (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   decode[  s    	i  
 ("   t   __doc__RY   Rp   RB   t   twisted.internetR    t   twisted.persistedR   t   twisted.pythonR   t	   ExceptionR   R
   R   R   R8   R;   R:   R>   RA   R<   R=   R?   R6   R   R9   t   Protocolt	   EphemeralR   Rƒ   R-   R!   R„   R…   (    (    (    s9   /usr/lib/python2.7/dist-packages/twisted/spread/banana.pyt   <module>   s2   $	
		
ø	
	