U
    4Je{                     @   s<  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!eeed	d
dZd"eeed	ddZeeedddZeeee	e f dddZ d#eeedddZ!d$e
e e
e edddZ"ee	e e	e dddZ#eed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   2/tmp/pip-unpacked-wheel-7_167w8m/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keyr   c                 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   )argsr   c                     s:   t t d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kr| S t | tr| S t | ttfrr| dd} | dd} | dd} | dd	} | d
d} | dd} nH| 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_moder   c                 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.
    ZANSI_QUOTESr*   z""`z``)r.   )r7   r8   quotedr   r   r   quote_identifier|   s
    r;   )versionreasonr   c                    s   t t d 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  tttd fdd}|S )zDecorate function.)r   kwargsr   c                     sn   d j  dg}r$|d  r8|d  t j}tjd|tt	|j
|jd  | |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>   rQ   r=   r<   )r>   r   decorate   s    zdeprecated.<locals>.decorate)r   )r<   r=   rU   r   rT   r   
deprecated   s    rV   )tls_versioncipher_suites_namesr   c                 C   s|   g }i }t t| d D ]}|tt|   q|D ]@}d|krN|| q6||krf|||  q6td| dq6|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   rD   r
   )rW   rX   Ztranslated_namesZcipher_suitesr[   namer   r   r   iani_to_openssl_cs_name   s    
r^   )datar   c                 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   )r_   r   r   r   ra      s    	ra   )r   )r   )r6   )NN)%__doc__r`   decimalrR   rE   rH   typingr   r   r   r   r   	constantsr   r	   errorsr
   typesr   r   r-   r   Z
BYTE_TYPESintfloatDecimalr,   strr   r   objectr   r5   r;   rV   r^   ra   r   r   r   r   <module>   s,   '4  