
    BR<i{                     f   d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlmZmZ eefZeeej2                  fZdded	ed
efdZdded	ed
efdZdeded
efdZded
eee	e   f   fdZ ddeded
efdZ!dde
e   de
e   d
efdZ"dede	e   d
e	e   fdZ#ded
efdZ$y) z&This module contains helper functions.    N)AnyCallableListOptionalUnion   )TLS_CIPHER_SUITESTLS_VERSIONS)InterfaceError)EscapeTypes
StrOrBytesvalueencodingreturnc                 H    t        | t              r| S | j                  |      S )zReturns an encoded version of the string as a bytes object.

    Args:
        encoding (str): The encoding.

    Resturns:
        bytes: The encoded version of the string as a bytes object.
    )
isinstancebytesencoder   r   s     O/var/www/html/delta-backend/venv/lib/python3.12/site-packages/mysqlx/helpers.pyencode_to_bytesr   /   s!     ue,5H%,,x2HH    c                 H    t        | t              r| j                  |      S | S )zReturns a string decoded from the given bytes.

    Args:
        value (bytes): The value to be decoded.
        encoding (str): The encoding.

    Returns:
        str: The value decoded from bytes.
    )r   r   decoder   s     r   decode_from_bytesr   ;   s!     &0u%=5<<!H5Hr   objkeyc                 D    t        | t              r| |   S t        | |      S )zGet item from dictionary or attribute from object.

    Args:
        obj (object): Dictionary or object.
        key (str): Key.

    Returns:
        object: The object for the provided key.
    )r   dictgetattr)r   r   s     r   get_item_or_attrr!   H   s#     "#t,3s8C'#s2CCr   argsc                      dt         dt         fd}t        |       dkD  r| D cg c]
  } ||       c}S  || d         S c c}w )a
  Escapes special characters as they are expected to be when MySQL
    receives them.
    As found in MySQL source mysys/charset.c

    Args:
        value (object): Value to be escaped.

    Returns:
        str: The value if not a string, or the escaped string.
    r   r   c                    | | S t        | t              r| S t        | t        t        f      rn| j	                  dd      } | j	                  dd      } | j	                  dd      } | j	                  dd      } | j	                  d	d
      } | j	                  dd      } | S | j	                  dd      } | j	                  dd      } | j	                  dd      } | j	                  dd      } | j	                  dd      } | j	                  dd      } | S )zEscapes special characters.   \s   \\   
s   \n   s   \r   's   \'   "s   \"   s   \\z\\
z\nz\r'z\'"z\"z\)r   NUMERIC_TYPESr   	bytearrayreplace)r   s    r   _escapezescape.<locals>._escapea   s    =Le]+LeeY/0MM%1EMM%0EMM%0EMM';7EMM';7EMM';7E  MM$/EMM$.EMM$.EMM&*5EMM&*5EMM&*5Er   r   r   )r   len)r"   r4   args      r   escaper7   U   sK    { { , 4y1}(,---47 .s   A 
identifiersql_modec                 l    |dk(  r| j                  dd      }d| dS | j                  dd      }d| dS )ac  Quote the given identifier with backticks, converting backticks (`)
    in the identifier name with the correct escape sequence (``) unless the
    identifier is quoted (") as in sql_mode set to ANSI_QUOTES.

    Args:
        identifier (str): Identifier to quote.

    Returns:
        str: Returns string with the identifier quoted with backticks.
    ANSI_QUOTESr/   z""`z``)r3   )r8   r9   quoteds      r   quote_identifierr>   |   sL     = ##C.6(!}T*Fvha=r   versionreasonc                 0     dt         dt         f fd}|S )a  This is a decorator used to mark functions as deprecated.

    Args:
        version (Optional[string]): Version when was deprecated.
        reason (Optional[string]): Reason or extra information to be shown.

    Returns:
        Callable: A decorator used to mark functions as deprecated.

    Usage:

    .. code-block:: python

       from mysqlx.helpers import deprecated

       @deprecated('8.0.12', 'Please use other_function() instead')
       def deprecated_function(x, y):
           return x + y
    funcr   c                 n     t        j                         dt        dt        dt        f fd       }|S )zDecorate function.r"   kwargsr   c                  p   dj                    dg}r|j                  d        r|j                  d        t        j                         j                  }t        j                  dj                  |      t        t        j                  |j                        |j                          | i |S )zWrapper function.

            Args:
                *args: Variable length argument list.
                **kwargs: Arbitrary keyword arguments.
            r.   z' is deprecatedz since version z.  )categoryfilenamelineno)__name__appendinspectcurrentframef_backwarningswarn_explicitjoinDeprecationWarninggetfilef_codef_lineno)r"   rD   messageframerB   r@   r?   s       r   wrapperz-deprecated.<locals>.decorate.<locals>.wrapper   s     4==/9:G	:;F8}-((*11E"" + 6~~	 (((r   )	functoolswrapsr   r   )rB   rX   r@   r?   s   ` r   decoratezdeprecated.<locals>.decorate   s<     
		)3 	)# 	)( 	) 
	)* r   )r   )r?   r@   r[   s   `` r   
deprecatedr\      s    *x H 6 Or   tls_versioncipher_suites_namesc                     g }i }t        t        j                  |       dz         D ]!  }|j                  t        t        |             # |D ]?  }d|v r|j                  |       ||v r|j                  ||          2t        d| d       |S )a  Translates a cipher suites names list; from IANI names to OpenSSL names.

    Args:
        TLS_version (str): The TLS version to look at for a translation.
        cipher_suite_names (list): A list of cipher suites names.

    Returns:
        List[str]: List of translated names.
    r   -zThe 'z.' in cipher suites is not a valid cipher suite)ranger
   indexupdater	   rK   r   )r]   r^   translated_namescipher_suitesrb   names         r   iani_to_openssl_cs_namerg      s     M |))+6:; E.|E/BCDE $ $;##D)]"##M$$78 vKL  r   datac                 J    t        j                  |       j                  d      S )zReturn the hexadecimal representation of the binary data.

    Args:
        data (bytes): The binary data.

    Returns:
        str: The decoded hexadecimal representation of data.
    utf-8)binasciihexlifyr   )rh   s    r   rl   rl      s      D!((11r   )rj   )rF   )NN)%__doc__rk   decimalrY   rL   rO   typingr   r   r   r   r   	constantsr	   r
   errorsr   typesr   r   r2   r   
BYTE_TYPESintfloatDecimalr1   strr   r   objectr!   r7   r>   r\   rg   rl    r   r   <module>rz      s5  : -      7 7 6 " *
eW__-	I: 	I 	I5 	I
IZ 
I3 
IS 
I
D& 
Ds 
Ds 
D$+ $%T+5F(F"G $N  S $0 0hsm 0x 0f+/9	#Y@	2% 	2C 	2r   