o
    h{                     @   sX  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jfZd'd	ed
edefddZd'd	ed
edefddZdededefddZdedeee	e f fddZ d(dededefddZ!d)de
e de
e defddZ"d ed!e	e de	e fd"d#Z#d$edefd%d&Z$dS )*z&This module contains helper functions.    N)AnyCallableListOptionalUnion   )TLS_CIPHER_SUITESTLS_VERSIONS)InterfaceError)EscapeTypes
StrOrBytesutf-8valueencodingreturnc                 C   s   t | tr| S | |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    r   I/var/www/html/scripts/venv/lib/python3.10/site-packages/mysqlx/helpers.pyencode_to_bytes/   s   	r   c                 C   s   t | tr
| |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   r   r   r   decode_from_bytes;   s   
r   objkeyc                 C   s   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   r   r   r   get_item_or_attrH   s   
r   argsc                     s<   dt dt fdd t| dkr fdd| D S  | d S )	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   s   | du r| S t | tr| S t | ttfr:| dd} | dd} | dd} | dd	} | d
d} | dd} | S | dd} | dd} | dd} | dd} | dd} | dd} | S )zEscapes special characters.N   \s   \\   
s   \n   s   \r   's   \'   "s   \"   s   \\z\\
z\nz\r'z\'"z\"z\)r   NUMERIC_TYPESr   	bytearrayreplace)r   r   r   r   _escapea   s&   
zescape.<locals>._escaper   c                    s   g | ]} |qS r   r   ).0argr/   r   r   
<listcomp>x   s    zescape.<locals>.<listcomp>r   )r   len)r   r   r2   r   escapeU   s   r5    
identifiersql_modec                 C   s8   |dkr|  dd}d| dS |  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``)r.   )r7   r8   quotedr   r   r   quote_identifier|   s
   r<   versionreasonc                    s   dt dt f fd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                    s,   t  dtdtdtf fdd}|S )zDecorate function.r   kwargsr   c                     sr   d j  dg}r|d  r|d  t j}tjd|tt	|j
|jd  | i |S )zWrapper function.

            Args:
                *args: Variable length argument list.
                **kwargs: Arbitrary keyword arguments.
            r)   z' is deprecatedz since version z. r6   )categoryfilenamelineno)__name__appendinspectcurrentframef_backwarningswarn_explicitjoinDeprecationWarninggetfilef_codef_lineno)r   r@   messageframe)r?   r>   r=   r   r   wrapper   s   

z-deprecated.<locals>.decorate.<locals>.wrapper)	functoolswrapsr   r   )r?   rR   r>   r=   )r?   r   decorate   s    zdeprecated.<locals>.decorate)r   )r=   r>   rV   r   rU   r   
deprecated   s   rW   tls_versioncipher_suites_namesc                 C   sz   g }i }t t| d D ]}|tt|   q|D ]}d|v r'|| q||v r3|||  qt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   rE   r
   )rX   rY   translated_namescipher_suitesr\   namer   r   r   iani_to_openssl_cs_name   s   
ra   datac                 C   s   t | dS )zReturn the hexadecimal representation of the binary data.

    Args:
        data (bytes): The binary data.

    Returns:
        str: The decoded hexadecimal representation of data.
    r   )binasciihexlifyr   )rb   r   r   r   rd      s   	rd   )r   )r6   )NN)%__doc__rc   decimalrS   rF   rI   typingr   r   r   r   r   	constantsr   r	   errorsr
   typesr   r   r-   r   
BYTE_TYPESintfloatDecimalr,   strr   r   objectr   r5   r<   rW   ra   rd   r   r   r   r   <module>   s4   ' 3
 