U
    fj                     @   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 ddl	m
Z
 eefZeeejfZdddZdd	d
Zdd Zdd ZdddZdddZdd Zdd ZdS )z&This module contains helper functions.    N   )TLS_CIPHER_SUITESTLS_VERSIONS)InterfaceErrorutf-8c                 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valueencoding r   2/tmp/pip-unpacked-wheel-mzf9r1ws/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_bytes9   s    
r   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)objkeyr   r   r   get_item_or_attrF   s    
r   c                     s2   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.
    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   _escape^   s$    
zescape.<locals>._escaper   c                    s   g | ]} |qS r   r   ).0argr&   r   r   
<listcomp>t   s     zescape.<locals>.<listcomp>r   )len)argsr   r)   r   escapeS   s    r-    c                 C   s,   |dkrd | ddS 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_QUOTESz"{0}"r!   z""z`{0}``z``)formatr%   )
identifierZsql_moder   r   r   quote_identifierx   s    r2   c                    s    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.

    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
    c                    s   t   fdd}|S )zDecorate function.c                     sl   d  jg}r"|d  r6|d  t j}tjd|t	t
|j|jd  | |S )zWrapper function.

            Args:
                *args: Variable length argument list.
                **kwargs: Arbitrary keyword arguments.
            z'{}' is deprecatedz since version {}z. {}r.   )categoryfilenamelineno)r0   __name__appendinspectcurrentframef_backwarningswarn_explicitjoinDeprecationWarninggetfilef_codef_lineno)r,   kwargsmessageframe)funcreasonversionr   r   wrapper   s    

z-deprecated.<locals>.decorate.<locals>.wrapper)	functoolswraps)rE   rH   rF   rG   )rE   r   decorate   s    zdeprecated.<locals>.decorater   )rG   rF   rL   r   rK   r   
deprecated   s    rM   c                 C   sz   g }i }t t| d D ]}|tt|   q|D ]>}d|krN|| q6||krf|||  q6td|q6|S )zTranslates 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.
    r   -z5The '{}' in cipher suites is not a valid cipher suite)ranger   indexupdater   r7   r   r0   )Ztls_versionZcipher_suites_namesZtranslated_namesZcipher_suitesrP   namer   r   r   iani_to_openssl_cs_name   s    rS   c                 C   s   t | dS )zReturn the hexadecimal representation of the binary data.

    Args:
        data (str): The binary data.

    Returns:
        bytes: The hexadecimal representation of data.
    r   )binasciihexlifyr   )datar   r   r   rU      s    	rU   )r   )r   )r.   )NN)__doc__rT   decimalrI   r8   r;   	constantsr   r   errorsr   r$   r   Z
BYTE_TYPESintfloatDecimalr#   r   r   r   r-   r2   rM   rS   rU   r   r   r   r   <module>   s"   

%

*