mfc@sdZddlZddlZddlmZddlmZddlmZm Z dej fdYZ dd d YZ d dd YZ d ZdS(sA class supporting chat-style (command/response) protocols. This class adds support for 'chat' style protocols - where one side sends a 'command', and the other sends a response (examples would be the common internet protocols - smtp, nntp, ftp, etc..). The handle_read() method looks at the input stream for the current 'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n' for multi-line output), calling self.found_terminator() on its receipt. for example: Say you build an async nntp client using this class. At the start of the connection, you'll have self.terminator set to '\r\n', in order to process the single-line greeting. Just before issuing a 'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST command will be accumulated (using your own 'collect_incoming_data' method) up to the terminator, and then control will be returned to you - by calling your self.found_terminator() method. iN(tdeque(t py3kwarning(tfilterwarningstcatch_warningst async_chatcBseZdZdZdZdddZdZdZdZ dZ dZ dZ d Z d Zd Zd Zd ZdZdZdZdZdZRS(sThis is an abstract class. You must derive from this class, and add the two methods collect_incoming_data() and found_terminator()icCs8d|_g|_t|_tjj|||dS(Nt(t ac_in_buffertincomingRt producer_fifotasyncoret dispatchert__init__(tselftsocktmap((s /usr/lib64/python2.7/asynchat.pyR @s   cCstddS(Nsmust be implemented in subclass(tNotImplementedError(R tdata((s /usr/lib64/python2.7/asynchat.pytcollect_incoming_dataQscCs|jj|dS(N(Rtappend(R R((s /usr/lib64/python2.7/asynchat.pyt_collect_incoming_dataTscCsdj|j}|j2|S(NR(tjoinR(R td((s /usr/lib64/python2.7/asynchat.pyt _get_dataWscCstddS(Nsmust be implemented in subclass(R(R ((s /usr/lib64/python2.7/asynchat.pytfound_terminator\scCs ||_dS(sRSet the input delimiter. Can be a fixed string of any length, an integer, or NoneN(t terminator(R tterm((s /usr/lib64/python2.7/asynchat.pytset_terminator_scCs|jS(N(R(R ((s /usr/lib64/python2.7/asynchat.pytget_terminatorcscCs y|j|j}Wn!tjk r9}|jdSX|j||_x|jrt|j}|j}|s|j|jd|_qMt |t st |t r)|}||kr|j|jd|_|j ||_ q|j|j| |j||_d|_ |j qMt|}|jj|}|dkr|dkrv|j|j| n|j|||_|j qMt|j|}|r||kr|j|j| |j| |_nPqM|j|jd|_qMWdS(NRii(trecvtac_in_buffer_sizetsocketterrort handle_errorRtlenRRt isinstancetinttlongRRtfindtfind_prefix_at_end(R RtwhytlbRtntterminator_lentindex((s /usr/lib64/python2.7/asynchat.pyt handle_readksJ            cCs|jdS(N(t initiate_send(R ((s /usr/lib64/python2.7/asynchat.pyt handle_writescCs|jdS(N(tclose(R ((s /usr/lib64/python2.7/asynchat.pyt handle_closescCsz|j}t||kr\xNtdt||D]!}|jj||||!q4Wn|jj||jdS(Ni(tac_out_buffer_sizeR!txrangeRRR-(R Rtsabsti((s /usr/lib64/python2.7/asynchat.pytpushs  "cCs|jj||jdS(N(RRR-(R tproducer((s /usr/lib64/python2.7/asynchat.pytpush_with_producerscCsdS(s4predicate for inclusion in the readable for select()i((R ((s /usr/lib64/python2.7/asynchat.pytreadablescCs|jp|j S(s4predicate for inclusion in the writable for select()(Rt connected(R ((s /usr/lib64/python2.7/asynchat.pytwritablescCs|jjddS(sAautomatically close this channel once the outgoing queue is emptyN(RRtNone(R ((s /usr/lib64/python2.7/asynchat.pytclose_when_donesc Csfx_|jra|jra|jd}|sO|jd=|dkrO|jdSn|j}y?t0tr~tddtnt |d|}WdQXWnCt k r|j }|r|jj |q|jd=qnXy|j |}Wntjk r|jdSX|r]|t|ks<|t|krP|||jd/s  (