
    ;)diw                    B   U d dl mZ d dlZd dlZd dlZd dlmZmZ ee	z  ee	         z  Z
ded<   d Zd Zd Zd	 Zd%dZd&dZd Zd Zd Zd Zd'dZd Zd(dZ G d d          Z G d de          Z G d d e          Z G d! d"e          Z G d# d$e          ZdS ))    )annotationsN)IO	TypeAliasr   ImageSourcec                    | |         S N )bitstrindexs     S/var/www/menimich/repos/protonApp/venv/lib/python3.11/site-packages/esptool/util.pybyter      s    %=    c                B    d}| dz  dk    r|dz  }| dz  } | dz  dk    |S )z9Return the index of the least significant bit in the maskr      r	   )maskshifts     r   mask_to_shiftr      s<    E
*//

 *// Lr   c                f    t          |           t          |          z   dz
  t          |          z  S )zReturn a/b rounded up to nearest integer,
    equivalent result to int(math.ceil(float(int(a)) / float(int(b))), only
    without possible floating point accuracy errors.
    r   )int)abs     r   div_roundupr      s*    
 FFSVVOaCFF**r   c                    | dS d| v r0t          | d|                     d                             dz  dz  S d| v r-t          | d|                     d                             dz  S t          d|            )zgGiven a flash size of the type passed in size
    (ie 512KB or 1MB) then return the size in bytes.
    NMBi   KBzUnknown size )r   r   
FatalError)sizes    r   flash_size_bytesr   %   s     |tt||4*$**T***+,,t3d::	4*$**T***+,,t33///000r   Tc                R    |rdndd                     fd| D                       S )Nz%02Xz%02x c              3  "   K   | ]	}|z  V  
d S r   r	   ).0c
format_strs     r   	<genexpr>zhexify.<locals>.<genexpr>5   s'      --a:>------r   )join)s	uppercaser$   s     @r   hexifyr)   3   s8    $0&J77----1------r      c                L    t          |           |z  }|dk    r| |||z
  z  z  } | S )z"Pad to the next alignment boundaryr   )len)data	alignmentpad_characterpad_mods       r   pad_tor1   8   s4    $ii)#G!||W!455Kr   c                    t          j        dd|           } t          j        dd|           } t          j        dd |           } | S )zKChange chip name to official form, e.g. `esp32s3beta2` -> `ESP32-S3(beta2)`z(esp32)(?!$)z\1-z	(beta\d*)z(\1)z^[^\(]+c                P    |                      d                                          S )Nr   )groupupper)xs    r   <lambda>z"expand_chip_name.<locals>.<lambda>G   s    QWWQZZ-=-=-?-? r   )resub	chip_names    r   expand_chip_namer<   @   sG     	::I|Wi88Iz#?#?KKIr   c                R    t          j        dd|                                           S )zLStrip chip name to normalized form, e.g. `ESP32-S3(beta2)` -> `esp32s3beta2`z[-()]r    )r8   r9   lowerr:   s    r   strip_chip_namer?   K   s     6(B	 1 1222r   c                    d}t          | d          5 }|                    dt          j                   |                                }ddd           n# 1 swxY w Y   |S )zReturns the file size in bytesr   rbN)openseekosSEEK_ENDtell)path_to_file	file_sizefs      r   get_file_sizerJ   P   s    I	lD	!	! Q	q"+FFHH	               s   5AAAc                T    |                      d                              dd          S )Nzutf-8 r    )decodereplace)byte_strings    r   sanitize_stringrP   Y   s&    g&&..tR888r   inputreturntuple[bytes, str | None]c                   t          | t                    r?t          | d          5 }|                                }| }ddd           n# 1 swxY w Y   nt          | t                    r| }d}nt          | d          ret          | d          rUt          | d          rE|                                 }|                                 }|                     |           | j        }nt          dt          |                      ||fS )a0  
    Normalize the input (file path, bytes, or an opened file-like object) into bytes
    and provide a name of the source.

    Args:
        input: The input file path, bytes, or an opened file-like object.

    Returns:
        A tuple containing the normalized bytes and the source of the input.
    rA   NreadwriteclosezInvalid input type )
