U
    ÏÐ~dª#  ã                   @   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k
r\   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 )é    )Úabsolute_importN)Úto_bytesc                 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)ÚserialÚ	iterbytesÚformatÚordÚdecode)ÚdataÚnÚb© r   ú[/var/www/html/myproject/myenv/lib/python3.8/site-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   Ú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   s.   | j r| j | j¡ | j |¡ | j ¡  dS )zshow received dataN)r#   r"   Úwriter$   Úflush©r'   r   r   r   r   ÚrxU   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   Útx\   s    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-   r1   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Úlabelr0   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   r<   r7   r8   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<   r7   r8   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)   r9   r<   r7   r8   r.   r   r   r   r1   ”   s    zFormatHexdump.controlN)r   )	r2   r3   r4   r5   r(   r<   r,   r-   r1   r   r   r   r   r6   h   s   

r6   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'   r0   r   r   r   rI   §   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   )ÚurlparseÚurlsplitÚschemer   ZSerialExceptionr   r6   ÚsysÚstderrÚparse_qsÚqueryÚitemsÚopenr   rC   Ú
ValueErrorrB   r   ÚnetlocÚpath)	r'   ÚurlÚpartsrB   r#   r"   Úoptionr   Úer   r   r   rK   ¬   s:    

þÿÿÿzSerial.from_urlc                    s$   t |ƒ}| j |¡ tt| ƒ |¡S rH   )r   rB   r-   rA   r@   r)   )r'   r-   rF   r   r   r)   Ë   s    zSerial.writer   c                    s*   t t| ƒ |¡}|s| jr&| j |¡ |S rH   )rA   r@   ÚreadrC   rB   r,   )r'   Úsizer,   rF   r   r   r`   Ð   s    
zSerial.readÚcancel_readc                    s    | j  dd¡ tt| ƒ ¡  d S )NúQ-RXrb   )rB   r1   rA   r@   rb   ©r'   rF   r   r   rb   ×   s    zSerial.cancel_readÚcancel_writec                    s    | j  dd¡ tt| ƒ ¡  d S )NúQ-TXre   )rB   r1   rA   r@   re   rd   rF   r   r   re   Ü   s    zSerial.cancel_writec                    s*   t t| ƒj}| jr&| j dd |¡¡ |S )Nrc   zin_waiting -> {})rA   r@   Ú
in_waitingrC   rB   r1   r   )r'   r   rF   r   r   rg   à   s    zSerial.in_waitingc                    s    | j  dd¡ tt| ƒ ¡  d S )Nrf   r*   )rB   r1   rA   r@   r*   rd   rF   r   r   r*   ç   s    zSerial.flushc                    s    | j  dd¡ tt| ƒ ¡  d S )Nrc   Úreset_input_buffer)rB   r1   rA   r@   rh   rd   rF   r   r   rh   ë   s    zSerial.reset_input_bufferc                    s    | j  dd¡ tt| ƒ ¡  d S )Nrf   Úreset_output_buffer)rB   r1   rA   r@   ri   rd   rF   r   r   ri   ï   s    zSerial.reset_output_bufferç      Ð?c                    s(   | j  dd |¡¡ tt| ƒ |¡ d S )NÚBRKzsend_break {}s)rB   r1   r   rA   r@   Ú
send_break)r'   ÚdurationrF   r   r   rl   ó   s    zSerial.send_breakc                 C   s*   | j  d|rdnd¡ tjj | |¡ d S )Nrk   ÚactiveÚinactive)rB   r1   r   r@   Úbreak_conditionrJ   ©r'   Úlevelr   r   r   rp   ÷   s    zSerial.break_conditionc                 C   s*   | j  d|rdnd¡ tjj | |¡ d S )NZRTSrn   ro   )rB   r1   r   r@   ÚrtsrJ   rq   r   r   r   rs   ü   s    z
Serial.rtsc                 C   s*   | j  d|rdnd¡ tjj | |¡ d S )NZDTRrn   ro   )rB   r1   r   r@   ÚdtrrJ   rq   r   r   r   rt     s    z
Serial.dtrc                    s&   t t| ƒj}| j d|rdnd¡ |S )NZCTSrn   ro   )rA   r@   ÚctsrB   r1   rq   rF   r   r   ru     s    z
Serial.ctsc                    s&   t t| ƒj}| j d|rdnd¡ |S )NZDSRrn   ro   )rA   r@   ÚdsrrB   r1   rq   rF   r   r   rv     s    z
Serial.dsrc                    s&   t t| ƒj}| j d|rdnd¡ |S )NZRIrn   ro   )rA   r@   ÚrirB   r1   rq   rF   r   r   rw     s    z	Serial.ric                    s&   t t| ƒj}| j d|rdnd¡ |S )NZCDrn   ro   )rA   r@   ÚcdrB   r1   rq   rF   r   r   rx     s    z	Serial.cd)r   )rj   )r2   r3   r4   r5   r(   r   r@   rI   ÚsetterrK   r)   r`   Úhasattrrb   re   Úpropertyrg   r*   rh   ri   rl   rp   rs   rt   ru   Úgetterrv   rw   rx   Ú__classcell__r   r   rF   r   r@   ›   s>   



r@   Ú__main__zspy:///dev/ttyS0)Ú
__future__r   rS   r7   r   Zserial.serialutilr   rP   ÚImportErrorÚurllib.parseÚparser   r   Úobjectr   r6   r@   r2   ZserrI   Úprintr   r   r   r   Ú<module>   s$   3 