3
(h                 @   s   d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
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S )z&This module contains helper functions.    N   )NUMERIC_TYPES)TLS_CIPHER_SUITESTLS_VERSIONS)InterfaceErrorutf-8c             C   s   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)valueencoding r   B/tmp/pip-install-q3hcpn_q/mysql-connector-python/mysqlx/helpers.pyencode_to_bytes'   s    	r   c             C   s   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	   decode)r   r   r   r   r   decode_from_bytes3   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_attr@   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| jdd} | jdd} | jdd} | jdd	} | jd
d} | jdd} nH| jdd} | jdd} | jdd} | jdd} | jdd} | j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   r   r	   	bytearrayreplace)r   r   r   r   _escapeX   s$    
zescape.<locals>._escaper   c                s   g | ]} |qS r   r   ).0arg)r%   r   r   
<listcomp>n   s    zescape.<locals>.<listcomp>r   )len)argsr   )r%   r   escapeM   s    r+    c             C   s,   |dkrdj | jddS dj | j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_identifierr   s    r0   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 j  fdd}|S )zDecorate function.c                 sl   dj  jg}r"|jdj  r6|jdj  tj j}tjdj|t	tj
|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)r.   __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)rC   rF   )rD   rE   )rC   r   decorate   s    zdeprecated.<locals>.decorater   )rE   rD   rI   r   )rD   rE   r   
deprecated   s    rJ   c             C   s   g }i }x,t tj| d D ]}|jtt|   qW xF|D ]>}d|krT|j| q<||krl|j||  q<tdj|q<W |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   r5   r   r.   )Ztls_versionZcipher_suites_namesZtranslated_namesZcipher_suitesrM   namer   r   r   iani_to_openssl_cs_name   s    
rP   )r   )r   )r,   )NN)__doc__rG   r6   r9   compatr   	constantsr   r   errorsr   r   r   r   r+   r0   rJ   rP   r   r   r   r   <module>   s   

%

)