
    Ϫf                        d 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  G d d      Z G d	 d
      Z G d d      Z G d d      Z G d de      Z eej"                         G d de             Z G d dej&                        Zd Zy)z
Utility classes for spread.
    )implementer)defer
interfaces)basic)Failure)pbc                       e Zd Zd Zd Zy)LocalMethodc                      || _         || _        y N)localname)selfr   r   s      5/usr/lib/python3/dist-packages/twisted/spread/util.py__init__zLocalMethod.__init__   s    
	    c                 V     | j                   j                  | j                  g|i |S r   )r   
callRemoter   )r   argskws      r   __call__zLocalMethod.__call__   s'    $tzz$$TYY<<<<r   N)__name__
__module____qualname__r   r    r   r   r
   r
      s    =r   r
   c                        e Zd ZdZdZd Zd Zy)LocalAsRemotezN
    A class useful for emulating the effects of remote behavior locally.
       c                 4   t        | d|z         r t        | d|z         |i |S 	 t        | d|z         }t        j                   ||i |      S # t        $ r> t               }| j                  r|j                          t        j                  |      cY S w xY w)ax  
        Call a specially-designated local method.

        self.callRemote('x') will first try to invoke a method named
        sync_x and return its result (which should probably be a
        Deferred).  Second, it will look for a method called async_x,
        which will be called and then have its result (or Failure)
        automatically wrapped in a Deferred.
        sync_async_)	hasattrgetattrr   succeedBaseExceptionr   reportAllTracebacksprintTracebackfail)r   r   r   r   methodfs         r   r   zLocalAsRemote.callRemote#   s     44(07440$="==	!T8d?3F==!4!455 	!	A''  "::a= 		!s   )A ABBc                     t        | |      S r   )r
   )r   r   s     r   remoteMethodzLocalAsRemote.remoteMethod8   s    4&&r   N)r   r   r   __doc__r&   r   r,   r   r   r   r   r      s     !*'r   r   c                   $    e Zd ZdZddZd Zd Zy)LocalAsyncForwarderzD
    A class useful for forwarding a locally-defined interface.
    c                 T    |j                  |      sJ || _        || _        || _        y r   )
providedBy	forwardedinterfaceClassfailWhenNotImplemented)r   r2   r3   r4   s       r   r   zLocalAsyncForwarder.__init__A   s-    ((333",&<#r   c                 :     t        | j                  |      |i |S r   )r#   r2   )r   r)   r   r   s       r   _callMethodzLocalAsyncForwarder._callMethodG   s    .wt~~v.;;;r   c                    | j                   j                  |      r&t        j                  | j                  |g|i |}|S | j
                  r&t        j                  t        t        d|z              S t        j                  d       S )NzNo Such Method in Interface: %s)
r3   queryDescriptionForr   maybeDeferredr6   r4   r(   r   NotImplementedErrorr$   )r   r)   r   r   results        r   r   zLocalAsyncForwarder.callRemoteJ   sz    226:(()9)96ODOBOFM((::+-NQW-WX  ==&&r   N)r   )r   r   r   r-   r   r6   r   r   r   r   r/   r/   <   s    =<	'r   r/   c                   0    e Zd ZdZddZd Zd Zd Zd Zy)	Pagerz5
    I am an object which pages out information.
    Nc                     t        |      r|| _        || _        || _        nd| _        d| _        || _        |j                  j                  |        y)z
        Create a pager with a Reference to a remote collector and
        an optional callable to invoke upon completion.
        Nr   )callablecallbackcallbackArgscallbackKeyword_stillPaging	collectorbrokerregisterPageProducer)r   rD   r@   r   r   s        r   r   zPager.__init__[   sM    
 H$DM $D#%D  DM"--d3r   c                     | j                   sO| j                  j                  dd       | j                  & | j                  | j                  i | j
                   | j                   S )5
        (internal) Method called by Broker.
        endedPagingFpbanswer)rC   rD   r   r@   rA   rB   r   s    r   stillPagingzPager.stillPagingj   sY       NN%%me%D}}(t00ID4H4HI   r   c                 \    | j                   j                  d| j                         d       y)rH   gotPageFrJ   N)rD   r   nextPagerL   s    r   sendNextPagezPager.sendNextPaget   s#     	!!)T]]_u!Mr   c                     t               )zO
        Override this to return an object to be sent to my collector.
        )r:   rL   s    r   rP   zPager.nextPagez   s     "##r   c                     d| _         y)z4
        Call this when you're done paging.
        r   N)rC   rL   s    r   
