a WW5 @sdZddlZddlZzeWneefy6eZYn0dZdddZdddZ Gd d d ej Z Gd d d ej Z Gd ddej Z Gddde ej Z Gddde ejZddZdS)aLPython '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. NZ0123456789ABCDEFstrictcCs tdd|Dt|fS)'40 41 42' -> b'@ab'cSsg|]}t|dqSint.0hr >/usr/lib/python3.9/site-packages/serial/tools/hexlify_codec.py &zhex_encode..)serialto_bytessplitlendataerrorsr r r hex_encode$srcCs&tdddt|Dt|fS)b'@ab' -> '40 41 42'css|]}dt|VqdSz{:02X} Nformatordr br r r +rzhex_decode..)unicodejoinr iterbytesrrr r r hex_decode)sr#c@s eZdZdddZdddZdS) CodecrcCstdd|DS)rcSsg|]}t|dqSrrrr r r r 1rz Codec.encode..)rrrselfrrr r r encode/sz Codec.encodecCstdddt|DS)rrcss|]}dt|VqdSrrrr r r r5rzCodec.decode..r r!rr"r%r r r decode3sz Codec.decodeN)r)r)__name__ __module__ __qualname__r'r)r r r r r$.s r$c@s<eZdZdZdddZddZddZd d Zdd d ZdS)IncrementalEncoderzIncremental hex encoderrcCs||_d|_dSNr)rstate)r&rr r r __init__;szIncrementalEncoder.__init__cCs d|_dSr.r/r&r r r reset?szIncrementalEncoder.resetcCs|jSNr1r2r r r getstateBszIncrementalEncoder.getstatecCs ||_dSr4r1)r&r/r r r setstateEszIncrementalEncoder.setstateFcCs|j}g}|D]|}|tvrRt|}|rD|||d@d}qd|d>}q|dkrv|rp|jdkrptdd}q|jdkrtd|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 rzodd number of hex digitsznon-hex digit found: {!r}) r/upper HEXDIGITSindexappendr UnicodeErrorrrr)r&rfinalr/Zencodedczr r r r'Hs"   zIncrementalEncoder.encodeN)r)F) r*r+r,__doc__r0r3r5r6r'r r r r r-8s  r-c@seZdZdZdddZdS)IncrementalDecoderzIncremental decoderFcCstdddt|DS)Nrcss|]}dt|VqdSrrrr r r rfrz,IncrementalDecoder.decode..r()r&rr@r r r r)eszIncrementalDecoder.decodeN)F)r*r+r,rCr)r r r r rDcsrDc@seZdZdZdS) StreamWriterz-Combination of hexlify codec and StreamWriterNr*r+r,rCr r r r rEisrEc@seZdZdZdS) StreamReaderz-Combination of hexlify codec and StreamReaderNrFr r r r rGmsrGc CstjdttttttdS)zencodings module APIZhexlify)namer'r)incrementalencoderincrementaldecoder streamwriter streamreader)codecs CodecInforr#r-rDrErGr r r r getregentryqsrO)r)r)rCrMrr NameErrorAttributeErrorstrr<rr#r$r-rDrErGrOr r r r  s     +