o
    äî‡^]  ã                	   @   sÆ   d Z ddlmZ ddlZddlZze W n eefy!   eZY nw 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.

é    )Úabsolute_importNÚ0123456789ABCDEFÚstrictc                 C   s    t  dd„ |  ¡ D ƒ¡t| ƒfS )ú'40 41 42' -> b'@ab'c                 S   ó   g | ]}t |d ƒ‘qS ©é   ©Úint©Ú.0Úh© r   ú</usr/lib/python3/dist-packages/serial/tools/hexlify_codec.pyÚ
<listcomp>(   ó    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   ó   � | ]
}d   t|ƒ¡V  qdS ©z{:02X} N©ÚformatÚord©r   Úbr   r   r   Ú	<genexpr>-   ó   € 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   r   r   r	   r   r   r   r   r   3   r   z Codec.encode.<locals>.<listcomp>)r   r   r   ©Úselfr   r   r   r   r   Úencode1   s   zCodec.encodec                 C   ó   t d dd„ t |¡D ƒ¡ƒS )r   r   c                 s   r   r   r   r!   r   r   r   r#   7   r$   zCodec.decode.<locals>.<genexpr>©r%   r&   r   r'   r*   r   r   r   Údecode5   s   zCodec.decodeN©r   )Ú__name__Ú
__module__Ú__qualname__r,   r/   r   r   r   r   r)   0   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 r5   ©r6   ©r+   r   r   r   ÚresetA   ó   
zIncrementalEncoder.resetc                 C   s   | j S ©Nr8   r9   r   r   r   ÚgetstateD   s   zIncrementalEncoder.getstatec                 C   s
   || _ d S r<   r8   )r+   r6   r   r   r   ÚsetstateG   r;   zIncrementalEncoder.setstateFc                 C   s    | j }g }| ¡ D ]>}|tv r)t |¡}|r"| ||d@  ¡ d}q	d|d>  }q	|dkr;|r8| jdkr8tdƒ‚d}q	| jdkrGt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})
r6   ÚupperÚ	HEXDIGITSÚindexÚappendr   ÚUnicodeErrorr   r   r   )r+   r   Úfinalr6   ÚencodedÚcÚzr   r   r   r,   J   s$   

ÿ
zIncrementalEncoder.encodeNr0   ©F)	r1   r2   r3   Ú__doc__r7   r:   r=   r>   r,   r   r   r   r   r4   :   s    
r4   c                   @   s   e Zd ZdZddd„ZdS )ÚIncrementalDecoderzIncremental decoderFc                 C   r-   )Nr   c                 s   r   r   r   r!   r   r   r   r#   h   r$   z,IncrementalDecoder.decode.<locals>.<genexpr>r.   )r+   r   rH   r   r   r   r/   g   s   zIncrementalDecoder.decodeNrL   )r1   r2   r3   rM   r/   r   r   r   r   rN   e   s    rN   c                   @   ó   e Zd ZdZdS )ÚStreamWriterz-Combination of hexlify codec and StreamWriterN©r1   r2   r3   rM   r   r   r   r   rP   k   ó    rP   c                   @   rO   )ÚStreamReaderz-Combination of hexlify codec and StreamReaderNrQ   r   r   r   r   rS   o   rR   rS   c                	   C   s   t jdttttttd�S )zencodings module APIÚhexlify)Únamer,   r/   ÚincrementalencoderÚincrementaldecoderÚstreamwriterÚstreamreader)ÚcodecsÚ	CodecInfor   r(   r4   rN   rP   rS   r   r   r   r   Úgetregentrys   s   ùr\   r0   )rM   Ú
__future__r   rZ   r   r%   Ú	NameErrorÚAttributeErrorÚstrrD   r   r(   r)   r4   rN   rP   rS   r\   r   r   r   r   Ú<module>   s$   ÿ


+