stopPagingzPager.stopPaging   s     r   r   )	r   r   r   r-   r   rM   rQ   rP   rT   r   r   r   r=   r=   V   s!    4!N$r   r=   c                       e Zd ZdZddZd Zy)StringPagerz:
    A simple pager that splits a string into chunks.
    Nc                 d    || _         d| _        || _        t        j                  | ||g|i | y )Nr   )stringpointer	chunkSizer=   r   )r   rD   strZ   r@   r   r   s          r   r   zStringPager.__init__   s1    "tY>4>2>r   c                    | j                   | j                  | j                  | j                  z    }| xj                  | j                  z  c_        | j                  t        | j                         k\  r| j	                          |S r   )rX   rY   rZ   lenrT   r   vals     r   rP   zStringPager.nextPage   sY    kk$,,)FG&<<3t{{++OO
r   )i    N)r   r   r   r-   r   rP   r   r   r   rV   rV      s    ?r   rV   c                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
	FilePagerzC
    Reads a file in chunks and sends the chunks as they come.
    Nc                 j    g | _         t        j                  | ||g|i | | j                  |       y r   )chunksr=   r   startProducing)r   rD   fdr@   r   r   s         r   r   zFilePager.__init__   s1    tY>4>2>Br   c                      t        j                         j                  |        _         j                  j	                   fd       y )Nc                 $    j                         S r   )rT   )xr   s    r   <lambda>z*FilePager.startProducing.<locals>.<lambda>   s    (9 r   )r   
FileSenderbeginFileTransferdeferredaddBoth)r   re   s   ` r   rd   zFilePager.startProducing   s3    ((*<<RF9:r   c                 L    || _         |s| j                   j                          y y r   )producerresumeProducing)r   ro   	streamings      r   registerProducerzFilePager.registerProducer   s!     MM))+ r   c                     d | _         y r   )ro   rL   s    r   unregisterProducerzFilePager.unregisterProducer   s	    r   c                 :    | j                   j                  |       y r   )rc   append)r   chunks     r   writezFilePager.write   s    5!r   c                     | j                   sy| j                   j                  d      }| j                  j                          | j                  j                  d|d       y)zD
        Get the first chunk read and send it to collector.
        Nr   rO   FrJ   )rc   popro   rp   rD   r   r^   s     r   rQ   zFilePager.sendNextPage   sI     {{kkooa %%'!!)S5!Ar   r   )
r   r   r   r-   r   rd   rr   rt   rx   rQ   r   r   r   ra   ra      s&     
;,
"Br   ra   c                   "    e Zd ZdZd Zd Zd Zy)CallbackPageCollectorz
    I receive pages from the peer. You may instantiate a Pager with a
    remote reference to me. I will call the callback with a list of pages
    once they are all received.
    c                      g | _         || _        y r   )pagesr@   )r   r@   s     r   r   zCallbackPageCollector.__init__   s    
 r   c                 :    | j                   j                  |       y r   )r~   rv   )r   pages     r   remote_gotPagez$CallbackPageCollector.remote_gotPage   s    

$r   c                 :    | j                  | j                         y r   )r@   r~   rL   s    r   remote_endedPagingz(CallbackPageCollector.remote_endedPaging   s    djj!r   N)r   r   r   r-   r   r   r   r   r   r   r|   r|      s    ! "r   r|   c                     t        j                         } | j                  |t        |j                        g|i | |S )zr
    A utility method that will call a remote method which expects a
    PageCollector as the first argument.
    )r   Deferredr   r|   r@   )referenceable
methodNamer   r   ds        r   getAllPagesr      s<    
 	AMZ)>qzz)JXTXUWXHr   N)r-   zope.interfacer   twisted.internetr   r   twisted.protocolsr   twisted.python.failurer   twisted.spreadr   r
   r   r/   r=   rV   	IConsumerra   Referenceabler|   r   r   r   r   <module>r      s    ' . # * = =' '@' '4. .b% & Z!!"!B !B #!BJ"B,, "$r   