isinstancestrrB   rU   byteshasattrrF   rC   namer   type)rQ   rI   r-   sourceposs        r   	get_bytesr`   ]   s8    % >% 	!6688DF	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
E5	!	! 	>			 >GE7$;$; >w@W@W >jjllzz||

3<tE{{<<===<s   A		AAc                N    |                                  D ]\  }}||k    r|c S dS )zO
    Get key from value in dictionary, assumes unique values in dictionary
    N)items)dictvalkeyvalues       r   get_key_from_valuerg   y   s;     jjll  
UC<<JJJ 4r   module_namerY   Nonec                    ddl }ddlm} |j        r|j        d         nd}|                    | dz             r|                    d|  d|  d           dS dS )	z*Check if called with deprecated .py suffixr   N)logr    z.pyzDEPRECATED: 'z .py' is deprecated. Please use 'zF' instead. The '.py' suffix will be removed in a future major release.)sysesptoolrk   argvendswithwarning)rh   rl   rk   script_names       r   check_deprecated_py_suffixrr      s    JJJ!$1#(1++rKK%/00 
SK S S S S S	
 	
 	
 	
 	

 
r   c                  "    e Zd ZdZddZddZdS )	PrintOncezU
    Class for printing messages just once. Can be useful when running in a loop
    rR   ri   c                "    d| _         || _        d S )NFalready_printedprint_callback)selfrx   s     r   __init__zPrintOnce.__init__   s    $,r   c                P    | j         s|                     |           d| _         d S d S )NTrv   )ry   texts     r   __call__zPrintOnce.__call__   s9    # 	(%%%#'D   	( 	(r   N)rR   ri   )__name__
__module____qualname____doc__rz   r}   r	   r   r   rt   rt      sF         - - - -( ( ( ( ( (r   rt   c                  .    e Zd ZdZd Zed             ZdS )r   z~
    Wrapper class for runtime errors that aren't caused by internal bugs, but by
    ESP ROM responses or input content.
    c                <    t                               | |           d S r   )RuntimeErrorrz   )ry   messages     r   rz   zFatalError.__init__   s    dG,,,,,r   c                h   i ddddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2}t          j        d3|d4d5                   }| d6                    t          |          |                    |d7         d8                    z  } t          |           S )9z
        Return a fatal error object that appends the hex values of
        'result' and its meaning as a string formatted argument.
           zUndefined errorsi  zThe input parameter is invalidi  z#Failed to malloc memory from systemi  zFailed to send out messagei  zFailed to receive messagei  z-The format of the received message is invalidi  z.Message is ok, but the running result is wrongi  zChecksum errori  zFlash write errori	  zFlash read errori
  zFlash read length errori  zDeflate failed errori  zDeflate Adler32 errori  zDeflate parameter errori  zInvalid RAM binary sizei  zInvalid RAM binary addressid  zInvalid parameterzInvalid formatzDescription too longzBad encoding descriptionzInsufficient storagezBad data lengthzBad data checksumzBad blocksizezInvalid commandzFailed SPI operationzFailed SPI unlockzNot in flash modezInflate errorzNot enough datazToo much datazCommand not implemented)ie  if  ig  ii  i   i   i   i   i   i   i   i   i   i   i   z>HN   z (result was {}: {})r   zUnknown result)structunpackformatr)   getr   )r   resulterr_defserr_codes       r   
WithResultzFatalError.WithResult   s   #
%#
 3#
 8	#

 /#
 .#
 B#
 C#
 ##
 &#
 %#
 ,#
 )#
 *#
 ,#
  ,!#
" /##
$ &%#
& $)-)%'#%*''#%#-E#
 #
 #
J =vbqbz22)006NNHLL!6FGG
 
 	
 '"""r   N)r~   r   r   r   rz   staticmethodr   r	   r   r   r   r      sH         
- - - /# /# \/# /# /#r   r   c                      e Zd ZdZd ZdS )NotImplementedInROMErrorz
    Wrapper class for the error thrown when a particular ESP bootloader function
    is not implemented in the ROM bootloader.
    c                \    t                               | |j         d|j         d           d S )Nz ROM does not support function .)r   rz   	CHIP_NAMEr~   )ry   
bootloaderfuncs      r   rz   z!NotImplementedInROMError.__init__   s@    #TTDMTTT	
 	
 	
 	
 	
r   Nr~   r   r   r   rz   r	   r   r   r   r      s-         

 
 
 
 
r   r   c                      e Zd Zd ZdS )NotSupportedErrorc                R    t                               | | d|j         d           d S )Nz is not supported by r   )r   rz   r   )ry   espfunction_names      r   rz   zNotSupportedError.__init__   s=    CC3=CCC	
 	
 	
 	
 	
r   N)r~   r   r   rz   r	   r   r   r   r      s#        
 
 
 
 
r   r   c                      e Zd ZdZd ZdS )UnsupportedCommandErrorz
    Wrapper class for when ROM loader returns an invalid command response.

    Usually this indicates the loader is running in Secure Download Mode.
    c                f    |j         rd|dd}nd|d}t                              | |           d S )NzThis command (z#xz*) is not supported in Secure Download ModezInvalid (unsupported) command )secure_download_moder   rz   )ry   r   opmsgs       r   rz   z UnsupportedCommandError.__init__   sN    # 	;T2TTTTCC:2:::CdC(((((r   Nr   r	   r   r   r   r      s-         ) ) ) ) )r   r   )T)r*   )rQ   r   rR   rS   )rh   rY   rR   ri   )
__future__r   rD   r8   r   typingr   r   rY   rZ   r   __annotations__r   r   r   r   r)   r1   r<   r?   rJ   rP   r`   rg   rr   rt   r   r   r   r   r   r	   r   r   <module>r      s!  
 # " " " " " " 				 				                  ur%y0 0 0 0 0    + + +1 1 1. . . .
     3 3 3
  9 9 9   8  

 

 

 

( ( ( ( ( ( ( (9# 9# 9# 9# 9# 9# 9# 9#x

 

 

 

 

z 

 

 


 
 
 
 

 
 
 
) ) ) ) )l ) ) ) ) )r   