U
    WW5                  	   @   s   d Z ddlZddlZze W n eefk
r8   eZY nX dZdddZdddZ	G d	d
 d
ej
Z
G dd dejZG dd dejZG dd de
ejZG dd de
ejZdd ZdS )aL  Python 'hex' Codec - 2-digit hex with spaces content transfer encoding.

Encode and decode may be a bit missleading at first sight...

The textual representation is a hex dump: e.g. "40 41"
The "encoded" data of this is the binary form, e.g. b"@A"

Therefore decoding is binary to text and thus converting binary data to hex dump.

    NZ0123456789ABCDEFstrictc                 C   s    t dd |  D t| fS )'40 41 42' -> b'@ab'c                 S   s   g | ]}t |d qS    int.0h r   </usr/lib/python3/dist-packages/serial/tools/hexlify_codec.py
<listcomp>&   s     zhex_encode.<locals>.<listcomp>)serialto_bytessplitlendataerrorsr   r   r   
hex_encode$   s    r   c                 C   s&   t ddd t| D t| fS )b'@ab' -> '40 41 42' c                 s   s   | ]}d  t|V  qdS z{:02X} Nformatordr	   br   r   r   	<genexpr>+   s     zhex_decode.<locals>.<genexpr>)unicodejoinr   	iterbytesr   r   r   r   r   
hex_decode)   s    r"   c                   @   s    e Zd ZdddZdddZdS )	Codecr   c                 C   s   t dd | D S )r   c                 S   s   g | ]}t |d qS r   r   r   r   r   r   r   1   s     z Codec.encode.<locals>.<listcomp>)r   r   r   selfr   r   r   r   r   encode/   s    zCodec.encodec                 C   s   t ddd t|D S )r   r   c                 s   s   | ]}d  t|V  qdS r   r   r   r   r   r   r   5   s     zCodec.decode.<locals>.<genexpr>r   r    r   r!   r$   r   r   r   decode3   s    zCodec.decodeN)r   )r   )__name__
__module____qualname__r&   r(   r   r   r   r   r#   .   s   
r#   c                   @   s<   e Zd ZdZdddZdd Zdd Zd	d
 ZdddZdS )IncrementalEncoderzIncremental hex encoderr   c                 C   s   || _ d| _d S Nr   )r   state)r%   r   r   r   r   __init__;   s    zIncrementalEncoder.__init__c                 C   s
   d| _ d S r-   r.   r%   r   r   r   reset?   s    zIncrementalEncoder.resetc                 C   s   | j S Nr0   r1   r   r   r   getstateB   s    zIncrementalEncoder.getstatec                 C   s
   || _ d S r3   r0   )r%   r.   r   r   r   setstateE   s    zIncrementalEncoder.setstateFc                 C   s   | j }g }| D ]|}|tkrRt|}|rD|||d@   d}qd|d>  }q|dkrv|rp| jdkrptdd}q| jdkrtd|q|| _ t	|S )	z        Incremental encode, keep track of digits and emit a byte when a pair
        of hex digits is found. The space is optional unless the error
        handling is defined to be 'strict'.
           r          r   zodd number of hex digitsznon-hex digit found: {!r})
r.   upper	HEXDIGITSindexappendr   UnicodeErrorr   r   r   )r%   r   finalr.   Zencodedczr   r   r   r&   H   s"    

zIncrementalEncoder.encodeN)r   )F)	r)   r*   r+   __doc__r/   r2   r4   r5   r&   r   r   r   r   r,   8   s   
r,   c                   @   s   e Zd ZdZdddZdS )IncrementalDecoderzIncremental decoderFc                 C   s   t ddd t|D S )Nr   c                 s   s   | ]}d  t|V  qdS r   r   r   r   r   r   r   f   s     z,IncrementalDecoder.decode.<locals>.<genexpr>r'   )r%   r   r?   r   r   r   r(   e   s    zIncrementalDecoder.decodeN)F)r)   r*   r+   rB   r(   r   r   r   r   rC   c   s   rC   c                   @   s   e Zd ZdZdS )StreamWriterz-Combination of hexlify codec and StreamWriterNr)   r*   r+   rB   r   r   r   r   rD   i   s   rD   c                   @   s   e Zd ZdZdS )StreamReaderz-Combination of hexlify codec and StreamReaderNrE   r   r   r   r   rF   m   s   rF   c                	   C   s   t jdttttttdS )zencodings module APIZhexlify)namer&   r(   incrementalencoderincrementaldecoderstreamwriterstreamreader)codecs	CodecInfor   r"   r,   rC   rD   rF   r   r   r   r   getregentryq   s    rN   )r   )r   )rB   rL   r   r   	NameErrorAttributeErrorstrr;   r   r"   r#   r,   rC   rD   rF   rN   r   r   r   r   <module>	   s   



+