o
    Ç1»_ª#  ã                   @   sÄ   d dl mZ d dlZd dlZd dlZd dlmZ zd dlZW n ey-   d dl	m
Z Y nw dd„ Zdd„ ZG dd	„ d	eƒZG d
d„ deƒZG dd„ dejƒZedkr`edƒZde_eeƒ dS dS )é    )Úabsolute_importN)Úto_bytesc                 c   s¸   � d}t  | ¡D ]2}d t|ƒ¡d|  krdk r!n n| d¡ndfV  |d7 }|dkr1d	V  q|d
kr:dV  d}q|dkrZ|d
k rU|d7 }|dkrNd	V  dV  |d
k sCdV  dS dS )z•    yield tuples of hex and ASCII display in multiples of 16. Includes a
    space after 8 bytes and (None, None) after 16 bytes and at the end.
    r   z{:02X} ó    ó   ÚasciiÚ.é   é   )ú Ú é   )NN)z   r
   N)ÚserialÚ	iterbytesÚformatÚordÚdecode)ÚdataÚnÚb© r   ú@/usr/lib/python3/dist-packages/serial/urlhandler/protocol_spy.pyÚsixteen%   s(   €6€ü
úr   c                 c   s~   � g }g }d}t | ƒD ]1\}}|du r2|d d |¡d |¡g¡fV  |dd…= |dd…= |d7 }q| |¡ | |¡ qdS )z yield lines with hexdump of datar   Nr
   r   r   )r   ÚjoinÚappend)r   Úvaluesr   ÚoffsetÚhÚar   r   r   Úhexdump<   s   € 



ør   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú	FormatRawz&Forward only RX and TX data to output.c                 C   s   || _ || _d| _d| _d S )Nú[32mú[31m)ÚoutputÚcolorÚrx_colorÚtx_color©Úselfr"   r#   r   r   r   Ú__init__O   s   
zFormatRaw.__init__c                 C   ó.   | j r
| j | j¡ | j |¡ | j ¡  dS )zshow received dataN)r#   r"   Úwriter$   Úflush©r'   r   r   r   r   ÚrxU   ó   zFormatRaw.rxc                 C   r)   )zshow transmitted dataN)r#   r"   r*   r%   r+   r,   r   r   r   Útx\   r.   zFormatRaw.txc                 C   s   dS )z(do not) show control callsNr   ©r'   ÚnameÚvaluer   r   r   Úcontrolc   s   zFormatRaw.controlN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r-   r/   r3   r   r   r   r   r   L   s    r   c                   @   s:   e Zd ZdZdd„ Zddd„Zdd„ Zd	d
„ Zdd„ ZdS )ÚFormatHexdumpax      Create a hex dump of RX ad TX data, show when control lines are read or
    written.

    output example::

        000000.000 Q-RX flushInput
        000002.469 RTS  inactive
        000002.773 RTS  active
        000003.001 TX   48 45 4C 4C 4F                                    HELLO
        000003.102 RX   48 45 4C 4C 4F                                    HELLO

    c                 C   s,   t   ¡ | _|| _|| _d| _d| _d| _d S )Nr    r!   z[37m)ÚtimeÚ
start_timer"   r#   r$   r%   Úcontrol_colorr&   r   r   r   r(   w   s   

zFormatHexdump.__init__r   c                 C   s&   | j  d ||||¡¡ | j  ¡  d S )Nz{:010.3f} {:4} {}{}
)r"   r*   r   r+   )r'   Ú	timestampÚlabelr2   Úvalue2r   r   r   Ú
write_line   s   zFormatHexdump.write_linec                 C   sj   | j r
| j | j¡ |r't|ƒD ]\}}|  t ¡ | j dd |¡|¡ qdS |  t ¡ | j dd¡ dS )zshow received data as hex dumpÚRXú{:04X}  z<empty>N)	r#   r"   r*   r$   r   r?   r9   r:   r   ©r'   r   r   Úrowr   r   r   r-   ƒ   s   "ÿzFormatHexdump.rxc                 C   sJ   | j r
| j | j¡ t|ƒD ]\}}|  t ¡ | j dd |¡|¡ qdS )z!show transmitted data as hex dumpÚTXrA   N)	r#   r"   r*   r%   r   r?   r9   r:   r   rB   r   r   r   r/   �   s
   "ÿzFormatHexdump.txc                 C   s0   | j r
| j | j¡ |  t ¡ | j ||¡ dS )zshow control callsN)r#   r"   r*   r;   r?   r9   r:   r0   r   r   r   r3   ”   s   zFormatHexdump.controlN)r   )	r4   r5   r6   r7   r(   r?   r-   r/   r3   r   r   r   r   r8   h   s    

r8   c                       sT  e Zd ZdZ‡ fdd„Zejjjdd„ ƒZdd„ Z	‡ fdd	„Z
d,‡ fdd„	Zeejdƒr2‡ fdd„Zeejdƒr>‡ fdd„Ze‡ fdd„ƒZ‡ fdd„Z‡ fdd„Z‡ fdd„Zd-‡ fdd„	Zejjjdd„ ƒZejjjd d!„ ƒZejjjd"d#„ ƒZejjj‡ fd$d%„ƒZejjj‡ fd&d'„ƒZejjj‡ fd(d)„ƒZejjj‡ fd*d+„ƒZ‡  ZS ).ÚSerialzc    Inherit the native Serial port implementation and wrap all the methods and
    attributes.
    c                    s&   t t| ƒj|i |¤Ž d | _d| _d S )NF)ÚsuperrE   r(   Ú	formatterÚshow_all)r'   ÚargsÚkwargs©Ú	__class__r   r   r(   ¢   s   
zSerial.__init__c                 C   s&   |d urt jj | |  |¡¡ d S d S ©N)r   rE   ÚportÚ__set__Úfrom_url)r'   r2   r   r   r   rN   §   s   ÿzSerial.portc           	   
   C   sì   t  |¡}|jdkrt d |j¡¡‚t}d}tj}z8t  	|j
d¡ ¡ D ],\}}|dkr4t|d dƒ}q$|dkr;d}q$|d	krBt}q$|d
krJd| _q$td |¡ƒ‚W n tyf } zt d |¡¡‚d}~ww |||ƒ| _d |j|jg¡S )z(extract host and port from an URL stringÚspyzmexpected a string in the form "spy://port[?option[=value][&option[=value]]]": not starting with spy:// ({!r})FTÚfiler   Úwr#   ÚrawÚallzunknown option: {!r}zPexpected a string in the form "spy://port[?option[=value][&option[=value]]]": {}Nr   )ÚurlparseÚurlsplitÚschemer   ÚSerialExceptionr   r8   ÚsysÚstderrÚparse_qsÚqueryÚitemsÚopenr   rH   Ú
ValueErrorrG   r   ÚnetlocÚpath)	r'   ÚurlÚpartsrG   r#   r"   Úoptionr   Úer   r   r   rP   ¬   s<   

ýöþ€ÿzSerial.from_urlc                    s$   t |ƒ}| j |¡ tt| ƒ |¡S rM   )r   rG   r/   rF   rE   r*   )r'   r/   rK   r   r   r*   Ë   s   zSerial.writer   c                    s*   t t| ƒ |¡}|s| jr| j |¡ |S rM   )rF   rE   ÚreadrH   rG   r-   )r'   Úsizer-   rK   r   r   rg   Ð   s   
zSerial.readÚcancel_readc                    ó    | j  dd¡ tt| ƒ ¡  d S )NúQ-RXri   )rG   r3   rF   rE   ri   ©r'   rK   r   r   ri   ×   ó   zSerial.cancel_readÚcancel_writec                    rj   )NúQ-TXrn   )rG   r3   rF   rE   rn   rl   rK   r   r   rn   Ü   rm   zSerial.cancel_writec                    s*   t t| ƒj}| jr| j dd |¡¡ |S )Nrk   zin_waiting -> {})rF   rE   Ú
in_waitingrH   rG   r3   r   )r'   r   rK   r   r   rp   à   s   zSerial.in_waitingc                    rj   )Nro   r+   )rG   r3   rF   rE   r+   rl   rK   r   r   r+   ç   rm   zSerial.flushc                    rj   )Nrk   Úreset_input_buffer)rG   r3   rF   rE   rq   rl   rK   r   r   rq   ë   rm   zSerial.reset_input_bufferc                    rj   )Nro   Úreset_output_buffer)rG   r3   rF   rE   rr   rl   rK   r   r   rr   ï   rm   zSerial.reset_output_bufferç      Ð?c                    s(   | j  dd |¡¡ tt| ƒ |¡ d S )NÚBRKzsend_break {}s)rG   r3   r   rF   rE   Ú
send_break)r'   ÚdurationrK   r   r   ru   ó   s   zSerial.send_breakc                 C   ó*   | j  d|rdnd¡ tjj | |¡ d S )Nrt   ÚactiveÚinactive)rG   r3   r   rE   Úbreak_conditionrO   ©r'   Úlevelr   r   r   rz   ÷   ó   zSerial.break_conditionc                 C   rw   )NÚRTSrx   ry   )rG   r3   r   rE   ÚrtsrO   r{   r   r   r   r   ü   r}   z
Serial.rtsc                 C   rw   )NÚDTRrx   ry   )rG   r3   r   rE   ÚdtrrO   r{   r   r   r   r�     r}   z
Serial.dtrc                    ó,   t t| ƒj}| j d|rd¡ |S d¡ |S )NÚCTSrx   ry   )rF   rE   ÚctsrG   r3   r{   rK   r   r   r„     ó
   ÿz
Serial.ctsc                    r‚   )NÚDSRrx   ry   )rF   rE   ÚdsrrG   r3   r{   rK   r   r   r‡     r…   z
Serial.dsrc                    r‚   )NÚRIrx   ry   )rF   rE   ÚrirG   r3   r{   rK   r   r   r‰     r…   z	Serial.ric                    r‚   )NÚCDrx   ry   )rF   rE   ÚcdrG   r3   r{   rK   r   r   r‹     r…   z	Serial.cd)r   )rs   )r4   r5   r6   r7   r(   r   rE   rN   ÚsetterrP   r*   rg   Úhasattrri   rn   Úpropertyrp   r+   rq   rr   ru   rz   r   r�   r„   Úgetterr‡   r‰   r‹   Ú__classcell__r   r   rK   r   rE   ›   s@    



rE   Ú__main__zspy:///dev/ttyS0)Ú
__future__r   rZ   r9   r   Úserial.serialutilr   rV   ÚImportErrorÚurllib.parseÚparser   r   Úobjectr   r8   rE   r4   ÚserrN   Úprintr   r   r   r   Ú<module>   s*   ÿ3 ý