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 