U
    œ™WW@#  ã                   @   sª   d dl Z d dlZd dlZzd dlZW n  ek
rD   d dlmZ Y nX 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 )é    Nc                 c   s¬   d}t  | ¡D ]d}d t|ƒ¡d|  kr2dk r@n n
| d¡ndfV  |d7 }|dkr`d	V  q|d
krdV  d}q|dkr¨|d
k r¢|d7 }|dkršd	V  dV  q|dV  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 ]b\}}|dkrb|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   Úhexdump9   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__L   s    zFormatRaw.__init__c                 C   s.   | j r| j | j¡ | j |¡ | j ¡  dS )zshow received dataN)r    r   Úwriter!   Úflush©r$   r   r   r   r   ÚrxR   s    zFormatRaw.rxc                 C   s.   | j r| j | j¡ | j |¡ | j ¡  dS )zshow transmitted dataN)r    r   r&   r"   r'   r(   r   r   r   ÚtxY   s    zFormatRaw.txc                 C   s   dS )z(do not) show control callsNr   ©r$   ÚnameÚvaluer   r   r   Úcontrol`   s    zFormatRaw.controlN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r)   r*   r.   r   r   r   r   r   I   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%   t   s    
zFormatHexdump.__init__r	   c                 C   s&   | j  d ||||¡¡ | j  ¡  d S )Nz{:010.3f} {:4} {}{}
)r   r&   r   r'   )r$   Z	timestampZlabelr-   Zvalue2r   r   r   Ú
write_line|   s    zFormatHexdump.write_linec                 C   sh   | j r| j | j¡ |rLt|ƒD ](\}}|  t ¡ | j dd |¡|¡ q n|  t ¡ | j dd¡ dS )zshow received data as hex dumpZRXú{:04X}  z<empty>N)	r    r   r&   r!   r   r7   r4   r5   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TXr8   N)	r    r   r&   r"   r   r7   r4   r5   r   r9   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&   r6   r7   r4   r5   r+   r   r   r   r.   ‘   s    zFormatHexdump.controlN)r	   )	r/   r0   r1   r2   r%   r7   r)   r*   r.   r   r   r   r   r3   e   s   

r3   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ƒrd‡ 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||Ž 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 k	rt jj | |  |¡¡ d S ©N)r   r;   ÚportÚ__set__Úfrom_url)r$   r-   r   r   r   rD   ¤   s    zSerial.portc           	   
   C   sø   t  |¡}|jdkr&t d |j¡¡‚t}d}tj}zrt  	|j
d¡ ¡ D ]Z\}}|dkrht|d dƒ}qH|dkrvd}qH|d	kr„t}qH|d
kr”d| _qHtd |¡ƒ‚qHW n2 tk
rØ } zt d |¡¡‚W 5 d}~X Y nX |||ƒ| _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   r3   ÚsysÚstderrZparse_qsZqueryÚitemsÚopenr   r>   Ú
ValueErrorr=   r   ZnetlocÚpath)	r$   ZurlÚpartsr=   r    r   Zoptionr   Úer   r   r   rF   ©   s:    

þÿÿÿzSerial.from_urlc                    s   | j  |¡ tt| ƒ |¡S rC   )r=   r*   r<   r;   r&   )r$   r*   rA   r   r   r&   È   s    zSerial.writer   c                    s*   t t| ƒ |¡}|s| jr&| j |¡ |S rC   )r<   r;   Úreadr>   r=   r)   )r$   Úsizer)   rA   r   r   rT   Ì   s    
zSerial.readÚcancel_readc                    s    | j  dd¡ tt| ƒ ¡  d S )NúQ-RXrV   )r=   r.   r<   r;   rV   ©r$   rA   r   r   rV   Ó   s    zSerial.cancel_readÚcancel_writec                    s    | j  dd¡ tt| ƒ ¡  d S )NúQ-TXrY   )r=   r.   r<   r;   rY   rX   rA   r   r   rY   Ø   s    zSerial.cancel_writec                    s*   t t| ƒj}| jr&| j dd |¡¡ |S )NrW   zin_waiting -> {})r<   r;   Ú
in_waitingr>   r=   r.   r   )r$   r   rA   r   r   r[   Ü   s    zSerial.in_waitingc                    s    | j  dd¡ tt| ƒ ¡  d S )NrZ   r'   )r=   r.   r<   r;   r'   rX   rA   r   r   r'   ã   s    zSerial.flushc                    s    | j  dd¡ tt| ƒ ¡  d S )NrW   Úreset_input_buffer)r=   r.   r<   r;   r\   rX   rA   r   r   r\   ç   s    zSerial.reset_input_bufferc                    s    | j  dd¡ tt| ƒ ¡  d S )NrZ   Úreset_output_buffer)r=   r.   r<   r;   r]   rX   rA   r   r   r]   ë   s    zSerial.reset_output_bufferç      Ð?c                    s(   | j  dd |¡¡ tt| ƒ |¡ d S )NÚBRKzsend_break {}s)r=   r.   r   r<   r;   Ú
send_break)r$   ZdurationrA   r   r   r`   ï   s    zSerial.send_breakc                 C   s*   | j  d|rdnd¡ tjj | |¡ d S )Nr_   ÚactiveÚinactive)r=   r.   r   r;   Úbreak_conditionrE   ©r$   Úlevelr   r   r   rc   ó   s    zSerial.break_conditionc                 C   s*   | j  d|rdnd¡ tjj | |¡ d S )NZRTSra   rb   )r=   r.   r   r;   ÚrtsrE   rd   r   r   r   rf   ø   s    z
Serial.rtsc                 C   s*   | j  d|rdnd¡ tjj | |¡ d S )NZDTRra   rb   )r=   r.   r   r;   ÚdtrrE   rd   r   r   r   rg   ý   s    z
Serial.dtrc                    s&   t t| ƒj}| j d|rdnd¡ |S )NZCTSra   rb   )r<   r;   Úctsr=   r.   rd   rA   r   r   rh     s    z
Serial.ctsc                    s&   t t| ƒj}| j d|rdnd¡ |S )NZDSRra   rb   )r<   r;   Údsrr=   r.   rd   rA   r   r   ri     s    z
Serial.dsrc                    s&   t t| ƒj}| j d|rdnd¡ |S )NZRIra   rb   )r<   r;   Úrir=   r.   rd   rA   r   r   rj     s    z	Serial.ric                    s&   t t| ƒj}| j d|rdnd¡ |S )NZCDra   rb   )r<   r;   Úcdr=   r.   rd   rA   r   r   rk     s    z	Serial.cd)r   )r^   )r/   r0   r1   r2   r%   r   r;   rD   ÚsetterrF   r&   rT   ÚhasattrrV   rY   Úpropertyr[   r'   r\   r]   r`   rc   rf   rg   rh   Úgetterri   rj   rk   Ú__classcell__r   r   rA   r   r;   ˜   s>   



r;   Ú__main__zspy:///dev/ttyS0)rL   r4   r   rK   ÚImportErrorZurllib.parseÚparser   r   Úobjectr   r3   r;   r/   ZserrD   Úprintr   r   r   r   Ú<module>   s    3 