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)serialZ	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.   r2   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&   Z	timestampZlabelr1   Z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 dumpZRX{:04X}  z<empty>N)	r"   r!   r)   r#   r   r;   r8   r9   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 dumpZTXr<   N)	r"   r!   r)   r$   r   r;   r8   r9   r   r=   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;   r8   r9   r/   r   r   r   r2      s   zFormatHexdump.controlN)r   )	r3   r4   r5   r6   r'   r;   r,   r.   r2   r   r   r   r   r7   h   s    

r7   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)superr?   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   r?   port__set__from_url)r&   r1   r   r   r   rH      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 stringZ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   )urlparseZurlsplitZschemer   ZSerialExceptionr   r7   sysstderrZparse_qsZqueryitemsopenr   rB   
ValueErrorrA   r   Znetlocpath)	r&   ZurlpartsrA   r"   r!   optionr   er   r   r   rJ      s<   

zSerial.from_urlc                    s$   t |}| j| tt| |S rG   )r   rA   r.   r@   r?   r)   )r&   r.   rE   r   r   r)      s   zSerial.writer   c                    s*   t t| |}|s| jr| j| |S rG   )r@   r?   readrB   rA   r,   )r&   sizer,   rE   r   r   rY      s   
zSerial.readcancel_readc                        | j dd tt|   d S )NQ-RXr[   )rA   r2   r@   r?   r[   r&   rE   r   r   r[         zSerial.cancel_readcancel_writec                    r\   )NQ-TXr`   )rA   r2   r@   r?   r`   r^   rE   r   r   r`      r_   zSerial.cancel_writec                    s*   t t| j}| jr| jdd| |S )Nr]   zin_waiting -> {})r@   r?   
in_waitingrB   rA   r2   r   )r&   r   rE   r   r   rb      s   zSerial.in_waitingc                    r\   )Nra   r*   )rA   r2   r@   r?   r*   r^   rE   r   r   r*      r_   zSerial.flushc                    r\   )Nr]   reset_input_buffer)rA   r2   r@   r?   rc   r^   rE   r   r   rc      r_   zSerial.reset_input_bufferc                    r\   )Nra   reset_output_buffer)rA   r2   r@   r?   rd   r^   rE   r   r   rd      r_   zSerial.reset_output_buffer      ?c                    s(   | j dd| tt| | d S )NBRKzsend_break {}s)rA   r2   r   r@   r?   
send_break)r&   ZdurationrE   r   r   rg      s   zSerial.send_breakc                 C   *   | j d|rdnd tjj| | d S )Nrf   activeinactive)rA   r2   r   r?   break_conditionrI   r&   levelr   r   r   rk         zSerial.break_conditionc                 C   rh   )NZRTSri   rj   )rA   r2   r   r?   rtsrI   rl   r   r   r   ro      rn   z
Serial.rtsc                 C   rh   )NZDTRri   rj   )rA   r2   r   r?   dtrrI   rl   r   r   r   rp     rn   z
Serial.dtrc                    ,   t t| j}| jd|rd |S d |S )NZCTSri   rj   )r@   r?   ctsrA   r2   rl   rE   r   r   rr     
   z
Serial.ctsc                    rq   )NZDSRri   rj   )r@   r?   dsrrA   r2   rl   rE   r   r   rt     rs   z
Serial.dsrc                    rq   )NZRIri   rj   )r@   r?   rirA   r2   rl   rE   r   r   ru     rs   z	Serial.ric                    rq   )NZCDri   rj   )r@   r?   cdrA   r2   rl   rE   r   r   rv     rs   z	Serial.cd)r   )re   )r3   r4   r5   r6   r'   r   r?   rH   setterrJ   r)   rY   hasattrr[   r`   propertyrb   r*   rc   rd   rg   rk   ro   rp   rr   getterrt   ru   rv   __classcell__r   r   rE   r   r?      s@    



r?   __main__zspy:///dev/ttyS0)Z
__future__r   rP   r8   r   Zserial.serialutilr   rO   ImportErrorZurllib.parseparser   r   objectr   r7   r?   r3   ZserrH   printr   r   r   r   <module>   s*   3 